This is a Kotlin based demo compiler for a functional language called Lm (TBD Register Alloc)
prog -> program scope
scope -> { sdecls } | €
sdecls -> decl sdecls | function sdecls | €
function -> type func id param block
param -> ( pdecls ) | ()
pdecls -> type id , pdecls | type id
block -> { decls stmts }
decls -> decl decls | €
decl -> type ids ;
ids -> id , ids | id
type -> basic dims
dims -> [ num ] dims | €
stmts -> stmt stmts | €
stmt -> ;
| if ( bool ) stmt
| if ( bool ) stmt else stmt
| while ( bool ) stmt
| do stmt while ( bool ) ;
| for ( assign ; bool ; assign ) stmt
| break ;
| block
| assign ;
| return bool ;
| bool ;
funcall -> id (paramcall)
paramcall -> bool, paramcall | bool
assign -> id offset = bool
offset -> [ id ] offset | [ num ] offset | €
bool -> bool or join | join
join -> join and equality | equality
equality -> equality eq rel | equality ne rel | rel
rel -> expr < expr | expr le expr | expr ge expr | expr > expr | expr
expr -> expr + term | expr - term | term
term -> term * unary | term / unary | unary
unary -> ! unary | - unary | factor
factor -> ( bool ) | funcall | id offset | num | real | bool | true | false
| Token classes | |
|---|---|
| id : identifier | or = | |
| num : Integer-number | and = && |
| real : Floating Point number | eq = == |
| char : sign | ne = != |
| bool : boolean value | le = <= |
| basic = num | real | char | bool | ge = >= |
if, else, while, do, for, break, true, false, program, func, return
program {
int i; int j; float v;
float x; float[100] a;
int func demo (int p) {
while( true ) {
do i = i + 1; while( a[i] < v);
do j = j-1; while( a[j] > v);
if ( i >= j ) break;
x = a[i]; a[i] = a[j]; a[j] = x;
}
return 2;
}
}
program {
float v; float[100] a;
float func myfunction (float f){
while (true) {
int i; int j;
do i = i+1; while (a[i] < v);
do j = j-1; while (a[j] > v);
if (i >= j) break;
{
float x;
x = a[i]; a[i] = a[j]; a[j] = x;
}
return j;
}
}
}