From 4f4c3e24c52d63d9075810b7a6d24a6f53670606 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 6 Apr 2014 02:47:14 +0900 Subject: [PATCH] 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)