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