From dda8ad367c06ce11322ca5b2d496b7a3cfee5f46 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 7 Dec 2013 23:17:28 -0800 Subject: [PATCH] split built-in functins into libraries --- include/picrin.h | 9 +++++++++ src/file.c | 16 ++++++++++------ src/port.c | 9 +++++++-- src/system.c | 14 +++++++++----- src/time.c | 10 +++++++--- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index 11287003..ffb8d341 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -127,6 +127,15 @@ void pic_in_library(pic_state *, pic_value); struct pic_lib *pic_make_library(pic_state *, pic_value); struct pic_lib *pic_find_library(pic_state *, pic_value); +#define DEFLIBRARY(pic,name) \ + { \ + struct pic_lib *lib__ = pic->lib; \ + pic_make_library(pic, pic_parse(pic, name)); \ + pic_in_library(pic, pic_parse(pic, name)); +#define ENDLIBRARY(pic) \ + pic->lib = lib__; \ + } + void pic_export(pic_state *, pic_sym); void pic_export_as(pic_state *, pic_sym, pic_sym); diff --git a/src/file.c b/src/file.c index 7c15d28d..307a6688 100644 --- a/src/file.c +++ b/src/file.c @@ -105,10 +105,14 @@ pic_file_delete(pic_state *pic) void pic_init_file(pic_state *pic) { - pic_defun(pic, "open-input-file", pic_file_open_input_file); - pic_defun(pic, "open-input-binary-file", pic_file_open_input_binary_file); - pic_defun(pic, "open-output-file", pic_file_open_output_file); - pic_defun(pic, "open-output-binary-file", pic_file_open_output_binary_file); - pic_defun(pic, "file-exists?", pic_file_exists_p); - pic_defun(pic, "delete-file", pic_file_delete); + DEFLIBRARY(pic, "(scheme file)") + { + pic_defun(pic, "open-input-file", pic_file_open_input_file); + pic_defun(pic, "open-input-binary-file", pic_file_open_input_binary_file); + pic_defun(pic, "open-output-file", pic_file_open_output_file); + pic_defun(pic, "open-output-binary-file", pic_file_open_output_binary_file); + pic_defun(pic, "file-exists?", pic_file_exists_p); + pic_defun(pic, "delete-file", pic_file_delete); + } + ENDLIBRARY(pic) } diff --git a/src/port.c b/src/port.c index fb5abd3c..312a57c1 100644 --- a/src/port.c +++ b/src/port.c @@ -331,11 +331,16 @@ pic_init_port(pic_state *pic) pic_defun(pic, "port?", pic_port_port_p); pic_defun(pic, "input-port-open?", pic_port_input_port_open_p); pic_defun(pic, "output-port-open?", pic_port_output_port_open_p); - pic_defun(pic, "write", pic_port_write); - pic_defun(pic, "newline", pic_port_newline); pic_defun(pic, "eof-object?", pic_port_eof_object_p); pic_defun(pic, "eof-object", pic_port_eof_object); pic_defun(pic, "close-port", pic_port_close_port); pic_defun(pic, "close-input-port", pic_port_close_port); pic_defun(pic, "close-output-port", pic_port_close_port); + + DEFLIBRARY(pic, "(scheme write)") + { + pic_defun(pic, "write", pic_port_write); + pic_defun(pic, "newline", pic_port_newline); + } + ENDLIBRARY(pic) } diff --git a/src/system.c b/src/system.c index 097389e0..8d7f2e00 100644 --- a/src/system.c +++ b/src/system.c @@ -115,9 +115,13 @@ pic_system_getenvs(pic_state *pic) void pic_init_system(pic_state *pic) { - pic_defun(pic, "command-line", pic_system_cmdline); - pic_defun(pic, "exit", pic_system_exit); - pic_defun(pic, "emergency-exit", pic_system_emergency_exit); - pic_defun(pic, "get-environment-variable", pic_system_getenv); - pic_defun(pic, "get-environment-variables", pic_system_getenvs); + DEFLIBRARY(pic, "(scheme process-context)") + { + pic_defun(pic, "command-line", pic_system_cmdline); + pic_defun(pic, "exit", pic_system_exit); + pic_defun(pic, "emergency-exit", pic_system_emergency_exit); + pic_defun(pic, "get-environment-variable", pic_system_getenv); + pic_defun(pic, "get-environment-variables", pic_system_getenvs); + } + ENDLIBRARY(pic) } diff --git a/src/time.c b/src/time.c index e6076b24..9bdd8aa0 100644 --- a/src/time.c +++ b/src/time.c @@ -37,7 +37,11 @@ pic_jiffies_per_second(pic_state *pic) void pic_init_time(pic_state *pic) { - pic_defun(pic, "current-second", pic_current_second); - pic_defun(pic, "current-jiffy", pic_current_jiffy); - pic_defun(pic, "jiffies-per-second", pic_jiffies_per_second); + DEFLIBRARY(pic, "(scheme time)") + { + pic_defun(pic, "current-second", pic_current_second); + pic_defun(pic, "current-jiffy", pic_current_jiffy); + pic_defun(pic, "jiffies-per-second", pic_jiffies_per_second); + } + ENDLIBRARY(pic) }