2013-10-10 03:15:41 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2013-10-13 02:14:15 -04:00
|
|
|
#include "picrin/gc.h"
|
2013-10-15 08:53:45 -04:00
|
|
|
#include "picrin/proc.h"
|
2013-11-26 11:30:30 -05:00
|
|
|
#include "picrin/macro.h"
|
2013-10-22 09:22:35 -04:00
|
|
|
#include "xhash/xhash.h"
|
2013-10-11 04:36:51 -04:00
|
|
|
|
2013-10-15 08:14:33 -04:00
|
|
|
void pic_init_core(pic_state *);
|
|
|
|
|
2013-10-10 03:15:41 -04:00
|
|
|
pic_state *
|
2013-10-20 22:51:02 -04:00
|
|
|
pic_open(int argc, char *argv[], char **envp)
|
2013-10-10 03:15:41 -04:00
|
|
|
{
|
2013-11-09 00:14:25 -05:00
|
|
|
pic_value t;
|
|
|
|
|
2013-10-10 03:15:41 -04:00
|
|
|
pic_state *pic;
|
2013-10-20 22:44:23 -04:00
|
|
|
int ai;
|
2013-10-10 03:15:41 -04:00
|
|
|
|
2013-10-11 04:36:51 -04:00
|
|
|
pic = (pic_state *)malloc(sizeof(pic_state));
|
2013-10-11 11:20:53 -04:00
|
|
|
|
2013-10-20 22:51:02 -04:00
|
|
|
/* command line */
|
|
|
|
pic->argc = argc;
|
|
|
|
pic->argv = argv;
|
|
|
|
pic->envp = envp;
|
|
|
|
|
2013-11-11 04:04:21 -05:00
|
|
|
/* root block */
|
|
|
|
pic->blk = (struct pic_block *)malloc(sizeof(struct pic_block));
|
|
|
|
pic->blk->prev = NULL;
|
|
|
|
pic->blk->depth = 0;
|
|
|
|
pic->blk->in = pic->blk->out = NULL;
|
2013-11-14 21:51:58 -05:00
|
|
|
pic->blk->refcnt = 1;
|
2013-11-11 04:04:21 -05:00
|
|
|
|
2013-10-11 11:20:53 -04:00
|
|
|
/* prepare VM stack */
|
2013-11-22 06:28:09 -05:00
|
|
|
pic->stbase = pic->sp = (pic_value *)calloc(PIC_STACK_SIZE, sizeof(pic_value));
|
2013-10-15 10:28:23 -04:00
|
|
|
pic->stend = pic->stbase + PIC_STACK_SIZE;
|
2013-10-11 11:20:53 -04:00
|
|
|
|
2013-10-15 10:29:34 -04:00
|
|
|
/* callinfo */
|
2013-11-22 06:28:09 -05:00
|
|
|
pic->cibase = pic->ci = (pic_callinfo *)calloc(PIC_STACK_SIZE, sizeof(pic_callinfo));
|
2013-11-09 00:11:54 -05:00
|
|
|
pic->ciend = pic->cibase + PIC_STACK_SIZE;
|
2013-10-15 10:29:34 -04:00
|
|
|
|
2013-11-17 02:02:58 -05:00
|
|
|
/* exception handlers */
|
2013-11-22 06:28:09 -05:00
|
|
|
pic->rescue = (struct pic_proc **)calloc(PIC_RESCUE_SIZE, sizeof(struct pic_proc *));
|
2013-11-17 02:02:58 -05:00
|
|
|
pic->ridx = 0;
|
|
|
|
pic->rlen = PIC_RESCUE_SIZE;
|
|
|
|
|
2013-10-13 02:14:15 -04:00
|
|
|
/* memory heap */
|
2013-11-22 10:19:31 -05:00
|
|
|
pic->heap = (struct pic_heap *)calloc(1, sizeof(struct pic_heap));
|
|
|
|
init_heap(pic->heap);
|
2013-10-13 02:14:15 -04:00
|
|
|
|
2013-10-20 01:05:35 -04:00
|
|
|
/* symbol table */
|
2013-10-28 13:11:31 -04:00
|
|
|
pic->sym_tbl = xh_new();
|
2013-11-22 06:28:09 -05:00
|
|
|
pic->sym_pool = (const char **)calloc(PIC_SYM_POOL_SIZE, sizeof(const char *));
|
2013-10-28 13:11:31 -04:00
|
|
|
pic->slen = 0;
|
|
|
|
pic->scapa = pic->slen + PIC_SYM_POOL_SIZE;
|
2013-11-26 02:38:39 -05:00
|
|
|
pic->uniq_sym_count = 0;
|
2013-10-20 01:05:35 -04:00
|
|
|
|
2013-10-15 22:53:25 -04:00
|
|
|
/* irep */
|
2013-11-22 06:28:09 -05:00
|
|
|
pic->irep = (struct pic_irep **)calloc(PIC_IREP_SIZE, sizeof(struct pic_irep *));
|
2013-10-15 22:53:25 -04:00
|
|
|
pic->ilen = 0;
|
|
|
|
pic->icapa = PIC_IREP_SIZE;
|
|
|
|
|
2013-10-17 11:15:15 -04:00
|
|
|
/* globals */
|
2013-10-22 09:22:35 -04:00
|
|
|
pic->global_tbl = xh_new();
|
2013-11-22 06:28:09 -05:00
|
|
|
pic->globals = (pic_value *)calloc(PIC_GLOBALS_SIZE, sizeof(pic_value));
|
2013-10-17 11:15:15 -04:00
|
|
|
pic->glen = 0;
|
|
|
|
pic->gcapa = PIC_GLOBALS_SIZE;
|
|
|
|
|
2013-12-07 08:36:14 -05:00
|
|
|
/* libraries */
|
|
|
|
pic->lib_tbl = pic_nil_value();
|
|
|
|
pic->lib = NULL;
|
2013-11-26 11:30:30 -05:00
|
|
|
|
2013-10-20 20:29:56 -04:00
|
|
|
/* pool */
|
2013-11-22 06:28:09 -05:00
|
|
|
pic->pool = (pic_value *)calloc(PIC_POOL_SIZE, sizeof(pic_value));
|
2013-10-20 20:29:56 -04:00
|
|
|
pic->plen = 0;
|
|
|
|
pic->pcapa = PIC_POOL_SIZE;
|
|
|
|
|
2013-10-20 05:17:12 -04:00
|
|
|
/* error handling */
|
|
|
|
pic->jmp = NULL;
|
|
|
|
pic->errmsg = NULL;
|
|
|
|
|
2013-10-13 04:02:29 -04:00
|
|
|
/* GC arena */
|
|
|
|
pic->arena_idx = 0;
|
|
|
|
|
2013-11-09 00:14:25 -05:00
|
|
|
/* native stack marker */
|
|
|
|
pic->native_stack_start = &t;
|
|
|
|
|
2013-11-26 09:40:48 -05:00
|
|
|
#define register_core_symbol(pic,slot,name) do { \
|
|
|
|
pic->slot = pic_intern_cstr(pic, name); \
|
|
|
|
} while (0)
|
|
|
|
|
2013-10-20 22:44:23 -04:00
|
|
|
ai = pic_gc_arena_preserve(pic);
|
2013-11-26 09:40:48 -05:00
|
|
|
register_core_symbol(pic, sDEFINE, "define");
|
|
|
|
register_core_symbol(pic, sLAMBDA, "lambda");
|
|
|
|
register_core_symbol(pic, sIF, "if");
|
|
|
|
register_core_symbol(pic, sBEGIN, "begin");
|
|
|
|
register_core_symbol(pic, sSETBANG, "set!");
|
|
|
|
register_core_symbol(pic, sQUOTE, "quote");
|
|
|
|
register_core_symbol(pic, sQUASIQUOTE, "quasiquote");
|
|
|
|
register_core_symbol(pic, sUNQUOTE, "unquote");
|
|
|
|
register_core_symbol(pic, sUNQUOTE_SPLICING, "unquote-splicing");
|
|
|
|
register_core_symbol(pic, sDEFINE_SYNTAX, "define-syntax");
|
2013-11-26 11:39:37 -05:00
|
|
|
register_core_symbol(pic, sDEFINE_MACRO, "define-macro");
|
2013-12-07 05:31:04 -05:00
|
|
|
register_core_symbol(pic, sDEFINE_LIBRARY, "define-library");
|
|
|
|
register_core_symbol(pic, sIMPORT, "import");
|
|
|
|
register_core_symbol(pic, sEXPORT, "export");
|
2013-11-26 09:40:48 -05:00
|
|
|
register_core_symbol(pic, sCONS, "cons");
|
|
|
|
register_core_symbol(pic, sCAR, "car");
|
|
|
|
register_core_symbol(pic, sCDR, "cdr");
|
|
|
|
register_core_symbol(pic, sNILP, "null?");
|
|
|
|
register_core_symbol(pic, sADD, "+");
|
|
|
|
register_core_symbol(pic, sSUB, "-");
|
|
|
|
register_core_symbol(pic, sMUL, "*");
|
|
|
|
register_core_symbol(pic, sDIV, "/");
|
|
|
|
register_core_symbol(pic, sEQ, "=");
|
|
|
|
register_core_symbol(pic, sLT, "<");
|
|
|
|
register_core_symbol(pic, sLE, "<=");
|
|
|
|
register_core_symbol(pic, sGT, ">");
|
|
|
|
register_core_symbol(pic, sGE, ">=");
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
2013-11-26 11:32:05 -05:00
|
|
|
|
2013-12-07 10:05:22 -05:00
|
|
|
/* set library */
|
|
|
|
pic_make_library(pic, pic_symbol_value(pic_intern_cstr(pic, "user")));
|
|
|
|
pic_in_library(pic, pic_symbol_value(pic_intern_cstr(pic, "user")));
|
|
|
|
|
2013-10-22 09:22:35 -04:00
|
|
|
pic_init_core(pic);
|
|
|
|
|
2013-10-10 03:15:41 -04:00
|
|
|
return pic;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_close(pic_state *pic)
|
|
|
|
{
|
|
|
|
free(pic);
|
|
|
|
}
|