-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunx.g4
38 lines (32 loc) · 1.91 KB
/
funx.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
grammar funx;
root : declareFunction* expr? EOF ;
declareFunction : ID_FUNCTION ID* '{' block '}' ;
block : instruc+ ;
instruc : '(' instruc ')' # parenthesizedInstruc
| expr # write
| ID '<-' expr # assignation
| 'if' expr '{' block '}' # conditional
| 'if' expr '{' block '}' 'else' '{' block '}' # conditional
| 'while' expr '{' block '}' # while
;
expr : '(' expr ')' # parenthesizedExpr
| ID_FUNCTION expr* # functionCall
| <assoc=right> expr '^' expr # operationExpr
| '-' expr # negativeExpr
| expr ('*' | '/' | '%') expr # operationExpr
| expr ('+' | '-') expr # operationExpr
| expr ('<' | '>' | '<=' | '>=' | '=' | '!=') expr # operationExpr
| ('not' | '!') expr # operationExpr
| expr ('and' | '&') expr # operationExpr
| expr ('or' | '|') expr # operationExpr
| BOOL # boolExpr
| NUM # numExpr
| ID # varExpr
;
BOOL : 'true' | 'false' ; // Boolean literals
ID : [a-z][a-zA-Z0-9_]* ; // Variable names
ID_FUNCTION : [A-Z][a-zA-Z0-9_]* ; // Function names
NUM : [0-9]+ ; // Numbers
BLOCK_COMMENT : '/*' .*? '*/' -> skip ; // Block comments
LINE_COMMENT : '#' ~[\r\n]* '\r'? '\n' -> skip ; // Line comments
WS : [ \n\r]+ -> skip ; // Whitespace