From 8275c9cac1e610c00bb4a9af3c9f41dcd783c8a7 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 21 Mar 2018 12:20:44 -0400 Subject: various fixes; new interpreter library --- src/heap.c | 2 ++ src/main.c | 7 +++--- src/script.c | 75 +++++++++++++++++++++++++++++++++--------------------------- 3 files changed, 46 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/heap.c b/src/heap.c index 52b5253..9a11cba 100644 --- a/src/heap.c +++ b/src/heap.c @@ -76,6 +76,8 @@ void free(void *buf) return; alloc_t *alloc = (alloc_t *)((uint8_t *)buf - sizeof(alloc_t)); + if (alloc->next != 0) + return; heap_used -= alloc->size; alloc->next = free_blocks; free_blocks = alloc; diff --git a/src/main.c b/src/main.c index 71ce68f..fc64edd 100644 --- a/src/main.c +++ b/src/main.c @@ -82,9 +82,8 @@ void kmain(void) void task_interpreter(void) { - interpreter it; - iinit(&it); - script_loadlib(&it); + instance *it = inewinstance(); + script_loadlib(it); char *s = initrd_getfile("init"); if (s == 0) { @@ -105,7 +104,7 @@ void task_interpreter(void) } strncpy(linebuf, s + prev, lc + 1); linebuf[lc] = '\0'; - ret = idoline(&it, linebuf); + ret = idoline(it, linebuf); if (ret < 0) break; prev = ++i; diff --git a/src/script.c b/src/script.c index 8fad2f3..1cd0d71 100644 --- a/src/script.c +++ b/src/script.c @@ -7,23 +7,25 @@ #include #include #include -#include +#include #include #include -int script_puts(interpreter *it); -int script_gets(interpreter *it); -int script_delay(interpreter *it); -int script_rect(interpreter *it); -int script_ppos(interpreter *it); -int script_line(interpreter *it); -int script_color(interpreter *it); -int script_rand(interpreter *it); -int script_getkey(interpreter *it); -int script_pixel(interpreter *it); - -void script_loadlib(interpreter *it) +#define igetarg_integer(it, n) ((int)igetarg(it, n)->value.f) + +int script_puts(instance *it); +int script_gets(instance *it); +int script_delay(instance *it); +int script_rect(instance *it); +int script_ppos(instance *it); +int script_line(instance *it); +int script_color(instance *it); +int script_rand(instance *it); +int script_getkey(instance *it); +int script_pixel(instance *it); + +void script_loadlib(instance *it) { inew_cfunc(it, "print", script_puts); inew_cfunc(it, "gets", script_gets); @@ -37,14 +39,20 @@ void script_loadlib(interpreter *it) inew_cfunc(it, "pixel", script_pixel); } -int script_puts(interpreter *it) +int script_puts(instance *it) { - const char *s = igetarg_string(it, 0); - dsp_puts(s); + variable *v = igetarg(it, 0); + if (v->type == NUMBER) { + char buf[33]; + snprintf(buf, 33, "%f", v->value.f); + dsp_puts(buf); + } else if (v->type == STRING) { + dsp_puts((const char *)v->value.p); + } return 0; } -int script_gets(interpreter *it) +int script_gets(instance *it) { char *s = malloc(64); char c[2] = {0, 0}; @@ -66,20 +74,19 @@ int script_gets(interpreter *it) s[index] = '\0'; variable *r = make_vars(0, s); - iret(it, r); + ipush(it, (uint32_t)r); free(s); - free(r); return 0; } -int script_delay(interpreter *it) +int script_delay(instance *it) { - int ms = igetarg_integer(it, 0); + int ms = (int)igetarg(it, 0)->value.f; delay(ms); return 0; } -int script_rect(interpreter *it) +int script_rect(instance *it) { dsp_rect(igetarg_integer(it, 0), igetarg_integer(it, 1), igetarg_integer(it, 2), igetarg_integer(it, 3), @@ -87,7 +94,7 @@ int script_rect(interpreter *it) return 0; } -int script_line(interpreter *it) +int script_line(instance *it) { int x = igetarg_integer(it, 0); int y = igetarg_integer(it, 1); @@ -98,42 +105,42 @@ int script_line(interpreter *it) return 0; } -int script_ppos(interpreter *it) +int script_ppos(instance *it) { dsp_cpos(0, 0); dsp_coff(igetarg_integer(it, 0), igetarg_integer(it, 1)); return 0; } -int script_color(interpreter *it) +int script_color(instance *it) { uint16_t c = dsp_color(igetarg_integer(it, 0), igetarg_integer(it, 1), igetarg_integer(it, 2)); - variable *v = make_varn(0, (float)c); - iret(it, v); + variable *v = make_varf(0, (float)c); + ipush(it, (uint32_t)v); free(v); return 0; } -int script_rand(interpreter *it) +int script_rand(instance *it) { unsigned int mod = igetarg_integer(it, 0); unsigned int val = random_get(); - variable *v = make_varn(0, (float)(mod % val)); - iret(it, v); + variable *v = make_varf(0, (float)(mod % val)); + ipush(it, (uint32_t)v); free(v); return 0; } -int script_getkey(interpreter *it) +int script_getkey(instance *it) { - variable *v = make_varn(0, (float)keypad_get()); - iret(it, v); + variable *v = make_varf(0, (float)keypad_get()); + ipush(it, (uint32_t)v); free(v); return 0; } -int script_pixel(interpreter *it) +int script_pixel(instance *it) { dsp_pixel(igetarg_integer(it, 0), igetarg_integer(it, 1), igetarg_integer(it, 2)); -- cgit v1.2.3