main_templates5/bf.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 02 Nov 2022 13:22:59 +0000
changeset 429 126d0e47ac85
parent 428 cdfa6a293453
permissions -rw-r--r--
updated
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
429
126d0e47ac85 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    50
// (5)
126d0e47ac85 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    51
def generate(msg: List[Char]) : String = ???
126d0e47ac85 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    52
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
399
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    55
// some sample bf-programs collected from the Internet
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    56
//=====================================================
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    59
// some contrived (small) programs
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    60
//---------------------------------
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
// clears the 0-cell
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    63
//run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    65
// moves content of the 0-cell to 1-cell
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    66
//run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
// copies content of the 0-cell to 2-cell and 4-cell
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    70
//run("[>>+>>+<<<<-]", Map(0 -> 42))    // Map(0 -> 0, 2 -> 42, 4 -> 42)
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// prints out numbers 0 to 9
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    74
//run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
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
// some more "useful" programs
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    78
//-----------------------------
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
// hello world program 1
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    81
//run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    82
//       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
// hello world program 2
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    85
//run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    86
//       +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    88
// hello world program 3
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    89
//run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    90
//       +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    92
 
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
// draws the Sierpinski triangle
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    94
//run(load_bff("sierpinski.bf"))
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
//fibonacci numbers below 100
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
    98
//run("""+++++++++++
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
//      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   106
//      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   107
//      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   108
//      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
//outputs the square numbers up to 10000
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   111
//run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   112
//       >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   113
//       <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   116
// calculates 2 to the power of 6 
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   117
//(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
   118
//run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
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
//       [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   129
//       >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   130
//       ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   131
//       ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""")
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
// a Mandelbrot set generator in brainf*** written by Erik Bosman
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   137
//
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   138
//run(load_bff("mandelbrot.bf"))
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
// a benchmark program (counts down from 'Z' to 'A')
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   142
//
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   143
//run(load_bff("benchmark.bf"))
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
338
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   145
// calculates the Collatz series for numbers from 1 to 30
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   146
//
Christian Urban <christian.urban@kcl.ac.uk>
parents: 285
diff changeset
   147
//run(load_bff("collatz.bf"))
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   149
}