12-11-2021, 12:29 AM
Previous: monic part 5: writing C programs on the calculator
Next: monic part 7: a little CAS
In the previous thread I introduced the mC ("monic C") high-level programming language for the monic calculator. In this thread I'll document the development of a compiler that translates the language into bytecode for execution by a virtual machine.
mC does not conform to any C standard. Features will include:
program \(\rightarrow\) code ENDFILE
code \(\rightarrow\) { var-declaration } { function-declaration } main-function-declaration
var-declaration \(\rightarrow\) ARRAY ID dimensions SEMICOLON | [ "*" ] ID SEMICOLON
dimensions \(\rightarrow\) LEFTBRACKET NUM RIGHTBRACKET { LEFTBRACKET NUM RIGHTBRACKET }
function-declaration \(\rightarrow\) ID [ LEFTPARENTHESIS params RIGHTPARENTHESIS ] LEFTBRACE local-declarations statement-list RIGHTBRACE
main-function-declaration \(\rightarrow\) MAIN LEFTBRACE local-declarations statement-list RIGHTBRACE
params \(\rightarrow\) [ param { COMMA param } ]
param \(\rightarrow\) ID { LEFTBRACKET NUM RIGHTBRACKET } | "*" ID
local-declarations \(\rightarrow\) { LOCAL var-declaration }
statement-list \(\rightarrow\) { statement }
statement \(\rightarrow\) compound-stmt | if-stmt | while-stmt | do-stmt | for-stmt| switch-stmt | return-stmt | read-stmt | write-stmt | assignment-stmt | expression-stmt | goto-stmt | label-stmt | nop-stmt
compound-stmt\(\rightarrow\) LEFTBRACE statement-list RIGHTBRACE
if-stmt \(\rightarrow\) IF LEFTPARENTHESIS Boolean-expression RIGHTPARENTHESIS statement [ ELSE statement ]
while-stmt \(\rightarrow\) WHILE LEFTPARENTHESIS Boolean-expression RIGHTPARENTHESIS statement
do-stmt \(\rightarrow\) DO statement WHILE LEFTPARENTHESIS Boolean-expression RIGHTPARENTHESIS SEMICOLON
for-stmt \(\rightarrow\) FOR LEFTPARENTHESIS [ assignment ] SEMICOLON Boolean-expression SEMICOLON [ addditive-expression ] RIGHTPARENTHESIS statement
switch-stmt \(\rightarrow\) SWITCH LEFTPARENTHESIS var RIGHTPARENTHESIS LEFTBRACE { CASE NUM COLON statement-list BREAK SEMICOLON } [ DEFAULT COLON statement-list ] RIGHTBRACE
var \(\rightarrow\) ID { LEFTBRACKET additive-expression RIGHTBRACKET } | "*" ID
return-stmt \(\rightarrow\) RETURN [ expression ] SEMICOLON
read-stmt \(\rightarrow\) READ var SEMICOLON
write-stmt \(\rightarrow\) WRITE expression SEMICOLON
assignment-stmt \(\rightarrow\) assignment SEMICOLON
expression-stmt \(\rightarrow\) [ expression ] SEMICOLON
goto-stmt \(\rightarrow\) GOTO ID SEMICOLON
label-stmt \(\rightarrow\) ID COLON statement
nop-stmt \(\rightarrow\) NOPR SEMICOLON
assignment \(\rightarrow\) var "=" expression
expression \(\rightarrow\) Boolean-expression | additive-expression
Boolean-expression \(\rightarrow\) logical-expression { boolop logical-expression }
logical-expression \(\rightarrow\) additive-expression relop additive-expression | var | TRUE | FALSE
additive-expression \(\rightarrow\) term { addop term }
term \(\rightarrow\) [ NEGATE ] factor { mulop [ NEGATE ] factor }
factor \(\rightarrow\) LEFTPARENTHESIS additive-expression RIGHTPARENTHESIS | NUM | var | ID LEFTPARENTHESIS args RIGHTPARENTHESIS | var "++" | var "--" | "&" ID { LEFTBRACKET additive-expression RIGHTBRACKET }
args \(\rightarrow\) [ expression { COMMA expression } ]
boolop \(\rightarrow\) "and" | "or" | "not"
relop \(\rightarrow\) "<=" | "<" | ">" | ">=" | "==" | "!="
addop \(\rightarrow\) "+" | "-"
mulop \(\rightarrow\) "*" | "/"
Next: monic part 7: a little CAS
In the previous thread I introduced the mC ("monic C") high-level programming language for the monic calculator. In this thread I'll document the development of a compiler that translates the language into bytecode for execution by a virtual machine.
mC does not conform to any C standard. Features will include:
- recursive functions
- local variables
- multi-dimensional arrays and pointers
program \(\rightarrow\) code ENDFILE
code \(\rightarrow\) { var-declaration } { function-declaration } main-function-declaration
var-declaration \(\rightarrow\) ARRAY ID dimensions SEMICOLON | [ "*" ] ID SEMICOLON
dimensions \(\rightarrow\) LEFTBRACKET NUM RIGHTBRACKET { LEFTBRACKET NUM RIGHTBRACKET }
function-declaration \(\rightarrow\) ID [ LEFTPARENTHESIS params RIGHTPARENTHESIS ] LEFTBRACE local-declarations statement-list RIGHTBRACE
main-function-declaration \(\rightarrow\) MAIN LEFTBRACE local-declarations statement-list RIGHTBRACE
params \(\rightarrow\) [ param { COMMA param } ]
param \(\rightarrow\) ID { LEFTBRACKET NUM RIGHTBRACKET } | "*" ID
local-declarations \(\rightarrow\) { LOCAL var-declaration }
statement-list \(\rightarrow\) { statement }
statement \(\rightarrow\) compound-stmt | if-stmt | while-stmt | do-stmt | for-stmt| switch-stmt | return-stmt | read-stmt | write-stmt | assignment-stmt | expression-stmt | goto-stmt | label-stmt | nop-stmt
compound-stmt\(\rightarrow\) LEFTBRACE statement-list RIGHTBRACE
if-stmt \(\rightarrow\) IF LEFTPARENTHESIS Boolean-expression RIGHTPARENTHESIS statement [ ELSE statement ]
while-stmt \(\rightarrow\) WHILE LEFTPARENTHESIS Boolean-expression RIGHTPARENTHESIS statement
do-stmt \(\rightarrow\) DO statement WHILE LEFTPARENTHESIS Boolean-expression RIGHTPARENTHESIS SEMICOLON
for-stmt \(\rightarrow\) FOR LEFTPARENTHESIS [ assignment ] SEMICOLON Boolean-expression SEMICOLON [ addditive-expression ] RIGHTPARENTHESIS statement
switch-stmt \(\rightarrow\) SWITCH LEFTPARENTHESIS var RIGHTPARENTHESIS LEFTBRACE { CASE NUM COLON statement-list BREAK SEMICOLON } [ DEFAULT COLON statement-list ] RIGHTBRACE
var \(\rightarrow\) ID { LEFTBRACKET additive-expression RIGHTBRACKET } | "*" ID
return-stmt \(\rightarrow\) RETURN [ expression ] SEMICOLON
read-stmt \(\rightarrow\) READ var SEMICOLON
write-stmt \(\rightarrow\) WRITE expression SEMICOLON
assignment-stmt \(\rightarrow\) assignment SEMICOLON
expression-stmt \(\rightarrow\) [ expression ] SEMICOLON
goto-stmt \(\rightarrow\) GOTO ID SEMICOLON
label-stmt \(\rightarrow\) ID COLON statement
nop-stmt \(\rightarrow\) NOPR SEMICOLON
assignment \(\rightarrow\) var "=" expression
expression \(\rightarrow\) Boolean-expression | additive-expression
Boolean-expression \(\rightarrow\) logical-expression { boolop logical-expression }
logical-expression \(\rightarrow\) additive-expression relop additive-expression | var | TRUE | FALSE
additive-expression \(\rightarrow\) term { addop term }
term \(\rightarrow\) [ NEGATE ] factor { mulop [ NEGATE ] factor }
factor \(\rightarrow\) LEFTPARENTHESIS additive-expression RIGHTPARENTHESIS | NUM | var | ID LEFTPARENTHESIS args RIGHTPARENTHESIS | var "++" | var "--" | "&" ID { LEFTBRACKET additive-expression RIGHTBRACKET }
args \(\rightarrow\) [ expression { COMMA expression } ]
boolop \(\rightarrow\) "and" | "or" | "not"
relop \(\rightarrow\) "<=" | "<" | ">" | ">=" | "==" | "!="
addop \(\rightarrow\) "+" | "-"
mulop \(\rightarrow\) "*" | "/"