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-10 03:15:41 -04:00
|
|
|
|
2013-10-11 04:36:51 -04:00
|
|
|
static struct pic_env *
|
|
|
|
pic_new_empty_env()
|
|
|
|
{
|
|
|
|
struct pic_env *env;
|
|
|
|
|
|
|
|
env = (struct pic_env *)malloc(sizeof(struct pic_env));
|
|
|
|
env->assoc = pic_nil_value();
|
|
|
|
env->parent = NULL;
|
|
|
|
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2013-10-15 08:14:33 -04:00
|
|
|
void pic_init_core(pic_state *);
|
|
|
|
|
2013-10-10 03:15:41 -04:00
|
|
|
pic_state *
|
|
|
|
pic_open()
|
|
|
|
{
|
|
|
|
pic_state *pic;
|
|
|
|
|
2013-10-11 04:36:51 -04:00
|
|
|
pic = (pic_state *)malloc(sizeof(pic_state));
|
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-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-13 04:02:29 -04:00
|
|
|
/* GC arena */
|
|
|
|
pic->arena_idx = 0;
|
|
|
|
|
2013-10-14 05:28:52 -04:00
|
|
|
pic->sDEFINE = pic_intern_cstr(pic, "define");
|
|
|
|
pic->sCONS = pic_intern_cstr(pic, "cons");
|
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-10 03:15:41 -04:00
|
|
|
|
2013-10-15 08:14:33 -04:00
|
|
|
/* global environment */
|
|
|
|
pic->global_env = pic_new_empty_env();
|
|
|
|
pic_init_core(pic);
|
|
|
|
|
2013-10-10 03:15:41 -04:00
|
|
|
return pic;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_close(pic_state *pic)
|
|
|
|
{
|
|
|
|
free(pic);
|
|
|
|
}
|