## Calculator Language to Jcoco Assembler
### Nicholas Pease
## Contents
/hacky-solution - Alternate Solution which Requires Python 3.2.6 and makes use of the Jcoco disassembler (Included as I thought it was interesting)<br/>
/hacky-solution/calc1.py - Calc.py file for hacky solution (DO NOT GRADE, INCLUDED AS IT WAS INTERESTING TO ME)<br/>
/hacky-solution/disassembler.py - Python to Jcoco disassembler<br/>
calc.py - Python compiler for calculator language to Jcoco. For use see below
## Usage
Feed lines of the calculator language in via the standard input. The compiler will listen for the EOF signal (CTRL+D) and then generate jcoco code to the standard output.
Valid operations are (+,-,/,//,*,**,%), unary minus (-num), assignments (=)
To feed in a file in linux and execute output in Jcoco:
cat filename | python3 calc.py > tester.casm && ./coco tester.casm
Jcoco also requires Java runtime to be installed, and the jcoco binaries
## Example inputs
5+4
10*3
4-5
5--2
(9*3) + 2
a = 7 * 3
a + 9
b = (a+4)
b
## Example output
Function: main/0
Constants: None,5,4,10,3,2,0,9,7
Globals: print
BEGIN
LOAD_CONST 1
LOAD_CONST 2
BINARY_ADD
LOAD_GLOBAL 0
ROT_TWO
CALL_FUNCTION 1
POP_TOP
LOAD_CONST 3
LOAD_CONST 4
BINARY_MULTIPLY
LOAD_GLOBAL 0
ROT_TWO
CALL_FUNCTION 1
POP_TOP
LOAD_CONST 2
LOAD_CONST 1
BINARY_SUBTRACT
LOAD_GLOBAL 0
ROT_TWO
CALL_FUNCTION 1
POP_TOP
LOAD_CONST 1
LOAD_CONST 5
LOAD_CONST 6
ROT_TWO
BINARY_SUBTRACT
BINARY_SUBTRACT
LOAD_GLOBAL 0
ROT_TWO
CALL_FUNCTION 1
POP_TOP
LOAD_CONST 7
LOAD_CONST 4
BINARY_MULTIPLY
LOAD_CONST 5
BINARY_ADD
LOAD_GLOBAL 0
ROT_TWO
CALL_FUNCTION 1
POP_TOP
LOAD_CONST 8
LOAD_CONST 4
BINARY_MULTIPLY
LOAD_CONST 7
BINARY_ADD
LOAD_GLOBAL 0
ROT_TWO
CALL_FUNCTION 1
POP_TOP
LOAD_CONST 8
LOAD_CONST 4
BINARY_MULTIPLY
LOAD_CONST 2
BINARY_ADD
LOAD_GLOBAL 0
ROT_TWO
CALL_FUNCTION 1
POP_TOP
LOAD_CONST 0
RETURN_VALUE
END