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