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