main_marking5/bf.scala
changeset 424 daf561a83ba6
parent 392 97ecdc8cb61b
child 463 0315d9983cd0
equal deleted inserted replaced
423:e9d14d58be3c 424:daf561a83ba6
     1 // Core Part about an Interpreter for 
     1 // Core Part about an Interpreter for 
     2 // the Brainf***++ language
     2 // the Brainf***++ language
     3 //==============================================
     3 //==============================================
     4 
     4 
     5 object CW10a {  
     5 object M5a {  
     6 
     6 
     7 
     7 
     8 // representation of Bf memory 
     8 // representation of Bf memory 
     9 
     9 
    10 type Mem = Map[Int, Int]
    10 type Mem = Map[Int, Int]
    94       case '>' => (pc + 1, mp + 1, mem)
    94       case '>' => (pc + 1, mp + 1, mem)
    95       case '<' => (pc + 1, mp - 1, mem)
    95       case '<' => (pc + 1, mp - 1, mem)
    96       case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
    96       case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
    97       case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
    97       case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
    98       case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
    98       case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
    99       //case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))
       
   100       //case ',' => (pc + 1, mp, write(mem, mp, scala.io.StdIn.readByte()))
       
   101       case '['  => 
    99       case '['  => 
   102 	      if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) 
   100 	      if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) 
   103       case ']'  => 
   101       case ']'  => 
   104 	      if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) 
   102 	      if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) 
   105  
   103  
   106       // new commands
       
   107       case '@' => (pc + 1, mp, write(mem, sread(mem, mp), sread(mem, mp - 1)))
       
   108       case '*' => (pc + 1, mp, write(mem, mp, sread(mem, mp) * sread(mem, mp -1)))
       
   109       case '#' => { println(s"${sread(mem, mp)}"); (pc + 1, mp, mem) }
       
   110       
       
   111       case _ => (pc + 1, mp, mem)
   104       case _ => (pc + 1, mp, mem)
   112     }		     
   105     }		     
   113     compute(prog, new_pc, new_mp, new_mem)	
   106     compute(prog, new_pc, new_mp, new_mem)	
   114   }
   107   }
   115   else mem
   108   else mem
   139 //run("[>>+>>+<<<<-]", Map(0 -> 42))    // Map(0 -> 0, 2 -> 42, 4 -> 42)
   132 //run("[>>+>>+<<<<-]", Map(0 -> 42))    // Map(0 -> 0, 2 -> 42, 4 -> 42)
   140 
   133 
   141 
   134 
   142 // prints out numbers 0 to 9
   135 // prints out numbers 0 to 9
   143 //run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
   136 //run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
   144 
       
   145 // bf++ program calculating the cube-function, 10 * 10 * 10 = 1000
       
   146 //run("""++++++++++#>+***#""")           // Map(0 -> 10, 1 -> 1000)
       
   147 
       
   148 
       
   149 // bf++ program copies 3 from 0-cell to to cells 1, 4, 5, 6 and 7
       
   150 // (note that because of how the program wprks cell 1 will contain 7) 
       
   151 //run("""+++>+@+@+@+@+@""")   // Map(0 -> 3, 1 -> 7, 4 -> 3, 5 -> 3, 6 -> 3, 7 -> 3)
       
   152 
       
   153 
   137 
   154 
   138 
   155 // some more "useful" programs
   139 // some more "useful" programs
   156 //-----------------------------
   140 //-----------------------------
   157 
   141 
   223 // calculates the Collatz series for numbers from 1 to 30
   207 // calculates the Collatz series for numbers from 1 to 30
   224 //
   208 //
   225 //run(load_bff("collatz.bf"))
   209 //run(load_bff("collatz.bf"))
   226 
   210 
   227 }
   211 }
       
   212 
       
   213 
       
   214 //M5a.run(M5a.load_bff("mandelbrot.bf"))
       
   215 //M5a.run(M5a.load_bff("collatz.bf"))