Skip to content

Instantly share code, notes, and snippets.

@tomleo
Forked from lawrancej/BF.java
Created August 7, 2012 01:10
Show Gist options
  • Save tomleo/3280300 to your computer and use it in GitHub Desktop.
Save tomleo/3280300 to your computer and use it in GitHub Desktop.

Revisions

  1. Joey Lawrance revised this gist Aug 6, 2012. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions Fun.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@

    // Demonstration of anonymous inner classes in Java
    // This is the closest thing Java has to function pointers.
    public class Fun {
    public interface Procedure {
    void execute();
    }
    static Procedure[] fun = new Procedure[256];
    public static void main(String[] args) {
    fun[0] = new Procedure() {
    public void execute() {
    System.out.println("Howdy!");
    }
    };
    fun[1] = new Procedure() {
    public void execute() {
    System.out.println("But so is RUM");
    }
    };
    fun[0].execute();
    fun[1].execute();
    }
    }
  2. Joey Lawrance revised this gist Aug 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ It is NOT necessary to make multiple passes through the source code.
    This language (RUM) is not that complex.
    Don't forget to write (and test) the `CompilerVisitor` for valid RUM programs!

    Also: `byte` in Java is unsigned. To make byte `b` unsigned, do: `b & 0xFF`
    Also: `byte` in Java is signed. To make byte `b` unsigned, do: `b & 0xFF`

    Deadline and submission
    =======================
  3. Joey Lawrance revised this gist Aug 4, 2012. No changes.
  4. Joey Lawrance revised this gist Aug 4, 2012. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,35 @@ FYI
    Your source code *MUST* be based on [BF.java](https://raw.github.com/gist/2917371/19e8fa54cada3e1a5638b0f972cbc18f646dad8e/BF.java), a working partial solution to last year's lab.
    You source code must also test that it works for valid RUM programs.

    Hints
    -----
    First, update the parser, `PrintVisitor` and `InterpreterVisitor` for the [pbrain](http://esolangs.org/wiki/Pbrain) subset of RUM.
    You'll need to add in two new Node types for procedure definition and invocation.
    Make sure it works with this program:

    (++++++++++<[>+>+<<-]>>[<<+>>-])>::::::::::::::<<<<<<<--------.>>>---------.+++++++..>---------.<<<<<<<------.<--------.>>>>>---.>>>.+++.<.--------.<<<<<<<+.

    That program is "Hello, World!" in pbrain.

    In the `InterpreterVisitor` you'll need this:

    private byte[] array;
    private int pointer;
    public interface Procedure {
    void execute();
    }
    private Procedure[] procedures;

    Inside the visit method for Program, add this:

    procedures = new Procedure[256];

    It is NOT necessary to make multiple passes through the source code.
    This language (RUM) is not that complex.
    Don't forget to write (and test) the `CompilerVisitor` for valid RUM programs!

    Also: `byte` in Java is unsigned. To make byte `b` unsigned, do: `b & 0xFF`

    Deadline and submission
    =======================
    You must complete this by the end of final exam time on August 10.
  5. Joey Lawrance revised this gist Aug 4, 2012. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,27 @@ FYI
    Your source code *MUST* be based on [BF.java](https://raw.github.com/gist/2917371/19e8fa54cada3e1a5638b0f972cbc18f646dad8e/BF.java), a working partial solution to last year's lab.
    You source code must also test that it works for valid RUM programs.

    Hints
    -----
    First, update the parser, `PrintVisitor` and `InterpreterVisitor` for the [pbrain](http://esolangs.org/wiki/Pbrain) subset of RUM.
    Make sure it works with this program:

    (++++++++++<[>+>+<<-]>>[<<+>>-])>::::::::::::::<<<<<<<--------.>>>---------.+++++++..>---------.<<<<<<<------.<--------.>>>>>---.>>>.+++.<.--------.<<<<<<<+.

    That program is "Hello, World!" in pbrain.

    In the `InterpreterVisitor` you'll need this:

    private byte[] array;
    private int pointer;
    public interface Procedure {
    void execute();
    }
    private Procedure[] procedures;

    It is NOT necessary to make multiple passes through the source code.
    This language (RUM) is not that complex.

    Deadline and submission
    =======================
    You must complete this by the end of final exam time on August 10.
  6. Joey Lawrance revised this gist Jul 31, 2012. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,9 @@ Deadline and submission
    =======================
    You must complete this by the end of final exam time on August 10.
    However, do not wait until then to start.
    Submit your solution via email to <[email protected]>

    This is a final exam.
    It is individual work.
    Therefore, submit your solution via email to <[email protected]>

    If you submit your solution, and you do not receive email confirmation from me before August 10 that it works correctly, then you must attend the final exam period.
  7. Joey Lawrance revised this gist Jul 31, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Goal
    ----
    Write a compiler and interpreter for [RUM](http://esolangs.org/wiki/RUM). RUM is an extension of [pbrain](http://esolangs.org/wiki/Pbrain), which in turn is an extension of [brainf--k.](http://esolangs.org/wiki/Brainfuck)

    * Also, finish any two of the four tasks defined in [Lab1-BF.pdf]((https://gist.github.com/raw/2917371/b15802afcb7c992d757639f39526ea015dda6696/Lab1-BF.pdf).
    * Also, finish any two of the four tasks defined in [Lab1-BF.pdf](https://gist.github.com/raw/2917371/b15802afcb7c992d757639f39526ea015dda6696/Lab1-BF.pdf).

    FYI
    ---
  8. Joey Lawrance revised this gist Jul 31, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Goal
    ----
    Write a compiler and interpreter for [RUM](http://esolangs.org/wiki/RUM). RUM is an extension of [pbrain](http://esolangs.org/wiki/Pbrain), which in turn is an extension of [brainf--k.](http://esolangs.org/wiki/Brainfuck)

    * Also, finish any two of the four tasks defined in [Lab1-BF.pdf]((https://gist.github.com/raw/2917371/b15802afcb7c992d757639f39526ea015dda6696/Lab1-BF.pdf)).
    * Also, finish any two of the four tasks defined in [Lab1-BF.pdf]((https://gist.github.com/raw/2917371/b15802afcb7c992d757639f39526ea015dda6696/Lab1-BF.pdf).

    FYI
    ---
  9. Joey Lawrance revised this gist Jul 31, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,8 @@ Write a compiler and interpreter for [RUM](http://esolangs.org/wiki/RUM). RUM is

    FYI
    ---
    Your source code must be based on [BF.java](https://raw.github.com/gist/2917371/19e8fa54cada3e1a5638b0f972cbc18f646dad8e/BF.java), a working partial solution to last year's lab.
    Your source code *MUST* be based on [BF.java](https://raw.github.com/gist/2917371/19e8fa54cada3e1a5638b0f972cbc18f646dad8e/BF.java), a working partial solution to last year's lab.
    You source code must also test that it works for valid RUM programs.

    Deadline and submission
    =======================
  10. Joey Lawrance revised this gist Jul 31, 2012. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,13 @@ Final

    Goal
    ----
    Write a compiler and interpreter for [RUM](http://esolangs.org/wiki/RUM).
    Write a compiler and interpreter for [RUM](http://esolangs.org/wiki/RUM). RUM is an extension of [pbrain](http://esolangs.org/wiki/Pbrain), which in turn is an extension of [brainf--k.](http://esolangs.org/wiki/Brainfuck)

    * Also, finish any two of the four tasks defined in [Lab1-BF.pdf]((https://gist.github.com/raw/2917371/b15802afcb7c992d757639f39526ea015dda6696/Lab1-BF.pdf)).

    FYI
    ---
    RUM is an extension of [pbrain](http://esolangs.org/wiki/Pbrain), which in turn is an extension of [brainf--k.](http://esolangs.org/wiki/Brainfuck)

    Your source code must be based on [BF.java](https://raw.github.com/gist/2917371/19e8fa54cada3e1a5638b0f972cbc18f646dad8e/BF.java), a working solution to last year's lab.
    Your source code must be based on [BF.java](https://raw.github.com/gist/2917371/19e8fa54cada3e1a5638b0f972cbc18f646dad8e/BF.java), a working partial solution to last year's lab.

    Deadline and submission
    =======================
  11. Joey Lawrance revised this gist Jul 31, 2012. 1 changed file with 16 additions and 5 deletions.
    21 changes: 16 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,22 @@
    Final
    =====

    Goal
    ----
    Write a compiler and interpreter for [RUM](http://esolangs.org/wiki/RUM).

    * Also, finish any two of the four tasks defined in [Lab1-BF.pdf]((https://gist.github.com/raw/2917371/b15802afcb7c992d757639f39526ea015dda6696/Lab1-BF.pdf)).

    FYI
    ---
    RUM is an extension of [pbrain](http://esolangs.org/wiki/Pbrain), which in turn is an extension of [brainf--k.](http://esolangs.org/wiki/Brainfuck)
    Start from here: <https://gist.github.com/2917371>
    Complete Lab1 for RUM, and finish 2 of the tasks in Section 4.

    Deadline
    ========
    Due August 10 at final exam time.
    Your source code must be based on [BF.java](https://raw.github.com/gist/2917371/19e8fa54cada3e1a5638b0f972cbc18f646dad8e/BF.java), a working solution to last year's lab.

    Deadline and submission
    =======================
    You must complete this by the end of final exam time on August 10.
    However, do not wait until then to start.
    Submit your solution via email to <[email protected]>

    If you submit your solution, and you do not receive email confirmation from me before August 10 that it works correctly, then you must attend the final exam period.
  12. Joey Lawrance revised this gist Jul 31, 2012. 2 changed files with 11 additions and 0 deletions.
    Binary file added Lab1-BF.pdf
    Binary file not shown.
    11 changes: 11 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    Final
    =====

    Write a compiler and interpreter for [RUM](http://esolangs.org/wiki/RUM).
    RUM is an extension of [pbrain](http://esolangs.org/wiki/Pbrain), which in turn is an extension of [brainf--k.](http://esolangs.org/wiki/Brainfuck)
    Start from here: <https://gist.github.com/2917371>
    Complete Lab1 for RUM, and finish 2 of the tasks in Section 4.

    Deadline
    ========
    Due August 10 at final exam time.
  13. Joey Lawrance created this gist Jun 12, 2012.
    147 changes: 147 additions & 0 deletions BF.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,147 @@
    import java.io.IOException;
    import java.util.LinkedList;

    public class BF {
    private interface Visitor {
    void visit (Loop loop);
    void visit (Left left);
    void visit (Right right);
    void visit (Increment increment);
    void visit (Decrement decrement);
    void visit (Input input);
    void visit (Output output);
    void visit (Program program);
    }
    private interface Node {
    void accept (Visitor v);
    }
    private class Left implements Node {
    public void accept (Visitor v) {
    v.visit(this);
    }
    }
    private class Right implements Node {
    public void accept (Visitor v) {
    v.visit(this);
    }
    }
    private class Increment implements Node {
    public void accept (Visitor v) {
    v.visit(this);
    }
    }
    private class Decrement implements Node {
    public void accept (Visitor v) {
    v.visit(this);
    }
    }
    private class Input implements Node {
    public void accept (Visitor v) {
    v.visit(this);
    }
    }
    private class Output implements Node {
    public void accept (Visitor v) {
    v.visit(this);
    }
    }
    private class Sequence implements Node {
    private LinkedList<Node> children;
    public Sequence() {
    children = new LinkedList<Node>();
    }
    public void accept (Visitor v) {
    for (Node child : children) {
    child.accept(v);
    }
    }
    public void add (Node instruction) {
    children.add(instruction);
    }
    }
    private class Loop implements Node {
    public Node body;
    public Loop (Node body) {
    this.body = body;
    }
    public void accept (Visitor v) {
    v.visit (this);
    }
    }
    public static class Program implements Node {
    public Node body;
    public Program (Node body) {
    this.body = body;
    }
    public void accept(Visitor v) {
    v.visit (this);
    }
    }
    private int i = 0;
    private Sequence doParse (String str) {
    Sequence seq = new Sequence();
    char c;
    while (i < str.length ()) {
    c = str.charAt (i);
    i++;
    if (c == '<') seq.add (new Left ());
    if (c == '>') seq.add (new Right ());
    if (c == '+') seq.add (new Increment ());
    if (c == '-') seq.add (new Decrement ());
    if (c == '.') seq.add (new Output ());
    if (c == ',') seq.add (new Input ());
    if (c == '[') seq.add (new Loop (doParse (str)));
    if (c == ']') return seq;
    }
    return seq;
    }
    public static Program parse (String str) {
    return new Program (new BF().doParse(str));
    }
    public static void main(String[] args) {
    Node ascii = BF.parse("+[.+]");
    ascii.accept(new BF.InterpreterVisitor());
    Node hello = BF.parse("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
    hello.accept(new BF.InterpreterVisitor());
    Node beer = BF.parse(">+++++++++[<+++++++++++>-]<[>[-]>[-]<<[>+>+<<-]>>[<<+>>-]>>>[-]<<<+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>>+<-]<<-<-]+++++++++>[<->-]>>+>[<[-]<<+>>>-]>[-]+<<[>+>-<<-]<<<[>>+>+<<<-]>>>[<<<+>>>-]>[<+>-]<<-[>[-]<[-]]>>+<[>[-]<-]<++++++++[<++++++<++++++>>-]>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>[-]>[-]++++[<++++++++>-]<.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.><+++++..--------.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++++++++++++++.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<+++++++++>-]<--.---------.>+++++++[<---------->-]<.>++++++[<+++++++++++>-]<.+++..+++++++++++++.>++++++++[<---------->-]<--.>+++++++++[<+++++++++>-]<--.-.>++++++++[<---------->-]<++.>++++++++[<++++++++++>-]<++++.------------.---.>+++++++[<---------->-]<+.>++++++++[<+++++++++++>-]<-.>++[<----------->-]<.+++++++++++..>+++++++++[<---------->-]<-----.---.>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>>++++[<++++++>-]<--.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.><+++++..--------.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++++++++++++++.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<+++++++++>-]<--.---------.>+++++++[<---------->-]<.>++++++[<+++++++++++>-]<.+++..+++++++++++++.>++++++++++[<---------->-]<-.---.>+++++++[<++++++++++>-]<++++.+++++++++++++.++++++++++.------.>+++++++[<---------->-]<+.>++++++++[<++++++++++>-]<-.-.---------.>+++++++[<---------->-]<+.>+++++++[<++++++++++>-]<--.+++++++++++.++++++++.---------.>++++++++[<---------->-]<++.>+++++[<+++++++++++++>-]<.+++++++++++++.----------.>+++++++[<---------->-]<++.>++++++++[<++++++++++>-]<.>+++[<----->-]<.>+++[<++++++>-]<..>+++++++++[<--------->-]<--.>+++++++[<++++++++++>-]<+++.+++++++++++.>++++++++[<----------->-]<++++.>+++++[<+++++++++++++>-]<.>+++[<++++++>-]<-.---.++++++.-------.----------.>++++++++[<----------->-]<+.---.[-]<<<->[-]>[-]<<[>+>+<<-]>>[<<+>>-]>>>[-]<<<+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>>+<-]<<-<-]+++++++++>[<->-]>>+>[<[-]<<+>>>-]>[-]+<<[>+>-<<-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<>>[<+>-]<<-[>[-]<[-]]>>+<[>[-]<-]<++++++++[<++++++<++++++>>-]>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>[-]>[-]++++[<++++++++>-]<.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.><+++++..--------.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++++++++++++++.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<+++++++++>-]<--.---------.>+++++++[<---------->-]<.>++++++[<+++++++++++>-]<.+++..+++++++++++++.>++++++++[<---------->-]<--.>+++++++++[<+++++++++>-]<--.-.>++++++++[<---------->-]<++.>++++++++[<++++++++++>-]<++++.------------.---.>+++++++[<---------->-]<+.>++++++++[<+++++++++++>-]<-.>++[<----------->-]<.+++++++++++..>+++++++++[<---------->-]<-----.---.+++.---.[-]<<<]");
    beer.accept(new BF.InterpreterVisitor());
    Node fibonacci = BF.parse("+++++++++++>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]");
    fibonacci.accept(new BF.InterpreterVisitor());
    Node rot13 = BF.parse("+[,+[-[>+>+<<-]>[<+>-]+>>++++++++[<-------->-]<-[<[-]>>>+[<+<+>>-]<[>+<-]<[<++>>>+[<+<->>-]<[>+<-]]>[<]<]>>[-]<<<[[-]<[>>+>+<<<-]>>[<<+>>-]>>++++++++[<-------->-]<->>++++[<++++++++>-]<-<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<<<<+>>>>++++[<++++++++>-]>-]<<-<-]>[<<<<[-]>>>>[<<<<->>>>-]]<<++++[<<++++++++>>-]<<-[>>+>+<<<-]>>[<<+>>-]+>>+++++[<----->-]<-[<[-]>>>+[<+<->>-]<[>+<-]<[<++>>>+[<+<+>>-]<[>+<-]]>[<]<]>>[-]<<<[[-]<<[>>+>+<<<-]>>[<<+>>-]+>------------[<[-]>>>+[<+<->>-]<[>+<-]<[<++>>>+[<+<+>>-]<[>+<-]]>[<]<]>>[-]<<<<<------------->>[[-]+++++[<<+++++>>-]<<+>>]<[>++++[<<++++++++>>-]<-]>]<[-]++++++++[<++++++++>-]<+>]<.[-]+>>+<]>[[-]<]<]");
    rot13.accept(new BF.InterpreterVisitor());
    }
    public static class PrintVisitor implements Visitor {
    public void visit (Left n) { System.out.print('<'); }
    public void visit (Right n) { System.out.print('>'); }
    public void visit (Increment n) { System.out.print('+'); }
    public void visit (Decrement n) { System.out.print('-'); }
    public void visit (Input n) { System.out.print(','); }
    public void visit (Output n) { System.out.print('.'); }
    public void visit (Loop n) { System.out.print('['); n.body.accept(this); System.out.print(']'); }
    public void visit (Program n) { n.body.accept(this); }
    }
    public static class InterpreterVisitor implements Visitor {
    private byte[] array;
    private int pointer;

    public void visit (Left n) { pointer--; }
    public void visit (Right n) { pointer++; }
    public void visit (Increment n) { array[pointer]++; }
    public void visit (Decrement n) { array[pointer]--; }
    public void visit (Input n) { try {
    array[pointer] = (byte) System.in.read();
    } catch (IOException e) {
    } }
    public void visit (Output n) { System.out.print((char)array[pointer]); }
    public void visit (Loop n) {
    while (array[pointer] != 0) {
    n.body.accept(this);
    }
    }
    public void visit (Program n) {
    array = new byte[30000];
    pointer = 0;
    n.body.accept(this);
    }
    }
    }