diff -r f5214da1976e -r 8067d0a8ba04 progs/compile_arrays.scala --- a/progs/compile_arrays.scala Sun Jul 28 21:10:39 2019 +0100 +++ b/progs/compile_arrays.scala Tue Jul 30 20:19:40 2019 +0100 @@ -69,9 +69,9 @@ .end method """ + // Compiler functions - // for generating new labels var counter = -1 @@ -186,33 +186,21 @@ (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name) } -// compiling and running files -// -// JVM files can be assembled with -// -// java -jar jvm/jasmin-2.4/jasmin.jar fib.j -// -// and started with -// -// java fib/fib - - +// main compiler functions import scala.util._ import scala.sys.process._ import scala.io def compile_tofile(bl: Block, class_name: String) = { val output = compile(bl, class_name) - val fw = new java.io.FileWriter(class_name + ".j") - fw.write(output) - fw.close() + scala.tools.nsc.io.File(s"${class_name}.j").writeAll(output) } def compile_all(bl: Block, class_name: String) : Unit = { compile_tofile(bl, class_name) println("compiled ") - val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!! + (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!! println("assembled ") } @@ -227,91 +215,20 @@ def compile_run(bl: Block, class_name: String) : Unit = { println("Start compilation") compile_all(bl, class_name) - println("running") - println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!)) -} - - -// BF Part - -// simple instructions -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 '[' => "while (field[ptr] != 0) do {" - case ']' => "skip};" - case _ => "" -} - -def instrs(prog: String) : String = - prog.toList.map(instr).mkString - - -// compound instructions -def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match { - case (Nil, acc) => acc - case (c :: cs, Nil) => splice(cs, List((c, 1))) - case (c :: cs, (d, n) :: acc) => - if (c == d) splice(cs, (c, n + 1) :: acc) - else splice(cs, (c, 1) :: (d, n) :: acc) + println("Start running") + println("Time: " + time_needed(1, (s"java ${class_name}/${class_name}").!)) } -def spl(s: String) = splice(s.toList, Nil).reverse - -def instr2(c: Char, n: Int) : String = c match { - case '>' => s"ptr := ptr + $n;" - case '<' => s"ptr := ptr - $n;" - case '+' => s"field[ptr] := field[ptr] + $n;" - case '-' => s"field[ptr] := field[ptr] - $n;" - case '.' => s"x := field[ptr]; write x;" - case '[' => s"while (field[ptr] != 0) do {" * n - case ']' => s"skip};" * n - case _ => "" -} - -def instrs2(prog: String) : String = - spl(prog).map{ case (c, n) => instr2(c, n) }.mkString - - -def bf_str(prog: String) : String = { - "\n" ++ - //"new field[30000];\n" ++ - "ptr := 15000;" ++ - instrs2(prog) ++ - "skip" -} +// a simple test case +val arr_test = + List(Array("a", 10), // a[10] + Array("b", 2), // b[2] + AssignA("a", Num(0), Num(10)), // a[0] := 10 + Assign("x", Ref("a", Num(0))), // x := a[0] + Write("x"), // write x + AssignA("b", Num(1), Num(5)), // b[1] := 5 + Assign("x", Ref("b", Num(1))), // x := b[1] + Write("x")) // write x -def bf_run(bfprog: String, name: String) = { - println("BF processing start") - val bf_string = bf_str(bfprog).replaceAll("\\s", "") - println(s"BF parsing start (string length ${bf_string.length})") - val bf_prog = Stmts.parse_all(bf_string).toList.head - println("BF Compile start") - compile_run(Array("field", 30000) :: bf_prog, name) -} - - - -val bf1 = """++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ - ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< - ]>.>+[>>]>+]""" +compile_run(arr_test, "a") -bf_run(bf1, "sier") - -bf_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++ - ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", "hello") - -bf_run("""+++++++++++ - >+>>>>++++++++++++++++++++++++++++++++++++++++++++ - >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> - +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[- - <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<< - -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]] - >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++ - +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++ - ++++++++++++++++++++++++++++++++++++++++++++.[-]<< - <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< - [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""", "fibs")