*** empty log message ***
[chuck-blob.git] / v2 / chuck_absyn.cpp
blob295e6cb54fb6719ece82a963ca0ddaef12eed7ed
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_absyn.cpp
27 // desc: ...
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // date: Autumn 2002
32 //-----------------------------------------------------------------------------
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include "chuck_absyn.h"
36 #include "chuck_utils.h"
39 a_Program new_program( a_Section section, int pos )
41 a_Program a = (a_Program)checked_malloc( sizeof( struct a_Program_ ) );
42 a->section = section;
43 a->linepos = pos;
45 return a;
48 a_Program prepend_program( a_Section section, a_Program program, int pos )
50 a_Program a = new_program( section, pos );
51 a->next = program;
52 a->linepos = pos;
54 return a;
57 a_Section new_section_stmt( a_Stmt_List list, int pos )
59 a_Section a = (a_Section)checked_malloc( sizeof( struct a_Section_ ) );
60 a->s_type = ae_section_stmt;
61 a->stmt_list = list;
62 a->linepos = pos;
64 return a;
67 a_Section new_section_func_def( a_Func_Def func_def, int pos )
69 a_Section a = (a_Section)checked_malloc( sizeof( struct a_Section_) );
70 a->s_type = ae_section_func;
71 a->func_def = func_def;
72 a->linepos = pos;
74 return a;
77 a_Section new_section_class_def( a_Class_Def class_def, int pos )
79 a_Section a = (a_Section)checked_malloc( sizeof( struct a_Section_) );
80 a->s_type = ae_section_class;
81 a->class_def = class_def;
82 a->linepos = pos;
84 return a;
87 a_Stmt_List new_stmt_list( a_Stmt stmt, int pos )
89 a_Stmt_List a = (a_Stmt_List)checked_malloc( sizeof( struct a_Stmt_List_ ) );
90 a->stmt = stmt;
91 a->next = NULL;
92 a->linepos = pos;
94 return a;
97 a_Stmt_List prepend_stmt_list( a_Stmt stmt, a_Stmt_List stmt_list, int pos )
99 a_Stmt_List a = new_stmt_list( stmt, pos );
100 a->next = stmt_list;
101 a->linepos = pos;
103 return a;
106 a_Stmt new_stmt_from_expression( a_Exp exp, int pos )
108 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
109 a->s_type = ae_stmt_exp;
110 a->stmt_exp = exp;
111 a->linepos = pos;
113 return a;
116 a_Stmt new_stmt_from_code( a_Stmt_List stmt_list, int pos )
118 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
119 a->s_type = ae_stmt_code;
120 a->stmt_code.stmt_list = stmt_list;
121 a->linepos = pos;
122 a->stmt_code.linepos = pos;
123 a->stmt_code.self = a;
125 return a;
128 a_Stmt new_stmt_from_if( a_Exp cond, a_Stmt if_body, a_Stmt else_body, int pos )
130 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
131 a->s_type = ae_stmt_if;
132 a->stmt_if.cond = cond;
133 a->stmt_if.if_body = if_body;
134 a->stmt_if.else_body = else_body;
135 a->linepos = pos;
136 a->stmt_if.linepos = pos;
137 a->stmt_if.self = a;
139 return a;
142 a_Stmt new_stmt_from_while( a_Exp cond, a_Stmt body, int pos )
144 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
145 a->s_type = ae_stmt_while;
146 a->stmt_while.is_do = 0;
147 a->stmt_while.cond = cond;
148 a->stmt_while.body = body;
149 a->linepos = pos;
150 a->stmt_while.linepos = pos;
151 a->stmt_while.self = a;
153 return a;
156 a_Stmt new_stmt_from_do_while( a_Exp cond, a_Stmt body, int pos )
158 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
159 a->s_type = ae_stmt_while;
160 a->stmt_while.is_do = 1;
161 a->stmt_while.cond = cond;
162 a->stmt_while.body = body;
163 a->linepos = pos;
164 a->stmt_while.linepos = pos;
165 a->stmt_while.self = a;
167 return a;
170 a_Stmt new_stmt_from_until( a_Exp cond, a_Stmt body, int pos )
172 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
173 a->s_type = ae_stmt_until;
174 a->stmt_until.is_do = 0;
175 a->stmt_until.cond = cond;
176 a->stmt_until.body = body;
177 a->linepos = pos;
178 a->stmt_until.linepos = pos;
179 a->stmt_until.self = a;
181 return a;
184 a_Stmt new_stmt_from_do_until( a_Exp cond, a_Stmt body, int pos )
186 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
187 a->s_type = ae_stmt_until;
188 a->stmt_until.is_do = 1;
189 a->stmt_until.cond = cond;
190 a->stmt_until.body = body;
191 a->linepos = pos;
192 a->stmt_until.linepos = pos;
193 a->stmt_until.self = a;
195 return a;
198 a_Stmt new_stmt_from_for( a_Stmt c1, a_Stmt c2, a_Exp c3, a_Stmt body, int pos )
200 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
201 a->s_type = ae_stmt_for;
202 a->stmt_for.c1 = c1;
203 a->stmt_for.c2 = c2;
204 a->stmt_for.c3 = c3;
205 a->stmt_for.body = body;
206 a->linepos = pos;
207 a->stmt_for.linepos = pos;
208 a->stmt_for.self = a;
210 return a;
213 a_Stmt new_stmt_from_loop( a_Exp cond, a_Stmt body, int pos )
215 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
216 a->s_type = ae_stmt_loop;
217 a->stmt_loop.cond = cond;
218 a->stmt_loop.body = body;
219 a->linepos = pos;
220 a->stmt_loop.linepos = pos;
221 a->stmt_loop.self = a;
223 return a;
226 a_Stmt new_stmt_from_switch( a_Exp val, int pos )
228 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
229 a->s_type = ae_stmt_switch;
230 a->stmt_switch.val = val;
231 a->linepos = pos;
232 a->stmt_switch.linepos = pos;
233 a->stmt_switch.self = a;
235 return a;
238 a_Stmt new_stmt_from_break( int pos )
240 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
241 a->s_type = ae_stmt_break;
242 a->linepos = pos;
243 a->stmt_break.linepos = pos;
244 a->stmt_break.self = a;
246 return a;
249 a_Stmt new_stmt_from_continue( int pos )
251 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
252 a->s_type = ae_stmt_continue;
253 a->linepos = pos;
254 a->stmt_continue.linepos = pos;
255 a->stmt_continue.self = a;
257 return a;
260 a_Stmt new_stmt_from_return( a_Exp exp, int pos )
262 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
263 a->s_type = ae_stmt_return;
264 a->stmt_return.val = exp;
265 a->linepos = pos;
266 a->stmt_return.linepos = pos;
267 a->stmt_return.self = a;
269 return a;
272 a_Stmt new_stmt_from_label( c_str xid, int pos )
274 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
275 a->s_type = ae_stmt_gotolabel;
276 a->stmt_gotolabel.name = insert_symbol( xid );
277 a->linepos = pos;
278 a->stmt_gotolabel.linepos = pos;
279 a->stmt_gotolabel.self = a;
281 return a;
284 a_Stmt new_stmt_from_case( a_Exp exp, int pos )
286 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
287 a->s_type = ae_stmt_case;
288 a->stmt_case.exp = exp;
289 a->linepos = pos;
290 a->stmt_case.linepos = pos;
291 a->stmt_case.self = a;
293 return a;
296 a_Exp prepend_expression( a_Exp exp, a_Exp list, int pos )
298 exp->next = list;
299 return exp;
302 a_Exp new_exp_from_binary( a_Exp lhs, ae_Operator oper, a_Exp rhs, int pos )
304 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
305 a->s_type = ae_exp_binary;
306 a->s_meta = ae_meta_value;
307 a->binary.lhs = lhs;
308 a->binary.op = oper;
309 a->binary.rhs = rhs;
310 a->linepos = pos;
311 a->binary.linepos = pos;
312 a->binary.self = a;
314 return a;
317 a_Exp new_exp_from_unary( ae_Operator oper, a_Exp exp, int pos )
319 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
320 a->s_type = ae_exp_unary;
321 a->s_meta = exp->s_meta;
322 a->unary.op = oper;
323 a->unary.exp = exp;
324 a->linepos = pos;
325 a->unary.linepos = pos;
326 a->unary.self = a;
328 return a;
331 a_Exp new_exp_from_unary2( ae_Operator oper, a_Type_Decl type,
332 a_Array_Sub array, int pos )
334 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
335 a->s_type = ae_exp_unary;
336 a->s_meta = ae_meta_value;
337 a->unary.op = oper;
338 a->unary.type = type;
339 a->unary.array = array;
340 a->linepos = pos;
341 a->unary.linepos = pos;
342 a->unary.self = a;
344 return a;
347 a_Exp new_exp_from_unary3( ae_Operator oper, a_Stmt code, int pos )
349 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
350 a->s_type = ae_exp_unary;
351 a->s_meta = ae_meta_value;
352 a->unary.op = oper;
353 a->unary.code = code;
354 a->linepos = pos;
355 a->unary.linepos = pos;
356 a->unary.self = a;
358 return a;
361 a_Exp new_exp_from_cast( a_Type_Decl type, a_Exp exp, int pos )
363 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
364 a->s_type = ae_exp_cast;
365 a->s_meta = ae_meta_value;
366 a->cast.type = type;
367 a->cast.exp = exp;
368 a->linepos = pos;
369 a->cast.linepos = pos;
370 a->cast.self = a;
372 return a;
375 a_Exp new_exp_from_array( a_Exp base, a_Array_Sub indices, int pos )
377 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
378 a->s_type = ae_exp_array;
379 a->s_meta = ae_meta_var;
380 a->array.base = base;
381 a->array.indices = indices;
382 a->linepos = pos;
383 a->array.linepos = pos;
384 a->array.self = a;
386 return a;
389 a_Exp new_exp_from_func_call( a_Exp base, a_Exp args, int pos )
391 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
392 a->s_type = ae_exp_func_call;
393 a->s_meta = ae_meta_value;
394 a->func_call.func = base;
395 a->func_call.args = args;
396 a->linepos = pos;
397 a->func_call.linepos = pos;
398 a->func_call.self = a;
400 return a;
403 a_Exp new_exp_from_member_dot( a_Exp base, c_str xid, int pos )
405 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
406 a->s_type = ae_exp_dot_member;
407 a->s_meta = ae_meta_var;
408 a->dot_member.base = base;
409 a->dot_member.xid = insert_symbol( xid );
410 a->linepos = pos;
411 a->dot_member.linepos = pos;
412 a->dot_member.self = a;
414 return a;
417 a_Exp new_exp_from_postfix( a_Exp base, ae_Operator op, int pos )
419 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
420 a->s_type = ae_exp_postfix;
421 a->s_meta = ae_meta_var;
422 a->postfix.exp = base;
423 a->postfix.op = op;
424 a->linepos = pos;
425 a->postfix.linepos = pos;
426 a->postfix.self = a;
428 return a;
431 a_Exp new_exp_from_dur( a_Exp base, a_Exp unit, int pos )
433 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
434 a->s_type = ae_exp_dur;
435 a->s_meta = ae_meta_value;
436 a->dur.base = base;
437 a->dur.unit = unit;
438 a->linepos = pos;
439 a->dur.linepos = pos;
440 a->dur.self = a;
442 return a;
445 a_Exp new_exp_from_id( c_str xid, int pos )
447 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
448 a->s_type = ae_exp_primary;
449 a->s_meta = ae_meta_var;
450 a->primary.s_type = ae_primary_var;
451 a->primary.var = insert_symbol( xid );
452 a->linepos = pos;
453 a->primary.linepos = pos;
454 a->primary.self = a;
456 return a;
459 a_Exp new_exp_from_int( long num, int pos )
461 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
462 a->s_type = ae_exp_primary;
463 a->s_meta = ae_meta_value;
464 a->primary.s_type = ae_primary_num;
465 a->primary.num = num;
466 a->linepos = pos;
467 a->primary.linepos = pos;
468 a->primary.self = a;
470 return a;
473 a_Exp new_exp_from_float( double num, int pos )
475 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
476 a->s_type = ae_exp_primary;
477 a->s_meta = ae_meta_value;
478 a->primary.s_type = ae_primary_float;
479 a->primary.fnum = num;
480 a->linepos = pos;
481 a->primary.linepos = pos;
482 a->primary.self = a;
484 return a;
487 a_Exp new_exp_from_str( c_str str, int pos )
489 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
490 a->s_type = ae_exp_primary;
491 a->s_meta = ae_meta_value;
492 a->primary.s_type = ae_primary_str;
493 a->primary.str = str;
494 a->linepos = pos;
495 a->primary.linepos = pos;
496 a->primary.self = a;
498 return a;
501 a_Exp new_exp_from_array_lit( a_Array_Sub exp_list, int pos )
503 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
504 a->s_type = ae_exp_primary;
505 a->s_meta = ae_meta_value;
506 a->primary.s_type = ae_primary_array;
507 a->primary.array = exp_list;
508 a->linepos = pos;
509 a->primary.linepos = pos;
510 a->primary.self = a;
512 return a;
515 a_Exp new_exp_from_if( a_Exp cond, a_Exp if_exp, a_Exp else_exp, int pos )
517 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
518 a->s_type = ae_exp_if;
519 a->s_meta = ( ( if_exp->s_meta == ae_meta_var &&
520 else_exp->s_meta == ae_meta_var ) ? ae_meta_var : ae_meta_value );
521 a->exp_if.cond = cond;
522 a->exp_if.if_exp = if_exp;
523 a->exp_if.else_exp = else_exp;
524 a->linepos = pos;
525 a->exp_if.linepos = pos;
526 a->exp_if.self = a;
528 return a;
531 a_Exp new_exp_decl( a_Type_Decl type, a_Var_Decl_List var_decl_list, int is_static, int pos )
533 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
534 a->s_type = ae_exp_decl;
535 a->s_meta = ae_meta_var;
536 a->decl.type = type;
537 a->decl.var_decl_list = var_decl_list;
538 a->linepos = pos;
539 a->decl.is_static = is_static;
540 a->decl.linepos = pos;
541 a->decl.self = a;
543 return a;
546 a_Exp new_exp_from_hack( a_Exp exp, int pos )
548 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
549 a->s_type = ae_exp_primary;
550 a->s_meta = ae_meta_value;
551 a->primary.s_type = ae_primary_hack;
552 a->primary.exp = exp;
553 a->primary.linepos = pos;
554 a->linepos = pos;
555 a->primary.self = a;
557 return a;
560 a_Exp new_exp_from_complex( a_Complex exp, int pos )
562 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
563 a->s_type = ae_exp_primary;
564 a->s_meta = ae_meta_value;
565 a->primary.s_type = ae_primary_complex;
566 a->primary.complex = exp;
567 a->primary.linepos = pos;
568 a->linepos = pos;
569 a->primary.complex->self = a;
570 a->primary.self = a;
572 return a;
575 a_Exp new_exp_from_polar( a_Polar exp, int pos )
577 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
578 a->s_type = ae_exp_primary;
579 a->s_meta = ae_meta_value;
580 a->primary.s_type = ae_primary_polar;
581 a->primary.polar = exp;
582 a->primary.linepos = pos;
583 a->linepos = pos;
584 a->primary.polar->self = a;
585 a->primary.self = a;
587 return a;
590 a_Exp new_exp_from_nil( int pos )
592 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
593 a->s_type = ae_exp_primary;
594 a->s_meta = ae_meta_value;
595 a->primary.s_type = ae_primary_nil;
596 a->primary.linepos = pos;
597 a->linepos = pos;
598 a->primary.self = a;
600 return a;
603 a_Var_Decl new_var_decl( c_constr xid, a_Array_Sub array, int pos )
605 a_Var_Decl a = (a_Var_Decl)checked_malloc( sizeof( struct a_Var_Decl_ ) );
606 a->xid = insert_symbol(xid);
607 a->array = array;
608 a->linepos = pos;
610 return a;
613 a_Var_Decl_List new_var_decl_list( a_Var_Decl var_decl, int pos )
615 a_Var_Decl_List a = (a_Var_Decl_List)checked_malloc(
616 sizeof( struct a_Var_Decl_List_ ) );
617 a->var_decl = var_decl;
618 a->linepos = pos;
620 return a;
623 a_Var_Decl_List prepend_var_decl_list( a_Var_Decl var_decl, a_Var_Decl_List list, int pos )
625 a_Var_Decl_List a = new_var_decl_list( var_decl, pos );
626 a->next = list;
628 return a;
631 a_Type_Decl new_type_decl( a_Id_List type, int ref, int pos )
633 a_Type_Decl a = (a_Type_Decl)checked_malloc(
634 sizeof( struct a_Type_Decl_ ) );
635 a->xid = type;
636 a->ref = ref;
637 a->linepos = pos;
639 return a;
642 a_Type_Decl add_type_decl_array( a_Type_Decl a, a_Array_Sub array, int pos )
644 assert( a->array == NULL );
645 a->array = array;
647 return a;
650 a_Arg_List new_arg_list( a_Type_Decl type_decl, a_Var_Decl var_decl, int pos )
652 a_Arg_List a = (a_Arg_List)checked_malloc(
653 sizeof( struct a_Arg_List_ ) );
654 a->type_decl = type_decl;
655 a->var_decl = var_decl;
656 a->next = NULL;
657 a->linepos = pos;
659 return a;
662 a_Arg_List prepend_arg_list( a_Type_Decl type_decl, a_Var_Decl var_decl,
663 a_Arg_List arg_list, int pos )
665 a_Arg_List a = new_arg_list( type_decl, var_decl, pos );
666 a->next = arg_list;
667 a->linepos = pos;
669 return a;
672 a_Func_Def new_func_def( ae_Keyword func_decl, ae_Keyword static_decl,
673 a_Type_Decl type_decl, c_str name,
674 a_Arg_List arg_list, a_Stmt code, int pos )
676 a_Func_Def a = (a_Func_Def)checked_malloc(
677 sizeof( struct a_Func_Def_ ) );
678 a->func_decl = func_decl;
679 a->static_decl = static_decl;
680 a->type_decl = type_decl;
681 a->name = insert_symbol( name );
682 a->arg_list = arg_list;
683 a->s_type = ae_func_user;
684 a->code = code;
685 a->linepos = pos;
687 return a;
690 a_Class_Def new_class_def( ae_Keyword class_decl, a_Id_List name,
691 a_Class_Ext ext, a_Class_Body body, int pos )
693 a_Class_Def a = (a_Class_Def)checked_malloc( sizeof( struct a_Class_Def_ ) );
694 a->decl = class_decl;
695 a->name = name;
696 a->ext = ext;
697 a->body = body;
698 a->linepos = pos;
700 return a;
703 a_Class_Def new_iface_def( ae_Keyword class_decl, a_Id_List name,
704 a_Class_Ext ext, a_Class_Body body, int pos )
706 a_Class_Def a = new_class_def( class_decl, name, ext, body, pos );
707 a->iface = 1;
709 return a;
712 a_Class_Body new_class_body( a_Section section, int pos )
714 a_Class_Body a = (a_Class_Body)checked_malloc( sizeof( struct a_Class_Body_ ) );
715 a->section = section;
716 a->linepos = pos;
718 return a;
721 a_Class_Body prepend_class_body( a_Section section, a_Class_Body body, int pos )
723 a_Class_Body a = new_class_body( section, pos );
724 a->next = body;
725 a->linepos = pos;
727 return a;
730 a_Class_Ext new_class_ext( a_Id_List extend_id, a_Id_List impl_list, int pos )
732 a_Class_Ext a = (a_Class_Ext)checked_malloc( sizeof( struct a_Class_Ext_ ) );
733 a->extend_id = extend_id;
734 a->impl_list = impl_list;
735 a->linepos = pos;
737 return a;
740 a_Id_List new_id_list( c_str xid, int pos )
742 a_Id_List a = (a_Id_List)checked_malloc( sizeof( struct a_Id_List_ ) );
743 a->xid = insert_symbol( xid );
744 a->next = NULL;
745 a->linepos = pos;
747 return a;
750 a_Id_List prepend_id_list( c_str xid, a_Id_List list, int pos )
752 a_Id_List a = new_id_list( xid, pos );
753 a->next = list;
754 a->linepos = pos;
756 return a;
759 void clean_exp( a_Exp exp )
761 if( !exp ) return;
762 clean_exp( exp->next );
763 free( exp );
766 a_Array_Sub new_array_sub( a_Exp exp, int pos )
768 a_Array_Sub a = (a_Array_Sub)checked_malloc( sizeof( struct a_Array_Sub_ ) );
769 a->exp_list = exp;
770 a->depth = 1;
771 a->linepos = pos;
773 // make sure no multi
774 /* if( exp && exp->next )
776 // primary exp?
777 if( is_primary )
779 a->exp_list = NULL;
780 a->exp_multi = exp;
782 else
784 a->err_num = 1; // multi
785 a->err_pos = exp->linepos; // set error for type-checker
786 goto error;
788 } */
790 return a;
792 /* error:
793 clean_exp( exp );
794 a->exp_list = NULL;
795 return a; */
798 a_Array_Sub prepend_array_sub( a_Array_Sub a, a_Exp exp, int pos )
800 // if already error
801 if( a->err_num ) goto error;
803 // make sure no multi
804 if( exp && exp->next )
806 a->err_num = 1; // multi
807 a->err_pos = exp->linepos; // set error for type-checker
808 goto error;
811 // empty or not
812 if( (exp && !a->exp_list) || (!exp && a->exp_list) )
814 a->err_num = 2; // partial
815 a->err_pos = pos; // set error for type-checker
816 goto error;
819 // prepend
820 if( exp )
822 exp->next = a->exp_list;
823 a->exp_list = exp;
826 // count
827 a->depth++;
828 return a;
830 error:
831 clean_exp( exp );
832 return a;
835 a_Complex new_complex( a_Exp re, int pos )
837 a_Complex a = (a_Complex)checked_malloc( sizeof( struct a_Complex_ ) );
838 a->re = re;
839 if( re ) a->im = re->next;
840 a->linepos = pos;
842 return a;
845 a_Polar new_polar( a_Exp mod, int pos )
847 a_Polar a = (a_Polar)checked_malloc( sizeof( struct a_Polar_ ) );
848 a->mod = mod;
849 if( mod ) a->phase = mod->next;
850 a->linepos = pos;
852 return a;
855 void delete_id_list( a_Id_List x )
857 if( !x ) return;
859 a_Id_List curr = x, next = x->next, temp;
861 while( curr )
863 temp = curr;
864 curr = next;
865 next = temp->next;
866 free( temp );
871 static const char * op_str[] = {
872 "+",
873 "-",
874 "*",
875 "/",
876 "==",
877 "!=",
878 "<",
879 "<=",
880 ">",
881 ">=",
882 "&&",
883 "||",
884 "|",
885 "&",
886 "<<",
887 ">>",
888 "%",
889 "^",
890 "=>",
891 "+=>",
892 "-=>",
893 "*=>",
894 "/=>",
895 "&=>",
896 "|=>",
897 "^=>",
898 ">>=>",
899 "<<=>",
900 "%=>",
901 "->",
902 "++",
903 "--",
904 "~",
905 "!",
906 "@=>",
907 "=<",
908 "spork ~",
909 "typeof",
910 "sizeof",
911 "new"
915 const char * op2str( ae_Operator op )
917 t_CKINT index = (t_CKINT)op;
918 if( index < 0 || index >= (t_CKINT)(sizeof(op_str)/sizeof(char *)) )
919 return "[non-existent operator]";
921 return op_str[index];