Skip to content

Commit

Permalink
Add op code for 'return.
Browse files Browse the repository at this point in the history
This is just short-hand for 'branch to end of function', but avoids
needing to patch the value after compiling the function.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Nov 28, 2019
1 parent 55e8839 commit c600b0b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion snek-code.c
Expand Up @@ -140,6 +140,8 @@ const char * const snek_op_names[] = {
[snek_op_range_start] = "range_start",
[snek_op_range_step] = "range_step",
[snek_op_in_step] = "in_step",
[snek_op_return] = "return",

[snek_op_null] = "null",
[snek_op_nop] = "nop",
[snek_op_line] = "line",
Expand Down Expand Up @@ -419,7 +421,6 @@ snek_code_finish(void)
{
if (snek_compile_size == 0)
return NULL;
snek_code_patch_forward(0, snek_compile_size, snek_forward_return, snek_code_current());
snek_code_t *code = snek_alloc(sizeof (snek_code_t) + snek_compile_size);

if (code) {
Expand Down Expand Up @@ -1178,6 +1179,9 @@ snek_code_run(snek_code_t *code_in)
snek_id_del(id);
}
break;
case snek_op_return:
ip = snek_code->size;
break;
case snek_op_assert:
if (!snek_poly_true(snek_a)) {
snek_error_0("AssertionError");
Expand Down
4 changes: 2 additions & 2 deletions snek-gram.ll
Expand Up @@ -38,7 +38,7 @@ command : @{ snek_print_val = snek_interactive; }@ stat
}@
OP opt-formals CP COLON suite
@{
if (snek_compile[snek_compile_prev] == snek_op_forward)
if (snek_compile[snek_compile_prev] == snek_op_return)
snek_code_delete_prev();
else
snek_code_add_op(snek_op_null);
Expand Down Expand Up @@ -99,7 +99,7 @@ small-stats-p : SEMI small-stat small-stats-p
;
small-stat : assign-expr
| RETURN ret-expr
@{ snek_code_add_forward(snek_forward_return); }@
@{ snek_code_add_op(snek_op_return); }@
| BREAK
@{ snek_code_add_forward(snek_forward_break); }@
| CONTINUE
Expand Down
2 changes: 1 addition & 1 deletion snek.h
Expand Up @@ -146,6 +146,7 @@ typedef enum {
snek_op_range_start,
snek_op_range_step,
snek_op_in_step,
snek_op_return,

snek_op_line,

Expand All @@ -157,7 +158,6 @@ typedef enum {
} __attribute__((packed)) snek_op_t;

typedef enum {
snek_forward_return,
snek_forward_break,
snek_forward_continue,
snek_forward_if,
Expand Down

0 comments on commit c600b0b

Please sign in to comment.