-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ljl.g4
60 lines (48 loc) · 1.21 KB
/
Ljl.g4
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
grammar Ljl;
file: top* EOF;
top : funcDefine
| funcExtern ';'
| typeDefine
;
typeDefine: 'type' ID 'struct' '{' (structMem )* '}' # structDefine
| 'type' ID '=' typ # typeAlias
;
structMem: ID ':' typ;
funcExtern: 'extern' funcProto;
funcDefine : funcProto block;
funcProto: 'fn' ID '(' paramList ')' '->' typ;
paramList:
| param (',' param)*
;
param: ID ':' typ;
typ: ID
| '(' ID ',' ID (',' ID)* ')'
| '(' ')'
;
block: '{' expr* '}';
expr: block # blockExpr
| expr op=('*'|'/') expr # mulDivExpr
| expr op=('+'|'-') expr # addSubExpr
| expr op=('>'|'<'|'>='|'<=') expr # cmpExpr
| expr op=('=='| '!=') expr # eqExpr
| '!' expr #notExpr
| expr '&&' expr # andExpr
| expr '||' expr # orExpr
| ID tupleVal # callExpr
| ID # valRefExpr
| literal # litExpr
| 'if' expr 'then' expr 'else' expr # ifExpr
| tupleVal #tupleExpr
| expr '.' ID #memberExpr
;
tupleVal: '(' ( expr (','expr)*)? ')';
literal: INT
| FLOAT
| STRING
;
INT: '-'? [0-9]+;
FLOAT: '-'? [0-9]+ '.' [0-9]+;
STRING: '"' .*? '"';
ID: [a-zA-Z_][a-zA-Z0-9_]*;
COMMENT: '/*' .*? '*/' -> skip;
WS: [ \t\r\n]+ ->skip;