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 |