add built-in library

This commit is contained in:
Yuichi Nishiwaki 2013-10-27 18:38:55 +09:00
parent f192c96650
commit 63c05209f8
2 changed files with 57 additions and 0 deletions

14
piclib/built-in.scm Normal file
View File

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

View File

@ -1,3 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "picrin.h" #include "picrin.h"
void pic_init_pair(pic_state *); 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_file(pic_state *);
void pic_init_proc(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); #define DONE pic_gc_arena_restore(pic, ai);
void void
@ -23,4 +64,6 @@ pic_init_core(pic_state *pic)
pic_init_system(pic); DONE; pic_init_system(pic); DONE;
pic_init_file(pic); DONE; pic_init_file(pic); DONE;
pic_init_proc(pic); DONE; pic_init_proc(pic); DONE;
pic_load_stdlib(pic); DONE;
} }