summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--com.c4
-rw-r--r--dis.c1
-rw-r--r--g.peg2
-rw-r--r--tests/mandel.bth21
-rw-r--r--tests/mandel.out31
-rw-r--r--vm.c3
-rw-r--r--vm.h1
7 files changed, 62 insertions, 1 deletions
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,