# HG changeset patch # User Christian Urban # Date 1595236003 -3600 # Node ID fc2e3609d5e50aa1e348484f24434bef9741ef36 # Parent 5d860ff0193833af943be57ed1697763ecd2fedc updatd diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/graphs.pdf Binary file handouts/graphs.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho01.pdf Binary file handouts/ho01.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho02.pdf Binary file handouts/ho02.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho03.pdf Binary file handouts/ho03.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho04.pdf Binary file handouts/ho04.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho05.pdf Binary file handouts/ho05.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho06.pdf Binary file handouts/ho06.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho07.pdf Binary file handouts/ho07.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho08.pdf Binary file handouts/ho08.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho09.pdf Binary file handouts/ho09.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/ho10.pdf Binary file handouts/ho10.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/notation.pdf Binary file handouts/notation.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 handouts/scala-ho.pdf Binary file handouts/scala-ho.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/hw01.pdf Binary file hws/hw01.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/hw02.pdf Binary file hws/hw02.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/hw03.pdf Binary file hws/hw03.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/hw04.pdf Binary file hws/hw04.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/hw05.pdf Binary file hws/hw05.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/hw06.pdf Binary file hws/hw06.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/hw07.pdf Binary file hws/hw07.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/hw08.pdf Binary file hws/hw08.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/hw09.pdf Binary file hws/hw09.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 hws/proof.pdf Binary file hws/proof.pdf has changed diff -r 5d860ff01938 -r fc2e3609d5e5 progs/fun/defs.fun --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/fun/defs.fun Mon Jul 20 10:06:43 2020 +0100 @@ -0,0 +1,85 @@ + +def zero(x) = 0; + +def suc(x) = x + 1; + +def pred(x) = + if x == 0 then x else x - 1; + +def add(x, y) = + if x == 0 then y else suc(add(x - 1, y)); + +def mult(x, y) = + if x == 0 then 0 else add(y, mult(x - 1, y)); + +def pow(x, y) = + if y == 0 then 1 else mult(x, pow(x, y - 1)); + +def fib(n) = if n == 0 then 0 + else if n == 1 then 1 + else fib(n - 1) + fib(n - 2); + +def fact(n) = + if n == 0 then 1 else n * fact(n - 1); + +def ack(m, n) = if m == 0 then n + 1 + else if n == 0 then ack(m - 1, 1) + else ack(m - 1, ack(m, n - 1)); + +def stack_test(x) = x + 1 + 2 + 3 + 4 + 5; + +def div(x, y) = x / y; //integer division + +def rem(x, y) = x % y; //remainder + +def gcd(a, b) = + if b == 0 then a else gcd(b, a % b); + +def is_prime_aux(n, i) = + if n % i == 0 then 0 + else if (i * i) <= n then is_prime_aux(n, i + 1) + else 1; + +def is_prime(n) = if n == 2 then 1 else is_prime_aux(n, 2); + +def primes(n) = + if n == 0 then 0 + else if is_prime(n) == 1 then (write n; primes(n - 1)) + else primes(n - 1); + +def is_collatz(n) = + if n == 1 then 1 + else if n % 2 == 0 then is_collatz(n / 2) + else is_collatz(3 * n + 1); + +def collatz_aux(n, i) = + if i > n then 0 + else if is_collatz(i) == 1 then (write i; collatz_aux(n, i + 1)) + else collatz_aux(n, i + 1); + +def collatz(n) = collatz_aux(n, 1); + +def facT(n, acc) = + if n == 0 then acc else facT(n - 1, n * acc); + + +//zero(3) +//suc(8) +//pred(7) +//write(add(3, 4)) +//mult(4,5) +//pow(2, 3) +//fib(20) +//(write(fact(5)) ; fact(6)) +//(write(1) ; 2) +//write(ack(3, 12)) // for tail-rec test +//stack_test(0) +//(write (div(11, 3)); rem(11, 3)) +//gcd(54, 24) +//is_prime(2) +primes(1000) +//primes(1000000) +//collatz(4000) +//collatz(5000) +//facT(6, 1) + diff -r 5d860ff01938 -r fc2e3609d5e5 progs/fun/fact.fun --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/fun/fact.fun Mon Jul 20 10:06:43 2020 +0100 @@ -0,0 +1,15 @@ +def fact(n) = + (if n == 0 then 1 else n * fact(n - 1)); + +def facT(n, acc) = + if n == 0 then acc else facT(n - 1, n * acc); + +def facTi(n) = facT(n, 1); + +//fact(10) +//facTi(10) + +write(facTi(6)) + +// a simple factorial program +// (including a tail recursive version) diff -r 5d860ff01938 -r fc2e3609d5e5 progs/fun/fun-bare.scala --- a/progs/fun/fun-bare.scala Sat Jul 04 22:12:18 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,187 +0,0 @@ -// A Small Compiler for a Simple Functional Language -// (it does not include a parser and lexer) - -// abstract syntax trees -abstract class Exp -abstract class BExp -abstract class Decl - -// functions and declarations -case class Def(name: String, args: List[String], body: Exp) extends Decl -case class Main(e: Exp) extends Decl - -// expressions -case class Call(name: String, args: List[Exp]) extends Exp -case class If(a: BExp, e1: Exp, e2: Exp) extends Exp -case class Write(e: Exp) extends Exp -case class Var(s: String) extends Exp -case class Num(i: Int) extends Exp -case class Aop(o: String, a1: Exp, a2: Exp) extends Exp -case class Sequ(e1: Exp, e2: Exp) extends Exp - -// boolean expressions -case class Bop(o: String, a1: Exp, a2: Exp) extends BExp - -// calculating the maximal needed stack size -def max_stack_exp(e: Exp): Int = e match { - case Call(_, args) => args.map(max_stack_exp).sum - case If(a, e1, e2) => max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) - case Write(e) => max_stack_exp(e) + 1 - case Var(_) => 1 - case Num(_) => 1 - case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) - case Sequ(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max -} -def max_stack_bexp(e: BExp): Int = e match { - case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) -} - -// compiler - built-in functions -// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html -// - -val library = """ -.class public XXX.XXX -.super java/lang/Object - -.method public static write(I)V - .limit locals 5 - .limit stack 5 - iload 0 - getstatic java/lang/System/out Ljava/io/PrintStream; - swap - invokevirtual java/io/PrintStream/println(I)V - return -.end method - -""" - -// for generating new labels -var counter = -1 - -def Fresh(x: String) = { - counter += 1 - x ++ "_" ++ counter.toString() -} - -// convenient string interpolations for -// generating instructions, labels etc -import scala.language.implicitConversions -import scala.language.reflectiveCalls - -// convenience for code-generation (string interpolations) -implicit def sring_inters(sc: StringContext) = new { - def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" // instructions - def l(args: Any*): String = sc.s(args:_*) ++ ":\n" // labels - def m(args: Any*): String = sc.s(args:_*) ++ "\n" // methods -} - -// variable / index environments -type Env = Map[String, Int] - -// compile expressions -def compile_exp(a: Exp, env : Env) : String = a match { - case Num(i) => i"ldc $i" - case Var(s) => i"iload ${env(s)}" - case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"iadd" - case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"isub" - case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"imul" - case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"idiv" - case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"irem" - case If(b, a1, a2) => { - val if_else = Fresh("If_else") - val if_end = Fresh("If_end") - compile_bexp(b, env, if_else) ++ - compile_exp(a1, env) ++ - i"goto $if_end" ++ - l"$if_else" ++ - compile_exp(a2, env) ++ - l"$if_end" - } - case Call(name, args) => { - val is = "I" * args.length - args.map(a => compile_exp(a, env)).mkString ++ - i"invokestatic XXX/XXX/$name($is)I" - } - case Sequ(a1, a2) => { - compile_exp(a1, env) ++ i"pop" ++ compile_exp(a2, env) - } - case Write(a1) => { - compile_exp(a1, env) ++ - i"dup" ++ - i"invokestatic XXX/XXX/write(I)V" - } -} - -// compile boolean expressions -def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { - case Bop("==", a1, a2) => - compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpne $jmp" - case Bop("!=", a1, a2) => - compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpeq $jmp" - case Bop("<", a1, a2) => - compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpge $jmp" - case Bop("<=", a1, a2) => - compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpgt $jmp" -} - -// compile functions and declarations -def compile_decl(d: Decl) : String = d match { - case Def(name, args, a) => { - val env = args.zipWithIndex.toMap - val is = "I" * args.length - m".method public static $name($is)I" ++ - m".limit locals ${args.length.toString}" ++ - m".limit stack ${1 + max_stack_exp(a)}" ++ - l"${name}_Start" ++ - compile_exp(a, env) ++ - i"ireturn" ++ - m".end method\n" - } - case Main(a) => { - m".method public static main([Ljava/lang/String;)V" ++ - m".limit locals 200" ++ - m".limit stack 200" ++ - compile_exp(a, Map()) ++ - i"invokestatic XXX/XXX/write(I)V" ++ - i"return" ++ - m".end method\n" - } -} - -// the main compilation function -def compile(prog: List[Decl], class_name: String) : String = { - val instructions = prog.map(compile_decl).mkString - (library + instructions).replaceAllLiterally("XXX", class_name) -} - - - - -// An example program (factorials) -// -// def fact(n) = if n == 0 then 1 else n * fact(n - 1); -// def facT(n, acc) = if n == 0 then acc else facT(n - 1, n * acc); -// -// fact(10) ; facT(15, 1) -// - - -val test_prog = - List(Def("fact", List("n"), - If(Bop("==",Var("n"),Num(0)), - Num(1), - Aop("*",Var("n"), - Call("fact",List(Aop("-",Var("n"),Num(1))))))), - - Def("facT",List("n", "acc"), - If(Bop("==",Var("n"),Num(0)), - Var("acc"), - Call("facT",List(Aop("-",Var("n"),Num(1)), - Aop("*",Var("n"),Var("acc")))))), - - Main(Sequ(Write(Call("fact",List(Num(10)))), - Write(Call("facT",List(Num(10), Num(1))))))) - -// prints out the JVM instructions -println(compile(test_prog, "fact")) diff -r 5d860ff01938 -r fc2e3609d5e5 progs/fun/fun-orig.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/fun/fun-orig.scala Mon Jul 20 10:06:43 2020 +0100 @@ -0,0 +1,210 @@ +// A Small Compiler for a Simple Functional Language +// (includes an external lexer and parser) +// +// call with +// +// amm fun.scala fact +// +// amm fun.scala defs +// +// this will generate a .j file and run the jasmin +// assembler (installed at jvm/jasmin-2.4/jasmin.jar) +// it runs the resulting JVM file twice for timing +// purposes. + + +import java.io._ +import scala.util._ +import scala.sys.process._ + +// Abstract syntax trees for the Fun language +abstract class Exp extends Serializable +abstract class BExp extends Serializable +abstract class Decl extends Serializable + +case class Def(name: String, args: List[String], body: Exp) extends Decl +case class Main(e: Exp) extends Decl + +case class Call(name: String, args: List[Exp]) extends Exp +case class If(a: BExp, e1: Exp, e2: Exp) extends Exp +case class Write(e: Exp) extends Exp +case class Var(s: String) extends Exp +case class Num(i: Int) extends Exp +case class Aop(o: String, a1: Exp, a2: Exp) extends Exp +case class Sequence(e1: Exp, e2: Exp) extends Exp +case class Bop(o: String, a1: Exp, a2: Exp) extends BExp + + +// compiler - built-in functions +// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html +// + +val library = """ +.class public XXX.XXX +.super java/lang/Object + +.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 + +""" + +// calculating the maximal needed stack size +def max_stack_exp(e: Exp): Int = e match { + case Call(_, args) => args.map(max_stack_exp).sum + case If(a, e1, e2) => + max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) + case Write(e) => max_stack_exp(e) + 1 + case Var(_) => 1 + case Num(_) => 1 + case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) + case Sequence(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max +} + +def max_stack_bexp(e: BExp): Int = e match { + case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) +} + + +// for generating new labels +var counter = -1 + +def Fresh(x: String) = { + counter += 1 + x ++ "_" ++ counter.toString() +} + +// convenient string interpolations +// for instructions, labels and methods +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" + def m(args: Any*): String = sc.s(args:_*) ++ "\n" +} + + +type Env = Map[String, Int] + +// compile expressions +def compile_exp(a: Exp, env : Env) : String = a match { + case Num(i) => i"ldc $i" + case Var(s) => i"iload ${env(s)}" + case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"iadd" + case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"isub" + case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"imul" + case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"idiv" + case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"irem" + case If(b, a1, a2) => { + val if_else = Fresh("If_else") + val if_end = Fresh("If_end") + compile_bexp(b, env, if_else) ++ + compile_exp(a1, env) ++ + i"goto $if_end" ++ + l"$if_else" ++ + compile_exp(a2, env) ++ + l"$if_end" + } + case Call(name, args) => { + val is = "I" * args.length + args.map(a => compile_exp(a, env)).mkString ++ + i"invokestatic XXX/XXX/$name($is)I" + } + case Sequence(a1, a2) => { + compile_exp(a1, env) ++ i"pop" ++ compile_exp(a2, env) + } + case Write(a1) => { + compile_exp(a1, env) ++ + i"dup" ++ + i"invokestatic XXX/XXX/write(I)V" + } +} + +// compile boolean expressions +def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { + case Bop("==", a1, a2) => + compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpne $jmp" + case Bop("!=", a1, a2) => + compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpeq $jmp" + case Bop("<", a1, a2) => + compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpge $jmp" + case Bop("<=", a1, a2) => + compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpgt $jmp" +} + +// compile function for declarations and main +def compile_decl(d: Decl) : String = d match { + case Def(name, args, a) => { + val env = args.zipWithIndex.toMap + val is = "I" * args.length + m".method public static $name($is)I" ++ + m".limit locals ${args.length}" ++ + m".limit stack ${1 + max_stack_exp(a)}" ++ + l"${name}_Start" ++ + compile_exp(a, env) ++ + i"ireturn" ++ + m".end method\n" + } + case Main(a) => { + m".method public static main([Ljava/lang/String;)V" ++ + m".limit locals 200" ++ + m".limit stack 200" ++ + compile_exp(a, Map()) ++ + i"invokestatic XXX/XXX/write(I)V" ++ + i"return" ++ + m".end method\n" + } +} + +// main compiler functions + +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 deserialise[T](fname: String) : Try[T] = { + import scala.util.Using + Using(new ObjectInputStream(new FileInputStream(fname))) { + in => in.readObject.asInstanceOf[T] + } +} + +def compile(class_name: String) : String = { + val ast = deserialise[List[Decl]](class_name ++ ".prs").getOrElse(Nil) + val instructions = ast.map(compile_decl).mkString + (library + instructions).replaceAllLiterally("XXX", class_name) +} + +def compile_to_file(class_name: String) = { + val output = compile(class_name) + scala.tools.nsc.io.File(s"${class_name}.j").writeAll(output) +} + +def compile_and_run(class_name: String) : Unit = { + compile_to_file(class_name) + (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!! + println("Time: " + time_needed(1, (s"java ${class_name}/${class_name}").!)) +} + + +// some examples of .fun files +//compile_to_file("fact") +//compile_and_run("fact") +//compile_and_run("defs") + + +def main(args: Array[String]) : Unit = + compile_and_run(args(0)) + + +} diff -r 5d860ff01938 -r fc2e3609d5e5 progs/fun/fun.sc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/fun/fun.sc Mon Jul 20 10:06:43 2020 +0100 @@ -0,0 +1,201 @@ +// A Small Compiler for a Simple Functional Language +// (it does not include a parser and lexer) +// +// call with +// +// amm fun.sc +// +// this will print out the JVM instructions for two +// factorial functions + + +// abstract syntax trees +abstract class Exp +abstract class BExp +abstract class Decl + +// functions and declarations +case class Def(name: String, args: List[String], body: Exp) extends Decl +case class Main(e: Exp) extends Decl + +// expressions +case class Call(name: String, args: List[Exp]) extends Exp +case class If(a: BExp, e1: Exp, e2: Exp) extends Exp +case class Write(e: Exp) extends Exp +case class Var(s: String) extends Exp +case class Num(i: Int) extends Exp +case class Aop(o: String, a1: Exp, a2: Exp) extends Exp +case class Sequ(e1: Exp, e2: Exp) extends Exp + +// boolean expressions +case class Bop(o: String, a1: Exp, a2: Exp) extends BExp + +// calculating the maximal needed stack size +def max_stack_exp(e: Exp): Int = e match { + case Call(_, args) => args.map(max_stack_exp).sum + case If(a, e1, e2) => + max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) + case Write(e) => max_stack_exp(e) + 1 + case Var(_) => 1 + case Num(_) => 1 + case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) + case Sequ(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max +} +def max_stack_bexp(e: BExp): Int = e match { + case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) +} + +// compiler - built-in functions +// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html +// + +val library = """ +.class public XXX.XXX +.super java/lang/Object + +.method public static write(I)V + .limit locals 5 + .limit stack 5 + iload 0 + getstatic java/lang/System/out Ljava/io/PrintStream; + swap + invokevirtual java/io/PrintStream/println(I)V + return +.end method + +""" + +// for generating new labels +var counter = -1 + +def Fresh(x: String) = { + counter += 1 + x ++ "_" ++ counter.toString() +} + +// convenient string interpolations for +// generating instructions, labels etc +import scala.language.implicitConversions +import scala.language.reflectiveCalls + +// convenience for code-generation (string interpolations) +implicit def sring_inters(sc: StringContext) = new { + def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" // instructions + def l(args: Any*): String = sc.s(args:_*) ++ ":\n" // labels + def m(args: Any*): String = sc.s(args:_*) ++ "\n" // methods +} + +// variable / index environments +type Env = Map[String, Int] + +// compile expressions +def compile_exp(a: Exp, env : Env) : String = a match { + case Num(i) => i"ldc $i" + case Var(s) => i"iload ${env(s)}" + case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"iadd" + case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"isub" + case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"imul" + case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"idiv" + case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"irem" + case If(b, a1, a2) => { + val if_else = Fresh("If_else") + val if_end = Fresh("If_end") + compile_bexp(b, env, if_else) ++ + compile_exp(a1, env) ++ + i"goto $if_end" ++ + l"$if_else" ++ + compile_exp(a2, env) ++ + l"$if_end" + } + case Call(name, args) => { + val is = "I" * args.length + args.map(a => compile_exp(a, env)).mkString ++ + i"invokestatic XXX/XXX/$name($is)I" + } + case Sequ(a1, a2) => { + compile_exp(a1, env) ++ i"pop" ++ compile_exp(a2, env) + } + case Write(a1) => { + compile_exp(a1, env) ++ + i"dup" ++ + i"invokestatic XXX/XXX/write(I)V" + } +} + +// compile boolean expressions +def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { + case Bop("==", a1, a2) => + compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpne $jmp" + case Bop("!=", a1, a2) => + compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpeq $jmp" + case Bop("<", a1, a2) => + compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpge $jmp" + case Bop("<=", a1, a2) => + compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpgt $jmp" +} + +// compile functions and declarations +def compile_decl(d: Decl) : String = d match { + case Def(name, args, a) => { + val env = args.zipWithIndex.toMap + val is = "I" * args.length + m".method public static $name($is)I" ++ + m".limit locals ${args.length.toString}" ++ + m".limit stack ${1 + max_stack_exp(a)}" ++ + l"${name}_Start" ++ + compile_exp(a, env) ++ + i"ireturn" ++ + m".end method\n" + } + case Main(a) => { + m".method public static main([Ljava/lang/String;)V" ++ + m".limit locals 200" ++ + m".limit stack 200" ++ + compile_exp(a, Map()) ++ + i"invokestatic XXX/XXX/write(I)V" ++ + i"return" ++ + m".end method\n" + } +} + +// the main compilation function +def compile(prog: List[Decl], class_name: String) : String = { + val instructions = prog.map(compile_decl).mkString + (library + instructions).replaceAllLiterally("XXX", class_name) +} + + + + +// An example program (two versions of factorial) +// +// def fact(n) = +// if n == 0 then 1 else n * fact(n - 1); +// +// def facT(n, acc) = +// if n == 0 then acc else facT(n - 1, n * acc); +// +// fact(10) ; facT(10, 1) +// + + +val test_prog = + List(Def("fact", List("n"), + If(Bop("==",Var("n"),Num(0)), + Num(1), + Aop("*",Var("n"), + Call("fact",List(Aop("-",Var("n"),Num(1))))))), + + Def("facT",List("n", "acc"), + If(Bop("==",Var("n"),Num(0)), + Var("acc"), + Call("facT",List(Aop("-",Var("n"),Num(1)), + Aop("*",Var("n"),Var("acc")))))), + + Main(Sequ(Write(Call("fact",List(Num(10)))), + Write(Call("facT",List(Num(10), Num(1))))))) + +// prints out the JVM instructions +@main +def test() = + println(compile(test_prog, "fact")) diff -r 5d860ff01938 -r fc2e3609d5e5 progs/fun/fun.scala --- a/progs/fun/fun.scala Sat Jul 04 22:12:18 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,213 +0,0 @@ -// A Small Compiler for a Simple Functional Language -// (includes an external lexer and parser) -// -// call with -// -// scala fun.scala fact -// -// scala fun.scala defs -// -// this will generate a .j file and run the jasmin -// assembler (installed at jvm/jasmin-2.4/jasmin.jar) -// it runs the resulting JVM file twice for timing -// purposes. - - - - -object Compiler { - -import java.io._ -import scala.util._ -import scala.sys.process._ - -// Abstract syntax trees for the Fun language -abstract class Exp extends Serializable -abstract class BExp extends Serializable -abstract class Decl extends Serializable - -case class Def(name: String, args: List[String], body: Exp) extends Decl -case class Main(e: Exp) extends Decl - -case class Call(name: String, args: List[Exp]) extends Exp -case class If(a: BExp, e1: Exp, e2: Exp) extends Exp -case class Write(e: Exp) extends Exp -case class Var(s: String) extends Exp -case class Num(i: Int) extends Exp -case class Aop(o: String, a1: Exp, a2: Exp) extends Exp -case class Sequence(e1: Exp, e2: Exp) extends Exp -case class Bop(o: String, a1: Exp, a2: Exp) extends BExp - - -// compiler - built-in functions -// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html -// - -val library = """ -.class public XXX.XXX -.super java/lang/Object - -.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 - -""" - -// calculating the maximal needed stack size -def max_stack_exp(e: Exp): Int = e match { - case Call(_, args) => args.map(max_stack_exp).sum - case If(a, e1, e2) => max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) - case Write(e) => max_stack_exp(e) + 1 - case Var(_) => 1 - case Num(_) => 1 - case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) - case Sequence(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max -} - -def max_stack_bexp(e: BExp): Int = e match { - case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) -} - - -// for generating new labels -var counter = -1 - -def Fresh(x: String) = { - counter += 1 - x ++ "_" ++ counter.toString() -} - -// convenient string interpolations -// for instructions, labels and methods -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" - def m(args: Any*): String = sc.s(args:_*) ++ "\n" -} - - -type Env = Map[String, Int] - -// compile expressions -def compile_exp(a: Exp, env : Env) : String = a match { - case Num(i) => i"ldc $i" - case Var(s) => i"iload ${env(s)}" - case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"iadd" - case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"isub" - case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"imul" - case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"idiv" - case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"irem" - case If(b, a1, a2) => { - val if_else = Fresh("If_else") - val if_end = Fresh("If_end") - compile_bexp(b, env, if_else) ++ - compile_exp(a1, env) ++ - i"goto $if_end" ++ - l"$if_else" ++ - compile_exp(a2, env) ++ - l"$if_end" - } - case Call(name, args) => { - val is = "I" * args.length - args.map(a => compile_exp(a, env)).mkString ++ - i"invokestatic XXX/XXX/$name($is)I" - } - case Sequence(a1, a2) => { - compile_exp(a1, env) ++ i"pop" ++ compile_exp(a2, env) - } - case Write(a1) => { - compile_exp(a1, env) ++ - i"dup" ++ - i"invokestatic XXX/XXX/write(I)V" - } -} - -// compile boolean expressions -def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { - case Bop("==", a1, a2) => - compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpne $jmp" - case Bop("!=", a1, a2) => - compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpeq $jmp" - case Bop("<", a1, a2) => - compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpge $jmp" - case Bop("<=", a1, a2) => - compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpgt $jmp" -} - -// compile function for declarations and main -def compile_decl(d: Decl) : String = d match { - case Def(name, args, a) => { - val env = args.zipWithIndex.toMap - val is = "I" * args.length - m".method public static $name($is)I" ++ - m".limit locals ${args.length}" ++ - m".limit stack ${1 + max_stack_exp(a)}" ++ - l"${name}_Start" ++ - compile_exp(a, env) ++ - i"ireturn" ++ - m".end method\n" - } - case Main(a) => { - m".method public static main([Ljava/lang/String;)V" ++ - m".limit locals 200" ++ - m".limit stack 200" ++ - compile_exp(a, Map()) ++ - i"invokestatic XXX/XXX/write(I)V" ++ - i"return" ++ - m".end method\n" - } -} - -// main compiler functions - -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 deserialise[T](fname: String) : Try[T] = { - import scala.util.Using - Using(new ObjectInputStream(new FileInputStream(fname))) { - in => in.readObject.asInstanceOf[T] - } -} - -def compile(class_name: String) : String = { - val ast = deserialise[List[Decl]](class_name ++ ".prs").getOrElse(Nil) - val instructions = ast.map(compile_decl).mkString - (library + instructions).replaceAllLiterally("XXX", class_name) -} - -def compile_to_file(class_name: String) = { - val output = compile(class_name) - scala.tools.nsc.io.File(s"${class_name}.j").writeAll(output) -} - -def compile_and_run(class_name: String) : Unit = { - compile_to_file(class_name) - (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!! - println("Time: " + time_needed(1, (s"java ${class_name}/${class_name}").!)) -} - - -// some examples of .fun files -//compile_to_file("fact") -//compile_and_run("fact") -//compile_and_run("defs") - - -def main(args: Array[String]) : Unit = - compile_and_run(args(0)) - - -}