From ac3d13839cd797d2992d9111a16b649668e2f06d Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Wed, 11 Nov 2015 11:27:44 -0500 Subject: stuff --- src/compile.c | 21 ++++++++++++--------- src/interp.c | 36 ++++++++++++++++++++++++++---------- src/opcodes.h | 1 + src/vm.c | 17 ++++++++++++----- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/compile.c b/src/compile.c index 1db46af..a5c3f9a 100644 --- a/src/compile.c +++ b/src/compile.c @@ -109,6 +109,13 @@ static void setVariable(const char *name, vartype val) write_varid(get_varid(name)); } +static void makeConstantVariable(const char *name, vartype val) +{ + write_instr(DECL_CONST); + write_varid(get_varid(name)); + write_imm(val); +} + static void setConst(const char *name, bool c) { if(c) @@ -743,7 +750,7 @@ static vartype eval_expr(char *str) } } } - + if(tstart) { //push_numstack(getValue(tstart, expr)); @@ -1210,14 +1217,10 @@ void ducky_compile(int fd, bool verbose, int out) /* initialize some other constants */ - setVariable(".", 0); - setConst(".", true); - - setVariable("true", 1); - setConst("true", true); - - setVariable("false", 0); - setConst("false", true); + makeConstantVariable(".", 0); + + makeConstantVariable("true", 1); + makeConstantVariable("false", 0); /* initialize labels (using output from index_lines) */ index_labels(file_des); diff --git a/src/interp.c b/src/interp.c index 41b778a..9a53c22 100644 --- a/src/interp.c +++ b/src/interp.c @@ -46,6 +46,8 @@ static off_t *line_offset = NULL; static unsigned lines_executed = 0, current_line = 0, num_lines; +static unsigned repeat_line; + static unsigned call_stack[CALL_STACK_SZ]; static unsigned stack_frame = 0; @@ -843,18 +845,31 @@ static int let_handler(char **save, int *repeats_left) static int repeat_handler(char **save, int *repeats_left) { - (void) save; (void) repeats_left; - char *tok = strtok_r(NULL, ";", save); - if(tok) + (void) save; + if(*repeats_left > 0) { - if(current_line == 1) - error("REPEAT without a previous instruction"); - *repeats_left = eval_expr(tok) - 1; - jump_line(file_des, current_line - 1); - return NEXT; + --(*repeats_left); + if(*repeats_left) + jump_line(file_des, repeat_line); } else - error("expected valid expression after REPEAT"); + { + char *tok = strtok_r(NULL, ";", save); + if(tok) + { + if(current_line == 1) + error("REPEAT without a previous instruction"); + *repeats_left = eval_expr(tok) - 1; + if(*repeats_left) + { + repeat_line = current_line - 1; + jump_line(file_des, repeat_line); + } + } + else + error("expected valid expression after REPEAT"); + } + return NEXT; } static int goto_handler(char **save, int *repeats_left) @@ -1304,7 +1319,7 @@ void ducky_main(int fd, bool verbose) } } while(tok); break_out: - + #if 0 if(repeats_left > 0) { --repeats_left; @@ -1317,6 +1332,7 @@ void ducky_main(int fd, bool verbose) jump_line(file_des, current_line + 2); } } + #endif next_line: ; } diff --git a/src/opcodes.h b/src/opcodes.h index 503ab59..a08157b 100644 --- a/src/opcodes.h +++ b/src/opcodes.h @@ -37,4 +37,5 @@ #define LSH 0x24 #define RSH 0x25 #define SQRT 0x26 +#define DECL_CONST 0x27 #define LINEMARK 0xFF diff --git a/src/vm.c b/src/vm.c index e4acc04..aae97af 100644 --- a/src/vm.c +++ b/src/vm.c @@ -138,8 +138,10 @@ static inline vartype getvar(varid_t varid) static inline void setvar(varid_t varid, vartype val) { - if(varid < ARRAYLEN(vars)) + if(varid < ARRAYLEN(vars) && !vars[varid].constant) vars[varid].val = val; + else + error("cannot modify variable"); } static inline void mkconst(varid_t varid) @@ -267,11 +269,8 @@ void logascii_handler(void) vid_writef("%c", pop()); } -#define CHECKSTACK(x) if(stack_pointer