From 63c05209f8743d6a6d4f230c0320fdae258fc909 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 27 Oct 2013 18:38:55 +0900 Subject: [PATCH] add built-in library --- piclib/built-in.scm | 14 ++++++++++++++ src/init.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 piclib/built-in.scm diff --git a/piclib/built-in.scm b/piclib/built-in.scm new file mode 100644 index 00000000..afe90b7a --- /dev/null +++ b/piclib/built-in.scm @@ -0,0 +1,14 @@ +(define (caar p) + (car (car p))) + +(define (cadr p) + (car (cdr p))) + +(define (cdar p) + (cdr (car p))) + +(define (cddr p) + (cdr (cdr p))) + +(define (list . args) + args) diff --git a/src/init.c b/src/init.c index 93886a2c..7db3fbd7 100644 --- a/src/init.c +++ b/src/init.c @@ -1,3 +1,6 @@ +#include +#include + #include "picrin.h" void pic_init_pair(pic_state *); @@ -8,6 +11,44 @@ void pic_init_system(pic_state *); void pic_init_file(pic_state *); void pic_init_proc(pic_state *); +void +pic_load_stdlib(pic_state *pic) +{ + static const char *fn = "piclib/built-in.scm"; + FILE *file; + bool r; + pic_value v; + struct pic_proc *proc; + + file = fopen(fn, "r"); + if (file == NULL) { + fputs("fatal error: could not read built-in.scm", stderr); + abort(); + } + + r = pic_parse_file(pic, file, &v); + if (! r) { + fputs("fatal error: built-in.scm broken", stderr); + abort(); + } + + proc = pic_codegen(pic, v); + if (proc == NULL) { + fputs("fatal error: built-in.scm compilation failure", stderr); + abort(); + } + + v = pic_run(pic, proc, pic_nil_value()); + if (pic_undef_p(v)) { + fputs("fatal error: built-in.scm evaluation failure", stderr); + abort(); + } + +#if DEBUG + puts("successfully loaded stdlib"); +#endif +} + #define DONE pic_gc_arena_restore(pic, ai); void @@ -23,4 +64,6 @@ pic_init_core(pic_state *pic) pic_init_system(pic); DONE; pic_init_file(pic); DONE; pic_init_proc(pic); DONE; + + pic_load_stdlib(pic); DONE; }