progs/compile.scala
changeset 471 9476086849ad
parent 373 b018234c9126
child 535 095be64c03c1
equal deleted inserted replaced
469:1f4e81950ab4 471:9476086849ad
     1 // A Small Compiler for the WHILE Language
     1 // A Small Compiler for the WHILE Language
     2 // 
     2 // (it does not use a parser and lexer)
     3 
     3 
     4 // the abstract syntax trees
     4 // the abstract syntax trees
     5 abstract class Stmt
     5 abstract class Stmt
     6 abstract class AExp
     6 abstract class AExp
     7 abstract class BExp 
     7 abstract class BExp 
    91 
    91 
    92    return
    92    return
    93 
    93 
    94 .end method
    94 .end method
    95 """
    95 """
       
    96 
       
    97 println("starting")
       
    98 
    96 
    99 
    97 // for generating new labels
   100 // for generating new labels
    98 var counter = -1
   101 var counter = -1
    99 
   102 
   100 def Fresh(x: String) = {
   103 def Fresh(x: String) = {
   219 //
   222 //
   220 // and started with
   223 // and started with
   221 //
   224 //
   222 //    java fib/fib
   225 //    java fib/fib
   223 
   226 
   224 
   227 import scala.util._
   225 
   228 import scala.sys.process._
   226 
   229 import scala.io
   227 
   230 
       
   231 def compile_tofile(bl: Block, class_name: String) = {
       
   232   val output = compile(bl, class_name)
       
   233   val fw = new java.io.FileWriter(class_name + ".j") 
       
   234   fw.write(output) 
       
   235   fw.close()
       
   236 }
       
   237 
       
   238 def compile_and_run(bl: Block, class_name: String) : Unit = {
       
   239   compile_tofile(bl, class_name)
       
   240   println("compiled ")
       
   241   val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!!
       
   242   println("assembled ")
       
   243   ("java " + class_name + "/" + class_name).!!
       
   244 }
       
   245 
       
   246 
       
   247 compile_and_run(fib_test, "fib")
       
   248