progs/glue.sc
changeset 861 f5709803272b
child 863 d59bcff69998
equal deleted inserted replaced
860:6f80e6df34f7 861:f5709803272b
       
     1 // Glue code for CW4
       
     2 //===================
       
     3 // generates j-files and runs class-files
       
     4 //
       
     5 //
       
     6 // needs 
       
     7 //
       
     8 //  tokenise(s: String) : List[Token]
       
     9 //
       
    10 //  Stmts - parser consuming Tokens
       
    11 //
       
    12 //  compile(bl: Block, class_name: String) : String
       
    13 
       
    14 
       
    15 
       
    16 import ammonite.ops._
       
    17 
       
    18 def parse_tks(tks : List[Token]) = {
       
    19   Stmts.parse_all(tks).head
       
    20 }
       
    21    
       
    22 
       
    23 
       
    24 def compile_to_file(bl: Block, class_name: String) : Unit = {
       
    25   println(s"Start of compilation")
       
    26   write.over(pwd / s"$class_name.j", compile(bl, class_name))  
       
    27   println(s"generated $class_name.j file")
       
    28   os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call()
       
    29   println(s"generated $class_name.class file ")
       
    30 }
       
    31 
       
    32 
       
    33 @main
       
    34 def main(fname: String) = {
       
    35     val path = os.pwd / fname
       
    36     val class_name = fname.stripSuffix("." ++ path.ext)
       
    37     val tks = tokenise(os.read(path))
       
    38     val ast = parse_tks(tks)
       
    39     compile_to_file(ast, class_name)
       
    40 }
       
    41 
       
    42 @main
       
    43 def run(fname: String) = {
       
    44     val path = os.pwd / fname
       
    45     val class_name = fname.stripSuffix("." ++ path.ext)
       
    46     val tks = tokenise(os.read(path))
       
    47     val ast = parse_tks(tks)
       
    48     compile_to_file(ast, class_name)
       
    49     os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit)
       
    50     println(s"done.")
       
    51 }
       
    52 
       
    53