blob: afe42094ffbae241b64550f1b37615e0d014b2d5 (
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
grammar Toc;
prog: (decl)+ EOF;
decl: varDecl ';'
| funcDecl
| structDecl
;
varDecl: 'var' var;
var: varName (':' type) ('=' expr)?;
varInit: varName (':' type) ('=' expr);
type: typeName (typeModifier)*;
typeModifier: '*' | ('[' (INT_LIT)? ']');
funcDecl: 'func' func;
func: funcName '(' parameter ')' (':' type) body;
parameter: (var (',' var)*)?;
body: '{' stmt* '}';
structDecl: 'struct' structName '{' structMember* '}';
structMember: structVar | structMethod;
structVar: var ';';
structMethod: func;
stmt: varDecl ';'
| ifStmt
| switchStmt
| forStmt
| whileStmt
| assignStmt ';'
| returnStmt ';'
| expr ';';
ifStmt: 'if' expr body elseIfStmt* elseStmt?;
elseIfStmt: 'else' 'if' expr body;
elseStmt: 'else' body;
switchStmt: 'switch' identifierExpr switchBody;
switchBody: '{' switchCase* '}';
switchCase: 'case' expr body;
forStmt: 'for' (varInit | assignStmt) ',' expr ',' expr body;
whileStmt: 'while' expr body;
assignStmt: identifierExpr '=' expr;
returnStmt: 'return' expr;
expr: funcExpr
| litExpr
| identifierExpr
| parenExpr
| accessExpr
| opExpr;
/* op */
nonOpExpr: funcExpr
| litExpr
| identifierExpr
| parenExpr
| accessExpr;
/* lit access op */
nonAccessExpr: funcExpr
| identifierExpr
| parenExpr;
funcExpr: funcName '(' (expr (',' expr)*)? ')';
opExpr: binaryOp | prefixOp | postfixOp | ternaryOp;
binaryOp: nonOpExpr binary_op nonOpExpr (binary_op nonOpExpr)*;
prefixOp: prefix_op nonOpExpr;
postfixOp: nonOpExpr postfix_op;
ternaryOp: nonOpExpr '?' expr ':' expr;
identifierExpr: varName;
litExpr: INT_LIT | DECIMAL_LIT | STRING_LIT | BOOL_LIT;
accessExpr: nonAccessExpr (accessSubExpr)+;
accessSubExpr: accessMember | accessBrackets;
accessMember: ('.' | '->') identifierExpr;
accessBrackets: '[' expr ']';
parenExpr: '(' expr ')';
funcName: NAME;
varName: NAME;
typeName: NAME;
structName: NAME;
postfix_op:
'++' | '--';
prefix_op:
'+' | '-' | '!' | '~' | '&' | '*' | postfix_op;
binary_op:
'+' | '-' | '*' | '/' | '%' | '&' | '<' | '|' | '^' | '>' |
'==' | '!=' | '<=' | '>=' | '<' | '>' |
'<<' | '>>' | '||' | '&&' | '&=' | '|=' | '^=' |
'<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%=';
INT_LIT: ('+' | '-')? [0-9]+;
DECIMAL_LIT: ('+' | '-')* [0-9]+ '.' [0-9]+;
STRING_LIT: '"' [^"]* '"';
BOOL_LIT: 'true' | 'false';
NAME: ([a-z] | [A-Z] | [0-9])+;
WS: [ \t\r\n]+ -> skip;
NEWLINE: [\r\n]+;
|