picrin/extlib/benz/state.c

484 lines
11 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2015-06-25 13:09:06 -04:00
void
pic_set_argv(pic_state *pic, int argc, char *argv[], char **envp)
{
pic->argc = argc;
pic->argv = argv;
pic->envp = envp;
}
2015-01-26 00:31:36 -05:00
void
pic_add_feature(pic_state *pic, const char *feature)
{
pic_push(pic, pic_obj_value(pic_intern_cstr(pic, feature)), pic->features);
}
2015-06-09 03:19:57 -04:00
void pic_init_undef(pic_state *);
2015-01-26 00:31:36 -05:00
void pic_init_bool(pic_state *);
void pic_init_pair(pic_state *);
void pic_init_port(pic_state *);
void pic_init_number(pic_state *);
void pic_init_proc(pic_state *);
void pic_init_symbol(pic_state *);
void pic_init_vector(pic_state *);
void pic_init_blob(pic_state *);
void pic_init_cont(pic_state *);
void pic_init_char(pic_state *);
void pic_init_error(pic_state *);
void pic_init_str(pic_state *);
void pic_init_macro(pic_state *);
void pic_init_var(pic_state *);
void pic_init_write(pic_state *);
void pic_init_read(pic_state *);
void pic_init_dict(pic_state *);
void pic_init_record(pic_state *);
void pic_init_eval(pic_state *);
void pic_init_lib(pic_state *);
void pic_init_attr(pic_state *);
2015-06-09 05:50:46 -04:00
void pic_init_reg(pic_state *);
2015-01-26 00:31:36 -05:00
2015-01-31 07:14:14 -05:00
extern const char pic_boot[][80];
2015-01-26 00:31:36 -05:00
static void
pic_init_features(pic_state *pic)
{
pic_add_feature(pic, "picrin");
#if PIC_ENABLE_FLOAT
2015-01-26 00:31:36 -05:00
pic_add_feature(pic, "ieee-float");
#endif
2015-01-26 00:31:36 -05:00
#if _POSIX_SOURCE
pic_add_feature(pic, "posix");
#endif
#if _WIN32
pic_add_feature(pic, "windows");
#endif
#if __unix__
pic_add_feature(pic, "unix");
#endif
#if __gnu_linux__
pic_add_feature(pic, "gnu-linux");
#endif
#if __FreeBSD__
pic_add_feature(pic, "freebsd");
#endif
#if __i386__
pic_add_feature(pic, "i386");
#elif __x86_64__
pic_add_feature(pic, "x86-64");
#elif __ppc__
pic_add_feature(pic, "ppc");
#elif __sparc__
pic_add_feature(pic, "sparc");
#endif
#if __ILP32__
pic_add_feature(pic, "ilp32");
#elif __LP64__
pic_add_feature(pic, "lp64");
#endif
#if defined(__BYTE_ORDER__)
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
pic_add_feature(pic, "little-endian");
# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
pic_add_feature(pic, "big-endian");
# endif
#else
# if __LITTLE_ENDIAN__
pic_add_feature(pic, "little-endian");
# elif __BIG_ENDIAN__
pic_add_feature(pic, "big-endian");
# endif
#endif
}
static pic_value
pic_features(pic_state *pic)
{
pic_get_args(pic, "");
return pic->features;
}
2015-01-26 00:31:36 -05:00
#define DONE pic_gc_arena_restore(pic, ai);
2015-06-27 04:43:42 -04:00
#define define_builtin_syntax(uid, name) \
pic_define_syntactic_keyword_(pic, pic->lib->env, pic_intern_cstr(pic, name), uid)
2015-07-01 17:17:27 -04:00
#define VM(uid, name) \
pic_define_syntactic_keyword_(pic, pic->lib->env, pic_intern_cstr(pic, name), uid)
#define VM2(proc, name) \
proc = pic_ref(pic, pic->lib, name)
2015-01-26 00:31:36 -05:00
static void
pic_init_core(pic_state *pic)
{
2015-06-27 04:43:42 -04:00
void pic_define_syntactic_keyword_(pic_state *, struct pic_env *, pic_sym *, pic_sym *);
2015-06-09 07:20:56 -04:00
2015-01-26 00:31:36 -05:00
pic_init_features(pic);
pic_deflibrary (pic, "(picrin base)") {
2015-05-27 10:08:47 -04:00
size_t ai = pic_gc_arena_preserve(pic);
2015-06-27 04:43:42 -04:00
define_builtin_syntax(pic->uDEFINE, "builtin:define");
define_builtin_syntax(pic->uSETBANG, "builtin:set!");
define_builtin_syntax(pic->uQUOTE, "builtin:quote");
define_builtin_syntax(pic->uLAMBDA, "builtin:lambda");
define_builtin_syntax(pic->uIF, "builtin:if");
define_builtin_syntax(pic->uBEGIN, "builtin:begin");
define_builtin_syntax(pic->uDEFINE_MACRO, "builtin:define-macro");
2015-01-26 00:31:36 -05:00
pic_defun(pic, "features", pic_features);
2015-07-01 17:17:27 -04:00
VM(pic->uCONS, "cons");
VM(pic->uCAR, "car");
VM(pic->uCDR, "cdr");
VM(pic->uNILP, "null?");
VM(pic->uSYMBOLP, "symbol?");
VM(pic->uPAIRP, "pair?");
VM(pic->uNOT, "not");
VM(pic->uADD, "+");
VM(pic->uSUB, "-");
VM(pic->uMUL, "*");
VM(pic->uDIV, "/");
VM(pic->uEQ, "=");
VM(pic->uLT, "<");
VM(pic->uLE, "<=");
2015-07-06 06:13:00 -04:00
VM(pic->uGT, ">");
VM(pic->uGE, ">=");
2015-07-01 17:17:27 -04:00
2015-06-09 03:19:57 -04:00
pic_init_undef(pic); DONE;
2015-01-26 00:31:36 -05:00
pic_init_bool(pic); DONE;
pic_init_pair(pic); DONE;
pic_init_port(pic); DONE;
pic_init_number(pic); DONE;
pic_init_proc(pic); DONE;
pic_init_symbol(pic); DONE;
pic_init_vector(pic); DONE;
pic_init_blob(pic); DONE;
pic_init_cont(pic); DONE;
pic_init_char(pic); DONE;
pic_init_error(pic); DONE;
pic_init_str(pic); DONE;
pic_init_macro(pic); DONE;
pic_init_var(pic); DONE;
pic_init_write(pic); DONE;
pic_init_read(pic); DONE;
pic_init_dict(pic); DONE;
pic_init_record(pic); DONE;
pic_init_eval(pic); DONE;
pic_init_lib(pic); DONE;
pic_init_attr(pic); DONE;
2015-06-09 05:50:46 -04:00
pic_init_reg(pic); DONE;
2015-01-26 00:31:36 -05:00
VM2(pic->pCONS, "cons");
VM2(pic->pCAR, "car");
VM2(pic->pCDR, "cdr");
VM2(pic->pNILP, "null?");
VM2(pic->pSYMBOLP, "symbol?");
VM2(pic->pPAIRP, "pair?");
VM2(pic->pNOT, "not");
VM2(pic->pADD, "+");
VM2(pic->pSUB, "-");
VM2(pic->pMUL, "*");
VM2(pic->pDIV, "/");
VM2(pic->pEQ, "=");
VM2(pic->pLT, "<");
VM2(pic->pLE, "<=");
2015-07-06 06:13:00 -04:00
VM2(pic->pGT, ">");
VM2(pic->pGE, ">=");
pic_try {
pic_load_cstr(pic, &pic_boot[0][0]);
}
pic_catch {
pic_print_backtrace(pic, xstdout);
pic_panic(pic, "");
}
2015-01-26 00:31:36 -05:00
}
2015-06-16 12:52:20 -04:00
pic_import(pic, pic->PICRIN_BASE);
2015-01-26 00:31:36 -05:00
}
2014-08-25 00:38:09 -04:00
pic_state *
2015-06-25 13:09:06 -04:00
pic_open(pic_allocf allocf, void *userdata)
2014-08-25 00:38:09 -04:00
{
struct pic_port *pic_make_standard_port(pic_state *, xFILE *, short);
2014-08-25 00:38:09 -04:00
char t;
pic_state *pic;
size_t ai;
2015-05-27 13:12:26 -04:00
pic = allocf(NULL, sizeof(pic_state));
2014-08-25 00:38:09 -04:00
2015-05-27 10:08:47 -04:00
if (! pic) {
goto EXIT_PIC;
}
2015-05-27 13:12:26 -04:00
/* allocator */
pic->allocf = allocf;
2015-06-25 13:09:06 -04:00
/* user data */
pic->userdata = userdata;
2015-01-18 22:14:29 -05:00
/* turn off GC */
pic->gc_enable = false;
2015-06-22 14:02:12 -04:00
/* continuation chain */
pic->cc = NULL;
pic->ccnt = 0;
2014-08-25 00:38:09 -04:00
/* root block */
2015-06-08 08:04:04 -04:00
pic->cp = NULL;
2014-08-25 00:38:09 -04:00
/* command line */
2015-06-25 13:09:06 -04:00
pic->argc = 0;
pic->argv = NULL;
pic->envp = NULL;
2014-08-25 00:38:09 -04:00
/* prepare VM stack */
2015-05-27 13:12:26 -04:00
pic->stbase = pic->sp = allocf(NULL, PIC_STACK_SIZE * sizeof(pic_value));
2014-08-25 00:38:09 -04:00
pic->stend = pic->stbase + PIC_STACK_SIZE;
2015-05-27 10:08:47 -04:00
if (! pic->sp) {
goto EXIT_SP;
}
2014-08-25 00:38:09 -04:00
/* callinfo */
2015-05-27 13:12:26 -04:00
pic->cibase = pic->ci = allocf(NULL, PIC_STACK_SIZE * sizeof(pic_callinfo));
2014-08-25 00:38:09 -04:00
pic->ciend = pic->cibase + PIC_STACK_SIZE;
2015-05-27 10:08:47 -04:00
if (! pic->ci) {
goto EXIT_CI;
}
2014-09-18 01:50:01 -04:00
/* exception handler */
2015-05-27 13:12:26 -04:00
pic->xpbase = pic->xp = allocf(NULL, PIC_RESCUE_SIZE * sizeof(struct pic_proc *));
2014-09-18 01:50:01 -04:00
pic->xpend = pic->xpbase + PIC_RESCUE_SIZE;
2015-05-27 10:08:47 -04:00
if (! pic->xp) {
goto EXIT_XP;
}
/* GC arena */
2015-05-27 13:12:26 -04:00
pic->arena = allocf(NULL, PIC_ARENA_SIZE * sizeof(struct pic_object *));
2015-05-27 10:08:47 -04:00
pic->arena_size = PIC_ARENA_SIZE;
pic->arena_idx = 0;
if (! pic->arena) {
goto EXIT_ARENA;
}
2014-08-25 00:38:09 -04:00
/* memory heap */
pic->heap = pic_heap_open(pic);
2014-08-25 00:38:09 -04:00
2015-06-09 05:31:46 -04:00
/* registries */
pic->regs = NULL;
2014-08-25 00:38:09 -04:00
/* symbol table */
2015-06-24 18:05:41 -04:00
kh_init(s, &pic->syms);
2014-08-25 00:38:09 -04:00
/* unique symbol count */
pic->ucnt = 0;
2014-08-25 00:38:09 -04:00
/* global variables */
pic->globals = NULL;
2014-08-25 00:38:09 -04:00
/* macros */
pic->macros = NULL;
2014-08-25 00:38:09 -04:00
2014-09-24 20:44:21 -04:00
/* attributes */
2015-06-09 05:31:46 -04:00
pic->attrs = NULL;
2014-09-24 20:44:21 -04:00
2014-09-09 12:41:10 -04:00
/* features */
pic->features = pic_nil_value();
2014-08-25 00:38:09 -04:00
/* libraries */
pic->libs = pic_nil_value();
pic->lib = NULL;
2014-09-18 01:50:01 -04:00
/* raised error object */
2015-06-09 03:02:23 -04:00
pic->err = pic_invalid_value();
2014-08-25 00:38:09 -04:00
2015-06-18 14:06:57 -04:00
/* file pool */
2015-06-18 16:00:36 -04:00
memset(pic->files, 0, sizeof pic->files);
2015-06-18 14:06:57 -04:00
2015-06-08 09:28:17 -04:00
/* parameter table */
pic->ptable = pic_nil_value();
2014-08-25 00:38:09 -04:00
/* native stack marker */
pic->native_stack_start = &t;
2015-01-18 22:14:29 -05:00
ai = pic_gc_arena_preserve(pic);
2015-06-09 12:06:56 -04:00
#define S(slot,name) pic->slot = pic_intern_cstr(pic, name)
2014-08-25 00:38:09 -04:00
2014-09-16 02:02:47 -04:00
S(sQUOTE, "quote");
S(sQUASIQUOTE, "quasiquote");
S(sUNQUOTE, "unquote");
S(sUNQUOTE_SPLICING, "unquote-splicing");
2015-06-10 02:18:03 -04:00
S(sSYNTAX_QUOTE, "syntax-quote");
S(sSYNTAX_QUASIQUOTE, "syntax-quasiquote");
S(sSYNTAX_UNQUOTE, "syntax-unquote");
S(sSYNTAX_UNQUOTE_SPLICING, "syntax-unquote-splicing");
2014-09-16 02:02:47 -04:00
S(sIMPORT, "import");
S(sEXPORT, "export");
S(sDEFINE_LIBRARY, "define-library");
S(sCOND_EXPAND, "cond-expand");
2015-01-19 00:35:42 -05:00
S(sCALL, "call");
S(sGREF, "gref");
S(sLREF, "lref");
S(sCREF, "cref");
2014-08-25 00:38:09 -04:00
pic_gc_arena_restore(pic, ai);
#define U(slot,name) pic->slot = pic_uniq(pic, pic_obj_value(pic_intern_cstr(pic, name)))
2015-06-09 12:06:56 -04:00
U(uDEFINE, "define");
U(uLAMBDA, "lambda");
U(uIF, "if");
U(uBEGIN, "begin");
U(uSETBANG, "set!");
U(uQUOTE, "quote");
U(uDEFINE_MACRO, "define-macro");
2015-06-09 12:06:56 -04:00
U(uIMPORT, "import");
U(uEXPORT, "export");
U(uDEFINE_LIBRARY, "define-library");
U(uCOND_EXPAND, "cond-expand");
U(uCONS, "cons");
U(uCAR, "car");
U(uCDR, "cdr");
U(uNILP, "null?");
U(uSYMBOLP, "symbol?");
U(uPAIRP, "pair?");
U(uADD, "+");
U(uSUB, "-");
U(uMUL, "*");
U(uDIV, "/");
U(uEQ, "=");
U(uLT, "<");
U(uLE, "<=");
U(uGT, ">");
U(uGE, ">=");
U(uNOT, "not");
2014-08-25 00:38:09 -04:00
pic_gc_arena_restore(pic, ai);
/* system procedures */
pic->pCONS = pic_invalid_value();
pic->pCAR = pic_invalid_value();
pic->pCDR = pic_invalid_value();
pic->pNILP = pic_invalid_value();
pic->pSYMBOLP = pic_invalid_value();
pic->pPAIRP = pic_invalid_value();
pic->pNOT = pic_invalid_value();
pic->pADD = pic_invalid_value();
pic->pSUB = pic_invalid_value();
pic->pMUL = pic_invalid_value();
pic->pDIV = pic_invalid_value();
pic->pEQ = pic_invalid_value();
pic->pLT = pic_invalid_value();
pic->pLE = pic_invalid_value();
2015-07-06 06:13:00 -04:00
pic->pGT = pic_invalid_value();
pic->pGE = pic_invalid_value();
/* root tables */
pic->globals = pic_make_dict(pic);
pic->macros = pic_make_dict(pic);
2015-06-09 05:31:46 -04:00
pic->attrs = pic_make_reg(pic);
2014-08-25 00:38:09 -04:00
/* root block */
2015-06-22 04:06:13 -04:00
pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TT_CP);
2015-06-08 08:04:04 -04:00
pic->cp->prev = NULL;
pic->cp->depth = 0;
pic->cp->in = pic->cp->out = NULL;
2014-08-25 00:38:09 -04:00
2015-01-18 22:14:29 -05:00
/* reader */
2015-06-18 14:14:55 -04:00
pic_reader_init(pic);
2015-06-08 09:28:17 -04:00
/* parameter table */
2015-06-24 16:56:47 -04:00
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_reg(pic)), pic->ptable);
2015-06-08 09:28:17 -04:00
/* standard libraries */
2015-05-30 11:25:40 -04:00
pic->PICRIN_BASE = pic_make_library(pic, pic_read_cstr(pic, "(picrin base)"));
pic->PICRIN_USER = pic_make_library(pic, pic_read_cstr(pic, "(picrin user)"));
pic->lib = pic->PICRIN_USER;
pic->prev_lib = NULL;
2014-08-25 00:38:09 -04:00
2015-01-18 22:14:29 -05:00
pic_gc_arena_restore(pic, ai);
/* turn on GC */
pic->gc_enable = true;
pic_init_core(pic);
2014-08-25 00:38:09 -04:00
2015-05-27 10:08:47 -04:00
pic_gc_arena_restore(pic, ai);
2014-08-25 00:38:09 -04:00
return pic;
2015-05-27 10:08:47 -04:00
EXIT_ARENA:
2015-05-27 13:12:26 -04:00
allocf(pic->xp, 0);
2015-05-27 10:08:47 -04:00
EXIT_XP:
2015-05-27 13:12:26 -04:00
allocf(pic->ci, 0);
2015-05-27 10:08:47 -04:00
EXIT_CI:
2015-05-27 13:12:26 -04:00
allocf(pic->sp, 0);
2015-05-27 10:08:47 -04:00
EXIT_SP:
2015-05-27 13:12:26 -04:00
allocf(pic, 0);
2015-05-27 10:08:47 -04:00
EXIT_PIC:
return NULL;
2014-08-25 00:38:09 -04:00
}
void
pic_close(pic_state *pic)
{
2015-06-24 18:05:41 -04:00
khash_t(s) *h = &pic->syms;
2015-05-27 13:12:26 -04:00
pic_allocf allocf = pic->allocf;
2014-08-25 00:38:09 -04:00
/* clear out root objects */
pic->sp = pic->stbase;
pic->ci = pic->cibase;
2014-09-18 01:50:01 -04:00
pic->xp = pic->xpbase;
2014-08-25 00:38:09 -04:00
pic->arena_idx = 0;
2015-06-09 03:02:23 -04:00
pic->err = pic_invalid_value();
pic->globals = NULL;
pic->macros = NULL;
2015-06-09 05:31:46 -04:00
pic->attrs = NULL;
2014-09-09 12:41:10 -04:00
pic->features = pic_nil_value();
2014-08-25 00:38:09 -04:00
pic->libs = pic_nil_value();
/* free all heap objects */
pic_gc_run(pic);
2015-06-10 09:37:05 -04:00
/* flush all xfiles */
2015-06-18 13:05:56 -04:00
xfflush(pic, NULL);
2015-06-10 09:37:05 -04:00
2014-08-25 00:38:09 -04:00
/* free heaps */
pic_heap_close(pic, pic->heap);
2014-08-25 00:38:09 -04:00
/* free reader struct */
2015-06-18 14:14:55 -04:00
pic_reader_destroy(pic);
2014-08-25 00:38:09 -04:00
/* free runtime context */
2015-05-27 13:12:26 -04:00
allocf(pic->stbase, 0);
allocf(pic->cibase, 0);
allocf(pic->xpbase, 0);
2014-08-25 00:38:09 -04:00
/* free global stacks */
2015-06-24 18:05:41 -04:00
kh_destroy(s, h);
2014-08-25 00:38:09 -04:00
/* free GC arena */
2015-05-27 13:12:26 -04:00
allocf(pic->arena, 0);
2014-08-25 00:38:09 -04:00
2015-05-27 13:12:26 -04:00
allocf(pic, 0);
2014-08-25 00:38:09 -04:00
}