main_testing5/bf.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 22:01:53 +0000
changeset 463 0315d9983cd0
parent 404 bf20a9fa5c29
child 475 59e005dcf163
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
     1
// Main Part 5 about an Interpreter for 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
     2
// the Brainf*** language
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     3
//==============================================
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     4
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     5
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
     6
object M5a {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
     7
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
     8
// representation of BF memory 
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     9
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    10
type Mem = Map[Int, Int]
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    11
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    12
import io.Source
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    13
import scala.util._
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    14
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    15
// ADD YOUR CODE BELOW
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    16
//======================
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    17
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    18
// (1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    19
def load_bff(name: String) : String = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    20
    Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("")
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    21
}
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    22
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    23
// (2) 
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    24
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    25
def sread(mem: Mem, mp: Int) : Int = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    26
    Try(mem.apply(mp)).getOrElse(0)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    27
}
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    28
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    29
def write(mem: Mem, mp: Int, v: Int) : Mem = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    30
    mem + (mp -> v)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    31
}
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    32
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    33
// sread(Map(), 2) == 0
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    34
// sread(Map(2 -> 1), 2) == 1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    35
// write(Map(), 1, 2) == Map(1 -> 2)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    36
// write(Map(1 -> 0), 1, 2) == Map(1 -> 2)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    37
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    38
// val current = sread(Map(2 -> 1), 2)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    39
// write(Map(2 -> 1), 2, current+1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    40
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    41
// (3) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    42
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    43
def jumpRight(prog: String, pc: Int, level: Int) : Int = prog.toList.apply(pc) match{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    44
    case '[' => jumpRight(prog, pc+1, level+1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    45
    case ']' => level match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    46
        case 0 => pc+1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    47
        case _ => if (pc+1 >= prog.length) prog.length else jumpRight(prog, pc+1, level-1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    48
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    49
    case _ => if (pc+1 >= prog.length) prog.length else jumpRight(prog, pc+1, level)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    50
}
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    51
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    52
def jumpLeft(prog: String, pc: Int, level: Int) : Int = prog.toList.apply(pc) match{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    53
    case ']' => jumpLeft(prog, pc-1, level+1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    54
    case '[' => level match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    55
        case 0 => pc+1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    56
        case _ => if (pc-1 < 0) -1 else jumpLeft(prog, pc-1, level-1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    57
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    58
    case _ => if (pc-1 < 0) -1 else jumpLeft(prog, pc-1, level)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    59
}
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    60
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    61
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    62
// testcases
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    63
// jumpRight("""--[..+>--],>,++""", 3, 0)         // => 10
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    64
// jumpLeft("""--[..+>--],>,++""", 8, 0)          // => 3
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    65
// jumpRight("""--[..[+>]--],>,++""", 3, 0)       // => 12
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    66
// jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    67
// jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    68
// jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    69
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    70
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    71
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    72
// (4)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    73
def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = (pc >= 0 && pc < prog.length) match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    74
    case false => mem
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    75
    case true => prog.toList.apply(pc) match{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    76
        case '>' => compute(prog, pc+1, mp+1, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    77
        case '<' => compute(prog, pc+1, mp-1, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    78
        case '+' => {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    79
            val current = sread(mem, mp)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    80
            compute(prog, pc+1, mp, write(mem, mp, current+1))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    81
        }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    82
        case '-' => {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    83
            val current = sread(mem, mp)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    84
            compute(prog, pc+1, mp, write(mem, mp, current-1))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    85
        }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    86
        case '.' => {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    87
            print(mem.apply(mp).toChar)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    88
            compute(prog, pc+1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    89
        }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    90
        case '[' => {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    91
            if (mem.apply(mp) == 0) compute(prog, jumpRight(prog, pc+1, 0), mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    92
            else compute(prog, pc+1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    93
        }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    94
        case ']' => {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    95
           if (mem.apply(mp) != 0) compute(prog, jumpLeft(prog, pc-1, 0), mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    96
           else compute(prog, pc+1, mp, mem) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    97
        }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    98
        case _ => compute(prog, pc, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    99
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   100
}
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   101
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   102
def run(prog: String, m: Mem = Map()) = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   103
    compute(prog, 0, 0, m)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   104
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   105
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   106
// run("[-]", Map(0 -> 100)) == Map(0 -> 0)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   107
// run("[->+<]", Map(0 -> 10)) == Map(0 -> 0, 1 -> 10)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   108
// run("[>>+>>+<<<<-]", Map(0 -> 42)) == Map(0 -> 0, 2 -> 42, 4 -> 42)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   109
// run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""") == Map(0 -> 0, 1 -> 58, 2 -> 32)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   110
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   111
// (5)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   112
def generate(msg: List[Char]) : String = msg match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   113
    case Nil => ""
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   114
    case c => "+"*c.head.toInt + ".[-]" + generate(msg.tail)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   115
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   116
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   117
// generate("ABC".toList)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   118
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   119
// run("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]", Map())
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   120
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   121
// some sample bf-programs collected from the Internet
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   122
//=====================================================
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   123
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   124
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   125
// some contrived (small) programs
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   126
//---------------------------------
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   127
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   128
// // clears the 0-cell
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   129
// run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   130
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   131
// // moves content of the 0-cell to 1-cell
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   132
// run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   133
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   134
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   135
// // copies content of the 0-cell to 2-cell and 4-cell
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   136
// run("[>>+>>+<<<<-]", Map(0 -> 42))    // Map(0 -> 0, 2 -> 42, 4 -> 42)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   137
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   138
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   139
// // prints out numbers 0 to 9
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   140
// run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   141
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   142
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   143
// // some more "useful" programs
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   144
// //-----------------------------
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   145
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   146
// // hello world program 1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   147
// run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   148
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   149
// // hello world program 2
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   150
// run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   151
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   152
// // hello world program 3
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   153
// run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   154
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   155
 
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   156
// // draws the Sierpinski triangle
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   157
// run(load_bff("sierpinski.bf"))
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   158
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   159
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   160
// //fibonacci numbers below 100
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   161
// run("""+++++++++++
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   162
//      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   163
//      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   164
//      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   165
//      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   166
//      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   167
//      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   168
//      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   169
//      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   170
//      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   171
//      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   172
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   173
// //outputs the square numbers up to 10000
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   174
// run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   175
//       >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   176
//       <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   177
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   178
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   179
// // calculates 2 to the power of 6 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   180
// // (example from a C-to-BF compiler at https://github.com/elikaski/BF-it)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   181
// run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   182
//       <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   183
//       <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+>
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   184
//       +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   185
//       <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-]
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   186
//       >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-]
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   187
//       <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]>
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   188
//       [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+>
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   189
//       >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<>
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   190
//       <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]>
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   191
//       [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   192
//       >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   193
//       ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   194
//       ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""")
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   195
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   196
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   197
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   198
// // a Mandelbrot set generator in brainf*** written by Erik Bosman
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   199
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   200
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   201
// run(load_bff("mandelbrot.bf"))
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   202
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   203
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   204
// // a benchmark program (counts down from 'Z' to 'A')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   205
// //
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   206
// run(load_bff("benchmark.bf"))
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   207
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   208
// // calculates the Collatz series for numbers from 1 to 30
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   209
// //
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   210
// run(load_bff("collatz.bf"))
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   211
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   212
}
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   213
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   214
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   215
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   216
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   217
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   218
// This template code is subject to copyright 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   219
// by King's College London, 2022. Do not 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   220
// make the template code public in any shape 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   221
// or form, and do not exchange it with other 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   222
// students under any circumstance.