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