3 //============================================== |
3 //============================================== |
4 |
4 |
5 |
5 |
6 object M5a { |
6 object M5a { |
7 |
7 |
8 |
|
9 // representation of BF memory |
8 // representation of BF memory |
10 |
9 |
11 type Mem = Map[Int, Int] |
10 type Mem = Map[Int, Int] |
12 |
11 |
13 |
|
14 // (1) Write a function that takes a file name as argument and |
|
15 // and requests the corresponding file from disk. It returns the |
|
16 // content of the file as a String. If the file does not exists, |
|
17 // the function should return the empty string. |
|
18 |
|
19 import io.Source |
12 import io.Source |
20 import scala.util._ |
13 import scala.util._ |
21 |
14 |
|
15 // ADD YOUR CODE BELOW |
|
16 //====================== |
|
17 |
|
18 // (1) |
22 def load_bff(name: String) : String = ??? |
19 def load_bff(name: String) : String = ??? |
23 |
20 |
24 |
21 // (2) |
25 |
|
26 // (2) Complete the functions for safely reading |
|
27 // and writing brainf*** memory. Safely read should |
|
28 // Return the value stored in the Map for a given memory |
|
29 // pointer, provided it exists; otherwise it Returns 0. The |
|
30 // writing function generates a new Map with the |
|
31 // same data, except at the given memory pointer the |
|
32 // value v is stored. |
|
33 |
|
34 |
22 |
35 def sread(mem: Mem, mp: Int) : Int = ??? |
23 def sread(mem: Mem, mp: Int) : Int = ??? |
36 |
24 |
37 def write(mem: Mem, mp: Int, v: Int) : Mem = ??? |
25 def write(mem: Mem, mp: Int, v: Int) : Mem = ??? |
38 |
26 |
39 |
27 // (3) |
40 |
|
41 // (3) Implement the two jumping instructions in the |
|
42 // brainf*** language. In jumpRight, given a program and |
|
43 // a program counter move the program counter to the right |
|
44 // until after the *matching* ]-command. Similarly, |
|
45 // jumpLeft implements the move to the left to just after |
|
46 // the *matching* [-command. |
|
47 |
28 |
48 def jumpRight(prog: String, pc: Int, level: Int) : Int = ??? |
29 def jumpRight(prog: String, pc: Int, level: Int) : Int = ??? |
49 |
30 |
50 def jumpLeft(prog: String, pc: Int, level: Int) : Int = ??? |
31 def jumpLeft(prog: String, pc: Int, level: Int) : Int = ??? |
51 |
32 |
58 //jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0) // => 22 (outside) |
39 //jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0) // => 22 (outside) |
59 //jumpLeft("""[******]***""", 7, 0) // => -1 (outside) |
40 //jumpLeft("""[******]***""", 7, 0) // => -1 (outside) |
60 |
41 |
61 |
42 |
62 |
43 |
63 // (4) Complete the compute function that interprets (runs) a brainf*** |
44 // (4) |
64 // program: the arguments are a program (represented as a string), a program |
|
65 // counter, a memory counter and a brainf*** memory. It Returns the |
|
66 // memory at the stage when the execution of the brainf*** program |
|
67 // finishes. The interpretation finishes once the program counter |
|
68 // pc is pointing to something outside the program string. |
|
69 // If the pc points to a character inside the program, the pc, |
|
70 // memory pointer and memory need to be updated according to |
|
71 // rules of the brainf*** language. Then, recursively, the compute |
|
72 // function continues with the command at the new program |
|
73 // counter. |
|
74 // |
|
75 // Implement the run function that calls compute with the program |
|
76 // counter and memory counter set to 0. |
|
77 |
|
78 |
45 |
79 def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ??? |
46 def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ??? |
80 |
47 |
81 def run(prog: String, m: Mem = Map()) = ??? |
48 def run(prog: String, m: Mem = Map()) = ??? |
82 |
49 |