From 74b44f4d84b8ce04ee2db0210810dcbd2cc5bf4d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 6 Apr 2014 02:43:49 +0900 Subject: [PATCH 1/5] add pic_load_cstr --- include/picrin.h | 1 + src/load.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/picrin.h b/include/picrin.h index 0869708d..7f44c8e2 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -155,6 +155,7 @@ pic_list pic_parse_file(pic_state *, FILE *); /* #f for incomplete input */ pic_list pic_parse_cstr(pic_state *, const char *); pic_value pic_load(pic_state *, const char *); +pic_value pic_load_cstr(pic_state *, const char *); pic_value pic_apply(pic_state *, struct pic_proc *, pic_value); pic_value pic_apply0(pic_state *, struct pic_proc *); diff --git a/src/load.c b/src/load.c index 6b219726..ec95b4dc 100644 --- a/src/load.c +++ b/src/load.c @@ -5,6 +5,34 @@ #include "picrin.h" #include "picrin/pair.h" +pic_value +pic_load_cstr(pic_state *pic, const char *src) +{ + int ai; + pic_value v, exprs; + struct pic_proc *proc; + + exprs = pic_parse_cstr(pic, src); + if (pic_undef_p(exprs)) { + pic_error(pic, "load: unexpected EOF"); + } + + pic_for_each (v, exprs) { + ai = pic_gc_arena_preserve(pic); + + proc = pic_compile(pic, v); + if (proc == NULL) { + pic_error(pic, "load: compilation failure"); + } + + pic_apply(pic, proc, pic_nil_value()); + + pic_gc_arena_restore(pic, ai); + } + + return pic_none_value(); +} + pic_value pic_load(pic_state *pic, const char *fn) { From d0e14a4acbc8e1c4efd060910467201ff9346c89 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 6 Apr 2014 02:44:38 +0900 Subject: [PATCH 2/5] s/pic_load_stdlib/pic_load_piclib/g --- src/init.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/init.c b/src/init.c index a322caff..b4b8b898 100644 --- a/src/init.c +++ b/src/init.c @@ -31,7 +31,7 @@ void pic_init_load(pic_state *); void pic_init_write(pic_state *); void -pic_load_stdlib(pic_state *pic) +pic_load_piclib(pic_state *pic) { pic_try { pic_load(pic, "piclib/built-in.scm"); @@ -114,7 +114,7 @@ pic_init_core(pic_state *pic) pic_init_load(pic); DONE; pic_init_write(pic); DONE; - pic_load_stdlib(pic); DONE; + pic_load_piclib(pic); DONE; pic_init_contrib(pic); DONE; From 4f4c3e24c52d63d9075810b7a6d24a6f53670606 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 6 Apr 2014 02:47:14 +0900 Subject: [PATCH 3/5] embed scheme library files into a c source file --- etc/libemb.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/init.c | 22 +---------------- 2 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 etc/libemb.py diff --git a/etc/libemb.py b/etc/libemb.py new file mode 100644 index 00000000..38ec9482 --- /dev/null +++ b/etc/libemb.py @@ -0,0 +1,66 @@ +import re +import os +from collections import OrderedDict + +outfn = 'src/load_piclib.c' + +if os.path.exists(outfn): + os.remove(outfn) + +piclibs = OrderedDict() +piclibs['piclib/built-in.scm'] = 'piclib_built_in' +piclibs['piclib/srfi/1.scm'] = 'piclib_srfi_1' +piclibs['piclib/srfi/26.scm'] = 'piclib_srfi_26' +piclibs['piclib/srfi/95.scm'] = 'piclib_srfi_95' + +def escape_scm(infn, outfn, vname): + with open(outfn, 'a') as output: + output.write('const char *{} =\n'.format(vname)) + with open(infn, 'r') as input: + for line in input: + output.write('"') + line = line.strip('\n') + line = re.sub('\\\\', '\\\\', line) + line = re.sub('\"', '\\"', line) + output.write(line) + output.write('\\n"\n') + output.write(';\n\n') + +piclib_load_head = """ +#include "picrin.h" +#include "picrin/error.h" +""" + +piclib_load_tail = """ +void +pic_load_piclib(pic_state *pic) +{{ + pic_try {{ + {} + }} + pic_catch {{ + /* error! */ + fputs("fatal error: failure in loading built-in.scm\\n", stderr); + fputs(pic_errmsg(pic), stderr); + abort(); + }} + +#if DEBUG + puts("successfully loaded stdlib"); +#endif + +}} +""" + +def gen_piclib_load_c(outfn): + with open(outfn, 'a') as f: + f.write(piclib_load_head) + loads = "" + for infn,vname in piclibs.items(): + escape_scm(infn, outfn, vname) + loads += "pic_load_cstr(pic, {});\n ".format(vname) + with open(outfn, 'a') as f: + f.write('\n\n') + f.write(piclib_load_tail.format(loads)) + +gen_piclib_load_c(outfn) diff --git a/src/init.c b/src/init.c index b4b8b898..22edcb13 100644 --- a/src/init.c +++ b/src/init.c @@ -30,27 +30,7 @@ void pic_init_var(pic_state *); void pic_init_load(pic_state *); void pic_init_write(pic_state *); -void -pic_load_piclib(pic_state *pic) -{ - pic_try { - pic_load(pic, "piclib/built-in.scm"); - pic_load(pic, "piclib/srfi/1.scm"); - pic_load(pic, "piclib/srfi/26.scm"); - pic_load(pic, "piclib/srfi/95.scm"); - } - pic_catch { - /* error! */ - fputs("fatal error: failure in loading built-in.scm\n", stderr); - fputs(pic_errmsg(pic), stderr); - abort(); - } - -#if DEBUG - puts("successfully loaded stdlib"); -#endif - -} +void pic_load_piclib(pic_state *); void pic_init_contrib(pic_state *pic) From ecd533babdfb35b18b39e1da52da1b9a25701fb5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 6 Apr 2014 02:47:40 +0900 Subject: [PATCH 4/5] run piclib generator at cmake run --- src/CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 93af4e22..65f58f6d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,12 @@ set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${PROJECT_SOURCE # xfile set(XFILE_SOURCES extlib/xfile/xfile.c) +# piclib +execute_process( + COMMAND python etc/libemb.py + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ) + # build! file(GLOB PICRIN_SOURCES ${PROJECT_SOURCE_DIR}/src/*.c) add_library(picrin SHARED ${PICRIN_SOURCES} ${FLEX_scan_OUTPUTS} ${XFILE_SOURCES} ${PICRIN_CONTRIB_SOURCES}) From c98ab2fedf4e593c74b3feea389ea54fa0701ac7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 6 Apr 2014 02:48:16 +0900 Subject: [PATCH 5/5] ignore src/load_piclib.c --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a2b1c37c..78488ae3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ build/* src/lex.yy.c src/lex.yy.h +src/load_piclib.c .dir-locals.el GPATH GRTAGS