templates3/template3/bf.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 07 Dec 2017 12:05:10 +0000
changeset 164 7ada03135b2e
parent 163 template3/bf.scala@84917f2e16cd
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 2 about an Interpreter for the Brainf*** language
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//========================================================
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW8b {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Mem = Map[Int, Int]
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
// (2a) Complete the functions for safely reading  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// and writing brainf*** memory. Safely read should
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// Return the value stored in the Map for a given memory
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
// pointer, if it exists; otherwise it Returns 0. The
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// writing function generates a new Map with the
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
// same data, except at the given memory pointer the
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// value v is stored.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
//def sread(mem: Mem, mp: Int) : Int = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
//def write(mem: Mem, mp: Int, v: Int) : Mem = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
// (2b) Implement the two jumping instructions in the 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// brainf*** language. In jumpRight, given a program and 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// a program counter move the counter to the right 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
// until the command after the *matching* ]-command. Similarly, 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
// jumpLeft implements the move to the left to just after
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
// the *matching* [-command. The levels are used to find the
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// *matching* bracket.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
//def jumpRight(prog: String, pc: Int, level: Int) : Int = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
//def jumpLeft(prog: String, pc: Int, level: Int) : Int = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
// (2c) Complete the run function that interprets (runs) a brainf***
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
// program: the arguments are a program, a program counter,
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
// a memory counter and a brainf*** memory. It Returns the
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// memory at the stage when the execution of the brainf*** program
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
// finishes. The interpretation finishes once the program counter
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
// pc is pointing to something outside the program string.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
// If the pc points to a character inside the program, the pc, 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
// memory pointer and memory need to be updated according to 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// rules of the brainf*** language. Then, recursively, the run 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// function continues with the command at the new program
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// counter. 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
//
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// Implement the start function that calls run with the program
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
// counter and memory counter set to 0.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
//def run(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
//def start(prog: String, mem: Mem) = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
// some sample bf programs collected from the Internet
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
//==================================================
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
/*
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
// first some contrived (small) programs
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
// clears the 0-cell
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
start("[-]", Map(0 -> 100)) 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// copies content of the 0-cell to 1-cell
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
start("[->+<]", Map(0 -> 10))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// copies content of the 0-cell to 2-cell and 4-cell
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
start("[>>+>>+<<<<-]", Map(0 -> 42))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
start("+++[>+++++<-]", Map(0 -> 10))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
// prints out numbers 0 to 9
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
start("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""", Map())
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
// some more "useful" programs
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
// hello world program 1
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
start("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", Map())
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
// hello world program 2
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
start("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
      +.<<+++++++++++++++.>.+++.------.--------.>+.>.""", Map())
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
// draws the Sierpinski triangle
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
start("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
      ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
      ]>.>+[>>]>+]""", Map())
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
//Fibonacci numbers below 100
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
start("""+++++++++++
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""", Map())
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
//outputs the square numbers up to 10000
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
start("""++++[>+++++<-]>[<+++++>-]+<+[
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
    >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
    >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
    <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""", Map())
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
//Collatz numbers (need to be typed in)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
start(""">,[[----------[
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
      >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<]
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
      ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
      <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>]
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
      >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]]
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
      >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>>
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
      ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""", Map())
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
// infinite Collatz (never stops)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
start(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[<
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
      <]>-]>[>>]>>[<<<<[<<]>+>[>>]>>-]<<<<[<<]+>>]<<[+++++[>+++++++
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
      +<-]>.<++++++[>--------<-]+<<]>>[>>]+[>>>>[<<+>+>-]<-[>+<-]+<
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
      [<<->>-[<<+>>[-]]]>>>[<<<+<<+>>>>>-]<<<[>>>+<<<-]<<[[-]>+>>->
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
      [<+<[<<+>>-]<[>+<-]<[>+<-]>>>>-]<[>+<-]+<[->[>>]<<[->[<+++>-[
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
      <+++>-[<+++>-[<[-]++>>[-]+>+<<-[<+++>-[<+++>-[<[-]+>>>+<<-[<+
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
      ++>-[<+++>-]]]]]]]]]<[>+<-]+<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
      +>-[<+>-[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-[<+>-]]]]]]]]]]]<[>+<-
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
      ]+>>]<<[<<]>]<[->>[->+>]<[-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<-
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
      >>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
      -[<->>+<-[<+>-[<->>+<-[<+>-]]]]]]]]]]]]]]]]]]]>[<+>-]<+<[<+++
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
      +++++++>-]<]>>[<+>->>]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<-]<<[<
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
      <]]++++++[>+++++++<-]>++.------------.[-]>[>>]<<[+++++[>+++++
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
      +++<-]>.<++++++[>--------<-]+<<]+<]>[<+>-]<]>>>[>>]<<[>[-]<-<
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
      <]++++++++++.[-]<<<[<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<+>-[<+>-
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
      [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""", Map())
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
*/ 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
}