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