templates5/bf.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 30 Oct 2019 11:51:02 +0000
changeset 285 bd9d142d2cd8
parent 231 eecbc9ae73c2
child 338 a1dc57326356
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: 231
diff changeset
     1
// Core Part about an Interpreter for 
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
     2
// the Brainf*** language
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
     3
//==============================================
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
     4
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
     5
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
     6
object CW10a {
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// representation of Bf memory 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
type Mem = Map[Int, Int]
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// (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
    15
// and requests the corresponding file from disk. It Returns the
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
// 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
    17
// the function should Return the empty string.
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
import io.Source
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
import scala.util._
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
//def load_bff(name: String) : String = ...
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
// (2) Complete the functions for safely reading  
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
// and writing brainf*** memory. Safely read should
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// Return the value stored in the Map for a given memory
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
// pointer, provided it exists; otherwise it Returns 0. The
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
// writing function generates a new Map with the
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
// same data, except at the given memory pointer the
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
// value v is stored.
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//def sread(mem: Mem, mp: Int) : Int = ...
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
//def write(mem: Mem, mp: Int, v: Int) : Mem = ...
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
// (3) Implement the two jumping instructions in the 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
// brainf*** language. In jumpRight, given a program and 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
// a program counter move the program counter to the right 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// until after the *matching* ]-command. Similarly, 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// jumpLeft implements the move to the left to just after
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// the *matching* [-command.
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
//def jumpRight(prog: String, pc: Int, level: Int) : Int = ...
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
//def jumpLeft(prog: String, pc: Int, level: Int) : Int = ...
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
// testcases
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
//jumpRight("""--[..+>--],>,++""", 3, 0)         // => 10
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
//jumpLeft("""--[..+>--],>,++""", 8, 0)          // => 3
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
//jumpRight("""--[..[+>]--],>,++""", 3, 0)       // => 12
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
//jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
//jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
//jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
// (4) Complete the compute function that interprets (runs) a brainf***
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
// program: the arguments are a program (represented as a string), a program 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
// counter, a memory counter and a brainf*** memory. It Returns the
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
// memory at the stage when the execution of the brainf*** program
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
// finishes. The interpretation finishes once the program counter
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
// pc is pointing to something outside the program string.
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
// If the pc points to a character inside the program, the pc, 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// memory pointer and memory need to be updated according to 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// rules of the brainf*** language. Then, recursively, the compute 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// function continues with the command at the new program
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// counter. 
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
//
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
// Implement the run function that calls compute with the program
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
// counter and memory counter set to 0.
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
//def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ...
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
//def run(prog: String, m: Mem = Map()) = ...
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
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
// some sample bf-programs collected from the Internet
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
//=====================================================
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// first some contrived (small) programs
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
// clears the 0-cell
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
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 1-cell
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
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
// copies content of the 0-cell to 2-cell and 4-cell
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
run("[>>+>>+<<<<-]", Map(0 -> 42))
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
// prints out numbers 0 to 9
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
// some more "useful" programs
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 1
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
// hello world program 2
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
      +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
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
// draws the Sierpinski triangle
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
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
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
run(load_bff("sierpinski.bf"))
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
//fibonacci numbers below 100
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
run("""+++++++++++
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
      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
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
//outputs the square numbers up to 10000
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
run("""++++[>+++++<-]>[<+++++>-]+<+[
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
    >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
    >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
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
//collatz numbers (needs a number to be typed in)
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
run(""">,[[----------[
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
      >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>>
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
      ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""")
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
// infinite collatz (never stops)
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
run(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[<
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
      [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""")
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
// a Mandelbrot set generator in brainf*** written by Erik Bosman
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
run(load_bff("mandelbrot.bf"))
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
// a benchmark program (counts down from 'Z' to 'A')
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
            [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
            ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
            +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
def time_needed[T](n: Int, code: => T) = {
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
  val start = System.nanoTime()
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
  for (i <- 0 until n) code
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
  val end = System.nanoTime()
231
eecbc9ae73c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 230
diff changeset
   193
  (end - start)/(n * 1.0e9)
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
}
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
time_needed(1, run(b1))
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
*/
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   199
}