From 680930e16512101d208e0b680ddb69a2c68e39e7 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 30 Apr 2019 14:51:28 -0700 Subject: [PATCH] Extend .builtin syntax to allow for constructed values This creates an ability to define variables with values that use code to build a value rather than simply allowing a floating point number. The value is specified with '= ', where '' is any C expression which returns a snek_poly_t value. Signed-off-by: Keith Packard --- posix/snek-main.c | 2 ++ samd21/ao-snek.c | 1 + snek-builtin.py | 29 +++++++++++++++++++++++++++-- snek-duino/snek-duino.c | 1 + windows/snek-windows.c | 2 ++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/posix/snek-main.c b/posix/snek-main.c index 122de50..5c71ecb 100644 --- a/posix/snek-main.c +++ b/posix/snek-main.c @@ -99,6 +99,8 @@ main (int argc, char **argv) printf("Welcome to Snek version %s\n", SNEK_VERSION); } + snek_init(); + bool ret = snek_parse() == snek_parse_success; if (snek_posix_input == stdin) diff --git a/samd21/ao-snek.c b/samd21/ao-snek.c index 76781b2..c7e1220 100644 --- a/samd21/ao-snek.c +++ b/samd21/ao-snek.c @@ -151,6 +151,7 @@ main(void) setjmp(snek_reset_buf); ao_snek_port_init(); + snek_init(); snek_builtin_eeprom_load(); printf("Welcome to snek " SNEK_VERSION "\n"); fflush(stdout); diff --git a/snek-builtin.py b/snek-builtin.py index e50e8e4..01057a0 100644 --- a/snek-builtin.py +++ b/snek-builtin.py @@ -27,7 +27,12 @@ class SnekBuiltin: def __init__(self, name, param, value): self.name = name - self.value = value + self.value = None + self.init = None + if value and value[0] == '=': + self.init = value[1:] + else: + self.value = value self.id = -1 if param[0].isalpha(): self.keyword = param @@ -45,6 +50,9 @@ def is_name(self): def is_value(self): return self.nformal == -2 and self.value is not None + def is_init(self): + return self.nformal == -2 and self.init is not None + def is_func(self): return self.nformal >= -1 @@ -111,13 +119,28 @@ def load_builtins(filename): bits = line.split(",") value = None if len(bits) > 2: - value = bits[2].strip() + value = ",".join(bits[2:]).strip() add_builtin(bits[0].strip(), bits[1].strip(), value) def dump_headers(fp): for line in headers: fprint("%s" % line, file=fp) +def dump_init(fp): + use_list_build = False + fprint("#define snek_init() {\\", file=fp) + for name in sorted(builtins): + if name.is_init(): + if 'snek_list_build' in name.init: + use_list_build = True + fprint(" {\\", file=fp) + fprint(" snek_stack_push((%s));\\" % name.init, file=fp) + fprint(" *snek_id_ref(%s, true) = snek_stack_pop();\\" % name.cpp_name(), file=fp) + fprint(" }\\", file=fp) + fprint("}", file=fp) + if use_list_build: + fprint("#define SNEK_LIST_BUILD", file=fp) + def dump_max_len(fp): max_len = 0 for name in builtins: @@ -258,6 +281,8 @@ def builtin_main(): dump_headers(fp) + dump_init(fp) + fprint("#ifndef SNEK_BUILTIN_NFORMAL", file=fp) fprint("#define SNEK_BUILTIN_NFORMAL(b) ((b)->nformal)", file=fp) fprint("#define SNEK_BUILTIN_FUNCV(b) ((b)->funcv)", file=fp) diff --git a/snek-duino/snek-duino.c b/snek-duino/snek-duino.c index 5a87f86..e799b51 100644 --- a/snek-duino/snek-duino.c +++ b/snek-duino/snek-duino.c @@ -113,6 +113,7 @@ main (void) stderr = stdout = stdin = &snek_duino_file; snek_uart_init(); port_init(); + snek_init(); for (;;) snek_parse(); } diff --git a/windows/snek-windows.c b/windows/snek-windows.c index 5b89418..ef6fad3 100644 --- a/windows/snek-windows.c +++ b/windows/snek-windows.c @@ -95,6 +95,8 @@ main (int argc, char **argv) printf("Welcome to Snek version %s\n", SNEK_VERSION); } + snek_init(); + bool ret = snek_parse() == snek_parse_success; if (snek_windows_input == stdin)