// Part 2 about an Interpreter for the Brainf*** language//========================================================object CW8b {type Mem = Map[Int, Int]// (2a) Complete the functions for safely reading // and writing brainf*** memory. Safely read should// Return the value stored in the Map for a given memory// pointer, if it exists; otherwise it Returns 0. The// writing function generates a new Map with the// same data, except at the given memory pointer the// value v is stored.def sread(mem: Mem, mp: Int) : Int = mem.getOrElse(mp, 0)def write(mem: Mem, mp: Int, v: Int) : Mem = mem.updated(mp, v)// (2b) Implement the two jumping instructions in the // 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// the *matching* [--command.def jumpRight(prog: String, pc: Int, level: Int) : Int = { if (prog.length <= pc) pc else (prog(pc), level) match { case (']', 0) => pc + 1 case (']', l) => jumpRight(prog, pc + 1, l - 1) case ('[', l) => jumpRight(prog, pc + 1, l + 1) case (_, l) => jumpRight(prog, pc + 1, l) }}def jumpLeft(prog: String, p: Int, level: Int) : Int = { if (p < 0) p else (prog(p), level) match { case ('[', 0) => p + 1 case ('[', l) => jumpLeft(prog, p - 1, l - 1) case (']', l) => jumpLeft(prog, p - 1, l + 1) case (_, l) => jumpLeft(prog, p - 1, l) }}//jumpLeft("[******]***", 7, 0)// (2c) Complete the run function that interpretes (runs) a brainf***// program: the arguments are a program, a program counter,// a memory counter and a brainf*** memory. It Returns the// memory at the stage when the excution 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, run // function continues with the command at the new program// counter. // Implement the start function that calls run with the program// counter and memory counter set to 0.def run(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = { if (0 <= pc && pc < prog.length) { val (new_pc, new_mp, new_mem) = prog(pc) match { case '>' => (pc + 1, mp + 1, mem) case '<' => (pc + 1, mp - 1, mem) 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 '[' => 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) case _ => (pc + 1, mp, mem) } run(prog, new_pc, new_mp, new_mem) } else mem}def start(prog: String, m: Mem) = run(prog, 0, 0, m)// some sample programs collected from the Internet//==================================================/*// first some contrived (small) programs// clears the 0-cellstart("[-]", Map(0 -> 100)) // copies content of the 0-cell to 1-cellstart("[->+<]", Map(0 -> 10))// copies content of the 0-cell to 2-cell and 4-cellstart("[>>+>>+<<<<-]", Map(0 -> 42))// prints out numbers 0 to 9start("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""", Map())// some more "useful" programs// hello world program 1start("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++ ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", Map())// hello world program 2start("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+ +.<<+++++++++++++++.>.+++.------.--------.>+.>.""", Map())// draws the Sierpinski trianglestart("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< ]>.>+[>>]>+]""", Map())//fibonacci numbers below 100start("""+++++++++++ >+>>>>++++++++++++++++++++++++++++++++++++++++++++ >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[- <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<< -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]] >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++ +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++ ++++++++++++++++++++++++++++++++++++++++++++.[-]<< <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""", Map())//outputs the square numbers up to 10000start("""++++[>+++++<-]>[<+++++>-]+<+[ >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+ >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""", Map())//collatz numbers (need to be typed in)start(""">,[[----------[ >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[ <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>] >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]] >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>> ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""", Map())// infinite collatz (never stops)start(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[< <]>-]>[>>]>>[<<<<[<<]>+>[>>]>>-]<<<<[<<]+>>]<<[+++++[>+++++++ +<-]>.<++++++[>--------<-]+<<]>>[>>]+[>>>>[<<+>+>-]<-[>+<-]+< [<<->>-[<<+>>[-]]]>>>[<<<+<<+>>>>>-]<<<[>>>+<<<-]<<[[-]>+>>-> [<+<[<<+>>-]<[>+<-]<[>+<-]>>>>-]<[>+<-]+<[->[>>]<<[->[<+++>-[ <+++>-[<+++>-[<[-]++>>[-]+>+<<-[<+++>-[<+++>-[<[-]+>>>+<<-[<+ ++>-[<+++>-]]]]]]]]]<[>+<-]+<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[< +>-[<+>-[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-[<+>-]]]]]]]]]]]<[>+<- ]+>>]<<[<<]>]<[->>[->+>]<[-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<- >>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+> -[<->>+<-[<+>-[<->>+<-[<+>-]]]]]]]]]]]]]]]]]]]>[<+>-]<+<[<+++ +++++++>-]<]>>[<+>->>]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<-]<<[< <]]++++++[>+++++++<-]>++.------------.[-]>[>>]<<[+++++[>+++++ +++<-]>.<++++++[>--------<-]+<<]+<]>[<+>-]<]>>>[>>]<<[>[-]<-< <]++++++++++.[-]<<<[<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<+>-[<+>- [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""", Map())start("""+++++[>+++++++++<-],[[>--.++>+<<-]>+.->[<.>-]<<,]""", Map())*/ def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match { case (Nil, acc) => acc case (c :: cs, Nil) => splice(cs, List((c, 1))) case (c :: cs, (d, n) :: acc) => if (c == d) splice(cs, (c, n + 1) :: acc) else splice(cs, (c, 1) :: (d, n) :: acc)}def spl(s: String) = splice(s.toList, Nil).reverse// simple instructionsdef instr(c: Char) : String = c match { case '>' => "ptr++;" case '<' => "ptr--;" case '+' => "(*ptr)++;" case '-' => "(*ptr)--;" case '.' => "putchar(*ptr);" case ',' => "*ptr = getchar();\n" case '[' => "while(*ptr){" case ']' => "}" case _ => ""}def instrs(prog: String) : String = prog.toList.map(instr(_)).mkString//optimised instructionsdef instr2(c: Char, n: Int) : String = c match { case '>' => "ptr += " + n.toString + ";" case '<' => "ptr -= " + n.toString + ";" case '+' => "(*ptr) += " + n.toString + ";" case '-' => "(*ptr) -= " + n.toString + ";" case '.' => "putchar(*ptr);" * n case ',' => "*ptr = getchar();\n" * n case '[' => "while(*ptr){" * n case ']' => "}" * n case _ => ""}def instrs2(prog: String) : String = spl(prog).map{ case (c, n) => instr2(c, n) }.mkString// peephole optimisers are at// https://github.com/Wilfred/bfc#peephole-optimisations// http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.htmldef compile_str(prog: String) : String = { "#include <string.h>\n" ++ "#include <stdio.h>\n" ++ "char field[30000];\n" ++ "char *ptr = &field[15000];" ++ "int main()\n{\n" ++ "memset(field, '\\0', 30000);\n" ++ instrs2(prog) ++ "\n return 0;\n}"}def compile(name: String, prog: String) = { val fw = new java.io.FileWriter(name + ".c") val is = compile_str(prog) println(is) fw.write(is) fw.close()}import sys.process._def compile_run(prog: String) = { compile("tmp", prog) "gcc -O3 -o tmp tmp.c".! "./tmp".! ()}compile("test.bf", """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++ ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")compile_run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< ]>.>+[>>]>+]""")compile_run(""" A mandelbrot set fractal viewer in brainf*** written by Erik Bosman+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<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>>[-<<<<<<<<+>[<->-<<+>>]<[->+<]>>>>>>>>]<<<<<<<[->>>>>>>+<<<<<<<]<+<<<<<<<<<]>+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>>>>[-<<<<<<->>>>>>]+<<<<<<[->>>>>>->>[-<<<<<<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<<<<<<]]>>>]""")compile_run(""">+>+>+>+>++<[>[<+++>- >>>>> >+>+>+>+>++<[>[<+++>- >>>>> >+>+>+>+>++<[>[<+++>- >>>>> >+>+>+>+>++<[>[<+++>- >>>>> +++[->+++++<]>[-]< <<<<< ]<<]>[-] <<<<< ]<<]>[-] <<<<< ]<<]>[-] <<<<<]<<]>.""")// benchmarkscompile_run(""">++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++.""")//compile_run("""++++[>++++<-]>[>+>++>[+++++++>]+++[<]>-]>>>>>>>>-.<<<<.<..+++.<.>>>>.<<<.+++.------.>-.<<+.<------.""")compile_run("""++++++++[->-[->-[->-[-]<]<]<]>++++++++[<++++++++++>-]<[>+>+<<-]>-.>-----.>""")// register to BF compiler// https://gergo.erdi.hu/blog/2010-09-06-from_register_machines_to_brainfuck,_part_1/}