main_templates5/bf.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 01 Nov 2022 15:03:48 +0000
changeset 428 cdfa6a293453
parent 400 e48ea8300b2d
child 429 126d0e47ac85
permissions -rw-r--r--
updated solutions and templates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
399
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     1
// Main Part 5 about an Interpreter for 
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     2
// the Brainf*** language
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
     3
//==============================================
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
     4
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
     5
399
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     6
object M5a {
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
399
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     8
// representation of BF memory 
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
type Mem = Map[Int, Int]
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
import io.Source
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
import scala.util._
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    15
// ADD YOUR CODE BELOW
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    16
//======================
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    17
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    18
// (1)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 338
diff changeset
    19
def load_bff(name: String) : String = ???
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    21
// (2) 
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 338
diff changeset
    23
def sread(mem: Mem, mp: Int) : Int = ???
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 338
diff changeset
    25
def write(mem: Mem, mp: Int, v: Int) : Mem = ???
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    27
// (3) 
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 338
diff changeset
    29
def jumpRight(prog: String, pc: Int, level: Int) : Int = ???
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 338
diff changeset
    31
def jumpLeft(prog: String, pc: Int, level: Int) : Int = ???
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// testcases
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//jumpRight("""--[..+>--],>,++""", 3, 0)         // => 10
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
//jumpLeft("""--[..+>--],>,++""", 8, 0)          // => 3
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
//jumpRight("""--[..[+>]--],>,++""", 3, 0)       // => 12
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
//jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
//jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    44
// (4) 
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 338
diff changeset
    46
def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ???
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 338
diff changeset
    48
def run(prog: String, m: Mem = Map()) = ???
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
399
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    52
// some sample bf-programs collected from the Internet
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    53
//=====================================================
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    56
// some contrived (small) programs
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    57
//---------------------------------
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
// clears the 0-cell
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    60
//run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    62
// moves content of the 0-cell to 1-cell
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    63
//run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
// copies content of the 0-cell to 2-cell and 4-cell
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    67
//run("[>>+>>+<<<<-]", Map(0 -> 42))    // Map(0 -> 0, 2 -> 42, 4 -> 42)
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// prints out numbers 0 to 9
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    71
//run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    72
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
// some more "useful" programs
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    75
//-----------------------------
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
// hello world program 1
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    78
//run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    79
//       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// hello world program 2
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    82
//run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    83
//       +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    85
// hello world program 3
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    86
//run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    87
//       +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    89
 
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// draws the Sierpinski triangle
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    91
//run(load_bff("sierpinski.bf"))
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
//fibonacci numbers below 100
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    95
//run("""+++++++++++
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    96
//      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    97
//      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    98
//      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    99
//      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   100
//      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   101
//      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   102
//      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   103
//      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   104
//      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   105
//      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
//outputs the square numbers up to 10000
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   108
//run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   109
//       >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   110
//       <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   113
// calculates 2 to the power of 6 
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   114
//(example from a C-to-BF compiler at https://github.com/elikaski/BF-it)
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   115
//run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   116
//       <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   117
//       <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+>
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   118
//       +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   119
//       <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-]
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   120
//       >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-]
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   121
//       <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]>
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   122
//       [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+>
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   123
//       >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<>
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   124
//       <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]>
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   125
//       [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   126
//       >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   127
//       ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   128
//       ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
// a Mandelbrot set generator in brainf*** written by Erik Bosman
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   134
//
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   135
//run(load_bff("mandelbrot.bf"))
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
// a benchmark program (counts down from 'Z' to 'A')
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   139
//
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   140
//run(load_bff("benchmark.bf"))
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   142
// calculates the Collatz series for numbers from 1 to 30
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   143
//
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   144
//run(load_bff("collatz.bf"))
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   146
}