Merge branch 'issue-25'. close #25

This commit is contained in:
Yuichi Nishiwaki 2014-04-06 02:51:22 +09:00
commit 48d477d538
6 changed files with 104 additions and 22 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
build/*
src/lex.yy.c
src/lex.yy.h
src/load_piclib.c
.dir-locals.el
GPATH
GRTAGS

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

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

View File

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

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_stdlib(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)
@ -114,7 +94,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;

View File

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