solutions/cw4/compiler.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 19 Sep 2023 09:54:41 +0100
changeset 920 7af2eea19646
parent 910 b655ce68983f
child 921 bb54e7aa1a3f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Compiler for JVM
910
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
     2
// 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
     3
// call with 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
     4
//
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
     5
//     amm2 compiler.sc 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
     6
//
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
import $file.lexer
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
import lexer._
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
import $file.parser
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
import parser._
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
val beginning = """
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
.class public XXX.XXX
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
.super java/lang/Object
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
.method public static write(I)V 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
    .limit locals 1 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
    .limit stack 2 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
    getstatic java/lang/System/out Ljava/io/PrintStream; 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
    iload 0
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
    invokevirtual java/io/PrintStream/print(I)V 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
    return 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
.end method
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
.method public static writes(Ljava/lang/String;)V
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
    .limit stack 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
    .limit locals 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
    getstatic java/lang/System/out Ljava/io/PrintStream;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
    aload 0
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
    invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
    return
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
.end method
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
.method public static read()I 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
    .limit locals 10 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
    .limit stack 10
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
    ldc 0 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
    istore 1  ; this will hold our final integer 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
Label1: 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
    getstatic java/lang/System/in Ljava/io/InputStream; 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
    invokevirtual java/io/InputStream/read()I 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
    istore 2 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
    iload 2 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
    ldc 10   ; the newline delimiter 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
    isub 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
    ifeq Label2 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
    iload 2 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
    ldc 32   ; the space delimiter 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    isub 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
    ifeq Label2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
    iload 2 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
    isub 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
    ldc 10 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
    iload 1 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
    imul 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
    iadd 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
    istore 1 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
    goto Label1 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
Label2: 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
    ;when we come here we have our integer computed in local variable 1 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
    iload 1 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
    ireturn 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
.end method
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
.method public static main([Ljava/lang/String;)V
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
   .limit locals 200
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
   .limit stack 200
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
; COMPILED CODE STARTS
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
"""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
val ending = """
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
; COMPILED CODE ENDS
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
   return
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
.end method
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
"""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
// Compiler
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
var counter = -1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
def Fresh(x: String) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
  counter += 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
  x ++ "_" ++ counter.toString()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
    95
extension (sc: StringContext) {
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
type Env = Map[String, Int]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
def compile_op(op: String) = op match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  case "+" => i"iadd"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
  case "-" => i"isub"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
  case "*" => i"imul"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  case "/" => i"idiv"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  case "%" => i"irem"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
def compile_aexp(a: AExp, env : Env) : String = a match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
  case Num(i) => i"ldc $i"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
  case Var(s) => i"iload ${env(s)} \t\t; $s"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
  case Aop(op, a1, a2) => 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
910
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   117
def compile_bop(op: String) = op match {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   118
  case "==" => "if_icmpne"
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   119
  case "!=" => "if_icmpeq"
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   120
  case "<" => "if_icmpge"
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   121
  case ">" => "if_icmple"
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   122
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   123
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  case True => ""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
  case False => i"goto $jmp"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  case And(b1, b2) => compile_bexp(b1, env, jmp) ++ compile_bexp(b2, env, jmp)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
  case Or(b1, b2) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
    val b1_false = Fresh("Or_second");
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
    val or_end = Fresh("Or_end");
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
    compile_bexp(b1, env, b1_false) ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
    i"goto $or_end" ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
    l"$b1_false" ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
    compile_bexp(b2, env, jmp) ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
    l"$or_end"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  }
910
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   137
  case Bop(op, a1, a2) => 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   138
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"${compile_bop(op)} $jmp"
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   141
def compile_stmt(s: Stmt, env: Env, break: String) : (String, Env) = s match {
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
  case Skip => ("", env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
  case Assign(x, a) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
    val index = env.getOrElse(x, env.keys.size)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
    (compile_aexp(a, env) ++ i"istore $index \t\t; $x", env + (x -> index))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
  } 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
  case If(b, bl1, bl2) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
    val if_else = Fresh("If_else")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
    val if_end = Fresh("If_end")
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   150
    val (instrs1, env1) = compile_block(bl1, env, break)
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   151
    val (instrs2, env2) = compile_block(bl2, env1, break)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
    (compile_bexp(b, env, if_else) ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
     instrs1 ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
     i"goto $if_end" ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
     l"$if_else" ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
     instrs2 ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
     l"$if_end", env2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  case While(b, bl) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
    val loop_begin = Fresh("Loop_begin")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
    val loop_end = Fresh("Loop_end")
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   162
    val (instrs1, env1) = compile_block(bl, env, break)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
    (l"$loop_begin" ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
     compile_bexp(b, env, loop_end) ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
     instrs1 ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
     i"goto $loop_begin" ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
     l"$loop_end", env1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
  case For(id, lower, upper, code) => {
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   170
      val (assignment_code, env1) = compile_stmt(Assign(id, lower), env, break)  // id := lower;
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
      val while_equivalent = While(
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
          Or(Bop("<", Var(id), upper), Bop("==", Var(id), upper)),    // while id <= upper do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
          code ++                                                     //    code  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
          List(
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
            Assign(id, Aop("+", Var(id), Num(1)))                     //    id := id + 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
          ))                                                          // };
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   177
      val new_break = Fresh("for_break")
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   178
      val (while_code, env2) = compile_stmt(while_equivalent, env1, new_break)
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   179
      (assignment_code ++ while_code ++ l"$new_break", env2)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
  }
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   181
  case Break => (i"goto $break", env)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
  case WriteId(x) => (i"iload ${env(x)} \t\t; $x" ++ 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
     i"invokestatic XXX/XXX/write(I)V", env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
  case WriteString(x) => (s"   ldc ${x}\n" ++
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
     i"invokestatic XXX/XXX/writes(Ljava/lang/String;)V", env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
  case Read(x) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
    val index = env.getOrElse(x, env.keys.size) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
    (i"invokestatic XXX/XXX/read()I" ++ 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
     i"istore $index \t\t; $x", env + (x -> index))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   193
def compile_block(bl: Block, env: Env, break: String) : (String, Env) = bl match {
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
  case Nil => ("", env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
  case s::bl => {
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   196
    val (instrs1, env1) = compile_stmt(s, env, break)
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   197
    val (instrs2, env2) = compile_block(bl, env1, break)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
    (instrs1 ++ instrs2, env2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
def compile(bl: Block, class_name: String) : String = {
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   203
  val prog_end = Fresh("Prog_end")
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   204
  val instructions = compile_block(bl, Map.empty, prog_end)._1
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   205
  (beginning ++ instructions ++ l"$prog_end" ++ ending).replaceAllLiterally("XXX", class_name)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
// Compiling and running
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   211
def compile_to_file(bl: Block, class_name: String) : Unit = 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   212
  os.write.over(os.pwd / s"$class_name.j", compile(bl, class_name))  
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
def compile_all(bl: Block, class_name: String) : Unit = {
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   215
  println(s"Start of compilation")
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   216
  compile_to_file(bl, class_name)
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   217
  println(s"generated $class_name.j file")
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   218
  os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call()
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   219
  println(s"generated $class_name.class file ")
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
def time_needed[T](i: Int, code: => T) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
  val start = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
  for (j <- 1 to i) code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
  val end = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
  (end - start)/(i * 1.0e9)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
def compile_run(bl: Block, class_name: String) : Unit = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
  compile_all(bl, class_name)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
  println("running")
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   232
  os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit)
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   233
  println(s"done.")
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
// ---- Q1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
// Fibonacci
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
val fibonacciProgram = """write "Fib";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
read n;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
minus1 := 0;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
minus2 := 1;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
while n > 0 do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
  temp := minus2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
  minus2 := minus1 + minus2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
  minus1 := temp;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
  n := n - 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
};
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
write "Result";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
write minus2"""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   253
compile_all(Stmts.parse_all(tokenise(fibonacciProgram)).head, "fib")
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
val factorialProgram = """write "Factorial";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
read n;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
fact := 1;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
while n > 0 do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
  fact := n * fact;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
  n := n - 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
};
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
write "Result";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
write fact
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
"""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
compile_all(Stmts.parse_all(tokenise(factorialProgram)).head, "factorial")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
// ---- Q3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   272
/* compile_run(Stmts.parse_all(tokenise("""for i := 1 upto 10 do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   273
  for i := 1 upto 10 do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   274
    write i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   275
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   276
}""")).head, "nestedloop") */
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   277
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   278
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   279
//compile_run(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))).head, "collatz2")
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   280
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   281
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   282
println(tokenise(os.read(os.pwd / "forloop.while")))
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 910
diff changeset
   283
compile_run(Stmts.parse_all(tokenise(os.read(os.pwd / "forloop.while"))).head, "forloop")