progs/while-arrays/compile_arrays.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 22 Nov 2024 12:42:07 +0000
changeset 974 0cb4bf2469d1
parent 958 fddf099a82f8
child 975 ae5c03560d4d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// A Small Compiler for the WHILE Language
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
// - this compiler contains support for "static" integer 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
//   arrays (they are mutable but cannot be re-sized)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// Call with 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
// amm compile_arrays.sc
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
  
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    10
//> using toolkit 0.6.0
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    11
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
// the abstract syntax trees for WHILE
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
abstract class Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
abstract class AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
abstract class BExp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
type Block = List[Stmt]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
// statements
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
case object Skip extends Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case class ArrayDef(s: String, n: Int) extends Stmt            // array definition
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
case class While(b: BExp, bl: Block) extends Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case class Assign(s: String, a: AExp) extends Stmt             // var := exp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
case class AssignA(s: String, a1: AExp, a2: AExp) extends Stmt // arr[exp1] := exp2
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case class Write(s: String) extends Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
// arithmetic expressions
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
case class Var(s: String) extends AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
case class Num(i: Int) extends AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
case class Ref(s: String, a: AExp) extends AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
// boolean expressions
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
case object True extends BExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
case object False extends BExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
// compiler headers needed for the JVM
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
// - contains a main method and a method for writing out an integer
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
// - the stack and locals are hard-coded
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
val beginning = """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
.class public XXX.XXX
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
.super java/lang/Object
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
.method public static write(I)V 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
    .limit locals 1 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
    .limit stack 2 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
    getstatic java/lang/System/out Ljava/io/PrintStream; 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    iload 0
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
    invokevirtual java/io/PrintStream/print(I)V
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
    return 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
.end method
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
.method public static main([Ljava/lang/String;)V
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
   .limit locals 200
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
   .limit stack 200
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
; COMPILED CODE STARTS   
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
val ending = """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
; COMPILED CODE ENDS
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
   return
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
.end method
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
// for generating new labels
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
var counter = -1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
def Fresh(x: String) = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  counter += 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  x ++ "_" ++ counter.toString()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
// environments for variables and indices
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
type Env = Map[String, Int]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
// convenient string interpolations 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
// for generating instructions and labels
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    93
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    94
extension (sc: StringContext) {
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    95
    def i(args: Any*): String = "   " ++ sc.s(args*) ++ "\n"
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
    96
    def l(args: Any*): String = sc.s(args*) ++ ":\n"
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    99
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
def compile_op(op: String) = op match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
  case "+" => i"iadd"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
  case "-" => i"isub"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  case "*" => i"imul"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
// arithmetic expression compilation
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
def compile_aexp(a: AExp, env : Env) : String = a match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case Num(i) => i"ldc $i"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  case Var(s) => i"iload ${env(s)}"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  case Aop(op, a1, a2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
  case Ref(s, a) =>
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
    i"aload ${env(s)}" ++ compile_aexp(a, env) ++  i"iaload"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
// boolean expression compilation
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  case True => ""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
  case False => i"goto $jmp"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  case Bop("==", a1, a2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpne $jmp"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  case Bop("!=", a1, a2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpeq $jmp"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  case Bop("<", a1, a2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpge $jmp"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
// statement compilation
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  case Skip => ("", env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  case Assign(x, a) => {
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   132
    val index = env.getOrElse(x, env.keys.size)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
    (compile_aexp(a, env) ++ i"istore $index \t\t; $x", env + (x -> index)) 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
  } 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
  case If(b, bl1, bl2) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
    val if_else = Fresh("If_else")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
    val if_end = Fresh("If_end")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
    val (instrs1, env1) = compile_block(bl1, env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
    val (instrs2, env2) = compile_block(bl2, env1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
    (compile_bexp(b, env, if_else) ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
     instrs1 ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
     i"goto $if_end" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
     l"$if_else" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
     instrs2 ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
     l"$if_end", env2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
  case While(b, bl) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
    val loop_begin = Fresh("Loop_begin")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
    val loop_end = Fresh("Loop_end")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
    val (instrs1, env1) = compile_block(bl, env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
    (l"$loop_begin" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
     compile_bexp(b, env, loop_end) ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
     instrs1 ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
     i"goto $loop_begin" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
     l"$loop_end", env1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
  case Write(x) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
    (i"iload ${env(x)} \t\t; $x" ++ 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
     i"invokestatic XXX/XXX/write(I)V", env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
  case ArrayDef(s: String, n: Int) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
    val index = if (env.isDefinedAt(s)) throw new Exception("array def error") else 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
                    env.keys.size
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
    (i"ldc $n" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
     i"newarray int" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
     i"astore $index", env + (s -> index))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
  case AssignA(s, a1, a2) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
    val index = if (env.isDefinedAt(s)) env(s) else 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
                    throw new Exception("array not defined")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
    (i"aload ${env(s)}" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
     compile_aexp(a1, env) ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
     compile_aexp(a2, env) ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
     i"iastore", env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
  } 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
// compile a block (i.e. list of statements)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
  case Nil => ("", env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
  case s::bl => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
    val (instrs1, env1) = compile_stmt(s, env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
    val (instrs2, env2) = compile_block(bl, env1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
    (instrs1 ++ instrs2, env2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
// main compile function for blocks (adds headers and proper JVM names)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
def compile(bl: Block, class_name: String) : String = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
  val instructions = compile_block(bl, Map())._1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
  (beginning ++ instructions ++ ending).replace("XXX", class_name)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
// contrived example involving arrays
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
val array_test = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
  List(ArrayDef("a", 10),               // array a[10]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
       ArrayDef("b", 2),                // array b[2]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
       AssignA("a", Num(0), Num(10)),   // a[0] := 10
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
       Assign("x", Ref("a", Num(0))),   // x := a[0]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
       Write("x"),            
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
       AssignA("b", Num(1), Num(5)),    // b[1] := 5
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
       Assign("x", Ref("b", Num(1))),   // x := b[1] 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
       Write("x"))                     
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
// prints out the JVM-assembly instructions for fib above
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
//    println(compile(array_test, "arr"))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
// can be assembled by hand with 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
//    java -jar jasmin.jar arr.j
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
// and run with
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
//    java arr/arr
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
// automating the above
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
   221
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   222
import os.*
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
   223
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
def compile_to_file(bl: Block, class_name: String) : Unit = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
  write.over(pwd / s"$class_name.j", compile(bl, class_name))  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
def compile_and_run(bl: Block, class_name: String) : Unit = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
  println(s"Start of compilation")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
  compile_to_file(bl, class_name)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
  println(s"generated $class_name.j file")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
  os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
  println(s"generated $class_name.class file ")
747
077fc2e5412c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
   233
  //println(os.proc("java", s"${class_name}/${class_name}").call().out.text())
077fc2e5412c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
   234
  os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
  println(s"done.")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
   
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   240
//@main def main() = {
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   241
//  compile_and_run(array_test, "arr")
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   242
//}
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
829
dc3c35673e94 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   245
dc3c35673e94 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   246
dc3c35673e94 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   247