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-10-20 01:05:35 -04:00
|
|
|
#include "picrin/symbol.h"
|
2013-10-22 09:22:35 -04:00
|
|
|
#include "xhash/xhash.h"
|
2013-10-11 04:36:51 -04:00
|
|
|
|
2013-10-20 22:44:23 -04:00
|
|
|
struct sym_tbl *
|
|
|
|
sym_tbl_new()
|
|
|
|
{
|
|
|
|
struct sym_tbl *s_tbl;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
s_tbl = (struct sym_tbl *)malloc(sizeof(struct sym_tbl));
|
|
|
|
s_tbl->size = PIC_SYM_TBL_SIZE;
|
|
|
|
|
|
|
|
for (i = 0; i < PIC_SYM_TBL_SIZE; ++i) {
|
|
|
|
s_tbl->tbl[i] = pic_nil_value();
|
|
|
|
}
|
|
|
|
return s_tbl;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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-10-11 11:20:53 -04:00
|
|
|
/* prepare VM stack */
|
2013-10-15 10:28:23 -04:00
|
|
|
pic->stbase = pic->sp = (pic_value *)malloc(sizeof(pic_value) * PIC_STACK_SIZE);
|
|
|
|
pic->stend = pic->stbase + PIC_STACK_SIZE;
|
2013-10-11 11:20:53 -04:00
|
|
|
|
2013-10-15 10:29:34 -04:00
|
|
|
/* callinfo */
|
|
|
|
pic->cibase = pic->ci = (pic_callinfo *)malloc(sizeof(pic_callinfo) * PIC_STACK_SIZE);
|
|
|
|
pic->ciend = pic->ciend + PIC_STACK_SIZE;
|
|
|
|
|
2013-10-13 02:14:15 -04:00
|
|
|
/* memory heap */
|
|
|
|
pic->heap = (struct heap_page *)malloc(sizeof(struct heap_page));
|
|
|
|
init_heap_page(pic->heap);
|
|
|
|
|
2013-10-20 01:05:35 -04:00
|
|
|
/* symbol table */
|
|
|
|
pic->sym_tbl = sym_tbl_new();
|
|
|
|
|
2013-10-15 22:53:25 -04:00
|
|
|
/* irep */
|
|
|
|
pic->irep = (struct pic_irep **)malloc(sizeof(struct pic_irep *) * PIC_IREP_SIZE);
|
|
|
|
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-10-19 14:05:42 -04:00
|
|
|
pic->globals = (pic_value *)malloc(sizeof(pic_value) * PIC_GLOBALS_SIZE);
|
2013-10-17 11:15:15 -04:00
|
|
|
pic->glen = 0;
|
|
|
|
pic->gcapa = PIC_GLOBALS_SIZE;
|
|
|
|
|
2013-10-20 20:29:56 -04:00
|
|
|
/* pool */
|
|
|
|
pic->pool = (pic_value *)malloc(sizeof(pic_value) * PIC_POOL_SIZE);
|
|
|
|
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-10-20 22:44:23 -04:00
|
|
|
ai = pic_gc_arena_preserve(pic);
|
2013-10-14 05:28:52 -04:00
|
|
|
pic->sDEFINE = pic_intern_cstr(pic, "define");
|
2013-10-15 22:32:30 -04:00
|
|
|
pic->sLAMBDA = pic_intern_cstr(pic, "lambda");
|
2013-10-16 04:42:47 -04:00
|
|
|
pic->sIF = pic_intern_cstr(pic, "if");
|
2013-10-17 04:54:28 -04:00
|
|
|
pic->sBEGIN = pic_intern_cstr(pic, "begin");
|
2013-10-20 20:29:56 -04:00
|
|
|
pic->sQUOTE = pic_intern_cstr(pic, "quote");
|
2013-10-22 23:39:48 -04:00
|
|
|
pic->sQUASIQUOTE = pic_intern_cstr(pic, "quasiquote");
|
|
|
|
pic->sUNQUOTE = pic_intern_cstr(pic, "unquote");
|
|
|
|
pic->sUNQUOTE_SPLICING = pic_intern_cstr(pic, "unquote-splicing");
|
2013-10-14 05:28:52 -04:00
|
|
|
pic->sCONS = pic_intern_cstr(pic, "cons");
|
2013-10-19 14:48:06 -04:00
|
|
|
pic->sCAR = pic_intern_cstr(pic, "car");
|
|
|
|
pic->sCDR = pic_intern_cstr(pic, "cdr");
|
2013-10-19 14:53:02 -04:00
|
|
|
pic->sNILP = pic_intern_cstr(pic, "null?");
|
2013-10-15 08:29:07 -04:00
|
|
|
pic->sADD = pic_intern_cstr(pic, "+");
|
|
|
|
pic->sSUB = pic_intern_cstr(pic, "-");
|
|
|
|
pic->sMUL = pic_intern_cstr(pic, "*");
|
|
|
|
pic->sDIV = pic_intern_cstr(pic, "/");
|
2013-10-20 22:44:23 -04:00
|
|
|
pic_gc_arena_restore(pic, ai);
|
2013-10-15 08:14:33 -04:00
|
|
|
|
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);
|
|
|
|
}
|