solutions/cw4/compiler.sc
changeset 959 64ec1884d860
parent 921 bb54e7aa1a3f
equal deleted inserted replaced
958:fddf099a82f8 959:64ec1884d860
   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"
   231   println("running")
   233   println("running")
   232   os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit)
   234   os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit)
   233   println(s"done.")
   235   println(s"done.")
   234 }
   236 }
   235 
   237 
       
   238 //compile_run(Stmts.parse_all(tokenise(os.read(os.pwd / "pr.while"))).head, "pr")
       
   239 
   236 // ---- Q1
   240 // ---- Q1
   237 
   241 
   238 // Fibonacci
   242 // Fibonacci
   239 
   243 
   240 val fibonacciProgram = """write "Fib";
   244 val fibonacciProgram = """write "Fib";
   248   n := n - 1
   252   n := n - 1
   249 };
   253 };
   250 write "Result";
   254 write "Result";
   251 write minus2"""
   255 write minus2"""
   252 
   256 
   253 compile_all(Stmts.parse_all(tokenise(fibonacciProgram)).head, "fib")
   257 //compile_all(Stmts.parse_all(tokenise(fibonacciProgram)).head, "fib")
   254 
   258 
   255 val factorialProgram = """write "Factorial";
   259 val factorialProgram = """write "Factorial";
   256 read n;
   260 read n;
   257 fact := 1;
   261 fact := 1;
   258 
   262 
   263 
   267 
   264 write "Result";
   268 write "Result";
   265 write fact
   269 write fact
   266 """
   270 """
   267 
   271 
   268 compile_all(Stmts.parse_all(tokenise(factorialProgram)).head, "factorial")
   272 //compile_all(Stmts.parse_all(tokenise(factorialProgram)).head, "factorial")
   269 
   273 
   270 // ---- Q3
   274 // ---- Q3
   271 
   275 
   272 /* compile_run(Stmts.parse_all(tokenise("""for i := 1 upto 10 do {
   276 /* compile_run(Stmts.parse_all(tokenise("""for i := 1 upto 10 do {
   273   for i := 1 upto 10 do {
   277   for i := 1 upto 10 do {
   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 */