| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Thu, 10 Nov 2022 19:41:04 +0000 | |
| changeset 439 | e27ff222fef3 | 
| parent 426 | 66d8cbd39ef6 | 
| 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 | ||
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 15 | // ADD YOUR CODE BELOW | 
| 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 16 | //====================== | 
| 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 17 | |
| 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 18 | // (1) | 
| 347 | 19 | def load_bff(name: String) : String = ??? | 
| 230 | 20 | |
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
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 | |
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
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 | ||
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
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 | |
| 426 | 50 | // (5) | 
| 51 | def generate(msg: List[Char]) : String = ??? | |
| 52 | ||
| 230 | 53 | |
| 54 | ||
| 399 | 55 | // some sample bf-programs collected from the Internet | 
| 56 | //===================================================== | |
| 230 | 57 | |
| 58 | ||
| 338 | 59 | // some contrived (small) programs | 
| 60 | //--------------------------------- | |
| 230 | 61 | |
| 62 | // clears the 0-cell | |
| 338 | 63 | //run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
 | 
| 230 | 64 | |
| 338 | 65 | // moves content of the 0-cell to 1-cell | 
| 66 | //run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
 | |
| 230 | 67 | |
| 68 | ||
| 69 | // copies content of the 0-cell to 2-cell and 4-cell | |
| 338 | 70 | //run("[>>+>>+<<<<-]", Map(0 -> 42))    // Map(0 -> 0, 2 -> 42, 4 -> 42)
 | 
| 230 | 71 | |
| 72 | ||
| 73 | // prints out numbers 0 to 9 | |
| 338 | 74 | //run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
 | 
| 75 | ||
| 230 | 76 | |
| 77 | // some more "useful" programs | |
| 338 | 78 | //----------------------------- | 
| 230 | 79 | |
| 80 | // hello world program 1 | |
| 338 | 81 | //run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
 | 
| 82 | // ..+++.>>.<-.<.+++.------.--------.>>+.>++.""") | |
| 230 | 83 | |
| 84 | // hello world program 2 | |
| 338 | 85 | //run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
 | 
| 86 | // +.<<+++++++++++++++.>.+++.------.--------.>+.>.""") | |
| 230 | 87 | |
| 338 | 88 | // hello world program 3 | 
| 89 | //run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..
 | |
| 90 | // +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""") | |
| 230 | 91 | |
| 338 | 92 | |
| 230 | 93 | // draws the Sierpinski triangle | 
| 338 | 94 | //run(load_bff("sierpinski.bf"))
 | 
| 230 | 95 | |
| 96 | ||
| 97 | //fibonacci numbers below 100 | |
| 338 | 98 | //run("""+++++++++++
 | 
| 99 | // >+>>>>++++++++++++++++++++++++++++++++++++++++++++ | |
| 100 | // >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> | |
| 101 | // +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[- | |
| 102 | // <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<< | |
| 103 | // -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]] | |
| 104 | // >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++ | |
| 105 | // +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++ | |
| 106 | // ++++++++++++++++++++++++++++++++++++++++++++.[-]<< | |
| 107 | // <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< | |
| 108 | // [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""") | |
| 230 | 109 | |
| 110 | //outputs the square numbers up to 10000 | |
| 338 | 111 | //run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
 | 
| 112 | // >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] | |
| 113 | // <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""") | |
| 230 | 114 | |
| 115 | ||
| 338 | 116 | // calculates 2 to the power of 6 | 
| 117 | //(example from a C-to-BF compiler at https://github.com/elikaski/BF-it) | |
| 118 | //run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
 | |
| 119 | // <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-] | |
| 120 | // <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+> | |
| 121 | // +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-] | |
| 122 | // <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-] | |
| 123 | // >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-] | |
| 124 | // <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]> | |
| 125 | // [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+> | |
| 126 | // >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<> | |
| 127 | // <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]> | |
| 128 | // [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+ | |
| 129 | // >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<< | |
| 130 | // ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++ | |
| 131 | // ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""") | |
| 230 | 132 | |
| 133 | ||
| 134 | ||
| 135 | // a Mandelbrot set generator in brainf*** written by Erik Bosman | |
| 136 | // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/) | |
| 338 | 137 | // | 
| 138 | //run(load_bff("mandelbrot.bf"))
 | |
| 230 | 139 | |
| 140 | ||
| 141 | // a benchmark program (counts down from 'Z' to 'A') | |
| 338 | 142 | // | 
| 143 | //run(load_bff("benchmark.bf"))
 | |
| 230 | 144 | |
| 338 | 145 | // calculates the Collatz series for numbers from 1 to 30 | 
| 146 | // | |
| 147 | //run(load_bff("collatz.bf"))
 | |
| 230 | 148 | |
| 285 | 149 | } |