Debian Codegraph @

src/core/chuck_absyn.cpp

raw
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_absyn.cpp
28 // desc: chuck abstract syntax
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // date: Autumn 2002
32 //-----------------------------------------------------------------------------
33 #include "chuck_absyn.h"
34 #include "chuck_errmsg.h"
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <string> // 1.5.1.5 for string concat
39
40
41 // 1.5.0.5 (ge) option to include in case we need something from flex/bison
42 // #include "chuck_yacc.h"
43 // ASSUME: on systems where the lexer/parser is generated using flex/bison,
44 // that chuck_yacc.h is the same as chuck.tab.h
45
46
47 //-----------------------------------------------------------------------------
48 // AST construction function (these are mostly called by bison)
49 //-----------------------------------------------------------------------------
50 a_Program new_program( a_Section section, uint32_t lineNum, uint32_t posNum )
51 {
52 // NB checked_malloc() zeros the allocated memory
53 a_Program a = (a_Program)checked_malloc( sizeof( struct a_Program_ ) );
54 a->section = section;
55 a->line = lineNum; a->where = posNum;
56
57 return a;
58 }
59
60 a_Program append_program( a_Program program, a_Section section, uint32_t lineNum, uint32_t posNum )
61 {
62 a_Program a = new_program( section, lineNum, posNum );
63 a_Program current = program->next;
64
65 // initial state
66 if( current == NULL )
67 {
68 program->next = a;
69 return program;
70 }
71
72 // go to the end
73 while( current->next ) current = current->next;
74 // append
75 current->next = a;
76
77 return program;
78 }
79
80 a_Section new_section_stmt( a_Stmt_List list, uint32_t lineNum, uint32_t posNum )
81 {
82 a_Section a = (a_Section)checked_malloc( sizeof( struct a_Section_ ) );
83 a->s_type = ae_section_stmt;
84 a->stmt_list = list;
85 a->line = lineNum; a->where = posNum;
86
87 return a;
88 }
89
90 a_Section new_section_func_def( a_Func_Def func_def, uint32_t lineNum, uint32_t posNum )
91 {
92 a_Section a = (a_Section)checked_malloc( sizeof( struct a_Section_) );
93 a->s_type = ae_section_func;
94 a->func_def = func_def;
95 a->line = lineNum; a->where = posNum;
96
97 return a;
98 }
99
100 a_Section new_section_class_def( a_Class_Def class_def, uint32_t lineNum, uint32_t posNum )
101 {
102 a_Section a = (a_Section)checked_malloc( sizeof( struct a_Section_) );
103 a->s_type = ae_section_class;
104 a->class_def = class_def;
105 a->line = lineNum; a->where = posNum;
106
107 return a;
108 }
109
110 a_Stmt_List new_stmt_list( a_Stmt stmt, uint32_t lineNum, uint32_t posNum )
111 {
112 a_Stmt_List a = (a_Stmt_List)checked_malloc( sizeof( struct a_Stmt_List_ ) );
113 a->stmt = stmt;
114 a->next = NULL;
115 a->line = lineNum; a->where = posNum;
116
117 return a;
118 }
119
120 a_Stmt_List append_stmt_list( a_Stmt_List stmt_list, a_Stmt stmt, uint32_t lineNum, uint32_t posNum )
121 {
122 a_Stmt_List a = new_stmt_list( stmt, lineNum, posNum );
123 a_Stmt_List current;
124
125 current = stmt_list->next;
126 if (current == NULL) {
127 stmt_list->next = a;
128 return stmt_list;
129 }
130
131 while (1)
132 {
133 if (current->next == NULL) {
134 current->next = a;
135 break;
136 } else {
137 current = current->next;
138 }
139 }
140 return stmt_list;
141 }
142
143 a_Stmt new_stmt_from_expression( a_Exp exp, uint32_t lineNum, uint32_t posNum )
144 {
145 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
146 a->s_type = ae_stmt_exp;
147 a->stmt_exp = exp;
148 a->line = lineNum; a->where = posNum;
149
150 return a;
151 }
152
153 a_Stmt new_stmt_from_code( a_Stmt_List stmt_list, uint32_t lineNum, uint32_t posNum )
154 {
155 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
156 a->s_type = ae_stmt_code;
157 a->stmt_code.stmt_list = stmt_list;
158 a->line = lineNum; a->where = posNum;
159 a->stmt_code.line = lineNum; a->stmt_code.where = posNum;
160 a->stmt_code.self = a;
161
162 return a;
163 }
164
165 a_Stmt new_stmt_from_if( a_Exp cond, a_Stmt if_body, a_Stmt else_body, uint32_t lineNum, uint32_t posNum )
166 {
167 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
168 a->s_type = ae_stmt_if;
169 a->stmt_if.cond = cond;
170 a->stmt_if.if_body = if_body;
171 a->stmt_if.else_body = else_body;
172 a->line = lineNum; a->where = posNum;
173 a->stmt_if.line = lineNum; a->stmt_if.where = posNum;
174 a->stmt_if.self = a;
175
176 return a;
177 }
178
179 a_Stmt new_stmt_from_while( a_Exp cond, a_Stmt body, uint32_t lineNum, uint32_t posNum )
180 {
181 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
182 a->s_type = ae_stmt_while;
183 a->stmt_while.is_do = 0;
184 a->stmt_while.cond = cond;
185 a->stmt_while.body = body;
186 a->line = lineNum; a->where = posNum;
187 a->stmt_while.line = lineNum; a->stmt_while.where = posNum;
188 a->stmt_while.self = a;
189
190 return a;
191 }
192
193 a_Stmt new_stmt_from_do_while( a_Exp cond, a_Stmt body, uint32_t lineNum, uint32_t posNum )
194 {
195 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
196 a->s_type = ae_stmt_while;
197 a->stmt_while.is_do = 1;
198 a->stmt_while.cond = cond;
199 a->stmt_while.body = body;
200 a->line = lineNum; a->where = posNum;
201 a->stmt_while.line = lineNum; a->stmt_while.where = posNum;
202 a->stmt_while.self = a;
203
204 return a;
205 }
206
207 a_Stmt new_stmt_from_until( a_Exp cond, a_Stmt body, uint32_t lineNum, uint32_t posNum )
208 {
209 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
210 a->s_type = ae_stmt_until;
211 a->stmt_until.is_do = 0;
212 a->stmt_until.cond = cond;
213 a->stmt_until.body = body;
214 a->line = lineNum; a->where = posNum;
215 a->stmt_until.line = lineNum; a->stmt_until.where = posNum;
216 a->stmt_until.self = a;
217
218 return a;
219 }
220
221 a_Stmt new_stmt_from_do_until( a_Exp cond, a_Stmt body, uint32_t lineNum, uint32_t posNum )
222 {
223 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
224 a->s_type = ae_stmt_until;
225 a->stmt_until.is_do = 1;
226 a->stmt_until.cond = cond;
227 a->stmt_until.body = body;
228 a->line = lineNum; a->where = posNum;
229 a->stmt_until.line = lineNum; a->stmt_until.where = posNum;
230 a->stmt_until.self = a;
231
232 return a;
233 }
234
235 a_Stmt new_stmt_from_for( a_Stmt c1, a_Stmt c2, a_Exp c3, a_Stmt body, uint32_t lineNum, uint32_t posNum )
236 {
237 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
238 a->s_type = ae_stmt_for;
239 a->stmt_for.c1 = c1;
240 a->stmt_for.c2 = c2;
241 a->stmt_for.c3 = c3;
242 a->stmt_for.body = body;
243 a->line = lineNum; a->where = posNum;
244 a->stmt_for.line = lineNum; a->stmt_for.where = posNum;
245 a->stmt_for.self = a;
246
247 return a;
248 }
249
250 a_Stmt new_stmt_from_foreach( a_Exp iter, a_Exp array, a_Stmt body, uint32_t lineNum, uint32_t posNum )
251 {
252 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
253 a->s_type = ae_stmt_foreach;
254 a->stmt_foreach.theIter = iter;
255 a->stmt_foreach.theArray = array;
256 a->stmt_foreach.body = body;
257 a->line = lineNum; a->where = posNum;
258 a->stmt_foreach.line = lineNum; a->stmt_foreach.where = posNum;
259 a->stmt_foreach.self = a;
260
261 return a;
262 }
263
264 a_Stmt new_stmt_from_loop( a_Exp cond, a_Stmt body, uint32_t lineNum, uint32_t posNum )
265 {
266 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
267 a->s_type = ae_stmt_loop;
268 a->stmt_loop.cond = cond;
269 a->stmt_loop.body = body;
270 a->line = lineNum; a->where = posNum;
271 a->stmt_loop.line = lineNum; a->stmt_loop.where = posNum;
272 a->stmt_loop.self = a;
273
274 return a;
275 }
276
277 a_Stmt new_stmt_from_switch( a_Exp val, uint32_t lineNum, uint32_t posNum )
278 {
279 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
280 a->s_type = ae_stmt_switch;
281 a->stmt_switch.val = val;
282 a->line = lineNum; a->where = posNum;
283 a->stmt_switch.line = lineNum; a->stmt_switch.where = posNum;
284 a->stmt_switch.self = a;
285
286 return a;
287 }
288
289 a_Stmt new_stmt_from_break( uint32_t lineNum, uint32_t posNum )
290 {
291 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
292 a->s_type = ae_stmt_break;
293 a->line = lineNum; a->where = posNum;
294 a->stmt_break.line = lineNum; a->stmt_break.where = posNum;
295 a->stmt_break.self = a;
296
297 return a;
298 }
299
300 a_Stmt new_stmt_from_continue( uint32_t lineNum, uint32_t posNum )
301 {
302 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
303 a->s_type = ae_stmt_continue;
304 a->line = lineNum; a->where = posNum;
305 a->stmt_continue.line = lineNum; a->stmt_continue.where = posNum;
306 a->stmt_continue.self = a;
307
308 return a;
309 }
310
311 a_Stmt new_stmt_from_return( a_Exp exp, uint32_t lineNum, uint32_t posNum )
312 {
313 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
314 a->s_type = ae_stmt_return;
315 a->stmt_return.val = exp;
316 a->line = lineNum; a->where = posNum;
317 a->stmt_return.line = lineNum; a->stmt_return.where = posNum;
318 a->stmt_return.self = a;
319
320 return a;
321 }
322
323 a_Stmt new_stmt_from_label( c_str xid, uint32_t lineNum, uint32_t posNum )
324 {
325 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
326 a->s_type = ae_stmt_gotolabel;
327 a->stmt_gotolabel.name = insert_symbol( xid );
328 a->line = lineNum; a->where = posNum;
329 a->stmt_gotolabel.line = lineNum; a->stmt_gotolabel.where = posNum;
330 a->stmt_gotolabel.self = a;
331
332 return a;
333 }
334
335 a_Stmt new_stmt_from_case( a_Exp exp, uint32_t lineNum, uint32_t posNum )
336 {
337 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
338 a->s_type = ae_stmt_case;
339 a->stmt_case.exp = exp;
340 a->line = lineNum; a->where = posNum;
341 a->stmt_case.line = lineNum; a->stmt_case.where = posNum;
342 a->stmt_case.self = a;
343
344 return a;
345 }
346
347 a_Stmt new_stmt_from_import( a_Import list, uint32_t line, uint32_t where ) // 1.5.4.0 (ge) added
348 {
349 a_Stmt a = (a_Stmt)checked_malloc( sizeof(struct a_Stmt_) );
350 a->s_type = ae_stmt_import;
351 a->stmt_import.list = list;
352 a->line = line; a->where = where;
353 a->stmt_import.line = line; a->stmt_import.where = where;
354 a->stmt_import.self = a;
355
356 return a;
357 }
358
359 a_Stmt new_stmt_from_doc( a_Doc list, uint32_t line, uint32_t where ) // 1.5.4.4 (ge) added
360 {
361 a_Stmt a = (a_Stmt)checked_malloc( sizeof(struct a_Stmt_) );
362 a->s_type = ae_stmt_doc;
363 a->stmt_doc.list = list;
364 a->line = line; a->where = where;
365 a->stmt_doc.line = line; a->stmt_doc.where = where;
366 a->stmt_doc.self = a;
367
368 return a;
369 }
370
371 a_Stmt new_stmt_from_example( a_Example list, uint32_t line, uint32_t where ) // 1.5.5.8 (ge,nick,alex) added
372 {
373 a_Stmt a = (a_Stmt)checked_malloc( sizeof(struct a_Stmt_) );
374 a->s_type = ae_stmt_example;
375 a->stmt_example.list = list;
376 a->line = line; a->where = where;
377 a->stmt_example.line = line; a->stmt_example.where = where;
378 a->stmt_example.self = a;
379
380 return a;
381 }
382
383 a_Import new_import( c_str str, a_Id_List list, uint32_t line, uint32_t where ) // 1.5.4.0 (ge) added
384 {
385 a_Import a = (a_Import)checked_malloc( sizeof(struct a_Import_) );
386
387 // check which option
388 if( str )
389 {
390 a->what = str; // no strdup( str ); <-- str should have been allocated in alloc_str()
391 }
392 else // id list
393 {
394 a_Id_List curr = list;
395 std::string result = "";
396
397 // iterate over id list
398 while( curr )
399 {
400 result += S_name(curr->xid);
401 // if not the last, appent DOT
402 if( curr->next ) result += ".";
403 // set to next
404 curr = curr->next;
405 }
406
407 // sum of string lengths, +1 for null terminator
408 size_t len = result.length() + 1;
409 // allocate
410 char * sc = (char *)checked_malloc( len );
411 // copy
412 strncpy( sc, result.c_str(), len );
413 // set
414 a->what = sc;
415 // clean up id list, since we will not have references to it after this
416 delete_id_list( list );
417 }
418
419 // set line info
420 a->line = line; a->where = where;
421
422 return a;
423 }
424
425 a_Import prepend_import( a_Import target, a_Import list, uint32_t lineNum, uint32_t posNum )
426 {
427 target->next = list;
428 return target;
429 }
430
431 a_Doc new_doc( c_str str, uint32_t line, uint32_t where ) // 1.5.4.4 (ge) added
432 {
433 a_Doc a = (a_Doc)checked_malloc( sizeof(struct a_Doc_) );
434
435 // copy allocated string pointer
436 a->desc = str; // no strdup( str ); <-- str should have been allocated in alloc_str()
437
438 // set line info
439 a->line = line; a->where = where;
440
441 return a;
442 }
443
444 a_Example prepend_example( a_Example target, a_Example list, uint32_t lineNum, uint32_t posNum )
445 {
446 target->next = list;
447 return target;
448 }
449
450 a_Example new_example( c_str str, uint32_t line, uint32_t where ) // 1.5.4.4 (ge) added
451 {
452 a_Example a = (a_Example)checked_malloc( sizeof(struct a_Example_) );
453
454 // copy allocated string pointer
455 a->desc = str; // no strdup( str ); <-- str should have been allocated in alloc_str()
456
457 // set line info
458 a->line = line; a->where = where;
459
460 return a;
461 }
462
463 a_Doc prepend_doc( a_Doc target, a_Doc list, uint32_t lineNum, uint32_t posNum )
464 {
465 target->next = list;
466 return target;
467 }
468
469 a_Exp append_expression( a_Exp list, a_Exp exp, uint32_t lineNum, uint32_t posNum )
470 {
471 a_Exp current;
472 current = list->next;
473 if( current == NULL )
474 {
475 list->next = exp;
476 return list;
477 }
478
479 while( true )
480 {
481 if( current->next == NULL ) {
482 current->next = exp;
483 break;
484 }
485 else {
486 current = current->next;
487 }
488 }
489 return list;
490 }
491
492 a_Exp new_exp_from_binary( a_Exp lhs, ae_Operator oper, a_Exp rhs, uint32_t lineNum, uint32_t posNum )
493 {
494 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
495 a->s_type = ae_exp_binary;
496 a->s_meta = ae_meta_value;
497 a->binary.lhs = lhs;
498 a->binary.op = oper;
499 a->binary.rhs = rhs;
500 a->line = lineNum; a->where = posNum;
501 a->binary.line = lineNum; a->binary.where = posNum;
502 a->binary.self = a;
503
504 return a;
505 }
506
507 a_Exp new_exp_from_unary( ae_Operator oper, a_Exp exp, uint32_t lineNum, uint32_t posNum )
508 {
509 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
510 a->s_type = ae_exp_unary;
511 a->s_meta = exp->s_meta;
512 a->unary.op = oper;
513 a->unary.exp = exp;
514 a->line = lineNum; a->where = posNum;
515 a->unary.line = lineNum; a->unary.where = posNum;
516 a->unary.self = a;
517
518 return a;
519 }
520
521 a_Exp new_exp_from_unary2( ae_Operator oper, a_Type_Decl type,
522 int ctor_invoked, a_Exp ctor_args, a_Array_Sub array,
523 uint32_t lineNum, uint32_t posNum )
524 {
525 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
526 a->s_type = ae_exp_unary;
527 a->s_meta = ae_meta_value;
528 a->unary.op = oper;
529 a->unary.type = type;
530 a->unary.ctor.invoked = ctor_invoked;
531 a->unary.ctor.args = ctor_args;
532 a->unary.array = array;
533 a->line = lineNum; a->where = posNum;
534 a->unary.line = lineNum; a->unary.where = posNum;
535 a->unary.self = a;
536
537 return a;
538 }
539
540 a_Exp new_exp_from_unary3( ae_Operator oper, a_Stmt code, uint32_t lineNum, uint32_t posNum )
541 {
542 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
543 a->s_type = ae_exp_unary;
544 a->s_meta = ae_meta_value;
545 a->unary.op = oper;
546 a->unary.code = code;
547 a->line = lineNum; a->where = posNum;
548 a->unary.line = lineNum; a->unary.where = posNum;
549 a->unary.self = a;
550
551 return a;
552 }
553
554 a_Exp new_exp_from_cast( a_Type_Decl type, a_Exp exp, uint32_t lineNum, uint32_t posNum, uint32_t castPos )
555 {
556 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
557 a->s_type = ae_exp_cast;
558 a->s_meta = ae_meta_value;
559 a->cast.type = type;
560 a->cast.exp = exp;
561 a->line = lineNum; a->where = posNum;
562 a->cast.line = lineNum; a->cast.where = castPos;
563 a->cast.self = a;
564
565 return a;
566 }
567
568 a_Exp new_exp_from_array( a_Exp base, a_Array_Sub indices, uint32_t lineNum, uint32_t posNum )
569 {
570 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
571 a->s_type = ae_exp_array;
572 a->s_meta = ae_meta_var;
573 a->array.base = base;
574 a->array.indices = indices;
575 a->line = lineNum; a->where = posNum;
576 a->array.line = lineNum; a->array.where = posNum;
577 a->array.self = a;
578
579 return a;
580 }
581
582 a_Exp new_exp_from_func_call( a_Exp base, a_Exp args, uint32_t lineNum, uint32_t posNum )
583 {
584 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
585 a->s_type = ae_exp_func_call;
586 a->s_meta = ae_meta_value;
587 a->func_call.func = base;
588 a->func_call.args = args;
589 a->line = lineNum; a->where = posNum;
590 a->func_call.line = lineNum; a->func_call.where = posNum;
591 a->func_call.self = a;
592
593 return a;
594 }
595
596 a_Exp new_exp_from_member_dot( a_Exp base, c_str xid, uint32_t lineNum, uint32_t posNum, uint32_t memberPos )
597 {
598 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
599 a->s_type = ae_exp_dot_member;
600 a->s_meta = ae_meta_var;
601 a->dot_member.base = base;
602 a->dot_member.xid = insert_symbol( xid );
603 a->line = lineNum; a->where = posNum;
604 a->dot_member.line = lineNum; a->dot_member.where = memberPos;
605 a->dot_member.self = a;
606
607 return a;
608 }
609
610 a_Exp new_exp_from_postfix( a_Exp base, ae_Operator op, uint32_t lineNum, uint32_t posNum )
611 {
612 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
613 a->s_type = ae_exp_postfix;
614 a->s_meta = ae_meta_var;
615 a->postfix.exp = base;
616 a->postfix.op = op;
617 a->line = lineNum; a->where = posNum;
618 a->postfix.line = lineNum; a->postfix.where = posNum;
619 a->postfix.self = a;
620
621 return a;
622 }
623
624 a_Exp new_exp_from_dur( a_Exp base, a_Exp unit, uint32_t lineNum, uint32_t posNum )
625 {
626 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
627 a->s_type = ae_exp_dur;
628 a->s_meta = ae_meta_value;
629 a->dur.base = base;
630 a->dur.unit = unit;
631 a->line = lineNum; a->where = posNum;
632 a->dur.line = lineNum; a->dur.where = posNum;
633 a->dur.self = a;
634
635 return a;
636 }
637
638 a_Exp new_exp_from_id( c_str xid, uint32_t lineNum, uint32_t posNum )
639 {
640 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
641 a->s_type = ae_exp_primary;
642 a->s_meta = ae_meta_var;
643 a->primary.s_type = ae_primary_var;
644 a->primary.var = insert_symbol( xid );
645 a->line = lineNum; a->where = posNum;
646 a->primary.line = lineNum; a->primary.where = posNum;
647 a->primary.self = a;
648
649 return a;
650 }
651
652 a_Exp new_exp_from_int( t_CKINT num, uint32_t lineNum, uint32_t posNum )
653 {
654 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
655 a->s_type = ae_exp_primary;
656 a->s_meta = ae_meta_value;
657 a->primary.s_type = ae_primary_num;
658 a->primary.num = num;
659 a->line = lineNum; a->where = posNum;
660 a->primary.line = lineNum; a->primary.where = posNum;
661 a->primary.self = a;
662
663 return a;
664 }
665
666 a_Exp new_exp_from_float( t_CKFLOAT num, uint32_t lineNum, uint32_t posNum )
667 {
668 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
669 a->s_type = ae_exp_primary;
670 a->s_meta = ae_meta_value;
671 a->primary.s_type = ae_primary_float;
672 a->primary.fnum = num;
673 a->line = lineNum; a->where = posNum;
674 a->primary.line = lineNum; a->primary.where = posNum;
675 a->primary.self = a;
676
677 return a;
678 }
679
680 a_Exp new_exp_from_str( c_str str, uint32_t lineNum, uint32_t posNum )
681 {
682 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
683 a->s_type = ae_exp_primary;
684 a->s_meta = ae_meta_value;
685 a->primary.s_type = ae_primary_str;
686 a->primary.str = str; // no strdup( str ); <-- str should have been allocated in alloc_str()
687 a->line = lineNum; a->where = posNum;
688 a->primary.line = lineNum; a->primary.where = posNum;
689 a->primary.self = a;
690
691 return a;
692 }
693
694 a_Exp new_exp_from_char( c_str chr, uint32_t lineNum, uint32_t posNum )
695 {
696 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
697 a->s_type = ae_exp_primary;
698 a->s_meta = ae_meta_value;
699 a->primary.s_type = ae_primary_char;
700 a->primary.chr = chr; // no strdup( chr ); <-- chr should have been allocated in alloc_str()
701 a->line = lineNum; a->where = posNum;
702 a->primary.line = lineNum; a->primary.where = posNum;
703 a->primary.self = a;
704
705 return a;
706 }
707
708 a_Exp new_exp_from_array_lit( a_Array_Sub exp_list, uint32_t lineNum, uint32_t posNum )
709 {
710 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
711 a->s_type = ae_exp_primary;
712 a->s_meta = ae_meta_value;
713 a->primary.s_type = ae_primary_array;
714 a->primary.array = exp_list;
715 a->primary.array->self = a; // 1.5.2.0 (ge & nshaheed) added
716 a->line = lineNum; a->where = posNum;
717 a->primary.line = lineNum; a->primary.where = posNum;
718 a->primary.self = a;
719 return a;
720 }
721
722 a_Exp new_exp_from_if( a_Exp cond, a_Exp if_exp, a_Exp else_exp, uint32_t lineNum, uint32_t posNum )
723 {
724 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
725 a->s_type = ae_exp_if;
726 a->s_meta = ( ( if_exp->s_meta == ae_meta_var &&
727 else_exp->s_meta == ae_meta_var ) ? ae_meta_var : ae_meta_value );
728 a->exp_if.cond = cond;
729 a->exp_if.if_exp = if_exp;
730 a->exp_if.else_exp = else_exp;
731 a->line = lineNum; a->where = posNum;
732 a->exp_if.line = lineNum; a->exp_if.where = posNum;
733 a->exp_if.self = a;
734
735 return a;
736 }
737
738 a_Exp new_exp_decl_external( a_Type_Decl type, a_Var_Decl_List var_decl_list, int is_static, int is_const, uint32_t lineNum, uint32_t posNum )
739 {
740 a_Exp a = new_exp_decl( type, var_decl_list, is_static, is_const, lineNum, posNum );
741 a->decl.is_global = 1;
742 EM_error2( posNum, "'external' keyword is deprecated. please use 'global'" );
743
744 return a;
745 }
746
747 a_Exp new_exp_decl_global( a_Type_Decl type, a_Var_Decl_List var_decl_list, int is_static, int is_const, uint32_t lineNum, uint32_t posNum )
748 {
749 a_Exp a = new_exp_decl( type, var_decl_list, is_static, is_const, lineNum, posNum );
750 a->decl.is_global = 1;
751
752 return a;
753 }
754
755 a_Exp new_exp_decl( a_Type_Decl type, a_Var_Decl_List var_decl_list, int is_static, int is_const, uint32_t lineNum, uint32_t posNum )
756 {
757 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
758 a->s_type = ae_exp_decl;
759 a->s_meta = ae_meta_var;
760 a->decl.type = type;
761 a->decl.var_decl_list = var_decl_list;
762 a->line = lineNum; a->where = posNum;
763 a->decl.is_static = is_static;
764 a->decl.is_const = is_const; // 1.5.1.3
765 a->decl.line = lineNum; a->decl.where = posNum;
766 a->decl.self = a;
767
768 return a;
769 }
770
771 a_Exp new_exp_from_hack( a_Exp exp, uint32_t lineNum, uint32_t posNum )
772 {
773 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
774 a->s_type = ae_exp_primary;
775 a->s_meta = ae_meta_value;
776 a->primary.s_type = ae_primary_hack;
777 a->primary.exp = exp;
778 a->primary.line = lineNum; a->primary.where = posNum;
779 a->line = lineNum; a->where = posNum;
780 a->primary.self = a;
781
782 return a;
783 }
784
785 a_Exp new_exp_from_complex( a_Complex exp, uint32_t lineNum, uint32_t posNum )
786 {
787 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
788 a->s_type = ae_exp_primary;
789 a->s_meta = ae_meta_value;
790 a->primary.s_type = ae_primary_complex;
791 a->primary.complex = exp;
792 a->primary.line = lineNum; a->primary.where = posNum;
793 a->line = lineNum; a->where = posNum;
794 a->primary.complex->self = a;
795 a->primary.self = a;
796
797 return a;
798 }
799
800 a_Exp new_exp_from_polar( a_Polar exp, uint32_t lineNum, uint32_t posNum )
801 {
802 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
803 a->s_type = ae_exp_primary;
804 a->s_meta = ae_meta_value;
805 a->primary.s_type = ae_primary_polar;
806 a->primary.polar = exp;
807 a->primary.line = lineNum; a->primary.where = posNum;
808 a->line = lineNum; a->where = posNum;
809 a->primary.polar->self = a;
810 a->primary.self = a;
811
812 return a;
813 }
814
815 a_Exp new_exp_from_vec( a_Vec exp, uint32_t lineNum, uint32_t posNum ) // ge: added 1.3.5.3
816 {
817 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
818 a->s_type = ae_exp_primary;
819 a->s_meta = ae_meta_value;
820 a->primary.s_type = ae_primary_vec;
821 a->primary.vec = exp;
822 a->primary.line = lineNum; a->primary.where = posNum;
823 a->line = lineNum; a->where = posNum;
824 a->primary.vec->self = a;
825 a->primary.self = a;
826
827 return a;
828 }
829
830 a_Exp new_exp_from_nil( uint32_t lineNum, uint32_t posNum )
831 {
832 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
833 a->s_type = ae_exp_primary;
834 a->s_meta = ae_meta_value;
835 a->primary.s_type = ae_primary_nil;
836 a->primary.line = lineNum; a->primary.where = posNum;
837 a->line = lineNum; a->where = posNum;
838 a->primary.self = a;
839
840 return a;
841 }
842
843 a_Var_Decl new_var_decl( c_constr xid, int ctor_invoked, a_Exp ctor_args, a_Array_Sub array, uint32_t lineNum, uint32_t posNum )
844 {
845 a_Var_Decl a = (a_Var_Decl)checked_malloc( sizeof( struct a_Var_Decl_ ) );
846 a->xid = insert_symbol(xid);
847 a->ctor.invoked = ctor_invoked;
848 a->ctor.args = ctor_args;
849 a->array = array;
850 a->line = lineNum; a->where = posNum;
851
852 return a;
853 }
854
855 a_Var_Decl_List new_var_decl_list( a_Var_Decl var_decl, uint32_t lineNum, uint32_t posNum )
856 {
857 a_Var_Decl_List a = (a_Var_Decl_List)checked_malloc(
858 sizeof( struct a_Var_Decl_List_ ) );
859 a->var_decl = var_decl;
860 a->line = lineNum; a->where = posNum;
861
862 return a;
863 }
864
865 a_Var_Decl_List prepend_var_decl_list( a_Var_Decl var_decl, a_Var_Decl_List list, uint32_t lineNum, uint32_t posNum )
866 {
867 a_Var_Decl_List a = new_var_decl_list( var_decl, lineNum, posNum );
868 a->next = list;
869
870 return a;
871 }
872
873 a_Type_Decl new_type_decl( a_Id_List type, int ref, uint32_t lineNum, uint32_t posNum )
874 {
875 a_Type_Decl a = (a_Type_Decl)checked_malloc(
876 sizeof( struct a_Type_Decl_ ) );
877 a->xid = type;
878 a->ref = ref;
879 a->line = lineNum; a->where = posNum;
880
881 return a;
882 }
883
884 a_Type_Decl add_type_decl_array( a_Type_Decl a, a_Array_Sub array, uint32_t lineNum, uint32_t posNum )
885 {
886 assert( a->array == NULL );
887 a->array = array;
888
889 return a;
890 }
891
892 a_Arg_List new_arg_list( a_Type_Decl type_decl, a_Var_Decl var_decl, uint32_t lineNum, uint32_t posNum )
893 {
894 a_Arg_List a = (a_Arg_List)checked_malloc(
895 sizeof( struct a_Arg_List_ ) );
896 a->type_decl = type_decl;
897 a->var_decl = var_decl;
898 a->next = NULL;
899 a->line = lineNum; a->where = posNum;
900
901 return a;
902 }
903
904 a_Arg_List prepend_arg_list( a_Type_Decl type_decl, a_Var_Decl var_decl,
905 a_Arg_List arg_list, uint32_t lineNum, uint32_t posNum )
906 {
907 a_Arg_List a = new_arg_list( type_decl, var_decl, lineNum, posNum );
908 a->next = arg_list;
909 a->line = lineNum; a->where = posNum;
910
911 return a;
912 }
913
914 a_Func_Def new_func_def( ae_Keyword func_decl, ae_Keyword static_decl,
915 a_Type_Decl type_decl, c_str name,
916 a_Arg_List arg_list, a_Stmt code,
917 uint32_t is_from_ast, uint32_t lineNum, uint32_t posNum )
918 {
919 a_Func_Def a = (a_Func_Def)checked_malloc(
920 sizeof( struct a_Func_Def_ ) );
921 a->func_decl = func_decl;
922 a->static_decl = static_decl;
923 // substitute if NULL | 1.5.2.0 (ge) for constructors
924 a->type_decl = type_decl ? type_decl : new_type_decl(new_id_list("void",0,0),0,0,0);
925 a->name = insert_symbol( name );
926 a->arg_list = arg_list;
927 a->s_type = ae_func_user;
928 a->code = code;
929 a->ast_owned = is_from_ast != 0; // 1.5.0.5 (ge) added
930 a->line = lineNum; a->where = posNum;
931
932 return a;
933 }
934
935 // @operator overload | 1.5.1.5
936 a_Func_Def new_op_overload( ae_Keyword func_decl, ae_Keyword static_decl,
937 a_Type_Decl type_decl, ae_Operator oper,
938 a_Arg_List arg_list, a_Stmt code,
939 uint32_t is_from_ast, uint32_t overload_post,
940 uint32_t lineNum, uint32_t posNum, uint32_t operPos )
941 {
942 a_Func_Def a = (a_Func_Def)checked_malloc(
943 sizeof( struct a_Func_Def_ ) );
944 a->func_decl = func_decl;
945 a->static_decl = static_decl;
946 a->type_decl = type_decl;
947 // construct op overload function name
948 std::string s( "@operator" ); s += op2str(oper);
949 a->name = insert_symbol( s.c_str() );
950 a->op2overload = oper;
951 a->arg_list = arg_list;
952 a->s_type = ae_func_user;
953 a->code = code;
954 a->ast_owned = is_from_ast != 0; // 1.5.0.5 (ge) added
955 a->overload_post = overload_post; // 1.5.1.5 (ge) added
956 a->line = lineNum; a->where = posNum; a->operWhere = operPos;
957
958 return a;
959 }
960
961 a_Class_Def new_class_def( ae_Keyword class_decl, a_Id_List name,
962 a_Class_Ext ext, a_Class_Body body, uint32_t lineNum, uint32_t posNum )
963 {
964 a_Class_Def a = (a_Class_Def)checked_malloc( sizeof( struct a_Class_Def_ ) );
965 a->decl = class_decl;
966 a->name = name;
967 a->ext = ext;
968 a->body = body;
969 a->line = lineNum; a->where = posNum;
970
971 return a;
972 }
973
974 a_Class_Def new_iface_def( ae_Keyword class_decl, a_Id_List name,
975 a_Class_Ext ext, a_Class_Body body, uint32_t lineNum, uint32_t posNum )
976 {
977 a_Class_Def a = new_class_def( class_decl, name, ext, body, lineNum, posNum );
978 a->iface = 1;
979
980 return a;
981 }
982
983 a_Class_Body new_class_body( a_Section section, uint32_t lineNum, uint32_t posNum )
984 {
985 a_Class_Body a = (a_Class_Body)checked_malloc( sizeof( struct a_Class_Body_ ) );
986 a->section = section;
987 a->line = lineNum; a->where = posNum;
988
989 return a;
990 }
991
992 a_Class_Body prepend_class_body( a_Section section, a_Class_Body body, uint32_t lineNum, uint32_t posNum )
993 {
994 a_Class_Body a = new_class_body( section, lineNum, posNum );
995 a->next = body;
996 a->line = lineNum; a->where = posNum;
997
998 return a;
999 }
1000
1001 a_Class_Ext new_class_ext( a_Id_List extend_id, a_Id_List impl_list, uint32_t lineNum, uint32_t posNum )
1002 {
1003 a_Class_Ext a = (a_Class_Ext)checked_malloc( sizeof( struct a_Class_Ext_ ) );
1004 a->extend_id = extend_id;
1005 a->impl_list = impl_list;
1006 a->line = lineNum; a->where = posNum;
1007
1008 return a;
1009 }
1010
1011 a_Id_List new_id_list( c_constr xid, uint32_t lineNum, uint32_t posNum /*, YYLTYPE * loc*/ )
1012 {
1013 a_Id_List a = (a_Id_List)checked_malloc( sizeof( struct a_Id_List_ ) );
1014 a->xid = insert_symbol( xid );
1015 a->next = NULL;
1016 a->line = lineNum; a->where = posNum;
1017
1018 // debug print
1019 // if( loc ) fprintf( stderr, "ID: %s %d %d %d %d | pos = %d\n", xid, loc->first_line, loc->first_column, loc->last_line, loc->last_column, posNum );
1020
1021 return a;
1022 }
1023
1024 a_Id_List prepend_id_list( c_constr xid, a_Id_List list, uint32_t lineNum, uint32_t posNum /*, YYLTYPE * loc */ )
1025 {
1026 a_Id_List a = new_id_list( xid, lineNum, posNum /*, loc */ );
1027 a->next = list;
1028 a->line = lineNum; a->where = posNum;
1029
1030 return a;
1031 }
1032
1033 a_Array_Sub new_array_sub( a_Exp exp, uint32_t lineNum, uint32_t posNum )
1034 {
1035 a_Array_Sub a = (a_Array_Sub)checked_malloc( sizeof( struct a_Array_Sub_ ) );
1036 a->exp_list = exp;
1037 a->depth = 1;
1038 a->line = lineNum; a->where = posNum;
1039
1040 // make sure no multi
1041 /* if( exp && exp->next )
1042 {
1043 // primary exp?
1044 if( is_primary )
1045 {
1046 a->exp_list = NULL;
1047 a->exp_multi = exp;
1048 }
1049 else
1050 {
1051 a->err_num = 1; // multi
1052 a->err_pos = exp->linepos; // set error for type-checker
1053 goto error;
1054 }
1055 } */
1056
1057 return a;
1058
1059 /* error:
1060 clean_exp( exp );
1061 a->exp_list = NULL;
1062 return a; */
1063 }
1064
1065 a_Array_Sub prepend_array_sub( a_Array_Sub a, a_Exp exp, uint32_t lineNum, uint32_t posNum )
1066 {
1067 // if already error
1068 if( a->err_num ) goto error;
1069
1070 // make sure no multi
1071 if( exp && exp->next )
1072 {
1073 a->err_num = 1; // multi
1074 a->err_pos = exp->where; // set error for type-checker
1075 goto error;
1076 }
1077
1078 // empty or not
1079 if( (exp && !a->exp_list) || (!exp && a->exp_list) )
1080 {
1081 a->err_num = 2; // partial
1082 a->err_pos = posNum; // set error for type-checker
1083 goto error;
1084 }
1085
1086 // prepend
1087 if( exp )
1088 {
1089 exp->next = a->exp_list;
1090 a->exp_list = exp;
1091 }
1092
1093 // count
1094 a->depth++;
1095 return a;
1096
1097 error:
1098 delete_exp( exp );
1099 return a;
1100 }
1101
1102 a_Complex new_complex( a_Exp re, uint32_t lineNum, uint32_t posNum )
1103 {
1104 a_Complex a = (a_Complex)checked_malloc( sizeof( struct a_Complex_ ) );
1105 a->re = re;
1106 // NOTE: if this ever changes, make sure to also update delete_complex | 1.5.1.0
1107 if( re ) a->im = re->next;
1108 a->line = lineNum; a->where = posNum;
1109
1110 return a;
1111 }
1112
1113 a_Polar new_polar( a_Exp mod, uint32_t lineNum, uint32_t posNum )
1114 {
1115 a_Polar a = (a_Polar)checked_malloc( sizeof( struct a_Polar_ ) );
1116 a->mod = mod;
1117 // NOTE: if this ever changes, make sure to also update delete_polar | 1.5.1.0
1118 if( mod ) a->phase = mod->next;
1119 a->line = lineNum; a->where = posNum;
1120
1121 return a;
1122 }
1123
1124 a_Vec new_vec( a_Exp e, uint32_t lineNum, uint32_t posNum ) // ge: added 1.3.5.3
1125 {
1126 a_Vec a = (a_Vec)checked_malloc( sizeof( struct a_Vec_ ) );
1127 a->args = e;
1128 while( e ) // count number of dims
1129 {
1130 a->numdims++;
1131 e = e->next;
1132 }
1133 a->line = lineNum; a->where = posNum;
1134
1135 return a;
1136 }
1137
1138
1139
1140
1141 // operator strings
1142 static const char * op_str[] = {
1143 "[no_op]",
1144 "+",
1145 "-",
1146 "*",
1147 "/",
1148 "==",
1149 "!=",
1150 "<",
1151 "<=",
1152 ">",
1153 ">=",
1154 "&&",
1155 "||",
1156 "|",
1157 "&",
1158 "<<",
1159 ">>",
1160 "%",
1161 "^",
1162 "=>",
1163 "+=>",
1164 "-=>",
1165 "*=>",
1166 "/=>",
1167 "&=>",
1168 "|=>",
1169 "^=>",
1170 ">>=>",
1171 "<<=>",
1172 "%=>",
1173 "++",
1174 "--",
1175 "~",
1176 "!",
1177 "@=>",
1178 "=<",
1179 "=^",
1180 "=v",
1181 "=",
1182 "$",
1183 "@@",
1184 "::",
1185 "spork ~",
1186 "typeof",
1187 "sizeof",
1188 "new",
1189 "<-",
1190 "->",
1191 "<--",
1192 "-->",
1193 ">--",
1194 "--<"
1195 };
1196 //-----------------------------------------------------------------------------
1197 // name: op2str()
1198 // desc: operator enumeration to string
1199 //-----------------------------------------------------------------------------
1200 const char * op2str( ae_Operator op )
1201 {
1202 t_CKINT index = (t_CKINT)op;
1203 if( index < 0 || index >= ae_op_count )
1204 return "[non-existent operator]";
1205
1206 return op_str[index];
1207 }
1208
1209
1210
1211
1212 //-----------------------------------------------------------------------------
1213 // name: str2op()
1214 // desc: string to operator enumeration
1215 //-----------------------------------------------------------------------------
1216 ae_Operator str2op( const char * str )
1217 {
1218 std::string s(str);
1219 for( t_CKUINT i = 1; i < ae_op_count; i++ )
1220 if( s == op_str[i] ) return (ae_Operator)i;
1221
1222 return ae_op_none;
1223 }
1224
1225
1226
1227
1228 // function pointer kind names | 1.5.2.0
1229 static const char * fpkind_str[] = {
1230 "[unknown]",
1231 "ctor",
1232 "dtor",
1233 "mfun",
1234 "sfun",
1235 "gfun",
1236 "addr"
1237 };
1238 //-----------------------------------------------------------------------------
1239 // name: fpkind2str() | 1.5.2.0
1240 // desc: convert fp kind by enum to str
1241 //-----------------------------------------------------------------------------
1242 const char * fpkind2str( ae_FuncPointerKind kind )
1243 {
1244 t_CKINT index = (t_CKINT)kind;
1245 if( index < 0 || index >= ae_fp_count )
1246 return "[non-existent func kind]";
1247
1248 return fpkind_str[index];
1249 }
1250
1251
1252
1253
1254 //-----------------------------------------------------------------------------
1255 // AST cleanup function (these are mostly called by chuck compiler)
1256 //-----------------------------------------------------------------------------
1257 void delete_program( a_Program program )
1258 {
1259 // pointer
1260 a_Program next = NULL;
1261
1262 // log
1263 EM_log( CK_LOG_FINEST, "deleting AST root PROGRAM [%p]...", (void *)program );
1264
1265 // iterate linearly instead of recursing to avoid stack overflow
1266 while( program )
1267 {
1268 // delete the section in this program node
1269 delete_section( program->section );
1270 // get the next node before we delete this one
1271 next = program->next;
1272 // delete this one
1273 CK_SAFE_FREE( program );
1274 // move to the next one
1275 program = next;
1276 }
1277 }
1278
1279 void delete_section( a_Section section )
1280 {
1281 if( !section ) return;
1282 EM_log( CK_LOG_FINEST, "deleting section [%p]...", (void *)section );
1283
1284 // check section type
1285 switch( section->s_type )
1286 {
1287 case ae_section_stmt: delete_stmt_list( section->stmt_list ); break;
1288 case ae_section_class: delete_class_def( section->class_def ); break;
1289 case ae_section_func: delete_func_def( section->func_def ); break;
1290 }
1291 CK_SAFE_FREE( section );
1292 }
1293
1294 // delete stmt list
1295 void delete_stmt_list( a_Stmt_List list )
1296 {
1297 // log
1298 EM_log( CK_LOG_FINEST, "deleting stmt list [%p]...", (void *)list );
1299
1300 // pointers
1301 a_Stmt_List next = NULL;
1302
1303 // iterate without recursion to avoid stack overflow
1304 while( list )
1305 {
1306 // delete the stmt at this node
1307 delete_stmt( list->stmt );
1308 // get the next node before we delete this one
1309 next = list->next;
1310 // delete this one
1311 CK_SAFE_FREE( list );
1312 // move to the next
1313 list = next;
1314 }
1315 }
1316
1317 // delete class_def
1318 void delete_class_def( a_Class_Def def )
1319 {
1320 if( !def ) return;
1321
1322 // TODO: release reference count for def->type
1323 // TODO: release reference count for def->home
1324 // TODO: what to do about iface?
1325 EM_log( CK_LOG_FINEST, "deleting class def [%p]...", (void *)def );
1326
1327 delete_id_list( def->name );
1328 delete_class_ext( def->ext );
1329 delete_class_body( def->body );
1330 CK_SAFE_FREE( def );
1331 }
1332
1333 void delete_class_body( a_Class_Body body )
1334 {
1335 if( !body ) return;
1336 EM_log( CK_LOG_FINEST, "deleting class body [%p]...", (void *)body );
1337
1338 delete_class_body( body->next );
1339 delete_section( body->section );
1340 CK_SAFE_FREE( body );
1341 }
1342
1343 void delete_class_ext( a_Class_Ext ext )
1344 {
1345 if( !ext ) return;
1346 EM_log( CK_LOG_FINEST, "deleting class ext [%p]...", (void *)ext );
1347
1348 delete_id_list( ext->extend_id );
1349 delete_id_list( ext->impl_list );
1350 CK_SAFE_FREE( ext );
1351 }
1352
1353 // delete func def
1354 void delete_func_def( a_Func_Def def )
1355 {
1356 if( !def ) return;
1357
1358 // safety check
1359 if( def->ast_owned && def->vm_refs > 0 )
1360 {
1361 EM_error2( 0, "(internal error): cannot delete Func_Def %s with %ld VM references...", S_name(def->name), def->vm_refs );
1362 return;
1363 }
1364
1365 EM_log( CK_LOG_FINEST, "deleting func def [%p]...", (void *)def );
1366
1367 // TODO: release reference def->ret_type
1368 // TODO: do something with Symbol def->name?
1369 // TODO: release reference def->ck_func;
1370
1371 delete_type_decl( def->type_decl );
1372 delete_arg_list( def->arg_list );
1373 delete_stmt( def->code );
1374 CK_SAFE_FREE( def );
1375 }
1376
1377 void delete_iface_def( a_Class_Def def )
1378 {
1379 delete_class_def( def );
1380 }
1381
1382 // delete stmt
1383 void delete_stmt( a_Stmt stmt )
1384 {
1385 if( !stmt ) return;
1386 EM_log( CK_LOG_FINEST, "deleting stmt [%p]...", (void *)stmt );
1387
1388 // unlike a_Exp, stmt are part of a list and don't have a ->next
1389
1390 // check statement type
1391 switch( stmt->s_type )
1392 {
1393 case ae_stmt_exp:
1394 delete_exp( stmt->stmt_exp );
1395 break;
1396 case ae_stmt_while:
1397 delete_stmt_from_while( stmt );
1398 break;
1399 case ae_stmt_until:
1400 delete_stmt_from_until( stmt );
1401 break;
1402 case ae_stmt_for:
1403 delete_stmt_from_for( stmt );
1404 break;
1405 case ae_stmt_foreach:
1406 delete_stmt_from_foreach( stmt );
1407 break;
1408 case ae_stmt_loop:
1409 delete_stmt_from_loop( stmt );
1410 break;
1411 case ae_stmt_if:
1412 delete_stmt_from_if( stmt );
1413 break;
1414 case ae_stmt_code:
1415 delete_stmt_from_code( stmt );
1416 break;
1417 case ae_stmt_switch:
1418 delete_stmt_from_switch( stmt );
1419 break;
1420 case ae_stmt_break:
1421 delete_stmt_from_break( stmt );
1422 break;
1423 case ae_stmt_continue:
1424 delete_stmt_from_continue( stmt );
1425 break;
1426 case ae_stmt_return:
1427 delete_stmt_from_return( stmt );
1428 break;
1429 case ae_stmt_case:
1430 // TODO?
1431 break;
1432 case ae_stmt_gotolabel:
1433 delete_stmt_from_label( stmt );
1434 break;
1435 case ae_stmt_import:
1436 delete_stmt_from_import( stmt );
1437 break;
1438 case ae_stmt_doc: // 1.5.4.4 (ge) added
1439 delete_stmt_from_doc( stmt );
1440 break;
1441 case ae_stmt_example: // 1.5.5.8 (ge,nick,alex) added
1442 delete_stmt_from_example( stmt );
1443 break;
1444 }
1445
1446 CK_SAFE_FREE( stmt );
1447 }
1448
1449 void delete_stmt_from_code( a_Stmt stmt )
1450 {
1451 EM_log( CK_LOG_FINEST, "deleting stmt %p (code)...", (void *)stmt );
1452 delete_stmt_list( stmt->stmt_code.stmt_list );
1453 }
1454
1455 void delete_stmt_from_while( a_Stmt stmt )
1456 {
1457 EM_log( CK_LOG_FINEST, "deleting stmt %p (while)...", (void *)stmt );
1458 delete_exp( stmt->stmt_while.cond );
1459 delete_stmt( stmt->stmt_while.body );
1460 }
1461
1462 void delete_stmt_from_until( a_Stmt stmt )
1463 {
1464 EM_log( CK_LOG_FINEST, "deleting stmt %p (until)...", (void *)stmt );
1465 delete_stmt( stmt->stmt_until.body );
1466 }
1467
1468 void delete_stmt_from_for( a_Stmt stmt )
1469 {
1470 EM_log( CK_LOG_FINEST, "deleting stmt %p (for)...", (void *)stmt );
1471 delete_stmt( stmt->stmt_for.c1 );
1472 delete_stmt( stmt->stmt_for.c2 );
1473 delete_exp( stmt->stmt_for.c3 );
1474 delete_stmt( stmt->stmt_for.body );
1475 }
1476
1477 void delete_stmt_from_foreach( a_Stmt stmt )
1478 {
1479 EM_log( CK_LOG_FINEST, "deleting stmt %p (foreach)...", (void *)stmt );
1480 delete_exp( stmt->stmt_foreach.theIter );
1481 delete_exp( stmt->stmt_foreach.theArray );
1482 delete_stmt( stmt->stmt_foreach.body );
1483 }
1484
1485 void delete_stmt_from_loop( a_Stmt stmt )
1486 {
1487 EM_log( CK_LOG_FINEST, "deleting stmt %p (loop)...", (void *)stmt );
1488 delete_exp( stmt->stmt_loop.cond );
1489 delete_stmt( stmt->stmt_loop.body );
1490 }
1491
1492 void delete_stmt_from_if( a_Stmt stmt )
1493 {
1494 EM_log( CK_LOG_FINEST, "deleting stmt %p (if)...", (void *)stmt );
1495 delete_exp( stmt->stmt_if.cond );
1496 delete_stmt( stmt->stmt_if.if_body );
1497 delete_stmt( stmt->stmt_if.else_body );
1498 }
1499
1500 void delete_stmt_from_return( a_Stmt stmt )
1501 {
1502 EM_log( CK_LOG_FINEST, "deleting stmt %p (return)...", (void *)stmt );
1503 delete_exp( stmt->stmt_return.val );
1504 }
1505
1506 void delete_stmt_from_switch( a_Stmt stmt )
1507 {
1508 // TODO
1509 }
1510
1511 void delete_stmt_from_break( a_Stmt stmt )
1512 {
1513 // nothing to do
1514 }
1515
1516 void delete_stmt_from_continue( a_Stmt stmt )
1517 {
1518 // nothing to do
1519 }
1520
1521 void delete_stmt_from_label( a_Stmt stmt )
1522 {
1523 // TODO: someting with S_Symbol stmt->gotolabel.name
1524 }
1525
1526 void delete_stmt_from_import( a_Stmt stmt )
1527 {
1528 EM_log( CK_LOG_FINEST, "deleting stmt %p (import)...", (void *)stmt );
1529
1530 // pointer
1531 a_Import next = NULL, i = stmt->stmt_import.list;
1532
1533 // iterate instead of recurse to avoid stack overflow
1534 while( i )
1535 {
1536 // delete the content
1537 CK_SAFE_FREE( i->what );
1538 // get next before we delete this one
1539 next = i->next;
1540 // delete the import target
1541 CK_SAFE_FREE( i );
1542 // move to the next one
1543 i = next;
1544 }
1545 }
1546
1547 void delete_stmt_from_doc( a_Stmt stmt )
1548 {
1549 EM_log( CK_LOG_FINEST, "deleting stmt %p (doc)...", (void *)stmt );
1550
1551 // pointer
1552 a_Doc next = NULL, i = stmt->stmt_doc.list;
1553
1554 // iterate instead of recurse to avoid stack overflow
1555 while( i )
1556 {
1557 // delete the content
1558 CK_SAFE_FREE( i->desc );
1559 // get next before we delete this one
1560 next = i->next;
1561 // delete the import target
1562 CK_SAFE_FREE( i );
1563 // move to the next one
1564 i = next;
1565 }
1566 }
1567
1568 void delete_stmt_from_example( a_Stmt stmt )
1569 {
1570 EM_log( CK_LOG_FINEST, "deleting stmt %p (example)...", (void *)stmt );
1571
1572 // pointer
1573 a_Example next = NULL, i = stmt->stmt_example.list;
1574
1575 // iterate instead of recurse to avoid stack overflow
1576 while( i )
1577 {
1578 // delete the content
1579 CK_SAFE_FREE( i->desc );
1580 // get next before we delete this one
1581 next = i->next;
1582 // delete the import target
1583 CK_SAFE_FREE( i );
1584 // move to the next one
1585 i = next;
1586 }
1587 }
1588
1589 void delete_exp_from_primary( a_Exp_Primary_ & p )
1590 {
1591 EM_log( CK_LOG_FINEST, "deleting exp (primary)..." );
1592
1593 switch( p.s_type )
1594 {
1595 case ae_primary_str:
1596 delete_exp_from_str( &p );
1597 break;
1598 case ae_primary_char:
1599 delete_exp_from_char( &p );
1600 break;
1601 case ae_primary_var:
1602 delete_exp_from_id( &p );
1603 break;
1604 case ae_primary_array:
1605 delete_exp_from_array_lit( &p );
1606 break;
1607 case ae_primary_exp:
1608 delete_exp( p.exp );
1609 break;
1610 case ae_primary_complex:
1611 delete_exp_from_complex( &p );
1612 break;
1613 case ae_primary_polar:
1614 delete_exp_from_polar( &p );
1615 break;
1616 case ae_primary_vec:
1617 delete_exp_from_vec( &p );
1618 break;
1619 case ae_primary_hack:
1620 delete_exp_from_hack( &p );
1621 break;
1622
1623 // have nothing to delete
1624 case ae_primary_num:
1625 EM_log( CK_LOG_FINEST, "deleting exp (primary NUM) [%d]...", (long)p.num );
1626 break;
1627 case ae_primary_float:
1628 EM_log( CK_LOG_FINEST, "deleting exp (primary FLOAT) [%f]...", p.fnum );
1629 break;
1630 case ae_primary_nil:
1631 EM_log( CK_LOG_FINEST, "deleting exp (primary NIL)..." );
1632 break;
1633 }
1634 }
1635
1636 // delete an exp
1637 void delete_exp_contents( a_Exp e )
1638 {
1639 // chekc
1640 if( !e ) return;
1641 // log
1642 EM_log( CK_LOG_FINEST, "deleting exp [%p] [next: %p]...", (void *)e, (void *)(e->next) );
1643
1644 // check expression type
1645 switch( e->s_type )
1646 {
1647 case ae_exp_binary:
1648 delete_exp_from_binary( e );
1649 break;
1650 case ae_exp_unary:
1651 delete_exp_from_unary( e );
1652 break;
1653 case ae_exp_cast:
1654 delete_exp_from_cast( e );
1655 break;
1656 case ae_exp_postfix:
1657 delete_exp_from_postfix( e );
1658 break;
1659 case ae_exp_dur:
1660 delete_exp_from_dur( e );
1661 break;
1662 case ae_exp_primary:
1663 delete_exp_from_primary( e->primary );
1664 break;
1665 case ae_exp_array:
1666 delete_exp_from_array( e );
1667 break;
1668 case ae_exp_func_call:
1669 delete_exp_from_func_call( e );
1670 break;
1671 case ae_exp_dot_member:
1672 delete_exp_from_member_dot( e );
1673 break;
1674 case ae_exp_if:
1675 delete_exp_from_if( e );
1676 break;
1677 case ae_exp_decl:
1678 delete_exp_decl( e );
1679 break;
1680 }
1681 }
1682
1683 // delete an exp
1684 void delete_exp( a_Exp e )
1685 {
1686 // pointer
1687 a_Exp next = NULL;
1688
1689 // iterate instead of recurse to avoid stack overflow
1690 while( e )
1691 {
1692 // TODO: release reference type
1693 // TODO: release reference owner
1694 // 1.5.4.4 (ge) actually -- the above may not be necessary if:
1695 // 1) they are not ref-counted in the first place; e.g., see type_engine_check_exp() which assigned `type`
1696 // AND 2) the AST is cleaned up before types and nspcs etc.
1697
1698 // delete content in this exp
1699 delete_exp_contents( e );
1700 // get next exp before we delete this one
1701 next = e->next;
1702 // delete this one
1703 CK_SAFE_FREE( e );
1704 // move to the next one
1705 e = next;
1706 }
1707 }
1708
1709 void delete_exp_from_binary( a_Exp e )
1710 {
1711 // TODO: release reference ck_func
1712 EM_log( CK_LOG_FINEST, "deleting exp (binary) [%p]...", (void *)e );
1713
1714 delete_exp( e->binary.lhs );
1715 delete_exp( e->binary.rhs );
1716 }
1717
1718 void delete_exp_from_unary( a_Exp e )
1719 {
1720 EM_log( CK_LOG_FINEST, "deleting exp (unary) [%p]...", (void *)e );
1721
1722 delete_exp( e->unary.exp );
1723 delete_type_decl( e->unary.type );
1724 delete_array_sub( e->unary.array );
1725 }
1726
1727 void delete_exp_from_cast( a_Exp e )
1728 {
1729 EM_log( CK_LOG_FINEST, "deleting exp (cast) [%p]...", (void *)e );
1730
1731 delete_type_decl( e->cast.type );
1732 delete_exp( e->cast.exp );
1733 }
1734
1735 void delete_exp_from_array( a_Exp e )
1736 {
1737 EM_log( CK_LOG_FINEST, "deleting exp (arry) [%p]...", (void *)e );
1738
1739 delete_exp( e->array.base );
1740 delete_array_sub( e->array.indices );
1741 }
1742
1743 void delete_exp_from_func_call( a_Exp e )
1744 {
1745 // TODO: release reference ret_type
1746 // TODO: release reference ck_func
1747 // TODO: release reference ck_vm_code
1748 EM_log( CK_LOG_FINEST, "deleting exp (func call) [%p]...", (void *)e );
1749
1750 delete_exp( e->func_call.func );
1751 delete_exp( e->func_call.args );
1752 }
1753
1754 void delete_exp_from_member_dot( a_Exp e )
1755 {
1756 // TODO: release reference t_base
1757 // TODO: do something about Symbol xid?
1758 EM_log( CK_LOG_FINEST, "deleting exp (member access) [%p]...", (void *)e );
1759
1760 delete_exp( e->dot_member.base );
1761 }
1762
1763 void delete_exp_from_postfix( a_Exp e )
1764 {
1765 EM_log( CK_LOG_FINEST, "deleting exp (postfix) [%p]...", (void *)e );
1766 delete_exp( e->postfix.exp );
1767 }
1768
1769 void delete_exp_from_dur( a_Exp e )
1770 {
1771 EM_log( CK_LOG_FINEST, "deleting exp (dur) [%p]...", (void *)e );
1772
1773 delete_exp( e->dur.base );
1774 delete_exp( e->dur.unit );
1775 }
1776
1777 void delete_exp_from_if( a_Exp e )
1778 {
1779 EM_log( CK_LOG_FINEST, "deleting exp (if) [%p]...", (void *)e );
1780
1781 delete_exp( e->exp_if.cond );
1782 delete_exp( e->exp_if.if_exp );
1783 delete_exp( e->exp_if.else_exp );
1784 }
1785
1786 void delete_exp_decl( a_Exp e )
1787 {
1788 // TODO: release reference ck_type
1789 EM_log( CK_LOG_FINEST, "deleting exp (decl) [%p]...", (void *)e );
1790
1791 delete_type_decl( e->decl.type );
1792 delete_var_decl_list( e->decl.var_decl_list );
1793 }
1794
1795 void delete_exp_from_id( a_Exp_Primary e )
1796 {
1797 // TODO: do we need to anything with the Symbol?
1798 // 1.5.4.4 (ge) symbols are additive but also also unique per string thus growth bounded -- can keep around, in practice
1799 EM_log( CK_LOG_FINEST, "deleting exp (primary ID '%s') [%p]...", S_name(e->var), (void *)e );
1800
1801 // TODO: release reference func_alias #2024-ctor-this
1802 // 1.5.4.4 (ge) actually -- the above may not be necessary if:
1803 // 1) func_alisa not ref-counted in the first place; e.g., see xxx() which assigned `func_alias`
1804 // AND 2) the AST is cleaned up before the func etc.
1805 }
1806
1807 void delete_exp_from_str( a_Exp_Primary e )
1808 {
1809 EM_log( CK_LOG_FINEST, "deleting exp (primary str) [%p]...", (void *)e );
1810
1811 CK_SAFE_FREE( e->str );
1812 }
1813
1814 void delete_exp_from_char( a_Exp_Primary e )
1815 {
1816 EM_log( CK_LOG_FINEST, "deleting exp (primary char) [%p]...", (void *)e );
1817
1818 CK_SAFE_FREE( e->chr );
1819 }
1820
1821 void delete_exp_from_array_lit( a_Exp_Primary e )
1822 {
1823 EM_log( CK_LOG_FINEST, "deleting exp (primary array lit) [%p]...", (void *)e );
1824
1825 delete_array_sub( e->array );
1826 }
1827
1828 void delete_exp_from_complex( a_Exp_Primary e )
1829 {
1830 EM_log( CK_LOG_FINEST, "deleting exp (primary complex) [%p]...", (void *)e );
1831
1832 delete_complex( e->complex );
1833 }
1834
1835 void delete_exp_from_polar( a_Exp_Primary e )
1836 {
1837 EM_log( CK_LOG_FINEST, "deleting exp (primary polar) [%p]...", (void *)e );
1838
1839 delete_polar( e->polar );
1840 }
1841
1842 void delete_exp_from_vec( a_Exp_Primary e )
1843 {
1844 EM_log( CK_LOG_FINEST, "deleting exp (primary vec) [%p]...", (void *)e );
1845
1846 delete_exp( e->vec->args );
1847 }
1848
1849 void delete_exp_from_hack( a_Exp_Primary e )
1850 {
1851 EM_log( CK_LOG_FINEST, "deleting exp (primary <<< hack >>>) [%p]...", (void *)e );
1852
1853 delete_exp( e->exp );
1854 }
1855
1856 void delete_var_decl_list( a_Var_Decl_List list )
1857 {
1858 if( !list ) return;
1859 delete_var_decl_list( list->next );
1860 EM_log( CK_LOG_FINEST, "deleting var decl list [%p] [next: %p]...", (void *)list, (void *)(list->next) );
1861 delete_var_decl( list->var_decl );
1862 CK_SAFE_FREE( list );
1863 }
1864
1865 void delete_var_decl( a_Var_Decl decl )
1866 {
1867 if( !decl ) return;
1868 EM_log( CK_LOG_FINEST, "deleting var decl [%p]...", (void *)decl );
1869
1870 // TODO: do anything with Symbol xid?
1871 // TODO: release reference value
1872 // TODO: release reference ck_type
1873
1874 delete_array_sub( decl->array );
1875 CK_SAFE_FREE( decl );
1876 }
1877
1878 void delete_type_decl( a_Type_Decl decl )
1879 {
1880 if( !decl ) return;
1881 EM_log( CK_LOG_FINEST, "deleting type decl [%p]...", (void *)decl );
1882
1883 delete_id_list( decl->xid );
1884 delete_array_sub( decl->array );
1885 CK_SAFE_FREE( decl );
1886 }
1887
1888 void delete_arg_list( a_Arg_List list )
1889 {
1890 // TODO: release reference type
1891 if( !list ) return;
1892 delete_arg_list( list->next );
1893 EM_log( CK_LOG_FINEST, "deleting arg list [%p] [next: %p]...", (void *)list, (void *)(list->next) );
1894 delete_type_decl( list->type_decl );
1895 CK_SAFE_FREE( list );
1896 }
1897
1898 void delete_array_sub( a_Array_Sub sub )
1899 {
1900 if( !sub ) return;
1901 EM_log( CK_LOG_FINEST, "deleting type decl [%p]...", (void *)sub );
1902 delete_exp( sub->exp_list );
1903 CK_SAFE_FREE( sub );
1904 }
1905
1906 void delete_complex( a_Complex c )
1907 {
1908 EM_log( CK_LOG_FINEST, "deleting complex [%p]...", (void *)c );
1909 delete_exp( c->re );
1910 // do not delete c->im, since it's just c->re->next
1911 // delete_exp( c->im );
1912 CK_SAFE_FREE( c );
1913 }
1914
1915 void delete_polar( a_Polar p )
1916 {
1917 EM_log( CK_LOG_FINEST, "deleting polar [%p]...", (void *)p );
1918 delete_exp( p->mod );
1919 // do not delete p->phase, since it's just c->re->next
1920 // delete_exp( p->phase );
1921 CK_SAFE_FREE( p );
1922 }
1923
1924 void delete_vec( a_Vec v )
1925 {
1926 EM_log( CK_LOG_FINEST, "deleting vec [%p]...", (void *)v );
1927 delete_exp( v->args );
1928 CK_SAFE_FREE( v );
1929 }
1930
1931 void delete_id_list( a_Id_List list )
1932 {
1933 // pointer
1934 a_Id_List next = NULL;
1935
1936 // iterate instead of recursive to avoid stack overflow
1937 while( list )
1938 {
1939 // TODO: what to do about Symbol xid?
1940 // log
1941 EM_log( CK_LOG_FINEST, "deleting id list [%p] [next: %p]...", (void *)list );
1942 // get next before we delete this
1943 next = list->next;
1944 // delete this one
1945 CK_SAFE_FREE( list );
1946 // move to the next one
1947 list = next;
1948 }
1949 }