progs/fun.scala
changeset 323 4ce07c4abdb4
parent 311 6719e8d10a0d
child 380 1e88390e81aa
equal deleted inserted replaced
322:698ed1c96cd0 323:4ce07c4abdb4
   113   case c::s => ders(s, der(c, r))
   113   case c::s => ders(s, der(c, r))
   114 }
   114 }
   115 
   115 
   116 
   116 
   117 // regular expressions for the While language
   117 // regular expressions for the While language
   118 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
   118 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_".toList)
   119 val DIGIT = RANGE("0123456789")
   119 val DIGIT = RANGE("0123456789".toList)
   120 val ID = SYM ~ (SYM | DIGIT).% 
   120 val ID = SYM ~ (SYM | DIGIT).% 
   121 val NUM = PLUS(DIGIT)
   121 val NUM = PLUS(DIGIT)
   122 val KEYWORD : Rexp = "if" | "then" | "else" | "write" | "def"
   122 val KEYWORD : Rexp = "if" | "then" | "else" | "write" | "def"
   123 val SEMI: Rexp = ";"
   123 val SEMI: Rexp = ";"
   124 val COMMA: Rexp = ","
   124 val COMMA: Rexp = ","
   443   }
   443   }
   444 }
   444 }
   445 
   445 
   446 def compile(class_name: String, input: String) : String = {
   446 def compile(class_name: String, input: String) : String = {
   447   val tks = tokenizer(input)
   447   val tks = tokenizer(input)
   448   //println(Prog.parse(tks))
   448   println(Prog.parse_single(tks).mkString("\n"))
   449   val ast = Prog.parse_single(tks)
   449   val ast = Prog.parse_single(tks)
   450   val instructions = ast.flatMap(compile_decl).mkString
   450   val instructions = ast.flatMap(compile_decl).mkString
   451   (library + instructions).replaceAllLiterally("XXX", class_name)
   451   (library + instructions).replaceAllLiterally("XXX", class_name)
   452 }
   452 }
       
   453 
       
   454 
   453 
   455 
   454 
   456 
   455 def compile_file(file_name: String) = {
   457 def compile_file(file_name: String) = {
   456   val class_name = file_name.split('.')(0)
   458   val class_name = file_name.split('.')(0)
   457   val output = compile(class_name, fromFile(file_name))
   459   val output = compile(class_name, fromFile(file_name))
   474   println("Time: " + time_needed(2, ("java " + class_name + "/" + class_name).!))
   476   println("Time: " + time_needed(2, ("java " + class_name + "/" + class_name).!))
   475 }
   477 }
   476 
   478 
   477 
   479 
   478 //examples
   480 //examples
   479 compile_run("defs.rec")
   481 compile_file("fact.rec")
       
   482 //compile_run("defs.rec")
   480 //compile_run("fact.rec")
   483 //compile_run("fact.rec")