embed scheme library files into a c source file

This commit is contained in:
Yuichi Nishiwaki 2014-04-06 02:47:14 +09:00
parent d0e14a4acb
commit 4f4c3e24c5
2 changed files with 67 additions and 21 deletions

66
etc/libemb.py Normal file
View File

@ -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)

View File

@ -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)