Skip to content

Commit

Permalink
Extend .builtin syntax to allow for constructed values
Browse files Browse the repository at this point in the history
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 '= <poly_expr>', where
'<poly_expr>' is any C expression which returns a snek_poly_t value.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Apr 30, 2019
1 parent 0df0cf8 commit 680930e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions posix/snek-main.c
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions samd21/ao-snek.c
Expand Up @@ -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);
Expand Down
29 changes: 27 additions & 2 deletions snek-builtin.py
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions snek-duino/snek-duino.c
Expand Up @@ -113,6 +113,7 @@ main (void)
stderr = stdout = stdin = &snek_duino_file;
snek_uart_init();
port_init();
snek_init();
for (;;)
snek_parse();
}
Expand Down
2 changes: 2 additions & 0 deletions windows/snek-windows.c
Expand Up @@ -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)
Expand Down

0 comments on commit 680930e

Please sign in to comment.