summary refs log tree commit diff
path: root/grammar/ANF.g4
blob: 6408f2eaa45c3a96c00458fecbd4fe2f9693f18e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
grammar ANF;

prog: def+;

def: 'def' IDENT '('(IDENT (',' IDENT)*)? ')' ':' cexp;

aexp
    : 'true' #true
    | 'false' # false
    | IDENT #var
    | NUMBER #num
    | STRING #str
    | '(' aexp '+' aexp ')' #add
    | '(' aexp '-' aexp ')' #sub
    | '(' aexp '*' aexp ')' #mul
    | '(' aexp '/' aexp ')' #div
    | '(' aexp '>' aexp ')' #gt
    | '(' aexp '<' aexp ')' #lt
    | '(' aexp '==' aexp ')' #eq
    | '(' aexp '<<' aexp ')' #bsl
    | '(' aexp '>>' aexp ')' #bsr
    | '(' aexp '&&' aexp ')' #and
    | '(' aexp '||' aexp ')' #or
    | '(' aexp '^^' aexp ')' #xor
    | '(' IDENT (',' IDENT)* ':' cexp ')' #lam
    ;

funcall
    : IDENT '(' aexp (',' aexp)* ')' #call
    | aexp #atom
    ;
cexp
    : 'let' IDENT '=' funcall 'in' cexp #let
    | 'if' aexp 'then' cexp 'else' cexp #if
    | funcall #fc
    ;


IDENT: Letter (Letter | Digit)*;
NUMBER: Digit+;
STRING: '"' ([^"]|'\\'.)* '"';

fragment Letter: 'A' .. 'Z' | 'a' .. 'z';
fragment Digit: '0'..'9';

WS: [ \t\n\r]+ -> skip;