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