progs/fun/funt.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 25 Oct 2020 01:25:01 +0000
changeset 789 f0696713177b
parent 734 progs/fun/funt.scala@5d860ff01938
child 813 059f970287d1
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
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     2
// (includes a lexer and a parser)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     3
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     4
import $file.fun_tokens, fun_tokens._
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     5
import $file.fun_parser, fun_parser._ 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     6
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
// compiler - built-in functions 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
// 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
     9
//
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
val library = """
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
.class public XXX.XXX
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
.super java/lang/Object
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
.method public static write(I)V 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    16
        .limit locals 1 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    17
        .limit stack 2 
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
        getstatic java/lang/System/out Ljava/io/PrintStream; 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    19
        iload 0
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
        invokevirtual java/io/PrintStream/println(I)V 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
        return 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
.end method
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
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
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    26
// calculating the maximal needed stack size
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    27
def max_stack_exp(e: Exp): Int = e match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    28
  case Call(_, args) => args.map(max_stack_exp).sum
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    29
  case If(a, e1, e2) => max_stack_bexp(a) + (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
    30
  case Write(e) => max_stack_exp(e) + 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    31
  case Var(_) => 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    32
  case Num(_) => 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    33
  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
    34
  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
    35
}
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    36
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    37
def max_stack_bexp(e: BExp): Int = e match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    38
  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
    39
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    40
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    41
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
// for generating new labels
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
var counter = -1
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
def Fresh(x: String) = {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  counter += 1
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  x ++ "_" ++ counter.toString()
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    50
// convenient string interpolations 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    51
// for instructions, labels and methods
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    52
import scala.language.implicitConversions
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    53
import scala.language.reflectiveCalls
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    54
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    55
implicit def sring_inters(sc: StringContext) = new {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    56
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    57
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    58
    def m(args: Any*): String = sc.s(args:_*) ++ "\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    59
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    60
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    62
type Env = Map[String, Int]
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    63
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    64
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    65
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
    66
  case Num(i) => i"ldc $i"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    67
  case Var(s) => i"iload ${env(s)}"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    68
  case Aop("+", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"iadd"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    69
  case Aop("-", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"isub"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    70
  case Aop("*", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"imul"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    71
  case Aop("/", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"idiv"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    72
  case Aop("%", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"irem"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
  case If(b, a1, a2) => {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
    val if_else = Fresh("If_else")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
    val if_end = Fresh("If_end")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
    compile_bexpT(b, env, if_else) ++
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
    compile_expT(a1, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    78
    i"goto $if_end" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    79
    l"$if_else" ++
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
    compile_expT(a2, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    81
    l"$if_end"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  case Call(n, args) => if (name == n) { 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    84
    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
    85
    args.map(a => compile_expT(a, env, "")).mkString ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    86
    stores.reverse.mkString ++ 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    87
    i"goto ${n}_Start" 
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
  } else {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
    val is = "I" * args.length
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    90
    args.map(a => compile_expT(a, env, "")).mkString ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    91
    i"invokestatic XXX/XXX/${n}(${is})I"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
  }
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    93
  case Sequence(a1, a2) => {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    94
    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
    95
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  case Write(a1) => {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
    compile_expT(a1, env, "") ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    98
    i"dup" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    99
    i"invokestatic XXX/XXX/write(I)V"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   103
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
   104
  case Bop("==", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   105
    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
   106
  case Bop("!=", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   107
    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
   108
  case Bop("<", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   109
    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
   110
  case Bop("<=", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   111
    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
   112
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   115
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
   116
  case Def(name, args, a) => { 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
    val env = args.zipWithIndex.toMap
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
    val is = "I" * args.length
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   119
    m".method public static $name($is)I" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   120
    m".limit locals ${args.length}" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   121
    m".limit stack ${1 + max_stack_exp(a)}" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   122
    l"${name}_Start" ++   
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
    compile_expT(a, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   124
    i"ireturn" ++ 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   125
    m".end method\n"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
  case Main(a) => {
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   128
    m".method public static main([Ljava/lang/String;)V" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   129
    m".limit locals 200" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   130
    m".limit stack 200" ++
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
    compile_expT(a, Map(), "") ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   132
    i"invokestatic XXX/XXX/write(I)V" ++
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   133
    i"return" ++
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   134
    m".end method"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   138
// main compiler functions
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   139
def compile(prog: List[Decl], class_name: String) : String = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   140
  val instructions = prog.map(compile_decl).mkString
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   141
  (library + instructions).replaceAllLiterally("XXX", class_name)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   142
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   143
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   144
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   145
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   146
def main(fname: String) = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   147
    val path = os.pwd / fname
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   148
    val class_name = fname.stripSuffix("." ++ path.ext)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   149
    val tks = tokenise(os.read(path))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   150
    val ast = parse_tks(tks)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   151
    println(compile(ast, class_name))
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   152
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   153
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   154
/*
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   155
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   156
import scala.sys.process._
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   157
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   158
def compile_run(class_name: String) : Unit = {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   159
  compile_file(class_name)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   160
  (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!!
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   161
  println("Time: " + time_needed(2, (s"java ${class_name}/${class_name}").!))
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
//examples
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   166
compile_run("defs")
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   167
compile_run("fact")
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   168
*/