--- a/progs/compile_arr.scala Sun Jan 26 14:16:30 2020 +0000
+++ b/progs/compile_arr.scala Mon Jan 27 10:11:44 2020 +0000
@@ -60,10 +60,12 @@
.limit locals 200
.limit stack 200
+; COMPILED CODE STARTS
+
"""
val ending = """
-
+; COMPILED CODE ENDS
return
.end method
@@ -359,7 +361,7 @@
("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
{ case _ ~ y ~ _ ~ u ~ _ ~w => If(y, u, w): Stmt } |
("while" ~ BExp ~ "do" ~ Block) ==> { case _ ~ y ~ _ ~ w => While(y, w) } |
- ("new" ~ IdParser ~ "[" ~ NumParser ~ "]") ==> {
+ ("new(" ~ IdParser ~ "[" ~ NumParser ~ "])") ==> {
case _ ~ y ~ _ ~ u ~ _ => ArrayDef(y, u) } |
("write" ~ IdParser) ==> { case _ ~ y => Write(y) }
@@ -405,11 +407,11 @@
def instr(c: Char) : String = c match {
case '>' => "ptr := ptr + 1;"
case '<' => "ptr := ptr - 1;"
- case '+' => "field[ptr] := field[ptr] + 1;"
- case '-' => "field[ptr] := field[ptr] - 1;"
- case '.' => "x := field[ptr]; write x;"
+ case '+' => "mem[ptr] := mem[ptr] + 1;"
+ case '-' => "mem[ptr] := mem[ptr] - 1;"
+ case '.' => "x := mem[ptr]; write x;"
//case ',' => "XXX" // "ptr = getchar();\n"
- case '[' => "while (field[ptr] != 0) do {"
+ case '[' => "while (mem[ptr] != 0) do {"
case ']' => "skip};"
case _ => ""
}
@@ -443,11 +445,11 @@
def instr2(c: Char, n: Int) : String = c match {
case '>' => s"ptr := ptr + $n;"
case '<' => s"ptr := ptr - $n;"
- case '0' => s"field[ptr] := 0;"
- case '+' => s"field[ptr] := field[ptr] + $n;"
- case '-' => s"field[ptr] := field[ptr] - $n;"
- case '.' => s"x := field[ptr]; write x;"
- case '[' => "while (field[ptr] != 0) do {" * n
+ case '0' => s"mem[ptr] := 0;"
+ case '+' => s"mem[ptr] := mem[ptr] + $n;"
+ case '-' => s"mem[ptr] := mem[ptr] - $n;"
+ case '.' => s"x := mem[ptr]; write x;"
+ case '[' => "while (mem[ptr] != 0) do {" * n
case ']' => "skip};" * n
case _ => ""
}
@@ -456,7 +458,7 @@
spl(prog.replaceAll("""\[-\]""", "0")).map{ case (c, n) => instr2(c, n) }.mkString
def bf_str(prog: String) : String = {
- "\n" ++
+ "new(mem[30000]);" ++
"ptr := 15000;" ++
instrs2(prog) ++
"skip"
@@ -468,7 +470,7 @@
println(s"BF parsing (program length ${bf_string.length} characters)")
val (time, bf_prog) = time_needed(1, Stmts.parse_all(bf_string).toList.head)
println(s"BF generated WHILE program (needed $time for parsing)")
- compile_and_run(ArrayDef("field", 30000) :: bf_prog, name)
+ compile_and_run(bf_prog, name)
}
// a benchmark program (counts down from 'Z' to 'A')