progs/fun/funt.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 06 Oct 2021 20:43:35 +0100
changeset 842 b53ac1bb5f43
parent 813 059f970287d1
child 869 81ee93b87258
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     1
// A Small Compiler for a Simple Functional Language
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     2
//  - includes a lexer and a parser
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     3
//  - performs tail-call optimisations
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     4
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     5
// call with
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     6
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     7
//    amm fun.sc main defs.fun
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     8
//    amm fun.sc main fact.fun
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     9
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    10
//  or
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    11
// 
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    12
//    amm fun.sc run defs.fun
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    13
//    amm fun.sc run fact.fun   
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    14
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    15
// the first prints out the JVM instructions
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    16
// the second runs the generated class files
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    17
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    18
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    19
import $file.fun_tokens, fun_tokens._
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    20
import $file.fun_parser, fun_parser._ 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    21
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
// compiler - built-in functions 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
//
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
val library = """
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
.class public XXX.XXX
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
.super java/lang/Object
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
.method public static write(I)V 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    31
        .limit locals 1 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    32
        .limit stack 2 
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
        getstatic java/lang/System/out Ljava/io/PrintStream; 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    34
        iload 0
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
        invokevirtual java/io/PrintStream/println(I)V 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
        return 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
.end method
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
"""
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    41
// calculating the maximal needed stack size
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    42
def max_stack_exp(e: Exp): Int = e match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    43
  case Call(_, args) => args.map(max_stack_exp).sum
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    44
  case If(a, e1, e2) => 
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    45
    max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max)
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    46
  case Write(e) => max_stack_exp(e) + 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    47
  case Var(_) => 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    48
  case Num(_) => 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    49
  case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    50
  case Sequence(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    51
}
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    52
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    53
def max_stack_bexp(e: BExp): Int = e match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    54
  case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    55
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    56
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    57
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
// for generating new labels
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
var counter = -1
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
def Fresh(x: String) = {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  counter += 1
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  x ++ "_" ++ counter.toString()
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    66
// convenient string interpolations 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    67
// for instructions, labels and methods
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    68
import scala.language.implicitConversions
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    69
import scala.language.reflectiveCalls
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    70
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    71
implicit def sring_inters(sc: StringContext) = new {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    72
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    73
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    74
    def m(args: Any*): String = sc.s(args:_*) ++ "\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    75
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    76
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    78
type Env = Map[String, Int]
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    79
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    80
def compile_op(op: String) = op match {
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    81
  case "+" => i"iadd"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    82
  case "-" => i"isub"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    83
  case "*" => i"imul"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    84
  case "/" => i"idiv"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    85
  case "%" => i"irem"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    86
}
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    87
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    88
def compile_expT(a: Exp, env : Env, name: String) : String = a match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    89
  case Num(i) => i"ldc $i"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    90
  case Var(s) => i"iload ${env(s)}"
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    91
  case Aop(op, a1, a2) => 
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    92
    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ compile_op(op)
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  case If(b, a1, a2) => {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
    val if_else = Fresh("If_else")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
    val if_end = Fresh("If_end")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
    compile_bexpT(b, env, if_else) ++
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
    compile_expT(a1, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    98
    i"goto $if_end" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    99
    l"$if_else" ++
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
    compile_expT(a2, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   101
    l"$if_end"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  case Call(n, args) => if (name == n) { 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   104
    val stores = args.zipWithIndex.map { case (x, y) => i"istore $y" } 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   105
    args.map(a => compile_expT(a, env, "")).mkString ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   106
    stores.reverse.mkString ++ 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   107
    i"goto ${n}_Start" 
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  } else {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
    val is = "I" * args.length
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   110
    args.map(a => compile_expT(a, env, "")).mkString ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   111
    i"invokestatic XXX/XXX/${n}(${is})I"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  }
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   113
  case Sequence(a1, a2) => {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   114
    compile_expT(a1, env, "") ++ i"pop" ++ compile_expT(a2, env, name)
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
  case Write(a1) => {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
    compile_expT(a1, env, "") ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   118
    i"dup" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   119
    i"invokestatic XXX/XXX/write(I)V"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   123
def compile_bexpT(b: BExp, env : Env, jmp: String) : String = b match {
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
  case Bop("==", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   125
    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpne $jmp"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
  case Bop("!=", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   127
    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpeq $jmp"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
  case Bop("<", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   129
    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpge $jmp"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
  case Bop("<=", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   131
    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpgt $jmp"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   135
def compile_decl(d: Decl) : String = d match {
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
  case Def(name, args, a) => { 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
    val env = args.zipWithIndex.toMap
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
    val is = "I" * args.length
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   139
    m".method public static $name($is)I" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   140
    m".limit locals ${args.length}" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   141
    m".limit stack ${1 + max_stack_exp(a)}" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   142
    l"${name}_Start" ++   
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
    compile_expT(a, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   144
    i"ireturn" ++ 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   145
    m".end method\n"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
  case Main(a) => {
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   148
    m".method public static main([Ljava/lang/String;)V" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   149
    m".limit locals 200" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   150
    m".limit stack 200" ++
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
    compile_expT(a, Map(), "") ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   152
    i"invokestatic XXX/XXX/write(I)V" ++
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   153
    i"return" ++
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   154
    m".end method"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   158
// main compiler functions
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   159
def compile(prog: List[Decl], class_name: String) : String = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   160
  val instructions = prog.map(compile_decl).mkString
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   161
  (library + instructions).replaceAllLiterally("XXX", class_name)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   162
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   163
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   164
import ammonite.ops._
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   165
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   166
def compile_to_file(prog: List[Decl], class_name: String) : Unit = 
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   167
  write.over(pwd / s"$class_name.j", compile(prog, class_name))  
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   168
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   169
def compile_and_run(prog: List[Decl], class_name: String) : Unit = {
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   170
  println(s"Start of compilation")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   171
  compile_to_file(prog, class_name)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   172
  println(s"generated $class_name.j file")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   173
  os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call()
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   174
  println(s"generated $class_name.class file")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   175
  println(s"Run program")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   176
  os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   177
  println(s"done.")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   178
}
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   179
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   180
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   181
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   182
def main(fname: String) = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   183
    val path = os.pwd / fname
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   184
    val class_name = fname.stripSuffix("." ++ path.ext)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   185
    val tks = tokenise(os.read(path))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   186
    val ast = parse_tks(tks)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   187
    println(compile(ast, class_name))
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   188
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   189
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   190
@main
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   191
def run(fname: String) = {
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   192
    val path = os.pwd / fname
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   193
    val class_name = fname.stripSuffix("." ++ path.ext)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   194
    val tks = tokenise(os.read(path))
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   195
    val ast = parse_tks(tks)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   196
    compile_and_run(ast, class_name)
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
}