solutions5/bf.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 27 Jan 2020 10:18:13 +0000
changeset 329 828326d1b3b2
parent 292 f260d89e791e
child 336 cccdae0fccc7
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) 
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
      case _ => (pc + 1, mp, mem)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
    }		     
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
    compute(prog, new_pc, new_mp, new_mem)	
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  }
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
  else mem
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
}
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
244
0ab369f53ac1 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   112
264
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   113
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
// some sample bf-programs collected from the Internet
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
//=====================================================
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
// first some contrived (small) programs
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
// clears the 0-cell
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   122
//run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
// copies content of the 0-cell to 1-cell
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   125
//run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
// 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
   129
//run("[>>+>>+<<<<-]", Map(0 -> 42))
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
// prints out numbers 0 to 9
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   133
//run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
// some more "useful" programs
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
// hello world program 1
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   139
//run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   140
//       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
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
// hello world program 2
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   143
//run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   144
//       +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
244
0ab369f53ac1 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   146
// hello world program 3
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   147
//run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   148
//       +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
244
0ab369f53ac1 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   150
 
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
// draws the Sierpinski triangle
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   152
//run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   153
//      ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   154
//      ]>.>+[>>]>+]""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   156
//run(load_bff("sierpinski.bf"))
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
//fibonacci numbers below 100
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   160
//run("""+++++++++++
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   161
//      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   162
//      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   163
//      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   164
//      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   165
//      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   166
//      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   167
//      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
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
//      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   172
/*
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
//outputs the square numbers up to 10000
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
run("""++++[>+++++<-]>[<+++++>-]+<+[
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
    >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
    >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
    <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
   179
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
//collatz numbers (needs a number to be typed in)
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
      >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]]
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
      >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>>
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
      ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""")
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
// infinite collatz (never stops)
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
run(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[<
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
      ++>-[<+++>-]]]]]]]]]<[>+<-]+<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
      +>-[<+>-[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-[<+>-]]]]]]]]]]]<[>+<-
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
264
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   208
// 2 to the power of 6 
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   209
//(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
   210
run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   211
       <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   212
       <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+>
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   213
       +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   214
       <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   215
       >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-]
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   216
       <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]>
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 244
diff changeset
   217
       [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+>
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
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
// a Mandelbrot set generator in brainf*** written by Erik Bosman
230
a0fd05d1e117 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   228
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
a0fd05d1e117 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   229
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
run(load_bff("mandelbrot.bf"))
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
// a benchmark program (counts down from 'Z' to 'A')
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
            [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
            ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
            +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
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
def time_needed[T](n: Int, code: => T) = {
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
  val start = System.nanoTime()
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  for (i <- 0 until n) code
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
  val end = System.nanoTime()
231
26b5a843c696 updated
Christian Urban <urbanc@in.tum.de>
parents: 230
diff changeset
   244
  (end - start)/(n * 1.0e9)
229
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
time_needed(1, run(b1))
292
f260d89e791e updated
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   248
*/
229
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
cfcaf4a5e5b4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
}
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
   251