1 // Part 1 about an Interpreter for the Brainf*** language |
1 // Part 1 about an Interpreter for the Brainf*** language |
2 //======================================================== |
2 //======================================================== |
3 |
3 |
|
4 |
4 object CW10a { |
5 object CW10a { |
5 |
|
6 type Mem = Map[Int, Int] |
|
7 |
|
8 |
6 |
9 import io.Source |
7 import io.Source |
10 import scala.util._ |
8 import scala.util._ |
|
9 |
|
10 |
|
11 type Mem = Map[Int, Int] |
|
12 |
11 |
13 |
12 // (1) Write a function that takes a file name as argument and |
14 // (1) Write a function that takes a file name as argument and |
13 // and requests the corresponding file from disk. It returns the |
15 // and requests the corresponding file from disk. It returns the |
14 // content of the file as a String. If the file does not exists, |
16 // content of the file as a String. If the file does not exists, |
15 // the function should return the empty string. |
17 // the function should return the empty string. |
90 case '<' => (pc + 1, mp - 1, mem) |
92 case '<' => (pc + 1, mp - 1, mem) |
91 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
93 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
92 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
94 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
93 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
95 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
94 case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte)) |
96 case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte)) |
|
97 //case ',' => (pc + 1, mp, write(mem, mp, scala.io.StdIn.readByte())) |
95 case '[' => |
98 case '[' => |
96 if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) |
99 if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) |
97 case ']' => |
100 case ']' => |
98 if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) |
101 if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) |
99 case _ => (pc + 1, mp, mem) |
102 case _ => (pc + 1, mp, mem) |
170 //outputs the square numbers up to 10000 |
173 //outputs the square numbers up to 10000 |
171 run("""++++[>+++++<-]>[<+++++>-]+<+[ |
174 run("""++++[>+++++<-]>[<+++++>-]+<+[ |
172 >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+ |
175 >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+ |
173 >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] |
176 >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] |
174 <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""") |
177 <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""") |
|
178 |
175 |
179 |
176 //collatz numbers (needs a number to be typed in) |
180 //collatz numbers (needs a number to be typed in) |
177 run(""">,[[----------[ |
181 run(""">,[[----------[ |
178 >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] |
182 >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] |
179 ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[ |
183 ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[ |