Create a front-end compiler for Kaleidoscope extending the skeleton template from a modified version of the code from My First Language Frontend with LLVM Tutorial.
The assignment is divided in 4 parts, each one giving you a grammar and reuqiring the implementation of it and of the necessaries AST nodes.
Fisrt part requires to implement assignment logic for variables.
Grammar
% start startsymb;
startsymb:
program
program:
% empty
| top ";" program % left ":";
top:
% empty
| definition
| external
| globalvar
definition:
"def" proto block
external:
"extern" proto
proto:
"id" "(" idseq ")"
globalvar :
" global " "id"
idseq :
% empty
| "id" idseq
%left ":";
%left " <" "==";
%left "+" " -";
%left "*" "/";
stmts :
stmt
| stmt ";" stmts
stmt :
assignment
| block
| exp
assignment
"id" "=" exp
block:
"{" stmts "}"
| "{" vardefs ";" stmts "}"
vardefs:
binding
| vardefs ";" binding
binding:
"var" "id" initexp
exp:
exp "+" exp
| exp " -" exp
| exp "*" exp
| exp "/" exp
| idexp
| "(" exp ")"
| "number"
| expif
initexp:
%empty
| "=" exp
expif:
condexp "?" exp ":" exp
condexp:
exp "<" exp
| exp "==" exp
idexp:
"id"
| "id" "(" optexp ")"
optexp:
%empty
| explist
explist:
exp
| exp "," explist
Implements rules and logic for if
construct, for
loops and initializations.
stmt:
assignment
| block
| ifstmt
| forstmt
| exp
ifstmt:
"if" "(" condexp ")" stmt
| "if" "(" condexp ")" stmt " else " stmt
forstmt:
"for" "(" init ";" condexp ";" assignment ")" stmt
init:
binding
| assignment
Implements boolean operators and
, or
and not
for consecutive conditional expressions.
condexp:
relexp
| relexp "and" condexp
| relexp "or" condexp
| "not" condexp
| "(" condexp ")"
relexp:
exp "<" exp
| exp "==" exp
TODO
binding:
"var" "id" initexp
| "var" "id" "[" "number" "]"
| "var" "id" "[" "number" "]" "=" "{" explist "}"
idexp:
"id"
| "id" "(" optexp ")"
| "id" "[" exp "]"
assignment:
"id" "=" exp
| "id" "[" exp "]" "=" exp