solutions/cw3/parser.sc
changeset 905 15973df32613
parent 894 02ef5c3abc51
child 919 53f08d873e09
equal deleted inserted replaced
904:d97283992d4f 905:15973df32613
   157     (T_PAREN("{") ~ Stmts ~ T_PAREN("}")) ==> { case x ~ y ~ z => y} ||
   157     (T_PAREN("{") ~ Stmts ~ T_PAREN("}")) ==> { case x ~ y ~ z => y} ||
   158     (Stmt ==> (s => List(s)))
   158     (Stmt ==> (s => List(s)))
   159 
   159 
   160 // Testing with programs 2 & 3
   160 // Testing with programs 2 & 3
   161 
   161 
   162 println("Fibonacci")
   162 //println("Fibonacci")
   163 println(Stmts.parse_all(tokenise(os.read(os.pwd / "fib.while"))))
   163 //println(Stmts.parse_all(tokenise(os.read(os.pwd / "fib.while"))))
   164 
   164 
   165 println("Loops")
   165 //println("Loops")
   166 println(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while"))))
   166 //println(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while"))))
   167 
   167 
   168 println("Collatz")
   168 //println("Collatz")
   169 println(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))))
   169 //println(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))))
   170 
   170 
   171 
   171 
   172 // Interpreter
   172 // Interpreter
   173 
   173 
   174 // Environment to store values of variables
   174 // Environment to store values of variables
   224   case s::bl => eval_bl(bl, eval_stmt(s, env))
   224   case s::bl => eval_bl(bl, eval_stmt(s, env))
   225 }
   225 }
   226 
   226 
   227 def eval(bl: Block) : Env = eval_bl(bl, Map())
   227 def eval(bl: Block) : Env = eval_bl(bl, Map())
   228 
   228 
   229 println("Primes eval")
   229 @main
   230 println(tokenise(os.read(os.pwd / "primes.while")))
   230 def main(file: String) = {
   231 println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "primes.while"))).head))
   231   val contents = os.read(os.pwd / file)
   232 
   232   println(s"Lex $file: ")
   233 println("Factors eval")
   233   println(tokenise(contents))
   234 println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "factors.while"))).head))
   234   println(s"Parse $file: ")
   235 
   235   println(Stmts.parse_all(tokenise(contents)).head)
   236 println("Collatz2 eval")
   236   println(s"Eval $file: ")
   237 println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))).head))
   237   println(eval(Stmts.parse_all(tokenise(contents)).head))
       
   238 }
   238 
   239 
   239 /*
   240 /*
   240 println("Loops eval")
   241 println("Loops eval")
   241 val start = System.nanoTime()
   242 val start = System.nanoTime()
   242 println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while"))).head))
   243 println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while"))).head))