/workspace/Lab1/README.md

https://bitbucket.org/vollingerm/compliers · Markdown · 50 lines · 28 code · 22 blank · 0 comment · 0 complexity · a6cf55ffff5fcc333e76cb1953c496e2 MD5 · raw file

  1. # RUM Lab
  2. [RUM](http://esolangs.org/wiki/RUM) is an extension of [pbrain](http://esolangs.org/wiki/Pbrain), which in turn is an extension of [Brainfuck.](http://esolangs.org/wiki/Brainfuck)
  3. In this lab, we will write a compiler (among other things) for RUM. Use RUM.java as a starting point for completing these steps; if you'd like to use a language other than Java, please let me know first and let's work something out.
  4. Complete the following.
  5. 1. Add new classes implementing **Node** for the additional Pbrain and RUM constructs (e.g., **ProcedureDefinition**, **ProcedureInvocation**, **Repetition**, **Strings**, **Breakpoint**).
  6. 2. Extend the **Visitor** interface to handle the additional **Node** types.
  7. 3. Extend classes implementing **Visitor** to handle the new `visit` methods for each new **Node** type. In particular, extend the **Interpreter** and **Printer** for RUM. Inside the **Interpreter** class, you'll need this:
  8. public interface Procedure {
  9. void execute();
  10. }
  11. private Procedure[] procedures;
  12. Inside the visit method for **Program**, add this:
  13. procedures = new Procedure[256];
  14. It is NOT necessary to make multiple passes through the source code. Also: `byte` in Java is signed. To make `byte b` unsigned, do: `b & 0xFF`.
  15. **Hints:** When handling **ProcedureInvocation**, do this (in Java):
  16. procedures[cell[pointer]].execute();
  17. To implement **ProcedureDefinition**, use anonymous inner classes (Java) or anonymous functions (Python, C#/C++)
  18. When handling **Strings**, it also affects input, because a String in RUM is inserted at the beginning of the input buffer. Null terminated means the String has a zero `'\0'` character at the end.
  19. When handling **Repetition**, it's only necessary to worry about the primitive commands, not loops or procedure definitions.
  20. 4. Write a **Compiler** class (implementing Visitor) for RUM (it's just like the Printer class, except that it should print out equivalent code in a language of your choice such as Java, C++, Python, or Ruby).
  21. **Hint** Print out the boilerplate (e.g., imports and includes) when visiting the **Program** node.
  22. 5. Implement one (or more) of the following Visitor classes (or suggest something else to me):
  23. * **VisualInterpreter**. Render the contents of the array and pointer into a nice GUI.
  24. * **AnotherCompiler**. Output code for another language, or even *gasp* assembly code that works with `masm`, `as` or `nasm`.
  25. * **Optimizer**. Traverse through the AST and create a new AST that improves execution performance (e.g., replace runs of the same instruction with a single instruction, so ++++ becomes a single Increment node with a count of 4, or replace a Sequence pattern with a special Node). This would involve changing the classes implementing Node, or even implementing new Node types.
  26. Be sure to test that all of this works for valid RUM programs. For example, this is "Hello, World!" in RUM:
  27. (++++++++++<[>+>+<<-]>>[<<+>>-])>::::::::::::::<<<<<<<--------.>>>---------.+++++++..>---------.<<<<<<<------.<--------.>>>>>---.>>>.+++.<.--------.<<<<<<<+.
  28. Try to see if it works with one of [these programs](http://esoteric.sange.fi/brainfuck/bf-source/prog/), particularly one that accepts input from the user.