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