-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_gen.g4
66 lines (52 loc) · 2.45 KB
/
d_gen.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
grammar d_gen;
program : function NEWLINE* EOF ;
function : ret_type = type f_name = IDENT
'(' arg_types += type args += IDENT (',' arg_types += type args += IDENT)* ')' body;
body : '{' NEWLINE (stmts += statement NEWLINE)* '}';
statement : assignment | return | break | continue |
define | for | while | if ;
assignment : simple_asg | crem_asg ;
simple_asg : (f_ident | array_lookup) (ASSG | ASSG_TYPE) logic_expr ;
crem_asg : (f_ident | array_lookup) CREM_TYPE ;
return : 'return' logic_expr ;
break : 'break' ;
continue : 'continue' ;
define : type IDENT (ASSG logic_expr)? ;
for : (precondition NEWLINE)? 'for' pre_asg = assignment? ';' logic_expr? ';' inc_asg = assignment? body ;
while : (precondition NEWLINE)? 'while' logic_expr body ;
if : (precondition NEWLINE)? 'if' logic_expr body ('else' body)? ;
precondition : '[' ('prob' ASSG NUM ';')? logic_expr? ']' ;
//condition : tl ( '||' tl )* ;
//tl : fl ( '&&' fl)* ;
//fl : '(' condition ')' | expr CMP_OP expr ;
logic_expr : operands += logic_t ( ops += '||' operands += logic_t)* ;
logic_t : operands += logic_f ( ops += '&&' operands += logic_f)* ;
logic_f : logic_inner_expr | cmp_op | expr ;
logic_inner_expr : '(' logic_expr ')' ;
cmp_op : expr CMP_OP expr ;
expr : operands += t ( (ops += ('+' | '-')) operands += t )* ;
t : operands += f ( ops += ('*' | '/') operands += f)* ;
f : inner_expr | const | f_ident | array_lookup |
array_create | property_lookup | unary_minus ;
inner_expr : '(' expr ')' ;
const : CHAR | STRING | NUM | BOOL;
f_ident : IDENT ;
array_lookup : IDENT ('[' expr ']')+ ;
array_create : type '[' expr ']' ;
property_lookup : IDENT '.' IDENT ;
unary_minus : '-' f ;
type : (types += SCALAR_TYPE types += NESTED_TYPE* );
//types
NESTED_TYPE : '[]' ;
SCALAR_TYPE : 'int' | 'string' | 'char' | 'bool' ;
ASSG : '=' ;
ASSG_TYPE : '+=' | '-=' | '*=' | '/=' ;
CREM_TYPE : '++' | '--' ;
CMP_OP : '<' | '<=' | '>' | '>=' | '==' | '!=' ;
BOOL : 'true' | 'false' ;
IDENT : [a-zA-Z][a-zA-Z0-9_]* ;
CHAR : '\'' (. | '\\n' | '\\t') '\'' ;
STRING : '"' .*? '"' ;
NUM : [1-9][0-9]* | [0-9] ;
NEWLINE : ('\r'? '\n' | '\r')+ ;
WHITESPACE : [ \t] -> skip ;