aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-11-11 11:27:44 -0500
committerFranklin Wei <git@fwei.tk>2015-11-11 11:27:44 -0500
commitac3d13839cd797d2992d9111a16b649668e2f06d (patch)
treeeaff2783f7a0af726fab70ddfdbfc5e712d3f0d2 /src
parentdc371f18db2fe07fd0be9e096666fbe9d07c8817 (diff)
downloadducky-ac3d13839cd797d2992d9111a16b649668e2f06d.zip
ducky-ac3d13839cd797d2992d9111a16b649668e2f06d.tar.gz
ducky-ac3d13839cd797d2992d9111a16b649668e2f06d.tar.bz2
ducky-ac3d13839cd797d2992d9111a16b649668e2f06d.tar.xz
stuff
Diffstat (limited to 'src')
-rw-r--r--src/compile.c21
-rw-r--r--src/interp.c36
-rw-r--r--src/opcodes.h1
-rw-r--r--src/vm.c17
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<x)error("not enough elements on stack");
-
void neg_handler(void)
{
- CHECKSTACK(1);
stack[stack_pointer - 1] = -stack[stack_pointer - 1];
}
@@ -428,6 +427,14 @@ void sqrt_handler(void)
push(sqrt(pop()));
}
+void decl_const(void)
+{
+ /* no checking, only the compiler can output this instruction */
+ varid_t varid = read_varid();
+ vars[varid].val = read_imm();
+ vars[varid].constant = true;
+}
+
void inc_line_pointer(void)
{
++current_line;
@@ -488,7 +495,7 @@ static void (*instr_tab[0x100])(void) = {
lsh_handler, /* 0x24 */
rsh_handler, /* 0x25 */
sqrt_handler, /* 0x26 */
- NULL, /* 0x27 */
+ decl_const, /* 0x27 */
NULL, /* 0x28 */
NULL, /* 0x29 */
NULL, /* 0x2a */