diff options
-rw-r--r-- | com.c | 4 | ||||
-rw-r--r-- | dis.c | 1 | ||||
-rw-r--r-- | g.peg | 2 | ||||
-rw-r--r-- | tests/mandel.bth | 21 | ||||
-rw-r--r-- | tests/mandel.out | 31 | ||||
-rw-r--r-- | vm.c | 3 | ||||
-rw-r--r-- | vm.h | 1 |
7 files changed, 62 insertions, 1 deletions
@@ -57,6 +57,10 @@ static void compile_node(State *S, Chunk *ch, AstNode a) { CK(l.len == 2, "puts requires exactly 1 argument"); compile_node(S, ch, l.vals[1]); chunk_wbc(S, ch, OP_PUTS); + } else if (0 == strcmp(name, "print")) { + CK(l.len == 2, "print requires exactly 1 argument"); + compile_node(S, ch, l.vals[1]); + chunk_wbc(S, ch, OP_PRINT); } else if (0 == strcmp(name, "set")) { CK(l.len == 3, "set requires exactly 2 arguments"); AstNode ident = l.vals[1]; @@ -67,6 +67,7 @@ void disasm_chunk(Chunk *ch) { case opcode: puts(str); break; SIMPLE_INSTR(OP_RET, "ret") SIMPLE_INSTR(OP_PUTS, "puts") + SIMPLE_INSTR(OP_PRINT, "print") SIMPLE_INSTR(OP_DROP, "drop") SIMPLE_INSTR(OP_ADD, "add") SIMPLE_INSTR(OP_SUB, "sub") @@ -37,7 +37,7 @@ list <- { $$ = astnode_new_list(); } number <- < [0-9]+ > (! ident_char) _ { $$ = astnode_new_num(atoi($1)); } ident <- < ident_char+ > _ { $$ = astnode_new_ident(strdup($1)); } -string <- '"' < [^"]+ > '"' _ { $$ = astnode_new_string(strdup($1)); } +string <- '"' < [^"]* > '"' _ { $$ = astnode_new_string(strdup($1)); } ident_char <- [-_a-zA-Z'+*/\\%=0-9<>] _ <- [ \t\n]* diff --git a/tests/mandel.bth b/tests/mandel.bth new file mode 100644 index 0000000..5eb3fcd --- /dev/null +++ b/tests/mandel.bth @@ -0,0 +1,21 @@ +(do + (set pxy 0) + (while (< pxy 30) + (set cim (/ (- pxy 15) 15)) + (set pxx 0) + (while (< pxx 60) + (set cre (/ (- pxx 40) 20)) + (set i 0) + (set zim cim) + (set zre cre) + (while (< i 25) + (set nzre (+ cre (- (* zre zre) (* zim zim)))) + (set nzim (+ cim (* 2 (* zre zim)))) + (set zre nzre) + (set zim nzim) + (set i (+ i 1))) + (set mag (+ (* zre zre) (* zim zim))) + (print (if (< mag 4) "#" ".")) + (set pxx (+ pxx 1))) + (puts "") + (set pxy (+ pxy 1)))) diff --git a/tests/mandel.out b/tests/mandel.out new file mode 100644 index 0000000..f9b5e6d --- /dev/null +++ b/tests/mandel.out @@ -0,0 +1,31 @@ +........................................#................... +............................................................ +......................................#..................... +....................................####.................... +....................................####.................... +..................................#..##..................... +.............................##..##########................. +.............................##################............. +.............................#################.............. +............................###################............. +...........................#######################.......... +.................#.##.....######################............ +.................#######..#######################........... +................#########.#######################........... +.............###################################............ +.#############################################.............. +.............###################################............ +................#########.#######################........... +.................#######..#######################........... +.................#.##.....######################............ +...........................#######################.......... +............................###################............. +.............................#################.............. +.............................##################............. +.............................##..##########................. +..................................#..##..................... +....................................####.................... +....................................####.................... +......................................#..................... +............................................................ +nil @@ -88,6 +88,9 @@ int runvm(State *S) { case OP_PUTS: println_val(PEEK()); break; + case OP_PRINT: + print_val(PEEK()); + break; case OP_DROP: --th->sp; break; @@ -39,6 +39,7 @@ typedef enum { OP_RET, OP_LOADK, OP_PUTS, + OP_PRINT, OP_ADD, OP_SUB, |