progs/fun-bare.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 28 Jul 2019 14:24:46 +0100
changeset 624 8d0af38389bc
parent 323 4ce07c4abdb4
child 625 6709fa87410b
permissions -rw-r--r--
updated to Scala 2.13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
     1
// A Small Compiler for a Simple Functional Language
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
     2
// (it does not use a parser and lexer)
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
// Abstract syntax trees
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
abstract class Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
abstract class BExp 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
abstract class Decl
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
// function declarations
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
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
    11
case class Main(e: Exp) extends Decl
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
// expressions
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
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
    15
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
    16
case class Write(e: Exp) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
case class Var(s: String) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
case class Num(i: Int) extends Exp
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
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
    20
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
    21
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
// boolean expressions
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
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
    24
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
// calculating the maximal needed stack size
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
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
    27
  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
    28
  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
    29
  case Write(e) => max_stack_exp(e) + 1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  case Var(_) => 1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  case Num(_) => 1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  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
    33
  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
    34
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
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
    36
  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
    37
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
// compiler - built-in functions 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
// 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
    41
//
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
val library = """
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
.class public XXX.XXX
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
.super java/lang/Object
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
.method public <init>()V
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
        aload_0
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
        invokenonvirtual java/lang/Object/<init>()V
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
        return
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
.end method
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
.method public static write(I)V 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
        .limit locals 5 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
        .limit stack 5 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
        iload 0 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
        getstatic java/lang/System/out Ljava/io/PrintStream; 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
        swap 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
        invokevirtual java/io/PrintStream/println(I)V 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
        return 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
.end method
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
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
// for generating new labels
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
var counter = -1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
def Fresh(x: String) = {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  counter += 1
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
  x ++ "_" ++ counter.toString()
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
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    73
// convenient string interpolations 
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    74
// for instructions, labels and methods
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    75
import scala.language.implicitConversions
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    76
import scala.language.reflectiveCalls
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    77
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    78
implicit def sring_inters(sc: StringContext) = new {
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    79
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    80
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    81
    def m(args: Any*): String = sc.s(args:_*) ++ "\n"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    82
}
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
type Env = Map[String, Int]
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
// compile expressions
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    87
def compile_exp(a: Exp, env : Env) : String = a match {
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    88
  case Num(i) => i"ldc $i"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    89
  case Var(s) => i"iload ${env(s)}"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    90
  case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"iadd"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    91
  case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"isub"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    92
  case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"imul"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    93
  case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"idiv"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    94
  case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"irem"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
  case If(b, a1, a2) => {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
    val if_else = Fresh("If_else")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
    val if_end = Fresh("If_end")
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
    compile_bexp(b, env, if_else) ++
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
    compile_exp(a1, env) ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   100
    i"goto $if_end" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   101
    l"$if_else" ++
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
    compile_exp(a2, env) ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   103
    l"$if_end"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
  }
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   105
  case Call(name, args) => {
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
    val is = "I" * args.length
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   107
    args.map(a => compile_exp(a, env)).mkString ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   108
    i"invokestatic XXX/XXX/$name($is)I"
323
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
  case Sequ(a1, a2) => {
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   111
    compile_exp(a1, env) ++ i"pop" ++ compile_exp(a2, env)
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  }
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
  case Write(a1) => {
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
    compile_exp(a1, env) ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   115
    i"dup" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   116
    i"invokestatic XXX/XXX/write(I)V"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
  }
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
// compile boolean expressions
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   121
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
  case Bop("==", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   123
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpne $jmp"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
  case Bop("!=", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   125
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpeq $jmp"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
  case Bop("<", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   127
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpge $jmp"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
  case Bop("<=", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   129
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpgt $jmp"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
// compile function for declarations and main
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   133
def compile_decl(d: Decl) : String = d match {
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
  case Def(name, args, a) => { 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
    val env = args.zipWithIndex.toMap
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
    val is = "I" * args.length
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   137
    m".method public static $name($is)I" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   138
    m".limit locals ${args.length.toString}" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   139
    m".limit stack ${1 + max_stack_exp(a)}" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   140
    l"${name}_Start" ++   
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
    compile_exp(a, env) ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   142
    i"ireturn" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   143
    m".end method\n"
323
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
  case Main(a) => {
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   146
    m".method public static main([Ljava/lang/String;)V" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   147
    m".limit locals 200" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   148
    m".limit stack 200" ++
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
    compile_exp(a, Map()) ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   150
    i"invokestatic XXX/XXX/write(I)V" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   151
    i"return" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   152
    m".end method\n"
323
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
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
// main compilation function
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
def compile(prog: List[Decl], class_name: String) : String = {
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   158
  val instructions = prog.map(compile_decl).mkString
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
  (library + instructions).replaceAllLiterally("XXX", class_name)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
}
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
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
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
// example program (factorials)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
val test_prog = 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
  List(Def("fact", List("n"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
         If(Bop("==",Var("n"),Num(0)),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
            Num(1),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
            Aop("*",Var("n"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
                    Call("fact",List(Aop("-",Var("n"),Num(1))))))),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
       Def("facT",List("n", "acc"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
         If(Bop("==",Var("n"),Num(0)),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
            Var("acc"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
            Call("facT",List(Aop("-",Var("n"),Num(1)), 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
                             Aop("*",Var("n"),Var("acc")))))),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
       Main(Sequ(Write(Call("fact",List(Num(10)))),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
                 Write(Call("facT",List(Num(10), Num(1)))))))
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
// prints out the JVM instructions
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   184
println(compile(test_prog, "fact"))