-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar
More file actions
161 lines (121 loc) · 2.9 KB
/
grammar
File metadata and controls
161 lines (121 loc) · 2.9 KB
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
155
156
157
158
159
160
#
# ASCII - The normal ASCII character set using escape
# sequences where necessary i.e. '\n'.
#
# IDENTIFIER
# : [_a-zA-Z][_a-zA-Z0-9]*
# LITERAL
# : 0x[0-9]*
# | [0-9]*
# | 'ASCII'
# | "ASCII*"
#
# Expressions only contain identifiers, literals and operators, where
# operators include arithmetic and boolean operators, the function call
# operator () the subscription operator [] and similar, and can be
# reduced to some kind of "value".
# Statements are the highest level and describe what can be on
# each line of code. Expressions are a subset of statements.
########################################################################
# Operators
########################################################################
unary_operator
: deref_operator
| '!' | '~' ;
deref_operator
: '*' ;
binary_operator
: bitwise_operator
| logical_operator
| math_operator
| assignment_operator ;
math_operator
: '+' | '-' | '/' | '*' | '%' ;
bitwise_operator
: '|' | '&' | '^' ;
logical_operator
: '||' | '&&' | '==' | '!=' | '<' | '>' | '<=' | '>=' ;
assignment_operator
: '=' ;
pointer_operator
: '.' | '->' ;
array_operator
: expr '[' expr ']' ;
########################################################################
# Expressions
########################################################################
expr
: expr_function
| expr_math
| expr_scope
| IDENTIFIER
| LITERAL ;
expr_scope
: '(' expr ')' ;
expr_math
: expr binary_operator expr
| unary_operator expr ;
parameter
: expr
| expr ',' parameter ;
expr_function
: IDENTIFIER '(' parameter ')' ;
########################################################################
# Types
########################################################################
type
: type_base
| type_pointer
| type_struct ;
type_base
: 'u8' | 'u32' | 'char' ;
type_struct
: 'struct' IDENTIFIER ;
type_pointer
: type_base '*'
| type_struct '*' ;
########################################################################
# Statements
########################################################################
stmt
: stmt_block
| stmt_multi
| stmt_expr
| stmt_conditional
| stmt_loop
| stmt_flow
| stmt_define
| stmt_define_func
| stmt_declare_struct
| stmt_declare_initialize
| ';' ;
stmt_block
: '{' stmt '}' ;
stmt_multi
: stmt stmt ;
stmt_expr
: expr ';' ;
stmt_conditional
: 'if' '(' expr ')' stmt
| 'if' '(' expr ')' stmt 'else' stmt ;
stmt_loop
: 'while' '(' expr ')' stmt
| 'for' '(' expr ';' expr ';' expr ')' stmt ;
stmt_flow
: 'break' ';'
| 'continue' ';' ;
stmt_return
: 'return' ';'
| 'return' expr ';' ;
argument
: type IDENTIFIER
| type IDENTIFIER ',' argument ;
stmt_declare_struct
: type_struct '{' stmt_define '}' ';' ;
stmt_define_func
: type IDENTIFIER '(' argument ')' '{' stmt '}' ;
stmt_define
: type IDENTIFIER ';'
| type IDENTIFIER '[' LITERAL ']' ';' ;
stmt_declare_initialize
: type IDENTIFIER '=' expr ';' ;