From d6ce4e22ad9b98ab06812506f5bec2d4226ee7d2 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 21 Jun 2024 16:23:03 +0100 Subject: mandelbrot --- com.c | 4 ++++ dis.c | 1 + g.peg | 2 +- tests/mandel.bth | 21 +++++++++++++++++++++ tests/mandel.out | 31 +++++++++++++++++++++++++++++++ vm.c | 3 +++ vm.h | 1 + 7 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/mandel.bth create mode 100644 tests/mandel.out diff --git a/com.c b/com.c index 6ffd7c2..45b1f80 100644 --- a/com.c +++ b/com.c @@ -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]; diff --git a/dis.c b/dis.c index de1e3ae..ba0e1db 100644 --- a/dis.c +++ b/dis.c @@ -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") diff --git a/g.peg b/g.peg index d6bca5c..a3314f3 100644 --- a/g.peg +++ b/g.peg @@ -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 diff --git a/vm.c b/vm.c index eac0033..e3509c7 100644 --- a/vm.c +++ b/vm.c @@ -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; diff --git a/vm.h b/vm.h index 8fa72a1..6329655 100644 --- a/vm.h +++ b/vm.h @@ -39,6 +39,7 @@ typedef enum { OP_RET, OP_LOADK, OP_PUTS, + OP_PRINT, OP_ADD, OP_SUB, -- cgit v1.2.3