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