author | Christian Urban <christian.urban@kcl.ac.uk> |
Tue, 01 Nov 2022 15:03:48 +0000 | |
changeset 428 | cdfa6a293453 |
parent 400 | e48ea8300b2d |
child 429 | 126d0e47ac85 |
permissions | -rw-r--r-- |
399 | 1 |
// Main Part 5 about an Interpreter for |
2 |
// the Brainf*** language |
|
285 | 3 |
//============================================== |
4 |
||
5 |
||
399 | 6 |
object M5a { |
230 | 7 |
|
399 | 8 |
// representation of BF memory |
230 | 9 |
|
10 |
type Mem = Map[Int, Int] |
|
11 |
||
12 |
import io.Source |
|
13 |
import scala.util._ |
|
14 |
||
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
400
diff
changeset
|
15 |
// ADD YOUR CODE BELOW |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
400
diff
changeset
|
16 |
//====================== |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
400
diff
changeset
|
17 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
400
diff
changeset
|
18 |
// (1) |
347 | 19 |
def load_bff(name: String) : String = ??? |
230 | 20 |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
400
diff
changeset
|
21 |
// (2) |
230 | 22 |
|
347 | 23 |
def sread(mem: Mem, mp: Int) : Int = ??? |
230 | 24 |
|
347 | 25 |
def write(mem: Mem, mp: Int, v: Int) : Mem = ??? |
230 | 26 |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
400
diff
changeset
|
27 |
// (3) |
230 | 28 |
|
347 | 29 |
def jumpRight(prog: String, pc: Int, level: Int) : Int = ??? |
230 | 30 |
|
347 | 31 |
def jumpLeft(prog: String, pc: Int, level: Int) : Int = ??? |
230 | 32 |
|
33 |
||
34 |
// testcases |
|
35 |
//jumpRight("""--[..+>--],>,++""", 3, 0) // => 10 |
|
36 |
//jumpLeft("""--[..+>--],>,++""", 8, 0) // => 3 |
|
37 |
//jumpRight("""--[..[+>]--],>,++""", 3, 0) // => 12 |
|
38 |
//jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18 |
|
39 |
//jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0) // => 22 (outside) |
|
40 |
//jumpLeft("""[******]***""", 7, 0) // => -1 (outside) |
|
41 |
||
42 |
||
43 |
||
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
400
diff
changeset
|
44 |
// (4) |
230 | 45 |
|
347 | 46 |
def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ??? |
230 | 47 |
|
347 | 48 |
def run(prog: String, m: Mem = Map()) = ??? |
230 | 49 |
|
50 |
||
51 |
||
399 | 52 |
// some sample bf-programs collected from the Internet |
53 |
//===================================================== |
|
230 | 54 |
|
55 |
||
338 | 56 |
// some contrived (small) programs |
57 |
//--------------------------------- |
|
230 | 58 |
|
59 |
// clears the 0-cell |
|
338 | 60 |
//run("[-]", Map(0 -> 100)) // Map will be 0 -> 0 |
230 | 61 |
|
338 | 62 |
// moves content of the 0-cell to 1-cell |
63 |
//run("[->+<]", Map(0 -> 10)) // Map will be 0 -> 0, 1 -> 10 |
|
230 | 64 |
|
65 |
||
66 |
// copies content of the 0-cell to 2-cell and 4-cell |
|
338 | 67 |
//run("[>>+>>+<<<<-]", Map(0 -> 42)) // Map(0 -> 0, 2 -> 42, 4 -> 42) |
230 | 68 |
|
69 |
||
70 |
// prints out numbers 0 to 9 |
|
338 | 71 |
//run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""") |
72 |
||
230 | 73 |
|
74 |
// some more "useful" programs |
|
338 | 75 |
//----------------------------- |
230 | 76 |
|
77 |
// hello world program 1 |
|
338 | 78 |
//run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++ |
79 |
// ..+++.>>.<-.<.+++.------.--------.>>+.>++.""") |
|
230 | 80 |
|
81 |
// hello world program 2 |
|
338 | 82 |
//run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+ |
83 |
// +.<<+++++++++++++++.>.+++.------.--------.>+.>.""") |
|
230 | 84 |
|
338 | 85 |
// hello world program 3 |
86 |
//run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++.. |
|
87 |
// +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""") |
|
230 | 88 |
|
338 | 89 |
|
230 | 90 |
// draws the Sierpinski triangle |
338 | 91 |
//run(load_bff("sierpinski.bf")) |
230 | 92 |
|
93 |
||
94 |
//fibonacci numbers below 100 |
|
338 | 95 |
//run("""+++++++++++ |
96 |
// >+>>>>++++++++++++++++++++++++++++++++++++++++++++ |
|
97 |
// >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> |
|
98 |
// +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[- |
|
99 |
// <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<< |
|
100 |
// -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]] |
|
101 |
// >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++ |
|
102 |
// +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++ |
|
103 |
// ++++++++++++++++++++++++++++++++++++++++++++.[-]<< |
|
104 |
// <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< |
|
105 |
// [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""") |
|
230 | 106 |
|
107 |
//outputs the square numbers up to 10000 |
|
338 | 108 |
//run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+ |
109 |
// >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] |
|
110 |
// <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""") |
|
230 | 111 |
|
112 |
||
338 | 113 |
// calculates 2 to the power of 6 |
114 |
//(example from a C-to-BF compiler at https://github.com/elikaski/BF-it) |
|
115 |
//run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-] |
|
116 |
// <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-] |
|
117 |
// <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+> |
|
118 |
// +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-] |
|
119 |
// <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-] |
|
120 |
// >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-] |
|
121 |
// <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]> |
|
122 |
// [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+> |
|
123 |
// >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<> |
|
124 |
// <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]> |
|
125 |
// [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+ |
|
126 |
// >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<< |
|
127 |
// ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++ |
|
128 |
// ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""") |
|
230 | 129 |
|
130 |
||
131 |
||
132 |
// a Mandelbrot set generator in brainf*** written by Erik Bosman |
|
133 |
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/) |
|
338 | 134 |
// |
135 |
//run(load_bff("mandelbrot.bf")) |
|
230 | 136 |
|
137 |
||
138 |
// a benchmark program (counts down from 'Z' to 'A') |
|
338 | 139 |
// |
140 |
//run(load_bff("benchmark.bf")) |
|
230 | 141 |
|
338 | 142 |
// calculates the Collatz series for numbers from 1 to 30 |
143 |
// |
|
144 |
//run(load_bff("collatz.bf")) |
|
230 | 145 |
|
285 | 146 |
} |