117 def compile_bop(op: String) = op match { |
117 def compile_bop(op: String) = op match { |
118 case "==" => "if_icmpne" |
118 case "==" => "if_icmpne" |
119 case "!=" => "if_icmpeq" |
119 case "!=" => "if_icmpeq" |
120 case "<" => "if_icmpge" |
120 case "<" => "if_icmpge" |
121 case ">" => "if_icmple" |
121 case ">" => "if_icmple" |
|
122 case "<=" => "if_icmpgt" |
|
123 case "=>" => "if_icmplt" |
122 } |
124 } |
123 |
125 |
124 def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { |
126 def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { |
125 case True => "" |
127 case True => "" |
126 case False => i"goto $jmp" |
128 case False => i"goto $jmp" |
277 |
281 |
278 |
282 |
279 //compile_run(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))).head, "collatz2") |
283 //compile_run(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))).head, "collatz2") |
280 |
284 |
281 |
285 |
282 println(tokenise(os.read(os.pwd / "forloop.while"))) |
286 //println(tokenise(os.read(os.pwd / "forloop.while"))) |
283 compile_all(Stmts.parse_all(tokenise(os.read(os.pwd / "forloop.while"))).head, "forloop") |
287 //compile_all(Stmts.parse_all(tokenise(os.read(os.pwd / "forloop.while"))).head, "forloop") |
284 |
288 |
|
289 |
|
290 //compile_all(Stmts.parse_all(tokenise(os.read(os.pwd / "primes.while"))).head, "primes") |
|
291 |
|
292 //compile_run(Stmts.parse_all(tokenise(os.read(os.pwd / "pr.while"))).head, "pr") |
285 |
293 |
286 |
294 |
287 // for automated testing |
295 // for automated testing |
288 |
296 |
289 @main |
297 @main |
290 def main(file: String) = { |
298 def main(file: String) = { |
291 // empty - nothing to run |
299 // empty - nothing to run |
292 } |
300 } |
293 |
301 |
294 @main |
302 @main |
295 def test(file: String) = { |
303 def run(file: String) = { |
296 val contents = os.read(os.pwd / file) |
304 val contents = os.read(os.pwd / file) |
297 val class_name = file.stripSuffix(".") |
305 val class_name = file.stripSuffix(".while") |
298 compile_all(Stmts.parse_all(tokenise(os.read(os.pwd / file))).head, class_name) |
306 val tks = tokenise(os.read(os.pwd / file)) |
299 tokenise(contents) |
307 //println(tks) |
300 } |
308 //println(Stmts.parse(tks)) |
301 |
309 compile_run(Stmts.parse_all(tks).head, class_name) |
302 |
310 //tokenise(contents) |
|
311 //println(Stmts.parse_all(tokenise(os.read(os.pwd / file))).head) |
|
312 } |
|
313 |
|
314 |
|
315 //println("TEST") |
|
316 //println(tokenise(os.read(os.pwd / "pr.while"))) |
|
317 //println(Stmts.parse_all(tokenise(os.read(os.pwd / "pr.while"))).head) |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 /* For with scopes |
|
323 |
|
324 case For(x,a,b,bl) => { |
|
325 val loop_begin = Fresh("Loop_begin") |
|
326 val loop_end = Fresh("Loop_end") |
|
327 |
|
328 val ind = env.getOrElse(x, -1) |
|
329 val new_ind = Fresh_index() |
|
330 val (assign,env1) = (compile_aexp(a, env) ++ i"istore $new_ind \t\t; ${x}-new", env + (x -> new_ind)) |
|
331 |
|
332 val bool = l"$loop_begin" ++ compile_aexp(Var(x), env1) ++ compile_aexp(b, env1) ++ i"if_icmpgt $loop_end" |
|
333 val (block, env2) = compile_block(bl,env1,loop_end) |
|
334 val inc = compile_stmt(Assign(x,Aop("+", Var(x), Num(1))),env2, "")._1 ++ i"goto $loop_begin" ++ l"$loop_end" |
|
335 (assign ++ bool ++ block ++ inc, if (ind != -1){env2 + (x -> ind)} else {env2 - x}) |
|
336 } |
|
337 |
|
338 |
|
339 */ |