58 case '>' => (pc + 1, mp + 1, mem) |
58 case '>' => (pc + 1, mp + 1, mem) |
59 case '<' => (pc + 1, mp - 1, mem) |
59 case '<' => (pc + 1, mp - 1, mem) |
60 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
60 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
61 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
61 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
62 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
62 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
63 case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte)) |
|
64 case '[' => |
63 case '[' => |
65 if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) |
64 if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) |
66 case ']' => |
65 case ']' => |
67 if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) |
66 if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) |
68 case _ => (pc + 1, mp, mem) |
67 case _ => (pc + 1, mp, mem) |
142 case '>' => (pc + 1, mp + 1, mem) |
141 case '>' => (pc + 1, mp + 1, mem) |
143 case '<' => (pc + 1, mp - 1, mem) |
142 case '<' => (pc + 1, mp - 1, mem) |
144 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
143 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
145 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
144 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
146 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
145 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
147 case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte)) |
146 case '[' => |
148 case '[' => |
|
149 if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
147 if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
150 case ']' => |
148 case ']' => |
151 if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
149 if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
152 case _ => (pc + 1, mp, mem) |
150 case _ => (pc + 1, mp, mem) |
153 } |
151 } |
170 // memory at the current location to 0. In the compute3 and run3 functions |
168 // memory at the current location to 0. In the compute3 and run3 functions |
171 // below you implement this command by writing the number 0 to mem(mp), |
169 // below you implement this command by writing the number 0 to mem(mp), |
172 // that is write(mem, mp, 0). |
170 // that is write(mem, mp, 0). |
173 // |
171 // |
174 // The easiest way to modify a string in this way is to use the regular |
172 // The easiest way to modify a string in this way is to use the regular |
175 // expression """[^<>+-.,\[\]]""", which recognises everything that is |
173 // expression """[^<>+-.\[\]]""", which recognises everything that is |
176 // not a bf-command and replace it by the empty string. Similarly the |
174 // not a bf-command and replace it by the empty string. Similarly the |
177 // regular expression """\[-\]""" finds all occurences of [-] and |
175 // regular expression """\[-\]""" finds all occurences of [-] and |
178 // by using the Scala method .replaceAll you can repplace it with the |
176 // by using the Scala method .replaceAll you can repplace it with the |
179 // string "0" standing for the new bf-command. |
177 // string "0" standing for the new bf-command. |
180 |
178 |
181 def optimise(s: String) : String = |
179 def optimise(s: String) : String = |
182 s.replaceAll("""[^<>+-.,\[\]]""","").replaceAll("""\[-\]""", "0") |
180 s.replaceAll("""[^<>+-.\[\]]""","").replaceAll("""\[-\]""", "0") |
183 |
181 |
184 |
182 |
185 def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
183 def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
186 if (0 <= pc && pc < pg.length) { |
184 if (0 <= pc && pc < pg.length) { |
187 val (new_pc, new_mp, new_mem) = pg(pc) match { |
185 val (new_pc, new_mp, new_mem) = pg(pc) match { |
189 case '>' => (pc + 1, mp + 1, mem) |
187 case '>' => (pc + 1, mp + 1, mem) |
190 case '<' => (pc + 1, mp - 1, mem) |
188 case '<' => (pc + 1, mp - 1, mem) |
191 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
189 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
192 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
190 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
193 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
191 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
194 case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte)) |
|
195 case '[' => |
192 case '[' => |
196 if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
193 if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
197 case ']' => |
194 case ']' => |
198 if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
195 if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
199 case _ => (pc + 1, mp, mem) |
196 case _ => (pc + 1, mp, mem) |
242 def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match { |
239 def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match { |
243 case (Nil, acc) => acc |
240 case (Nil, acc) => acc |
244 case ('[' :: cs, acc) => splice(cs, ('[', 1) :: acc) |
241 case ('[' :: cs, acc) => splice(cs, ('[', 1) :: acc) |
245 case (']' :: cs, acc) => splice(cs, (']', 1) :: acc) |
242 case (']' :: cs, acc) => splice(cs, (']', 1) :: acc) |
246 case ('.' :: cs, acc) => splice(cs, ('.', 1) :: acc) |
243 case ('.' :: cs, acc) => splice(cs, ('.', 1) :: acc) |
247 case (',' :: cs, acc) => splice(cs, (',', 1) :: acc) |
|
248 case ('0' :: cs, acc) => splice(cs, ('0', 1) :: acc) |
244 case ('0' :: cs, acc) => splice(cs, ('0', 1) :: acc) |
249 case (c :: cs, Nil) => splice(cs, List((c, 1))) |
245 case (c :: cs, Nil) => splice(cs, List((c, 1))) |
250 case (c :: cs, (d, n) :: acc) => |
246 case (c :: cs, (d, n) :: acc) => |
251 if (c == d && n < 26) splice(cs, (c, n + 1) :: acc) |
247 if (c == d && n < 26) splice(cs, (c, n + 1) :: acc) |
252 else splice(cs, (c, 1) :: (d, n) :: acc) |
248 else splice(cs, (c, 1) :: (d, n) :: acc) |
276 case '>' => (pc + 2, mp + (pg(pc + 1) - '@'), mem) |
272 case '>' => (pc + 2, mp + (pg(pc + 1) - '@'), mem) |
277 case '<' => (pc + 2, mp - (pg(pc + 1) - '@'), mem) |
273 case '<' => (pc + 2, mp - (pg(pc + 1) - '@'), mem) |
278 case '+' => (pc + 2, mp, write(mem, mp, sread(mem, mp) + (pg(pc + 1) - '@'))) |
274 case '+' => (pc + 2, mp, write(mem, mp, sread(mem, mp) + (pg(pc + 1) - '@'))) |
279 case '-' => (pc + 2, mp, write(mem, mp, sread(mem, mp) - (pg(pc + 1) - '@'))) |
275 case '-' => (pc + 2, mp, write(mem, mp, sread(mem, mp) - (pg(pc + 1) - '@'))) |
280 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
276 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
281 case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte)) |
277 case '[' => |
282 case '[' => |
|
283 if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
278 if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
284 case ']' => |
279 case ']' => |
285 if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
280 if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
286 case _ => (pc + 1, mp, mem) |
281 case _ => (pc + 1, mp, mem) |
287 } |
282 } |