From 15fdbc9c7da469613611ec1f93558d8f8a9648b8 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 29 Oct 2013 02:49:38 +0900 Subject: [PATCH] add some symbol primitive functions --- src/init.c | 2 ++ src/symbol.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/init.c b/src/init.c index 42c572ea..46fcf0c3 100644 --- a/src/init.c +++ b/src/init.c @@ -10,6 +10,7 @@ void pic_init_time(pic_state *); void pic_init_system(pic_state *); void pic_init_file(pic_state *); void pic_init_proc(pic_state *); +void pic_init_symbol(pic_state *); void pic_load_stdlib(pic_state *pic) @@ -66,6 +67,7 @@ pic_init_core(pic_state *pic) pic_init_system(pic); DONE; pic_init_file(pic); DONE; pic_init_proc(pic); DONE; + pic_init_symbol(pic); DONE; pic_load_stdlib(pic); DONE; } diff --git a/src/symbol.c b/src/symbol.c index 7a84641b..b8212ba2 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -30,3 +30,49 @@ pic_symbol_name(pic_state *pic, pic_sym sym) { return pic->sym_pool[sym]; } + +static pic_value +pic_symbol_symbol_p(pic_state *pic) +{ + pic_value v; + + pic_get_args(pic, "o", &v); + + return pic_bool_value(pic_symbol_p(v)); +} + +static pic_value +pic_symbol_symbol_to_string(pic_state *pic) +{ + pic_value v; + + pic_get_args(pic, "o", &v); + + if (! pic_symbol_p(v)) { + pic_error(pic, "symbol->string: expected symbol"); + } + + return pic_str_new_cstr(pic, pic_symbol_name(pic, pic_sym(v))); +} + +static pic_value +pic_symbol_string_to_symbol(pic_state *pic) +{ + pic_value v; + + pic_get_args(pic, "o", &v); + + if (! pic_str_p(v)) { + pic_error(pic, "string->symbol: expected string"); + } + + return pic_symbol_value(pic_intern_cstr(pic, pic_str_ptr(v)->str)); +} + +void +pic_init_symbol(pic_state *pic) +{ + pic_defun(pic, "symbol?", pic_symbol_symbol_p); + pic_defun(pic, "symbol->string", pic_symbol_symbol_to_string); + pic_defun(pic, "string->symbol", pic_symbol_string_to_symbol); +}