solutions5/bfc.scala
changeset 234 c51305a2217f
parent 233 38ea26f227af
child 236 e461b5325b5e
equal deleted inserted replaced
233:38ea26f227af 234:c51305a2217f
     1 // Part 2 about a "Compiler" for the Brainf*** language
     1 // Part 2 about a "Compiler" for the Brainf*** language
     2 //======================================================
     2 //======================================================
     3 
     3 
     4 //object CW10b {
     4 object CW10b {
     5 
     5 
     6 // !!! Copy any function you need from file bf.scala !!!
     6 // !!! Copy any function you need from file bf.scala !!!
     7 //
     7 //
     8 // If you need any auxiliary function, feel free to 
     8 // If you need any auxiliary function, feel free to 
     9 // implement it, but do not make any changes to the
     9 // implement it, but do not make any changes to the
   252     else splice(cs, (c, 1) :: (d, n) :: acc)
   252     else splice(cs, (c, 1) :: (d, n) :: acc)
   253 }
   253 }
   254 
   254 
   255 def spl(s: String) = splice(s.toList, Nil).reverse
   255 def spl(s: String) = splice(s.toList, Nil).reverse
   256 
   256 
   257 spl(load_bff("benchmark.bf"))
   257 //spl(load_bff("benchmark.bf"))
   258 
   258 
   259 def combine(s: String) : String = {
   259 def combine(s: String) : String = {
   260   (for ((c, n) <- spl(s)) yield c match {
   260   (for ((c, n) <- spl(s)) yield c match {
   261     case '>' => List('>', (n + '@').toChar)
   261     case '>' => List('>', (n + '@').toChar)
   262     case '<' => List('<', (n + '@').toChar)
   262     case '<' => List('<', (n + '@').toChar)
   265     case _ => List(c)
   265     case _ => List(c)
   266   }).flatten.mkString
   266   }).flatten.mkString
   267 }
   267 }
   268 
   268 
   269 
   269 
   270 combine(load_bff("benchmark.bf"))
   270 //combine(load_bff("benchmark.bf"))
   271 
   271 
   272 def compute4(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = {
   272 def compute4(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = {
   273   if (0 <= pc && pc < pg.length) { 
   273   if (0 <= pc && pc < pg.length) { 
   274     val (new_pc, new_mp, new_mem) = pg(pc) match {
   274     val (new_pc, new_mp, new_mem) = pg(pc) match {
   275       case '0' => (pc + 1, mp, write(mem, mp, 0))
   275       case '0' => (pc + 1, mp, write(mem, mp, 0))
   293 def run4(pg: String, m: Mem = Map()) = { 
   293 def run4(pg: String, m: Mem = Map()) = { 
   294   val pg_opt = combine(optimise(pg))
   294   val pg_opt = combine(optimise(pg))
   295   compute4(pg_opt, jtable(pg_opt), 0, 0, m)
   295   compute4(pg_opt, jtable(pg_opt), 0, 0, m)
   296 }
   296 }
   297 
   297 
   298 
   298 // testcases
   299 combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[....."""
   299 //combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[....."""
   300 
   300 
   301 //time_needed(1, run4(load_bff("benchmark.bf")))
   301 //time_needed(1, run4(load_bff("benchmark.bf")))
   302 
   302 
   303 //time_needed(1, run(load_bff("sierpinski.bf"))) 
   303 //time_needed(1, run(load_bff("sierpinski.bf"))) 
   304 //time_needed(1, run4(load_bff("sierpinski.bf"))) 
   304 //time_needed(1, run4(load_bff("sierpinski.bf"))) 
   305 
   305 
   306 //time_needed(1, run4(load_bff("mandelbrot.bf")))
   306 //time_needed(1, run4(load_bff("mandelbrot.bf")))
   307 
   307 
   308 
   308 
   309 //}
   309 }