templates5/bf.scala
changeset 338 a1dc57326356
parent 285 bd9d142d2cd8
equal deleted inserted replaced
337:c0d9e6548b08 338:a1dc57326356
     1 // Core Part about an Interpreter for 
     1 // Core Part about an Interpreter for 
     2 // the Brainf*** language
     2 // the Brainf***++ language
     3 //==============================================
     3 //==============================================
     4 
     4 
     5 
     5 
     6 object CW10a {
     6 object CW10a {
     7 
     7 
    22 //def load_bff(name: String) : String = ...
    22 //def load_bff(name: String) : String = ...
    23 
    23 
    24 
    24 
    25 
    25 
    26 // (2) Complete the functions for safely reading  
    26 // (2) Complete the functions for safely reading  
    27 // and writing brainf*** memory. Safely read should
    27 // and writing brainf***++ memory. Safely read should
    28 // Return the value stored in the Map for a given memory
    28 // Return the value stored in the Map for a given memory
    29 // pointer, provided it exists; otherwise it Returns 0. The
    29 // pointer, provided it exists; otherwise it Returns 0. The
    30 // writing function generates a new Map with the
    30 // writing function generates a new Map with the
    31 // same data, except at the given memory pointer the
    31 // same data, except at the given memory pointer the
    32 // value v is stored.
    32 // value v is stored.
    37 //def write(mem: Mem, mp: Int, v: Int) : Mem = ...
    37 //def write(mem: Mem, mp: Int, v: Int) : Mem = ...
    38 
    38 
    39 
    39 
    40 
    40 
    41 // (3) Implement the two jumping instructions in the 
    41 // (3) Implement the two jumping instructions in the 
    42 // brainf*** language. In jumpRight, given a program and 
    42 // brainf***++ language. In jumpRight, given a program and 
    43 // a program counter move the program counter to the right 
    43 // a program counter move the program counter to the right 
    44 // until after the *matching* ]-command. Similarly, 
    44 // until after the *matching* ]-command. Similarly, 
    45 // jumpLeft implements the move to the left to just after
    45 // jumpLeft implements the move to the left to just after
    46 // the *matching* [-command.
    46 // the *matching* [-command.
    47 
    47 
    58 //jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
    58 //jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
    59 //jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
    59 //jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
    60 
    60 
    61 
    61 
    62 
    62 
    63 // (4) Complete the compute function that interprets (runs) a brainf***
    63 // (4) Complete the compute function that interprets (runs) a brainf***++
    64 // program: the arguments are a program (represented as a string), a program 
    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
    65 // counter, a memory counter and a brainf***++ memory. It Returns the
    66 // memory at the stage when the execution of the brainf*** program
    66 // memory at the stage when the execution of the brainf***++ program
    67 // finishes. The interpretation finishes once the program counter
    67 // finishes. The interpretation finishes once the program counter
    68 // pc is pointing to something outside the program string.
    68 // pc is pointing to something outside the program string.
    69 // If the pc points to a character inside the program, the pc, 
    69 // If the pc points to a character inside the program, the pc, 
    70 // memory pointer and memory need to be updated according to 
    70 // memory pointer and memory need to be updated according to 
    71 // rules of the brainf*** language. Then, recursively, the compute 
    71 // rules of the brainf***++ language. Then, recursively, the compute 
    72 // function continues with the command at the new program
    72 // function continues with the command at the new program
    73 // counter. 
    73 // counter. 
    74 //
    74 //
    75 // Implement the run function that calls compute with the program
    75 // Implement the run function that calls compute with the program
    76 // counter and memory counter set to 0.
    76 // counter and memory counter set to 0.
    79 //def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ...
    79 //def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ...
    80 
    80 
    81 //def run(prog: String, m: Mem = Map()) = ...
    81 //def run(prog: String, m: Mem = Map()) = ...
    82 
    82 
    83 
    83 
    84 /*
       
    85 
    84 
    86 // some sample bf-programs collected from the Internet
    85 // some sample bf/bf++-programs collected from the Internet
    87 //=====================================================
    86 //==========================================================
    88 
    87 
    89 
    88 
    90 // first some contrived (small) programs
    89 // some contrived (small) programs
       
    90 //---------------------------------
    91 
    91 
    92 // clears the 0-cell
    92 // clears the 0-cell
    93 run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
    93 //run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
    94 
    94 
    95 // copies content of the 0-cell to 1-cell
    95 // moves content of the 0-cell to 1-cell
    96 run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
    96 //run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
    97 
    97 
    98 
    98 
    99 // copies content of the 0-cell to 2-cell and 4-cell
    99 // copies content of the 0-cell to 2-cell and 4-cell
   100 run("[>>+>>+<<<<-]", Map(0 -> 42))
   100 //run("[>>+>>+<<<<-]", Map(0 -> 42))    // Map(0 -> 0, 2 -> 42, 4 -> 42)
   101 
   101 
   102 
   102 
   103 // prints out numbers 0 to 9
   103 // prints out numbers 0 to 9
   104 run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
   104 //run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
       
   105 
       
   106 // bf++ program calculating the cube-function, 10 * 10 * 10 = 1000
       
   107 //run("""++++++++++#>+***#""")           // Map(0 -> 10, 1 -> 1000)
       
   108 
       
   109 
       
   110 // bf++ program copies 3 from 0-cell to to cells 1, 4, 5, 6 and 7
       
   111 // (note that because of how the program wprks cell 1 will contain 7) 
       
   112 //run("""+++>+@+@+@+@+@""")   // Map(0 -> 3, 1 -> 7, 4 -> 3, 5 -> 3, 6 -> 3, 7 -> 3)
       
   113 
   105 
   114 
   106 
   115 
   107 // some more "useful" programs
   116 // some more "useful" programs
       
   117 //-----------------------------
   108 
   118 
   109 // hello world program 1
   119 // hello world program 1
   110 run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
   120 //run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
   111        ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
   121 //       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
   112 
   122 
   113 // hello world program 2
   123 // hello world program 2
   114 run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
   124 //run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
   115       +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
   125 //       +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
   116 
   126 
       
   127 // hello world program 3
       
   128 //run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..
       
   129 //       +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
   117 
   130 
       
   131  
   118 // draws the Sierpinski triangle
   132 // draws the Sierpinski triangle
   119 run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
   133 //run(load_bff("sierpinski.bf"))
   120       ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
       
   121       ]>.>+[>>]>+]""")
       
   122 
       
   123 run(load_bff("sierpinski.bf"))
       
   124 
   134 
   125 
   135 
   126 //fibonacci numbers below 100
   136 //fibonacci numbers below 100
   127 run("""+++++++++++
   137 //run("""+++++++++++
   128       >+>>>>++++++++++++++++++++++++++++++++++++++++++++
   138 //      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
   129       >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
   139 //      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
   130       +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
   140 //      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
   131       <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
   141 //      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
   132       -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
   142 //      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
   133       >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
   143 //      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
   134       +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
   144 //      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
   135       ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
   145 //      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
   136       <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
   146 //      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
   137       [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
   147 //      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
       
   148 
       
   149 //outputs the square numbers up to 10000
       
   150 //run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
       
   151 //       >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
       
   152 //       <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
   138 
   153 
   139 
   154 
   140 //outputs the square numbers up to 10000
   155 // calculates 2 to the power of 6 
   141 run("""++++[>+++++<-]>[<+++++>-]+<+[
   156 //(example from a C-to-BF compiler at https://github.com/elikaski/BF-it)
   142     >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
   157 //run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
   143     >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
   158 //       <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]
   144     <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
   159 //       <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+>
   145 
   160 //       +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]
   146 //collatz numbers (needs a number to be typed in)
   161 //       <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-]
   147 run(""">,[[----------[
   162 //       >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-]
   148       >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<]
   163 //       <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]>
   149       ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[
   164 //       [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+>
   150       <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>]
   165 //       >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<>
   151       >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]]
   166 //       <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]>
   152       >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>>
   167 //       [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+
   153       ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""")
   168 //       >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<
   154 
   169 //       ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++
   155 
   170 //       ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""")
   156 // infinite collatz (never stops)
       
   157 run(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[<
       
   158       <]>-]>[>>]>>[<<<<[<<]>+>[>>]>>-]<<<<[<<]+>>]<<[+++++[>+++++++
       
   159       +<-]>.<++++++[>--------<-]+<<]>>[>>]+[>>>>[<<+>+>-]<-[>+<-]+<
       
   160       [<<->>-[<<+>>[-]]]>>>[<<<+<<+>>>>>-]<<<[>>>+<<<-]<<[[-]>+>>->
       
   161       [<+<[<<+>>-]<[>+<-]<[>+<-]>>>>-]<[>+<-]+<[->[>>]<<[->[<+++>-[
       
   162       <+++>-[<+++>-[<[-]++>>[-]+>+<<-[<+++>-[<+++>-[<[-]+>>>+<<-[<+
       
   163       ++>-[<+++>-]]]]]]]]]<[>+<-]+<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<
       
   164       +>-[<+>-[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-[<+>-]]]]]]]]]]]<[>+<-
       
   165       ]+>>]<<[<<]>]<[->>[->+>]<[-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<-
       
   166       >>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>
       
   167       -[<->>+<-[<+>-[<->>+<-[<+>-]]]]]]]]]]]]]]]]]]]>[<+>-]<+<[<+++
       
   168       +++++++>-]<]>>[<+>->>]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<-]<<[<
       
   169       <]]++++++[>+++++++<-]>++.------------.[-]>[>>]<<[+++++[>+++++
       
   170       +++<-]>.<++++++[>--------<-]+<<]+<]>[<+>-]<]>>>[>>]<<[>[-]<-<
       
   171       <]++++++++++.[-]<<<[<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<+>-[<+>-
       
   172       [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""")
       
   173 
   171 
   174 
   172 
   175 
   173 
   176 // a Mandelbrot set generator in brainf*** written by Erik Bosman
   174 // a Mandelbrot set generator in brainf*** written by Erik Bosman
   177 // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
   175 // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
   178 
   176 //
   179 run(load_bff("mandelbrot.bf"))
   177 //run(load_bff("mandelbrot.bf"))
   180 
   178 
   181 
   179 
   182 // a benchmark program (counts down from 'Z' to 'A')
   180 // a benchmark program (counts down from 'Z' to 'A')
   183 val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
   181 //
   184             [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
   182 //run(load_bff("benchmark.bf"))
   185             ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
       
   186             +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
       
   187 
   183 
   188 
   184 // calculates the Collatz series for numbers from 1 to 30
   189 def time_needed[T](n: Int, code: => T) = {
   185 //
   190   val start = System.nanoTime()
   186 //run(load_bff("collatz.bf"))
   191   for (i <- 0 until n) code
       
   192   val end = System.nanoTime()
       
   193   (end - start)/(n * 1.0e9)
       
   194 }
       
   195 
       
   196 time_needed(1, run(b1))
       
   197 */
       
   198 
   187 
   199 }
   188 }