picrin/lib/state.c

427 lines
8.6 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "picrin/extra.h"
2017-05-05 23:53:20 -04:00
#include "value.h"
2017-03-28 10:09:40 -04:00
#include "object.h"
#include "state.h"
2014-08-25 00:38:09 -04:00
2017-04-09 09:14:04 -04:00
void
pic_add_feature(pic_state *pic, const char *feature)
{
pic_push(pic, pic_intern_cstr(pic, feature), pic->features);
}
2016-02-23 13:59:51 -05:00
static pic_value
2017-04-09 09:14:04 -04:00
pic_state_features(pic_state *pic)
2016-02-23 13:59:51 -05:00
{
pic_get_args(pic, "");
return pic->features;
}
2017-04-09 09:14:04 -04:00
static pic_value
pic_state_global_objects(pic_state *pic)
{
pic_get_args(pic, "");
return pic->globals;
}
static pic_value
pic_state_error(pic_state *pic)
{
const char *msg;
int argc;
pic_value *args;
pic_get_args(pic, "z*", &msg, &argc, &args);
pic->panicf(pic, msg, argc, args);
PIC_UNREACHABLE();
}
2015-01-26 00:31:36 -05:00
static void
2017-04-09 09:14:04 -04:00
pic_init_state(pic_state *pic)
2015-01-26 00:31:36 -05:00
{
2017-04-09 09:14:04 -04:00
pic_defun(pic, "features", pic_state_features);
pic_defun(pic, "global-objects", pic_state_global_objects);
pic_defun(pic, "error", pic_state_error);
2016-02-23 13:59:51 -05:00
2015-01-26 00:31:36 -05:00
pic_add_feature(pic, "picrin");
2015-07-19 13:19:41 -04:00
#if __STDC_IEC_559__
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
}
2016-02-09 11:38:05 -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_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 *);
2017-04-29 11:23:38 -04:00
void pic_init_attr(pic_state *);
2017-04-09 09:14:04 -04:00
void pic_init_file(pic_state *);
void pic_init_state(pic_state *);
2017-04-14 10:58:02 -04:00
void pic_init_eval(pic_state *);
2016-03-03 04:59:07 -05:00
#define DONE pic_leave(pic, ai);
2016-02-09 11:38:05 -05:00
2015-01-26 00:31:36 -05:00
static void
pic_init_core(pic_state *pic)
{
2016-03-03 04:59:07 -05:00
size_t ai = pic_enter(pic);
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_char(pic); DONE;
pic_init_str(pic); DONE;
pic_init_var(pic); DONE;
pic_init_dict(pic); DONE;
pic_init_record(pic); DONE;
2017-04-29 11:23:38 -04:00
pic_init_attr(pic); DONE;
pic_init_state(pic); DONE;
2017-04-25 01:08:37 -04:00
#if PIC_USE_CALLCC
pic_init_cont(pic); DONE;
#endif
#if PIC_USE_READ
pic_init_read(pic); DONE;
#endif
2016-02-22 14:37:58 -05:00
#if PIC_USE_WRITE
pic_init_write(pic); DONE;
#endif
#if PIC_USE_FILE
2017-04-09 09:14:04 -04:00
pic_init_file(pic); DONE;
#endif
2017-04-14 10:58:02 -04:00
#if PIC_USE_EVAL
pic_init_eval(pic); DONE;
#endif
#if PIC_USE_ERROR
pic_init_error(pic); DONE;
#endif
2015-01-26 00:31:36 -05:00
}
2014-08-25 00:38:09 -04:00
pic_state *
pic_open(pic_allocf allocf, void *userdata, pic_panicf panicf)
2014-08-25 00:38:09 -04:00
{
pic_state *pic;
2015-08-10 08:13:23 -04:00
pic = allocf(userdata, 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;
/* panic handler */
pic->panicf = panicf;
2017-04-14 10:40:07 -04:00
/* context */
2017-04-20 16:22:28 -04:00
pic->default_cxt.ai = 0;
2017-04-14 10:40:07 -04:00
pic->default_cxt.pc = NULL;
pic->default_cxt.fp = NULL;
pic->default_cxt.sp = NULL;
pic->default_cxt.irep = NULL;
pic->default_cxt.prev = NULL;
2017-04-25 13:30:17 -04:00
pic->default_cxt.conts = pic_nil_value(pic);
2017-04-14 10:40:07 -04:00
pic->cxt = &pic->default_cxt;
2015-05-27 10:08:47 -04:00
2017-04-09 02:05:59 -04:00
/* arena */
2016-02-21 06:32:00 -05:00
pic->arena = allocf(userdata, NULL, PIC_ARENA_SIZE * sizeof(struct object *));
2015-05-27 10:08:47 -04:00
pic->arena_size = PIC_ARENA_SIZE;
2017-04-15 16:20:55 -04:00
pic->ai = 0;
2015-05-27 10:08:47 -04:00
if (! pic->arena) {
goto EXIT_ARENA;
}
2017-04-09 02:05:59 -04:00
/* turn off GC */
pic->gc_enable = false;
2014-08-25 00:38:09 -04:00
/* memory heap */
pic->heap = pic_heap_open(pic);
2014-08-25 00:38:09 -04:00
/* symbol table */
2016-02-14 22:59:58 -05:00
kh_init(oblist, &pic->oblist);
2014-08-25 00:38:09 -04:00
/* global variables */
2017-04-09 02:05:59 -04:00
pic->globals = pic_make_dict(pic);
2014-08-25 00:38:09 -04:00
2014-09-09 12:41:10 -04:00
/* features */
pic->features = pic_nil_value(pic);
2014-09-09 12:41:10 -04:00
2017-03-31 01:39:01 -04:00
/* dynamic environment */
2017-04-29 11:23:38 -04:00
pic->dyn_env = pic_list(pic, 1, pic_make_attr(pic));
2017-03-31 01:39:01 -04:00
2017-04-14 10:40:07 -04:00
/* top continuation */
{
2017-04-22 20:16:40 -04:00
static const code_t halt_code[] = { 0x00 };
2017-04-14 10:40:07 -04:00
struct irep *irep;
struct proc *proc;
irep = (struct irep *)pic_obj_alloc(pic, PIC_TYPE_IREP);
irep->argc = 1;
irep->flags = IREP_CODE_STATIC;
irep->frame_size = 1;
irep->irepc = 0;
irep->objc = 0;
irep->irep = NULL;
irep->obj = NULL;
irep->code = halt_code;
2017-04-22 20:16:40 -04:00
irep->codec = sizeof halt_code / sizeof halt_code[0];
2017-04-14 10:40:07 -04:00
proc = (struct proc *)pic_obj_alloc(pic, PIC_TYPE_PROC_IREP);
proc->u.irep = irep;
proc->env = NULL;
pic->halt = obj_value(pic, proc);
}
2017-04-09 02:05:59 -04:00
/* panic handler */
2016-02-21 04:59:18 -05:00
pic->panicf = NULL;
2014-08-25 00:38:09 -04:00
2015-01-18 22:14:29 -05:00
/* turn on GC */
pic->gc_enable = true;
pic_init_core(pic);
2014-08-25 00:38:09 -04:00
2016-02-20 14:34:26 -05:00
pic_leave(pic, 0); /* empty arena */
2015-05-27 10:08:47 -04:00
2014-08-25 00:38:09 -04:00
return pic;
2015-05-27 10:08:47 -04:00
EXIT_ARENA:
2015-08-10 08:13:23 -04:00
allocf(userdata, 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-05-27 13:12:26 -04:00
pic_allocf allocf = pic->allocf;
2014-08-25 00:38:09 -04:00
/* clear out root objects */
2017-04-14 10:40:07 -04:00
pic->cxt = &pic->default_cxt;
2017-04-15 16:20:55 -04:00
pic->ai = 0;
2017-04-14 10:40:07 -04:00
pic->halt = pic_invalid_value(pic);
2016-02-20 05:00:41 -05:00
pic->globals = pic_invalid_value(pic);
2017-03-31 01:39:01 -04:00
pic->features = pic_invalid_value(pic);
pic->dyn_env = pic_invalid_value(pic);
2017-04-20 16:22:28 -04:00
assert(pic->cxt->ai == 0);
2017-04-14 10:40:07 -04:00
assert(pic->cxt->pc == NULL);
assert(pic->cxt->fp == NULL);
assert(pic->cxt->sp == NULL);
assert(pic->cxt->irep == NULL);
assert(pic->cxt->prev == NULL);
2014-08-25 00:38:09 -04:00
/* free all heap objects */
2016-02-13 23:17:12 -05:00
pic_gc(pic);
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 global stacks */
kh_destroy(oblist, &pic->oblist);
2014-08-25 00:38:09 -04:00
/* free GC arena */
2015-08-10 08:13:23 -04:00
allocf(pic->userdata, pic->arena, 0);
allocf(pic->userdata, pic, 0);
2014-08-25 00:38:09 -04:00
}
2017-03-28 11:03:23 -04:00
void
pic_warnf(pic_state *PIC_UNUSED(pic), const char *PIC_UNUSED(fmt), ...)
{
#if PIC_USE_FILE
va_list ap;
pic_value err;
va_start(ap, fmt);
err = pic_vstrf_value(pic, fmt, ap);
va_end(ap);
2017-05-09 11:49:15 -04:00
pic_fprintf(pic, pic_stderr(pic), "warn: %s\n", pic_cstr(pic, err, NULL));
#endif
}
2017-03-28 11:03:23 -04:00
pic_value
pic_global_ref(pic_state *pic, pic_value sym)
2017-03-28 11:03:23 -04:00
{
2017-04-02 09:19:11 -04:00
if (! pic_dict_has(pic, pic->globals, sym)) {
pic_error(pic, "undefined variable", 1, sym);
2017-03-28 11:03:23 -04:00
}
2017-04-14 13:48:20 -04:00
return pic_dict_ref(pic, pic->globals, sym);
2017-03-28 11:03:23 -04:00
}
void
pic_global_set(pic_state *pic, pic_value sym, pic_value value)
2017-03-28 11:03:23 -04:00
{
2017-04-02 09:19:11 -04:00
pic_dict_set(pic, pic->globals, sym, value);
2017-03-28 11:03:23 -04:00
}
pic_value
pic_ref(pic_state *pic, const char *name)
2017-03-28 11:03:23 -04:00
{
2017-04-19 01:00:02 -04:00
size_t ai = pic_enter(pic);
pic_value r = pic_global_ref(pic, pic_intern_cstr(pic, name));
pic_leave(pic, ai);
return pic_protect(pic, r);
2017-03-28 11:03:23 -04:00
}
void
pic_set(pic_state *pic, const char *name, pic_value val)
2017-03-28 11:03:23 -04:00
{
2017-04-19 01:00:02 -04:00
size_t ai = pic_enter(pic);
pic_global_set(pic, pic_intern_cstr(pic, name), val);
2017-04-19 01:00:02 -04:00
pic_leave(pic, ai);
2017-03-28 11:03:23 -04:00
}
void
pic_define(pic_state *pic, const char *name, pic_value val)
2017-03-28 11:03:23 -04:00
{
pic_value sym = pic_intern_cstr(pic, name);
2017-03-28 11:03:23 -04:00
2017-04-02 09:19:11 -04:00
if (pic_dict_has(pic, pic->globals, sym)) {
2017-05-09 11:49:15 -04:00
pic_warnf(pic, "redefining variable: %s", name);
2017-03-28 11:03:23 -04:00
}
2017-04-02 09:19:11 -04:00
pic_dict_set(pic, pic->globals, sym, val);
2017-03-28 11:03:23 -04:00
}
void
pic_defun(pic_state *pic, const char *name, pic_func_t f)
{
2017-05-06 13:41:21 -04:00
pic_define(pic, name, pic_lambda(pic, f, 0));
2017-03-28 11:03:23 -04:00
}
void
pic_defvar(pic_state *pic, const char *name, pic_value init)
{
pic_define(pic, name, pic_make_var(pic, init, pic_false_value(pic)));
2017-03-28 11:03:23 -04:00
}
pic_value
pic_funcall(pic_state *pic, const char *name, int n, ...)
2017-03-28 11:03:23 -04:00
{
2017-04-19 01:00:02 -04:00
size_t ai = pic_enter(pic);
2017-03-28 11:03:23 -04:00
pic_value proc, r;
va_list ap;
proc = pic_ref(pic, name);
2017-03-28 11:03:23 -04:00
TYPE_CHECK(pic, proc, proc);
va_start(ap, n);
r = pic_vcall(pic, proc, n, ap);
va_end(ap);
2017-04-19 01:00:02 -04:00
pic_leave(pic, ai);
return pic_protect(pic, r);
2017-03-28 11:03:23 -04:00
}
#if PIC_USE_LIBC
void
2017-05-05 23:53:20 -04:00
pic_default_panicf(pic_state *PIC_UNUSED(pic), const char *msg, int PIC_UNUSED(n), pic_value *PIC_UNUSED(args))
{
fprintf(stderr, "panic!: %s\n", msg);
abort();
}
#endif
void
pic_error(pic_state *pic, const char *msg, int n, ...)
{
va_list ap;
va_start(ap, n);
pic_verror(pic, msg, n, ap);
va_end(ap);
PIC_UNREACHABLE();
}
void
pic_verror(pic_state *pic, const char *msg, int n, va_list ap)
{
pic_value error = pic_ref(pic, "error");
int i;
pic_value *args;
args = pic_alloca(pic, sizeof(pic_value) * (n + 1));
args[0] = pic_cstr_value(pic, msg);
for (i = 0; i < n; ++i) {
args[i + 1] = va_arg(ap, pic_value);
}
pic_apply(pic, error, n + 1, args);
PIC_UNREACHABLE();
}