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