forked from countingpine/tinybasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_expr.bas
More file actions
377 lines (301 loc) · 7.91 KB
/
parse_expr.bas
File metadata and controls
377 lines (301 loc) · 7.91 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
'::::::::
declare function parse_expression _
( _
) as node_t ptr
'::::::::
function parse_atom_proccall _
( _
byval old_sym as sym_t ptr _
) as node_t ptr
dim as string ident
dim as sym_t ptr sym
dim as node_t ptr expr(0 to 15)
dim as integer expr_cnt
ident = lexer_stack.curr_lex->tk_str
read_token( )
sym = sym_find( ident, old_sym )
if match( TK_CHAR_LPAREN ) then
while (lexer_stack.curr_lex->tk_typ <> TK_CHAR_RPAREN) _
and (lexer_stack.curr_lex->tk_typ <> TK_EOF)
expr(expr_cnt) = parse_expression( )
expr_cnt += 1
if match( TK_CHAR_COMMA ) = 0 then
exit while
end if
wend
if match( TK_CHAR_RPAREN ) = 0 then
expected( "')'", lexer_stack.curr_lex->tk_str )
end if
function = tree_node_proccall( sym, @expr(0), expr_cnt )
else
expected( "'('", lexer_stack.curr_lex->tk_str )
end if
end function
'::::::::
function parse_atom_array_access _
( _
byval old_sym as sym_t ptr _
) as node_t ptr
dim as string ident
dim as sym_t ptr sym
dim as node_t ptr expr(0 to 15)
dim as integer expr_cnt
ident = lexer_stack.curr_lex->tk_str
read_token( )
sym = sym_find( ident, old_sym )
if match( TK_CHAR_LPAREN ) then
while (lexer_stack.curr_lex->tk_typ <> TK_CHAR_RPAREN) _
and (lexer_stack.curr_lex->tk_typ <> TK_EOF)
expr(expr_cnt) = parse_expression( )
expr_cnt += 1
if lexer_stack.curr_lex->tk_str = "," then
read_token( )
else
exit while
end if
wend
if match( TK_CHAR_RPAREN ) = 0 then
expected( "')'", lexer_stack.curr_lex->tk_str )
end if
function = tree_node_array_access( sym, @expr(0), expr_cnt )
else
expected( "'('", lexer_stack.curr_lex->tk_str )
end if
end function
'::::::::
function parse_atom_ptr_array_access _
( _
byval sym as sym_t ptr, _
byval old_sym as sym_t ptr _
) as node_t ptr
dim as node_t ptr expr(0 to 15)
dim as integer expr_cnt
if match( TK_CHAR_LSBRAC ) = 0 then
expected( "'['", lexer_stack.curr_lex->tk_str )
end if
while (lexer_stack.curr_lex->tk_typ <> TK_CHAR_RSBRAC) _
and (lexer_stack.curr_lex->tk_typ <> TK_EOF)
expr(expr_cnt) = parse_expression( )
expr_cnt += 1
if lexer_stack.curr_lex->tk_str = "," then
read_token( )
else
exit while
end if
wend
if match( TK_CHAR_RSBRAC ) = 0 then
expected( "']'", lexer_stack.curr_lex->tk_str )
end if
function = tree_node_ptr_array_access( sym, @expr(0), expr_cnt )
end function
'::::::::
function parse_atom _
( _
byval old_sym as sym_t ptr _
) as node_t ptr
if lexer_stack.curr_lex->tk_typ = TK_LITINT then
function = tree_node_litint( lexer_stack.curr_lex->tk_str )
read_token( )
elseif lexer_stack.curr_lex->tk_typ = TK_LITSTR then
function = tree_node_litstr( lexer_stack.curr_lex->tk_str )
read_token( )
elseif lexer_stack.curr_lex->tk_typ = TK_IDENT then
if old_sym then
if old_sym->typ = SYM_DATATYPE_FWD then
old_sym = sym_find( old_sym->_real, 0 )
end if
end if
dim as sym_t ptr sym = sym_find( lexer_stack.curr_lex->tk_str, old_sym )
dim as string ident
if sym = 0 then
' Label?
ident = lexer_stack.curr_lex->tk_str
read_token( )
if match( TK_CHAR_COLON ) then
sym = sym_add_label( ident )
return tree_node_label( sym )
end if
expected( "identifier", "'" & ident & "'" )
end if
if sym->is_proc then
function = parse_atom_proccall( old_sym )
elseif sym->is_array then
function = parse_atom_array_access( old_sym )
elseif (sym->typ = SYM_VAR) or (sym->typ = SYM_EXTERN_VAR) then
read_token( ) ' dump ident
if lexer_stack.curr_lex->tk_typ = TK_CHAR_LSBRAC then
return parse_atom_ptr_array_access( sym, old_sym )
end if
function = tree_node_var( sym )
elseif sym->typ = SYM_LABEL then
read_token( ) ' dump ident
function = tree_node_label( sym )
elseif sym->typ = SYM_DATATYPE then
read_token( ) ' dump ident
function = tree_node_type( sym )
elseif sym->typ = SYM_ENUM_MEMBER then
read_token( ) ' dump ident
function = tree_node_enum_val( sym )
else
die( "Unhandled expr" )
end if
elseif match( TK_CHAR_LPAREN ) then
function = parse_expression( )
if match( TK_CHAR_RPAREN ) = 0 then
expected( "')'", lexer_stack.curr_lex->tk_str )
end if
else
expected( "atom", lexer_stack.curr_lex->tk_str )
end if
end function
'::::::::
function parse_access _
( _
) as node_t ptr
dim as node_t ptr node
dim as node_t ptr r
node = parse_atom( 0 )
r = node
while (lexer_stack.curr_lex->tk_typ = TK_ARROW) _
or (lexer_stack.curr_lex->tk_typ = TK_CHAR_DOT)
dim as string bop = lexer_stack.curr_lex->tk_str
read_token( )
dim as sym_t ptr sym
if r->sym->dt->_dt = DT_TYPE then
sym = r->sym->dt->_sym
else
print "NOT A TYPE!!"
exit_( 1 )
end if
node = tree_node_bop( bop, node, parse_atom( sym ) )
node->dt = node->r->dt
r = node->r
wend
function = node
end function
'::::::::
function parse_addrof_deref _
( _
) as node_t ptr
dim as node_t ptr node
dim as string uop
if (lexer_stack.curr_lex->tk_typ = TK_CHAR_AT) or (lexer_stack.curr_lex->tk_typ = TK_CHAR_ASTERISK) then
uop = lexer_stack.curr_lex->tk_str
read_token( )
node = tree_node_uop( uop, parse_addrof_deref( ) )
else
node = parse_access( )
end if
function = node
end function
'::::::::
function parse_negate _
( _
) as node_t ptr
if match( TK_CHAR_MINUS ) then
function = tree_node_uop( "-", parse_negate( ) )
else
function = parse_addrof_deref( )
end if
end function
'::::::::
function parse_muldiv _
( _
) as node_t ptr
dim as node_t ptr node
node = parse_negate( )
while (lexer_stack.curr_lex->tk_typ = TK_CHAR_ASTERISK) _
or (lexer_stack.curr_lex->tk_str = "/")
dim as string bop = lexer_stack.curr_lex->tk_str
read_token( )
node = tree_node_bop( bop, node, parse_negate( ) )
wend
function = node
end function
'::::::::
function parse_addsub _
( _
) as node_t ptr
dim as node_t ptr node
node = parse_muldiv( )
while (lexer_stack.curr_lex->tk_typ = TK_CHAR_PLUS) _
or (lexer_stack.curr_lex->tk_typ = TK_CHAR_MINUS)
dim as string bop = lexer_stack.curr_lex->tk_str
read_token( )
node = tree_node_bop( bop, node, parse_muldiv( ) )
wend
function = node
end function
'::::::::
function parse_strconcat _
( _
) as node_t ptr
dim as node_t ptr node
node = parse_addsub( )
while lexer_stack.curr_lex->tk_str = "&"
dim as string bop = lexer_stack.curr_lex->tk_str
read_token( )
node = tree_node_bop( bop, node, parse_addsub( ) )
dim as datatype_t ptr dt = callocate( sizeof( datatype_t ) )
dt->_dt = DT_STRING
node->dt = *dt
if node->l->dt._dt <> DT_STRING then
node->l = tree_node_convert( node->l, dt )
end if
if node->r->dt._dt <> DT_STRING then
node->r = tree_node_convert( node->r, dt )
end if
free(dt)
wend
function = node
end function
'::::::::
function parse_rel _
( _
) as node_t ptr
dim as node_t ptr node
node = parse_strconcat( )
while (lexer_stack.curr_lex->tk_typ = TK_CHAR_EQ) _
or (lexer_stack.curr_lex->tk_typ = TK_CHAR_LT) _
or (lexer_stack.curr_lex->tk_typ = TK_CHAR_GT) _
or (lexer_stack.curr_lex->tk_typ = TK_NE) _
or (lexer_stack.curr_lex->tk_typ = TK_LE) _
or (lexer_stack.curr_lex->tk_typ = TK_GE)
dim as string bop = lexer_stack.curr_lex->tk_str
read_token( )
node = tree_node_bop( bop, node, parse_strconcat( ) )
wend
function = node
end function
'::::::::
function parse_and _
( _
) as node_t ptr
dim as node_t ptr node
node = parse_rel( )
while lexer_stack.curr_lex->tk_typ = TK_AND
dim as string bop = lexer_stack.curr_lex->tk_str
read_token( )
node = tree_node_bop( bop, node, parse_rel( ) )
wend
function = node
end function
'::::::::
function parse_or _
( _
) as node_t ptr
dim as node_t ptr node
node = parse_and( )
while lexer_stack.curr_lex->tk_typ = TK_OR
dim as string bop = lexer_stack.curr_lex->tk_str
read_token( )
node = tree_node_bop( bop, node, parse_and( ) )
wend
function = node
end function
'::::::::
function parse_expression _
( _
) as node_t ptr
function = parse_or( )
end function