# HG changeset patch # User Christian Urban # Date 1564344639 -3600 # Node ID f5214da1976e201a046f41908626e9eb10bef141 # Parent 2d91b210765644dbd6913827baf57f92ce6e3426 updated diff -r 2d91b2107656 -r f5214da1976e progs/comb2.scala --- a/progs/comb2.scala Sun Jul 28 17:29:53 2019 +0100 +++ b/progs/comb2.scala Sun Jul 28 21:10:39 2019 +0100 @@ -76,6 +76,8 @@ case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt case class While(b: BExp, bl: Block) extends Stmt case class Assign(s: String, a: AExp) extends Stmt +case class Write(s: String) extends Stmt + case class Var(s: String) extends AExp case class Num(i: Int) extends AExp @@ -84,7 +86,8 @@ case object True extends BExp case object False extends BExp case class Bop(o: String, a1: AExp, a2: AExp) extends BExp - +case class And(b1: BExp, b2: BExp) extends BExp +case class Or(b1: BExp, b2: BExp) extends BExp case object IdParser extends Parser[String, String] { val reg = "[a-z][a-z,0-9]*".r @@ -95,28 +98,33 @@ } lazy val AExp: Parser[String, AExp] = - ((Te ~ "+" ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z):AExp } || - (Te ~ "-" ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z):AExp } || Te) + (Te ~ "+" ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } || + (Te ~ "-" ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || Te lazy val Te: Parser[String, AExp] = - (Fa ~ "*" ~ Te) ==> { case ((x, y), z) => Aop("*", x, z):AExp } || Fa + (Fa ~ "*" ~ Te) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || + (Fa ~ "/" ~ Te) ==> { case ((x, y), z) => Aop("/", x, z): AExp } || Fa lazy val Fa: Parser[String, AExp] = - (("(" ~ AExp ~ ")") ==> { case ((x, y), z) => y } || + ("(" ~ AExp ~ ")") ==> { case ((x, y), z) => y } || IdParser ==> Var || - NumParser ==> Num) + NumParser ==> Num -// boolean expressions +// boolean expressions with some simple nesting lazy val BExp: Parser[String, BExp] = - ((AExp ~ "=" ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z):BExp } || - (AExp ~ "!=" ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z):BExp } || - (AExp ~ "<" ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z):BExp } || - (AExp ~ ">" ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z):BExp } || - ("true" ==> ((_) => True:BExp )) || - ("false" ==> ((_) => False:BExp )) || - ("(" ~ BExp ~ ")") ==> { case ((x, y), z) => y}) + (AExp ~ "==" ~ AExp) ==> { case ((x, y), z) => Bop("==", x, z) }: Bexp || + (AExp ~ "!=" ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || + (AExp ~ "<" ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || + (AExp ~ ">" ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z): BExp } || + ("(" ~ BExp ~ ")" ~ "&&" ~ BExp) ==> { case ((((x, y), z), u), v) => And(y, v): BExp } || + ("(" ~ BExp ~ ")" ~ "||" ~ BExp) ==> { case ((((x, y), z), u), v) => Or(y, v): BExp } || + ("true" ==> ((_) => True: BExp )) || + ("false" ==> ((_) => False: BExp )) || + ("(" ~ BExp ~ ")") ==> { case ((x, y), z) => y} +// statements lazy val Stmt: Parser[String, Stmt] = (("skip" ==> ((_) => Skip: Stmt)) || (IdParser ~ ":=" ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } || + ("write(" ~ IdParser ~ ")") ==> { case ((x, y), z) => Write(y): Stmt } || ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==> { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } || ("while" ~ BExp ~ "do" ~ Block) ==> { case (((x, y), z), w) => While(y, w) }) @@ -134,7 +142,17 @@ Block.parse_all("{x:=5;y:=8}") Block.parse_all("if(false)then{x:=5}else{x:=10}") -val fib = """{n:=10;minus1:=0;minus2:=1;temp:=0;while(n>0)do{temp:=minus2;minus2:=minus1+minus2;minus1:=temp;n:=n-1};result:=minus2}""" +val fib = """{n := 10; + minus1 := 0; + minus2 := 1; + temp := 0; + while (n > 0) do { + temp := minus2; + minus2 := minus1 + minus2; + minus1 := temp; + n := n - 1 + }; + result := minus2}""".replaceAll("\\s+", "") Block.parse_all(fib) @@ -147,15 +165,18 @@ case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env) case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env) case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env) + case Aop("/", a1, a2) => eval_aexp(a1, env) / eval_aexp(a2, env) } def eval_bexp(b: BExp, env: Env) : Boolean = b match { case True => true case False => false - case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env) + case Bop("==", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env) case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env)) case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env) case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env) + case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env) + case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env) } def eval_stmt(s: Stmt, env: Env) : Env = s match { @@ -165,6 +186,7 @@ case While(b, bl) => if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env)) else env + case Write(x) => { println(env(x)) ; env } } def eval_bl(bl: Block, env: Env) : Env = bl match { @@ -177,3 +199,39 @@ // parse + evaluate fib program; then lookup what is // stored under the variable result println(eval(Block.parse_all(fib).head)("result")) + + +// more examles + +// calculate and print all factors bigger +// than 1 and smaller than n +println("Factors") + +val factors = + """{n := 12; + f := 2; + while (f < n / 2 + 1) do { + if ((n / f) * f == n) then { write(f) } else { skip }; + f := f + 1 + }}""".replaceAll("\\s+", "") + +eval(Block.parse_all(factors).head) + +// calculate all prime numbers up to a number +println("Primes") + +val primes = + """{end := 100; + n := 2; + while (n < end) do { + f := 2; + tmp := 0; + while ((f < n / 2 + 1) && (tmp == 0)) do { + if ((n / f) * f == n) then { tmp := 1 } else { skip }; + f := f + 1 + }; + if (tmp == 0) then { write(n) } else { skip }; + n := n + 1 + }}""".replaceAll("\\s+", "") + +eval(Block.parse_all(primes).head) diff -r 2d91b2107656 -r f5214da1976e progs/compile_arrays.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/compile_arrays.scala Sun Jul 28 21:10:39 2019 +0100 @@ -0,0 +1,317 @@ +// A Small Compiler for the WHILE Language with Arrays +// (it does not use a parser and lexer) + +// the new parts are +// - declaring an array +// - references an array cell +// - assigning an array cell + +// the abstract syntax trees +abstract class Stmt +abstract class AExp +abstract class BExp +type Block = List[Stmt] + +// statements +case object Skip extends Stmt +case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt +case class While(b: BExp, bl: Block) extends Stmt +case class Assign(s: String, a: AExp) extends Stmt +case class AssignA(s: String, a1: AExp, a2: AExp) extends Stmt +case class Write(s: String) extends Stmt +case class Array(s: String, n: Int) extends Stmt + +// arithmetic expressions +case class Var(s: String) extends AExp +case class Num(i: Int) extends AExp +case class Aop(o: String, a1: AExp, a2: AExp) extends AExp +case class Ref(s: String, a1: AExp) extends AExp + +// boolean expressions +case object True extends BExp +case object False extends BExp +case class Bop(o: String, a1: AExp, a2: AExp) extends BExp + + +// compiler headers needed for the JVM +// (contains an init method, as well as methods for read and write) +val beginning = """ +.class public XXX.XXX +.super java/lang/Object + +.method public ()V + aload_0 + invokenonvirtual java/lang/Object/()V + return +.end method + +.method public static write(I)V + .limit locals 1 + .limit stack 2 + getstatic java/lang/System/out Ljava/io/PrintStream; + iload 0 + invokevirtual java/io/PrintStream/println(I)V + return +.end method + +.method public static main([Ljava/lang/String;)V + .limit locals 200 + .limit stack 200 + +; COMPILED CODE STARTS + +""" + +val ending = """ +; COMPILED CODE ENDS + return + +.end method +""" + +// Compiler functions + + +// for generating new labels +var counter = -1 + +def Fresh(x: String) = { + counter += 1 + x ++ "_" ++ counter.toString() +} + +// convenient string interpolations +// for instructions and labels +import scala.language.implicitConversions +import scala.language.reflectiveCalls + +implicit def sring_inters(sc: StringContext) = new { + def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" + def l(args: Any*): String = sc.s(args:_*) ++ ":\n" +} + + +// environments +type Env = Map[String, String] + +// arithmetic expression compilation +def compile_aexp(a: AExp, env : Env) : String = a match { + case Num(i) => i"ldc $i" + case Var(s) => i"iload ${env(s)}" + case Aop("+", a1, a2) => + compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"iadd" + case Aop("-", a1, a2) => + compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"isub" + case Aop("*", a1, a2) => + compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"imul" + case Ref(s, a1) => + i"aload ${env(s)}" ++ compile_aexp(a1, env) ++ i"iaload" +} + +// boolean expression compilation +def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { + case True => "" + case False => i"goto $jmp" + case Bop("=", a1, a2) => + compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpne $jmp" + case Bop("!=", a1, a2) => + compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpeq $jmp" + case Bop("<", a1, a2) => + compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpge $jmp" +} + +// statement compilation +def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match { + case Skip => ("", env) + case Assign(x, a) => { + val index = if (env.isDefinedAt(x)) env(x) else + env.keys.size.toString + (compile_aexp(a, env) ++ i"istore $index", env + (x -> index)) + } + case If(b, bl1, bl2) => { + val if_else = Fresh("If_else") + val if_end = Fresh("If_end") + val (instrs1, env1) = compile_block(bl1, env) + val (instrs2, env2) = compile_block(bl2, env1) + (compile_bexp(b, env, if_else) ++ + instrs1 ++ + i"goto $if_end" ++ + l"$if_else" ++ + instrs2 ++ + l"$if_end", env2) + } + case While(b, bl) => { + val loop_begin = Fresh("Loop_begin") + val loop_end = Fresh("Loop_end") + val (instrs1, env1) = compile_block(bl, env) + (l"$loop_begin" ++ + compile_bexp(b, env, loop_end) ++ + instrs1 ++ + i"goto $loop_begin" ++ + l"$loop_end", env1) + } + case Write(x) => + (i"iload ${env(x)}" ++ + i"invokestatic XXX/XXX/write(I)V", env) + case Array(s, n) => { + val index = if (env.isDefinedAt(s)) throw new Exception("Array already defined") else + env.keys.size.toString + (i"ldc $n" ++ + i"newarray int" ++ + i"astore $index", env + (s -> index)) + } + case AssignA(s, a1, a2) => { + val index = if (env.isDefinedAt(s)) env(s) else + throw new Exception("Array not yet defined") + (i"aload $index" ++ + compile_aexp(a1, env) ++ + compile_aexp(a2, env) ++ + i"iastore", env) + } +} + +// compilation of a block (i.e. list of instructions) +def compile_block(bl: Block, env: Env) : (String, Env) = bl match { + case Nil => ("", env) + case s::bl => { + val (instrs1, env1) = compile_stmt(s, env) + val (instrs2, env2) = compile_block(bl, env1) + (instrs1 ++ instrs2, env2) + } +} + +// main compilation function for blocks +def compile(bl: Block, class_name: String) : String = { + val instructions = compile_block(bl, Map.empty)._1 + (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 + + + +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() +} + +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").!! + println("assembled ") +} + +def time_needed[T](i: Int, code: => T) = { + val start = System.nanoTime() + for (j <- 1 to i) code + val end = System.nanoTime() + (end - start)/(i * 1.0e9) +} + + +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) +} + +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" +} + +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 = """++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ + ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< + ]>.>+[>>]>+]""" + +bf_run(bf1, "sier") + +bf_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++ + ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", "hello") + +bf_run("""+++++++++++ + >+>>>>++++++++++++++++++++++++++++++++++++++++++++ + >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> + +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[- + <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<< + -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]] + >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++ + +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++ + ++++++++++++++++++++++++++++++++++++++++++++.[-]<< + <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< + [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""", "fibs")