progs/fun-bare.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 06 Apr 2016 12:18:54 +0100
changeset 400 e4afe3f46c29
parent 323 4ce07c4abdb4
child 624 8d0af38389bc
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
// A Small Compiler for a simple functional language
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
// Abstract syntax trees
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
abstract class Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
abstract class BExp 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
abstract class Decl
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
// function declarations
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case class Def(name: String, args: List[String], body: Exp) extends Decl
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class Main(e: Exp) extends Decl
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
// expressions
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
case class Call(name: String, args: List[Exp]) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
case class Write(e: Exp) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
case class Var(s: String) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
case class Num(i: Int) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
case class Sequ(e1: Exp, e2: Exp) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
// boolean expressions
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
// calculating the maximal needed stack size
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
def max_stack_exp(e: Exp): Int = e match {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  case Call(_, args) => args.map(max_stack_exp).sum
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  case If(a, e1, e2) => max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  case Write(e) => max_stack_exp(e) + 1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  case Var(_) => 1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  case Num(_) => 1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  case Sequ(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
def max_stack_bexp(e: BExp): Int = e match {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
// compiler - built-in functions 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
//
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
val library = """
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
.class public XXX.XXX
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
.super java/lang/Object
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
.method public <init>()V
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
        aload_0
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
        invokenonvirtual java/lang/Object/<init>()V
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
        return
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
.end method
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
.method public static write(I)V 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
        .limit locals 5 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
        .limit stack 5 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
        iload 0 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
        getstatic java/lang/System/out Ljava/io/PrintStream; 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
        swap 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
        invokevirtual java/io/PrintStream/println(I)V 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
        return 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
.end method
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
"""
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
// for generating new labels
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
var counter = -1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
def Fresh(x: String) = {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  counter += 1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  x ++ "_" ++ counter.toString()
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
type Env = Map[String, Int]
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
type Instrs = List[String]
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
// compile expressions
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
def compile_exp(a: Exp, env : Env) : Instrs = a match {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
  case Num(i) => List("ldc " + i.toString + "\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  case Var(s) => List("iload " + env(s).toString + "\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("iadd\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
  case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("isub\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("imul\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("idiv\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("irem\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  case If(b, a1, a2) => {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
    val if_else = Fresh("If_else")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
    val if_end = Fresh("If_end")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
    compile_bexp(b, env, if_else) ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
    compile_exp(a1, env) ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
    List("goto " + if_end + "\n") ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
    List("\n" + if_else + ":\n\n") ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
    compile_exp(a2, env) ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
    List("\n" + if_end + ":\n\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  }
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
  case Call(n, args) => {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
    val is = "I" * args.length
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
    args.flatMap(a => compile_exp(a, env)) ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
    List ("invokestatic XXX/XXX/" + n + "(" + is + ")I\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  }
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
  case Sequ(a1, a2) => {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
    compile_exp(a1, env) ++ List("pop\n") ++ compile_exp(a2, env)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  }
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  case Write(a1) => {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
    compile_exp(a1, env) ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
    List("dup\n",
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
         "invokestatic XXX/XXX/write(I)V\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  }
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
// compile boolean expressions
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
def compile_bexp(b: BExp, env : Env, jmp: String) : Instrs = b match {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  case Bop("==", a1, a2) => 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpne " + jmp + "\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
  case Bop("!=", a1, a2) => 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpeq " + jmp + "\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
  case Bop("<", a1, a2) => 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpge " + jmp + "\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
  case Bop("<=", a1, a2) => 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpgt " + jmp + "\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
// compile function for declarations and main
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
def compile_decl(d: Decl) : Instrs = d match {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
  case Def(name, args, a) => { 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
    val env = args.zipWithIndex.toMap
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
    val is = "I" * args.length
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
    List(".method public static " + name + "(" + is + ")I \n",
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
         ".limit locals " + args.length.toString + "\n",
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
         ".limit stack " + (1 + max_stack_exp(a)).toString + "\n",
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
         name + "_Start:\n") ++   
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
    compile_exp(a, env) ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
    List("ireturn\n",
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
         ".end method \n\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
  }
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
  case Main(a) => {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
    List(".method public static main([Ljava/lang/String;)V\n",
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
         ".limit locals 200\n",
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
         ".limit stack 200\n") ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
    compile_exp(a, Map()) ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
    List("invokestatic XXX/XXX/write(I)V\n",
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
         "return\n",
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
         ".end method\n")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
  }
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
// main compilation function
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
def compile(prog: List[Decl], class_name: String) : String = {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
  val instructions = prog.flatMap(compile_decl).mkString
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
  (library + instructions).replaceAllLiterally("XXX", class_name)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
// example program (factorials)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
val test_prog = 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
  List(Def("fact", List("n"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
         If(Bop("==",Var("n"),Num(0)),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
            Num(1),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
            Aop("*",Var("n"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
                    Call("fact",List(Aop("-",Var("n"),Num(1))))))),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
       Def("facT",List("n", "acc"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
         If(Bop("==",Var("n"),Num(0)),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
            Var("acc"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
            Call("facT",List(Aop("-",Var("n"),Num(1)), 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
                             Aop("*",Var("n"),Var("acc")))))),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
       Main(Sequ(Write(Call("fact",List(Num(10)))),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
                 Write(Call("facT",List(Num(10), Num(1)))))))
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
// prints out the JVM instructions
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
println(compile(test, "fact"))