diff -r 25d9c3b2bc99 -r c0d9e6548b08 solutions5/bf.scala --- a/solutions5/bf.scala Sun Aug 23 14:39:58 2020 +0100 +++ b/solutions5/bf.scala Tue Aug 25 01:18:17 2020 +0100 @@ -1,4 +1,4 @@ -// Part 1 about an Interpreter for the Brainf*** language +// Part 1 about an Interpreter for the Brainf***++ language //======================================================== @@ -21,7 +21,7 @@ // (2) Complete the functions for safely reading -// and writing brainf*** memory. Safely read should +// and writing brainf***++ memory. Safely read should // Return the value stored in the Map for a given memory // pointer, provided it exists; otherwise it Returns 0. The // writing function generates a new Map with the @@ -37,7 +37,7 @@ // (3) Implement the two jumping instructions in the -// brainf*** language. In jumpRight, given a program and +// brainf***++ language. In jumpRight, given a program and // a program counter move the program counter to the right // until after the *matching* ]-command. Similarly, // jumpLeft implements the move to the left to just after @@ -64,22 +64,22 @@ } // test cases -//jumpRight("""--[..+>--],>,++""", 3, 0) // => 10 -//jumpLeft("""--[..+>--],>,++""", 8, 0) // => 3 -//jumpRight("""--[..[+>]--],>,++""", 3, 0) // => 12 -//jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18 -//jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0) // => 22 (outside) +//jumpRight("""--[..+>--].>.++""", 3, 0) // => 10 +//jumpLeft("""--[..+>--].>.++""", 8, 0) // => 3 +//jumpRight("""--[..[+>]--].>.++""", 3, 0) // => 12 +//jumpRight("""--[..[[-]+>[.]]--].>.++""", 3, 0) // => 18 +//jumpRight("""--[..[[-]+>[.]]--.>.++""", 3, 0) // => 22 (outside) //jumpLeft("""[******]***""", 7, 0) // => -1 (outside) -// (4) Complete the compute function that interprets (runs) a brainf*** +// (4) Complete the compute function that interprets (runs) a brainf***++ // program: the arguments are a program (represented as a string), a program -// counter, a memory counter and a brainf*** memory. It Returns the -// memory at the stage when the execution of the brainf*** program +// counter, a memory counter and a brainf***++ memory. It Returns the +// memory at the stage when the execution of the brainf***++ program // finishes. The interpretation finishes once the program counter // pc is pointing to something outside the program string. // If the pc points to a character inside the program, the pc, // memory pointer and memory need to be updated according to -// rules of the brainf*** language. Then, recursively, the compute +// rules of the brainf***++ language. Then, recursively, the compute // function continues with the command at the new program // counter. // Implement the run function that calls compute with the program @@ -93,19 +93,18 @@ case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } - case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte)) + //case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte)) //case ',' => (pc + 1, mp, write(mem, mp, scala.io.StdIn.readByte())) case '[' => - if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) + if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) case ']' => - if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) + if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) // new commands case '@' => (pc + 1, mp, write(mem, sread(mem, mp), sread(mem, mp - 1))) case '*' => (pc + 1, mp, write(mem, mp, sread(mem, mp) * sread(mem, mp -1))) - case '#' => { println(s"$mp: ${sread(mem, mp)}"); (pc + 1, mp, mem) } + case '#' => { println(s"${sread(mem, mp)}"); (pc + 1, mp, mem) } - case _ => (pc + 1, mp, mem) } compute(prog, new_pc, new_mp, new_mem) @@ -119,11 +118,12 @@ -// some sample bf-programs collected from the Internet -//===================================================== +// some sample bf/bf++-programs collected from the Internet +//========================================================== -// first some contrived (small) programs +// some contrived (small) programs +//--------------------------------- // clears the 0-cell //run("[-]", Map(0 -> 100)) // Map will be 0 -> 0 @@ -133,14 +133,24 @@ // copies content of the 0-cell to 2-cell and 4-cell -//run("[>>+>>+<<<<-]", Map(0 -> 42)) +//run("[>>+>>+<<<<-]", Map(0 -> 42)) // Map(0 -> 0, 2 -> 42, 4 -> 42) // prints out numbers 0 to 9 //run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""") +// bf++ program calculating the cube-function, 10 * 10 * 10 = 1000 +//run("""++++++++++#>+***#""") // Map(0 -> 10, 1 -> 1000) + + +// bf++ program copies 3 from 0-cell to to cells 1, 4, 5, 6 and 7 +// (note that because of how the program wprks cell 1 will contain 7) +//run("""+++>+@+@+@+@+@""") // Map(0 -> 3, 1 -> 7, 4 -> 3, 5 -> 3, 6 -> 3, 7 -> 3) + + // some more "useful" programs +//----------------------------- // hello world program 1 //run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++ @@ -156,10 +166,6 @@ // draws the Sierpinski triangle -//run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ -// ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< -// ]>.>+[>>]>+]""") - //run(load_bff("sierpinski.bf")) @@ -176,105 +182,43 @@ // <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< // [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""") -/* //outputs the square numbers up to 10000 -run("""++++[>+++++<-]>[<+++++>-]+<+[ - >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+ - >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] - <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""") - - -//collatz numbers (needs a number to be typed in) -run(""">,[[----------[ - >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] - ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[ - <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>] - >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]] - >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>> - ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""") +//run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+ +// >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] +// <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""") -// infinite collatz (never stops) -run(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[< - <]>-]>[>>]>>[<<<<[<<]>+>[>>]>>-]<<<<[<<]+>>]<<[+++++[>+++++++ - +<-]>.<++++++[>--------<-]+<<]>>[>>]+[>>>>[<<+>+>-]<-[>+<-]+< - [<<->>-[<<+>>[-]]]>>>[<<<+<<+>>>>>-]<<<[>>>+<<<-]<<[[-]>+>>-> - [<+<[<<+>>-]<[>+<-]<[>+<-]>>>>-]<[>+<-]+<[->[>>]<<[->[<+++>-[ - <+++>-[<+++>-[<[-]++>>[-]+>+<<-[<+++>-[<+++>-[<[-]+>>>+<<-[<+ - ++>-[<+++>-]]]]]]]]]<[>+<-]+<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[< - +>-[<+>-[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-[<+>-]]]]]]]]]]]<[>+<- - ]+>>]<<[<<]>]<[->>[->+>]<[-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<- - >>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+> - -[<->>+<-[<+>-[<->>+<-[<+>-]]]]]]]]]]]]]]]]]]]>[<+>-]<+<[<+++ - +++++++>-]<]>>[<+>->>]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<-]<<[< - <]]++++++[>+++++++<-]>++.------------.[-]>[>>]<<[+++++[>+++++ - +++<-]>.<++++++[>--------<-]+<<]+<]>[<+>-]<]>>>[>>]<<[>[-]<-< - <]++++++++++.[-]<<<[<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<+>-[<+>- - [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""") - -// 2 to the power of 6 +// calculates 2 to the power of 6 //(example from a C-to-BF compiler at https://github.com/elikaski/BF-it) -run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-] - <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-] - <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+> - +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-] - <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-] - >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-] - <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]> - [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+> - >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<> - <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]> - [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+ - >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<< - ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++ - ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""") +//run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-] +// <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-] +// <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+> +// +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-] +// <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-] +// >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-] +// <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]> +// [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+> +// >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<> +// <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]> +// [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+ +// >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<< +// ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++ +// ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""") // a Mandelbrot set generator in brainf*** written by Erik Bosman // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/) - -run(load_bff("mandelbrot.bf")) +// +//run(load_bff("mandelbrot.bf")) // a benchmark program (counts down from 'Z' to 'A') -val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++ - [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++ - ++++++++[>++++++++++[>++++++++++[>++++++++++[>+ - +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++.""" - +// +//run(load_bff("benchmark.bf")) -def time_needed[T](n: Int, code: => T) = { - val start = System.nanoTime() - for (i <- 0 until n) code - val end = System.nanoTime() - (end - start)/(n * 1.0e9) -} - -time_needed(1, run(b1)) -*/ +// calculates the Collatz series for numbers from 1 to 30 +// +//run(load_bff("collatz.bf")) } - -def time_needed[T](n: Int, code: => T) = { - val start = System.nanoTime() - for (i <- 0 until n) code - val end = System.nanoTime() - (end - start)/(n * 1.0e9) -} - -import CW10a._ - -// draws the Sierpinski triangle -run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ - ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< - ]>.>+[>>]>+]""") - - -println(run("""++++++++++>+***""")) - -println(run("""+++>+!+!+!+!+!""")) - - -println(time_needed(2, run(load_bff("a.bf")))) -