solutions5/bf.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 23 Aug 2020 14:39:58 +0100
changeset 336 cccdae0fccc7
parent 329 828326d1b3b2
child 337 bf01c5eccbc7
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about an Interpreter for the Brainf*** language
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//========================================================
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
     4
290
f1d4ee6afe5d updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
     5
object CW10a {  
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
     7
import io.Source
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
     8
import scala.util._
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
    11
type Mem = Map[Int, Int]
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
    12
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// (1) Write a function that takes a file name as argument and
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
// and requests the corresponding file from disk. It returns the
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
// content of the file as a String. If the file does not exists,
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
// the function should return the empty string.
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def load_bff(name: String) : String = 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("")
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// (2) Complete the functions for safely reading  
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// and writing brainf*** memory. Safely read should
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
// Return the value stored in the Map for a given memory
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
// pointer, provided it exists; otherwise it Returns 0. The
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
// writing function generates a new Map with the
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// same data, except at the given memory pointer the
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
// value v is stored.
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
def sread(mem: Mem, mp: Int) : Int = 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  mem.getOrElse(mp, 0)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
def write(mem: Mem, mp: Int, v: Int) : Mem =
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  mem.updated(mp, v)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// (3) Implement the two jumping instructions in the 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
// brainf*** language. In jumpRight, given a program and 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
// a program counter move the program counter to the right 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
// until after the *matching* ]-command. Similarly, 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
// jumpLeft implements the move to the left to just after
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// the *matching* [-command.
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
def jumpRight(prog: String, pc: Int, level: Int) : Int = {
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  if (prog.length <= pc) pc 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  else (prog(pc), level) match {
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
    case (']', 0) => pc + 1
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    case (']', l) => jumpRight(prog, pc + 1, l - 1)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    case ('[', l) => jumpRight(prog, pc + 1, l + 1)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
    case (_, l) => jumpRight(prog, pc + 1, l)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  }
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
def jumpLeft(prog: String, pc: Int, level: Int) : Int = {
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  if (pc < 0) pc 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  else (prog(pc), level) match {
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    case ('[', 0) => pc + 1
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
    case ('[', l) => jumpLeft(prog, pc - 1, l - 1)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
    case (']', l) => jumpLeft(prog, pc - 1, l + 1)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
    case (_, l) => jumpLeft(prog, pc - 1, l)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  }
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
}
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
// test cases
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
//jumpRight("""--[..+>--],>,++""", 3, 0)         // => 10
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
//jumpLeft("""--[..+>--],>,++""", 8, 0)          // => 3
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
//jumpRight("""--[..[+>]--],>,++""", 3, 0)       // => 12
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
//jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
//jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
230
a0fd05d1e117 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    74
// (4) Complete the compute function that interprets (runs) a brainf***
a0fd05d1e117 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    75
// program: the arguments are a program (represented as a string), a program 
a0fd05d1e117 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    76
// counter, a memory counter and a brainf*** memory. It Returns the
a0fd05d1e117 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    77
// memory at the stage when the execution of the brainf*** program
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
// finishes. The interpretation finishes once the program counter
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
// pc is pointing to something outside the program string.
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
// If the pc points to a character inside the program, the pc, 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// memory pointer and memory need to be updated according to 
230
a0fd05d1e117 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    82
// rules of the brainf*** language. Then, recursively, the compute 
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
// function continues with the command at the new program
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
// counter. 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
// Implement the run function that calls compute with the program
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
// counter and memory counter set to 0.
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = {
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  if (0 <= pc && pc < prog.length) { 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
    val (new_pc, new_mp, new_mem) = prog(pc) match {
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
      case '>' => (pc + 1, mp + 1, mem)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
      case '<' => (pc + 1, mp - 1, mem)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
      case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
      case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
      case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
      case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
    97
      //case ',' => (pc + 1, mp, write(mem, mp, scala.io.StdIn.readByte()))
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
      case '['  => 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
	if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
      case ']'  => 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
	if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) 
336
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   102
 
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   103
      // new commands
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   104
      case '@' => (pc + 1, mp, write(mem, sread(mem, mp), sread(mem, mp - 1)))
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   105
      case '*' => (pc + 1, mp, write(mem, mp, sread(mem, mp) * sread(mem, mp -1)))
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   106
      case '#' => { println(s"$mp: ${sread(mem, mp)}"); (pc + 1, mp, mem) }
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   107
      
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   108
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
      case _ => (pc + 1, mp, mem)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
    }		     
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
    compute(prog, new_pc, new_mp, new_mem)	
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  }
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  else mem
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
}
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
244
0ab369f53ac1 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   119
264
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   120
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
// some sample bf-programs collected from the Internet
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
//=====================================================
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
// first some contrived (small) programs
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
// clears the 0-cell
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   129
//run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
336
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   131
// moves content of the 0-cell to 1-cell
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   132
//run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
// copies content of the 0-cell to 2-cell and 4-cell
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   136
//run("[>>+>>+<<<<-]", Map(0 -> 42))
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
// prints out numbers 0 to 9
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   140
//run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
// some more "useful" programs
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
// hello world program 1
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   146
//run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   147
//       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
// hello world program 2
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   150
//run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   151
//       +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
244
0ab369f53ac1 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   153
// hello world program 3
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   154
//run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   155
//       +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
244
0ab369f53ac1 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   157
 
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
// draws the Sierpinski triangle
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   159
//run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   160
//      ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   161
//      ]>.>+[>>]>+]""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   163
//run(load_bff("sierpinski.bf"))
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
//fibonacci numbers below 100
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   167
//run("""+++++++++++
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   168
//      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   169
//      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   170
//      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   171
//      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   172
//      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   173
//      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   174
//      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   175
//      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   176
//      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   177
//      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   179
/*
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
//outputs the square numbers up to 10000
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
run("""++++[>+++++<-]>[<+++++>-]+<+[
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
    >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
    >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
    <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
   186
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
//collatz numbers (needs a number to be typed in)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
run(""">,[[----------[
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
      >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<]
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
      ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
      <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>]
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
      >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]]
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
      >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>>
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
      ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""")
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
// infinite collatz (never stops)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
run(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[<
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
      <]>-]>[>>]>>[<<<<[<<]>+>[>>]>>-]<<<<[<<]+>>]<<[+++++[>+++++++
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
      +<-]>.<++++++[>--------<-]+<<]>>[>>]+[>>>>[<<+>+>-]<-[>+<-]+<
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
      [<<->>-[<<+>>[-]]]>>>[<<<+<<+>>>>>-]<<<[>>>+<<<-]<<[[-]>+>>->
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
      [<+<[<<+>>-]<[>+<-]<[>+<-]>>>>-]<[>+<-]+<[->[>>]<<[->[<+++>-[
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
      <+++>-[<+++>-[<[-]++>>[-]+>+<<-[<+++>-[<+++>-[<[-]+>>>+<<-[<+
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
      ++>-[<+++>-]]]]]]]]]<[>+<-]+<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
      +>-[<+>-[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-[<+>-]]]]]]]]]]]<[>+<-
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
      ]+>>]<<[<<]>]<[->>[->+>]<[-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<-
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
      >>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
      -[<->>+<-[<+>-[<->>+<-[<+>-]]]]]]]]]]]]]]]]]]]>[<+>-]<+<[<+++
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
      +++++++>-]<]>>[<+>->>]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<-]<<[<
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
      <]]++++++[>+++++++<-]>++.------------.[-]>[>>]<<[+++++[>+++++
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
      +++<-]>.<++++++[>--------<-]+<<]+<]>[<+>-]<]>>>[>>]<<[>[-]<-<
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
      <]++++++++++.[-]<<<[<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<+>-[<+>-
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
      [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""")
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
264
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   215
// 2 to the power of 6 
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   216
//(example from a C-to-BF compiler at https://github.com/elikaski/BF-it)
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   217
run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   218
       <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   219
       <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+>
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   220
       +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   221
       <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   222
       >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   223
       <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]>
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   224
       [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+>
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   225
       >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<>
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   226
       <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]>
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   227
       [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   228
       >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   229
       ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   230
       ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""")
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   231
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
// a Mandelbrot set generator in brainf*** written by Erik Bosman
230
a0fd05d1e117 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   235
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
a0fd05d1e117 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   236
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
run(load_bff("mandelbrot.bf"))
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
// a benchmark program (counts down from 'Z' to 'A')
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
            [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
            ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
            +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
def time_needed[T](n: Int, code: => T) = {
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
  val start = System.nanoTime()
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
  for (i <- 0 until n) code
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
  val end = System.nanoTime()
231
26b5a843c696 updated
Christian Urban <urbanc@in.tum.de>
parents: 230
diff changeset
   251
  (end - start)/(n * 1.0e9)
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
}
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
time_needed(1, run(b1))
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   255
*/
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
}
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
   258
336
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   259
def time_needed[T](n: Int, code: => T) = {
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   260
  val start = System.nanoTime()
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   261
  for (i <- 0 until n) code
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   262
  val end = System.nanoTime()
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   263
  (end - start)/(n * 1.0e9)
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   264
}
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   265
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   266
import CW10a._
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   267
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   268
// draws the Sierpinski triangle
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   269
run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   270
      ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   271
      ]>.>+[>>]>+]""")
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   272
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   273
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   274
println(run("""++++++++++>+***"""))
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   275
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   276
println(run("""+++>+!+!+!+!+!"""))
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   277
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   278
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   279
println(time_needed(2, run(load_bff("a.bf"))))
cccdae0fccc7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   280