aboutsummaryrefslogtreecommitdiffstats
path: root/src/script.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/script.c')
-rw-r--r--src/script.c75
1 files changed, 41 insertions, 34 deletions
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 <heap.h>
#include <random.h>
#include <serial.h>
-#include <stack.h>
+#include <stdlib.h>
#include <keypad.h>
#include <string.h>
-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));