-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlo.g4
154 lines (127 loc) · 4.47 KB
/
Flo.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Flo.g4
grammar Flo;
options {
superClass=FloParserBase;
}
// Keywords
TRUE : 'true';
FALSE : 'false';
PRINT : 'print';
IF : 'if';
FOR : 'for';
ELSE : 'else';
ELIF : 'elif';
LOGICAL_OR : 'or';
LOGICAL_AND : 'and';
FUNC : 'func';
RETURN : 'return';
// Relational ops
EQUALS : '==';
NOT_EQUALS : '!=';
LESS : '<';
LESS_OR_EQUALS : '<=';
GREATER : '>';
GREATER_OR_EQUALS : '>=';
// Arithmetic ops
OR : '|';
MOD : '%';
LSHIFT : '<<';
RSHIFT : '>>';
BIT_CLEAR : '&^';
// Unary ops
NOT : 'not';
// Other ops
MULTIPLY : '*';
POWER : '**';
DIVIDE : '/';
PLUS_PLUS : '++';
MINUS_MINUS : '--';
ADDITION : '+';
SUBTRACTION : '-';
// Hidden tokens
WS : [ \t]+ -> channel(HIDDEN);
COMMENT : '/*' .*? '*/' -> channel(HIDDEN);
TERMINATOR : [\r\n]+ -> channel(HIDDEN);
LINE_COMMENT : '//' ~[\r\n]* -> channel(HIDDEN);
// Other tokens
L_CURLY : '{';
R_CURLY : '}';
L_BRACKET : '[';
R_BRACKET : ']';
L_PAREN : '(';
R_PAREN : ')';
SEMI : ';';
INTEGER : [0-9]+;
CHARS : [']~[']*['];
FLOATING_POINT : [0-9]+[.][0-9]*;
IDENTIFIER : [_a-zA-Z]+[_a-zA-Z0-9]*;
// Rules
start : multi_stmts EOF
;
multi_stmts : (stmt eos)+
;
stmt : simple_stmt
| if_stmt
| for_stmt
| func_decl
| print_stmt
| return_stmt
| block
;
simple_stmt : expression
| assign_stmt
;
assign_stmt : IDENTIFIER '=' expression
;
if_stmt : IF condition_block ( ELIF condition_block )* ( ELSE '{' multi_stmts? '}' )?
;
for_stmt : FOR for_clause_block
;
func_decl : FUNC IDENTIFIER '(' parameters? ')' '{' multi_stmts? '}'
;
parameters : IDENTIFIER ( ',' IDENTIFIER)*
;
for_clause_block : simple_stmt SEMI expression SEMI simple_stmt '{' multi_stmts? '}'
;
condition_block: expression '{' multi_stmts? '}'
;
print_stmt : PRINT expression
;
return_stmt : RETURN expression?
;
block : '{' multi_stmts? '}'
;
expression
: '(' expression ')' # ExpressionGroup
| FUNC '(' parameters? ')' '{' multi_stmts? '}' # AnonFunc
| expression '(' expression_list? ')' # CallExpression
| expression '**' expression # Power
| op=('+'|'-') expression # Unary
| IDENTIFIER op=('++'|'--') # UnaryIncDec
| expression L_BRACKET expression R_BRACKET # GetItem
| expression op=('*'|'/'|'%') expression # MulDiv
| expression op=('+'|'-') expression # AddSub
| expression op=('<<'|'>>') expression # BitShift
| expression '&' expression # BitwiseAnd
| expression '^' expression # XOR
| expression '|' expression # BitwiseOr
| expression op=('<'|'<='|'>'|'>='|'=='|'!=') expression # Relational
| NOT expression # Not
| expression LOGICAL_AND expression # And
| expression LOGICAL_OR expression # Or
| L_BRACKET expression_list? R_BRACKET # List
| INTEGER # Integer
| FLOATING_POINT # Float
| IDENTIFIER # ReadIdentifier
| TRUE # Boolean
| FALSE # Boolean
| CHARS # String
;
expression_list : expression ( ',' expression )*
;
eos
: ';'
| EOF
| {p.lineTerminatorAhead()}?
| {p.checkPreviousTokenText("}")}?
;