author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 30 Aug 2021 15:39:43 +0100 | |
changeset 828 | bdcaecdee9eb |
parent 816 | 2b6e23985982 |
child 943 | 5365ef60707e |
permissions | -rw-r--r-- |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
1 |
// A Small Compiler for the WHILE Language |
674 | 2 |
// (it does not use a parser nor lexer) |
790 | 3 |
// |
4 |
// cal with |
|
5 |
// |
|
6 |
// amm compile.sc test |
|
7 |
// amm compile.sc test2 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
8 |
|
624 | 9 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
10 |
// the abstract syntax trees |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
11 |
abstract class Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
12 |
abstract class AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
13 |
abstract class BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
14 |
type Block = List[Stmt] |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
15 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
16 |
// statements |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
17 |
case object Skip extends Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
18 |
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
19 |
case class While(b: BExp, bl: Block) extends Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
20 |
case class Assign(s: String, a: AExp) extends Stmt |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
21 |
case class Write(s: String) extends Stmt |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
22 |
case class Read(s: String) extends Stmt |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
23 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
24 |
// arithmetic expressions |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
25 |
case class Var(s: String) extends AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
26 |
case class Num(i: Int) extends AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
27 |
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
28 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
29 |
// boolean expressions |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
30 |
case object True extends BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
31 |
case object False extends BExp |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
32 |
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
|
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
35 |
// compiler headers needed for the JVM |
695
484b74bc057e
deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents:
690
diff
changeset
|
36 |
// (contains methods for read and write) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
37 |
val beginning = """ |
80
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
38 |
.class public XXX.XXX |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
39 |
.super java/lang/Object |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
40 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
41 |
.method public static write(I)V |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
42 |
.limit locals 1 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
43 |
.limit stack 2 |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
44 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
45 |
iload 0 |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
46 |
invokevirtual java/io/PrintStream/println(I)V |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
47 |
return |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
48 |
.end method |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
49 |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
50 |
.method public static main([Ljava/lang/String;)V |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
51 |
.limit locals 200 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
52 |
.limit stack 200 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
53 |
|
624 | 54 |
; COMPILED CODE STARTS |
55 |
||
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
56 |
""" |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
57 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
58 |
val ending = """ |
624 | 59 |
; COMPILED CODE ENDS |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
60 |
return |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
61 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
62 |
.end method |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
63 |
""" |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
64 |
|
624 | 65 |
// Compiler functions |
471 | 66 |
|
67 |
||
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
68 |
// for generating new labels |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
69 |
var counter = -1 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
70 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
71 |
def Fresh(x: String) = { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
72 |
counter += 1 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
73 |
x ++ "_" ++ counter.toString() |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
} |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
75 |
|
624 | 76 |
// convenient string interpolations |
77 |
// for instructions and labels |
|
78 |
import scala.language.implicitConversions |
|
79 |
import scala.language.reflectiveCalls |
|
80 |
||
81 |
implicit def sring_inters(sc: StringContext) = new { |
|
82 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" |
|
83 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" |
|
84 |
} |
|
85 |
||
716 | 86 |
// this allows us to write things like |
809 | 87 |
// i"iadd" and l"Label" |
687 | 88 |
|
624 | 89 |
|
90 |
// environments |
|
668 | 91 |
type Env = Map[String, Int] |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
92 |
|
674 | 93 |
|
94 |
def compile_op(op: String) = op match { |
|
95 |
case "+" => i"iadd" |
|
96 |
case "-" => i"isub" |
|
97 |
case "*" => i"imul" |
|
98 |
} |
|
99 |
||
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
100 |
// arithmetic expression compilation |
624 | 101 |
def compile_aexp(a: AExp, env : Env) : String = a match { |
102 |
case Num(i) => i"ldc $i" |
|
687 | 103 |
case Var(s) => i"iload ${env(s)} \t\t; $s" |
674 | 104 |
case Aop(op, a1, a2) => |
105 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op) |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
106 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
107 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
108 |
// boolean expression compilation |
687 | 109 |
// - the jump-label is for where to jump if the condition is not true |
624 | 110 |
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { |
111 |
case True => "" |
|
112 |
case False => i"goto $jmp" |
|
687 | 113 |
case Bop("==", a1, a2) => |
624 | 114 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpne $jmp" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
115 |
case Bop("!=", a1, a2) => |
624 | 116 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpeq $jmp" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
117 |
case Bop("<", a1, a2) => |
624 | 118 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpge $jmp" |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
119 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
120 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
121 |
// statement compilation |
624 | 122 |
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match { |
123 |
case Skip => ("", env) |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
124 |
case Assign(x, a) => { |
687 | 125 |
val index = env.getOrElse(x, env.keys.size) |
126 |
(compile_aexp(a, env) ++ i"istore $index \t\t; $x", env + (x -> index)) |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
127 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
128 |
case If(b, bl1, bl2) => { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
129 |
val if_else = Fresh("If_else") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
130 |
val if_end = Fresh("If_end") |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
131 |
val (instrs1, env1) = compile_block(bl1, env) |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
132 |
val (instrs2, env2) = compile_block(bl2, env1) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
133 |
(compile_bexp(b, env, if_else) ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
134 |
instrs1 ++ |
624 | 135 |
i"goto $if_end" ++ |
136 |
l"$if_else" ++ |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
137 |
instrs2 ++ |
624 | 138 |
l"$if_end", env2) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
139 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
140 |
case While(b, bl) => { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
141 |
val loop_begin = Fresh("Loop_begin") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
142 |
val loop_end = Fresh("Loop_end") |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
143 |
val (instrs1, env1) = compile_block(bl, env) |
624 | 144 |
(l"$loop_begin" ++ |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
145 |
compile_bexp(b, env, loop_end) ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
146 |
instrs1 ++ |
624 | 147 |
i"goto $loop_begin" ++ |
148 |
l"$loop_end", env1) |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
149 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
150 |
case Write(x) => |
687 | 151 |
(i"iload ${env(x)} \t\t; $x" ++ |
624 | 152 |
i"invokestatic XXX/XXX/write(I)V", env) |
811 | 153 |
//case Read(x) => { |
154 |
// val index = env.getOrElse(x, env.keys.size) |
|
155 |
// (i"invokestatic XXX/XXX/read()I" ++ |
|
156 |
// i"istore $index \t\t; $x", env + (x -> index)) |
|
157 |
//} |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
158 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
159 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
160 |
// compilation of a block (i.e. list of instructions) |
624 | 161 |
def compile_block(bl: Block, env: Env) : (String, Env) = bl match { |
162 |
case Nil => ("", env) |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
163 |
case s::bl => { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
164 |
val (instrs1, env1) = compile_stmt(s, env) |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
165 |
val (instrs2, env2) = compile_block(bl, env1) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
166 |
(instrs1 ++ instrs2, env2) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
167 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
168 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
169 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
170 |
// main compilation function for blocks |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
171 |
def compile(bl: Block, class_name: String) : String = { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
172 |
val instructions = compile_block(bl, Map.empty)._1 |
690 | 173 |
(beginning ++ instructions ++ ending).replaceAllLiterally("XXX", class_name) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
174 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
175 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
176 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
177 |
|
609 | 178 |
|
179 |
||
674 | 180 |
// Fibonacci numbers as a bare-bone test-case |
609 | 181 |
val fib_test = |
811 | 182 |
List(Assign("n", Num(9)), // n := 9; |
609 | 183 |
Assign("minus1",Num(0)), // minus1 := 0; |
184 |
Assign("minus2",Num(1)), // minus2 := 1; |
|
185 |
Assign("temp",Num(0)), // temp := 0; |
|
811 | 186 |
While(Bop("<",Num(0),Var("n")), // while 0 < n do { |
625 | 187 |
List(Assign("temp",Var("minus2")), // temp := minus2; |
609 | 188 |
Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), |
189 |
// minus2 := minus1 + minus2; |
|
190 |
Assign("minus1",Var("temp")), // minus1 := temp; |
|
191 |
Assign("n",Aop("-",Var("n"),Num(1))))), // n := n - 1 }; |
|
192 |
Write("minus1")) // write minus1 |
|
193 |
||
194 |
||
790 | 195 |
|
196 |
// prints out the JVM instructions |
|
197 |
@main |
|
198 |
def test() = |
|
199 |
println(compile(fib_test, "fib")) |
|
200 |
||
201 |
||
687 | 202 |
|
203 |
||
812 | 204 |
|
790 | 205 |
// compiling and running .j-files |
206 |
// |
|
207 |
// JVM files can be assembled with |
|
208 |
// |
|
209 |
// java -jar jasmin.jar fib.j |
|
210 |
// |
|
211 |
// and started with |
|
212 |
// |
|
213 |
// java fib/fib |
|
609 | 214 |
|
215 |
||
790 | 216 |
def run(bl: Block, class_name: String) = { |
217 |
val code = compile(bl, class_name) |
|
218 |
os.write.over(os.pwd / s"$class_name.j", code) |
|
219 |
os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call() |
|
815 | 220 |
os.proc("java", s"$class_name/$class_name").call(stdout = os.Inherit, stdin = os.Inherit) |
790 | 221 |
} |
222 |
||
816 | 223 |
|
790 | 224 |
@main |
225 |
def test2() = |
|
226 |
run(fib_test, "fib") |
|
809 | 227 |
|
228 |
||
229 |
/* Jasmin code for reading an integer |
|
230 |
||
231 |
.method public static read()I |
|
232 |
.limit locals 10 |
|
233 |
.limit stack 10 |
|
234 |
||
235 |
ldc 0 |
|
236 |
istore 1 ; this will hold our final integer |
|
237 |
Label1: |
|
238 |
getstatic java/lang/System/in Ljava/io/InputStream; |
|
239 |
invokevirtual java/io/InputStream/read()I |
|
240 |
istore 2 |
|
241 |
iload 2 |
|
242 |
ldc 10 ; the newline delimiter |
|
243 |
isub |
|
244 |
ifeq Label2 |
|
245 |
iload 2 |
|
246 |
ldc 32 ; the space delimiter |
|
247 |
isub |
|
248 |
ifeq Label2 |
|
249 |
||
250 |
iload 2 |
|
251 |
ldc 48 ; we have our digit in ASCII, have to subtract it from 48 |
|
252 |
isub |
|
253 |
ldc 10 |
|
254 |
iload 1 |
|
255 |
imul |
|
256 |
iadd |
|
257 |
istore 1 |
|
258 |
goto Label1 |
|
259 |
Label2: |
|
828 | 260 |
; when we come here we have our integer computed in local variable 1 |
809 | 261 |
iload 1 |
262 |
ireturn |
|
263 |
.end method |
|
264 |
||
812 | 265 |
*/ |
828 | 266 |
|
267 |
||
268 |
||
269 |
||
270 |
||
271 |
// runs with amm2 and amm3 |