*** empty log message ***
[chuck-blob.git] / exile / v1 / src / chuck_absyn.cpp
blobf00739f79915afebb68ddd0e52922be4644e811e
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"
38 a_Program new_program( a_Section section, int pos )
40 a_Program a = (a_Program)checked_malloc( sizeof( struct a_Program_ ) );
41 a->section = section;
42 a->linepos = pos;
44 return a;
47 a_Program prepend_program( a_Section section, a_Program program, int pos )
49 a_Program a = new_program( section, pos );
50 a->next = program;
51 a->linepos = pos;
53 return a;
56 a_Section new_section_stmt( a_Stmt_List list, int pos )
58 a_Section a = (a_Section)checked_malloc( sizeof( struct a_Section_ ) );
59 a->s_type = ae_section_stmt;
60 a->stmt_list = list;
61 a->linepos = pos;
63 return a;
66 a_Section new_section_func_def( a_Func_Def func_def, int pos )
68 a_Section a = (a_Section)checked_malloc( sizeof( struct a_Section_) );
69 a->s_type = ae_section_func;
70 a->func_def = func_def;
71 a->linepos = pos;
73 return a;
76 a_Section new_section_class_def( a_Class_Def class_def, int pos )
78 a_Section a = (a_Section)checked_malloc( sizeof( struct a_Section_) );
79 a->s_type = ae_section_class;
80 a->class_def = class_def;
81 a->linepos = pos;
83 return a;
86 a_Stmt_List new_stmt_list( a_Stmt stmt, int pos )
88 a_Stmt_List a = (a_Stmt_List)checked_malloc( sizeof( struct a_Stmt_List_ ) );
89 a->stmt = stmt;
90 a->next = NULL;
91 a->linepos = pos;
93 return a;
96 a_Stmt_List prepend_stmt_list( a_Stmt stmt, a_Stmt_List stmt_list, int pos )
98 a_Stmt_List a = new_stmt_list( stmt, pos );
99 a->next = stmt_list;
100 a->linepos = pos;
102 return a;
105 a_Stmt new_stmt_from_expression( a_Exp exp, int pos )
107 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
108 a->s_type = ae_stmt_exp;
109 a->stmt_exp = exp;
110 a->linepos = pos;
112 return a;
115 a_Stmt new_stmt_from_code( a_Stmt_List stmt_list, int pos )
117 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
118 a->s_type = ae_stmt_code;
119 a->stmt_code.stmt_list = stmt_list;
120 a->linepos = pos;
121 a->stmt_code.linepos = pos;
123 return a;
126 a_Stmt new_stmt_from_if( a_Exp cond, a_Stmt if_body, a_Stmt else_body, int pos )
128 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
129 a->s_type = ae_stmt_if;
130 a->stmt_if.cond = cond;
131 a->stmt_if.if_body = if_body;
132 a->stmt_if.else_body = else_body;
133 a->linepos = pos;
134 a->stmt_if.linepos = pos;
136 return a;
139 a_Stmt new_stmt_from_while( a_Exp cond, a_Stmt body, int pos )
141 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
142 a->s_type = ae_stmt_while;
143 a->stmt_while.is_do = 0;
144 a->stmt_while.cond = cond;
145 a->stmt_while.body = body;
146 a->linepos = pos;
147 a->stmt_while.linepos = pos;
149 return a;
152 a_Stmt new_stmt_from_do_while( a_Exp cond, a_Stmt body, int pos )
154 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
155 a->s_type = ae_stmt_while;
156 a->stmt_while.is_do = 1;
157 a->stmt_while.cond = cond;
158 a->stmt_while.body = body;
159 a->linepos = pos;
160 a->stmt_while.linepos = pos;
162 return a;
165 a_Stmt new_stmt_from_until( a_Exp cond, a_Stmt body, int pos )
167 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
168 a->s_type = ae_stmt_until;
169 a->stmt_until.is_do = 0;
170 a->stmt_until.cond = cond;
171 a->stmt_until.body = body;
172 a->linepos = pos;
173 a->stmt_until.linepos = pos;
175 return a;
178 a_Stmt new_stmt_from_do_until( a_Exp cond, a_Stmt body, int pos )
180 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
181 a->s_type = ae_stmt_until;
182 a->stmt_until.is_do = 1;
183 a->stmt_until.cond = cond;
184 a->stmt_until.body = body;
185 a->linepos = pos;
186 a->stmt_until.linepos = pos;
188 return a;
191 a_Stmt new_stmt_from_for( a_Stmt c1, a_Stmt c2, a_Exp c3, a_Stmt body, int pos )
193 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
194 a->s_type = ae_stmt_for;
195 a->stmt_for.c1 = c1;
196 a->stmt_for.c2 = c2;
197 a->stmt_for.c3 = c3;
198 a->stmt_for.body = body;
199 a->linepos = pos;
200 a->stmt_for.linepos = pos;
202 return a;
205 a_Stmt new_stmt_from_switch( a_Exp val, int pos )
207 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
208 a->s_type = ae_stmt_switch;
209 a->stmt_switch.val = val;
210 a->linepos = pos;
211 a->stmt_switch.linepos = pos;
213 return a;
216 a_Stmt new_stmt_from_break( int pos )
218 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
219 a->s_type = ae_stmt_break;
220 a->linepos = pos;
222 return a;
225 a_Stmt new_stmt_from_continue( int pos )
227 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
228 a->s_type = ae_stmt_continue;
229 a->linepos = pos;
231 return a;
234 a_Stmt new_stmt_from_return( a_Exp exp, int pos )
236 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
237 a->s_type = ae_stmt_return;
238 a->stmt_return.val = exp;
239 a->linepos = pos;
240 a->stmt_return.linepos = pos;
242 return a;
245 a_Stmt new_stmt_from_label( c_str id, int pos )
247 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
248 a->s_type = ae_stmt_gotolabel;
249 a->stmt_gotolabel.name = insert_symbol( id );
250 a->linepos = pos;
251 a->stmt_gotolabel.linepos = pos;
253 return a;
256 a_Stmt new_stmt_from_case( a_Exp exp, int pos )
258 a_Stmt a = (a_Stmt)checked_malloc( sizeof( struct a_Stmt_ ) );
259 a->s_type = ae_stmt_case;
260 a->stmt_case.exp = exp;
261 a->linepos = pos;
262 a->stmt_case.linepos = pos;
264 return a;
267 a_Exp prepend_expression( a_Exp exp, a_Exp list, int pos )
269 exp->next = list;
270 return exp;
273 a_Exp new_exp_from_binary( a_Exp lhs, ae_Operator oper, a_Exp rhs, int pos )
275 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
276 a->s_type = ae_exp_binary;
277 a->s_meta = ae_meta_value;
278 a->binary.lhs = lhs;
279 a->binary.op = oper;
280 a->binary.rhs = rhs;
281 a->linepos = pos;
282 a->binary.linepos = pos;
284 return a;
287 a_Exp new_exp_from_unary( ae_Operator oper, a_Exp exp, int pos )
289 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
290 a->s_type = ae_exp_unary;
291 a->s_meta = exp->s_meta;
292 a->unary.op = oper;
293 a->unary.exp = exp;
294 a->linepos = pos;
295 a->unary.linepos = pos;
297 return a;
300 a_Exp new_exp_from_cast( c_str type, a_Exp exp, int pos )
302 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
303 a->s_type = ae_exp_cast;
304 a->s_meta = ae_meta_value;
305 a->cast.type = insert_symbol( type );
306 a->cast.exp = exp;
307 a->linepos = pos;
308 a->cast.linepos = pos;
310 return a;
313 a_Exp new_exp_from_array( a_Exp base, a_Exp index, int pos )
315 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
316 a->s_type = ae_exp_array;
317 a->s_meta = ae_meta_var;
318 a->array.base = base;
319 a->array.index = index;
320 a->linepos = pos;
321 a->array.linepos = pos;
323 return a;
326 a_Exp new_exp_from_func_call( a_Exp base, a_Exp args, int pos )
328 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
329 a->s_type = ae_exp_func_call;
330 a->s_meta = ae_meta_value;
331 a->func_call.func = base;
332 a->func_call.args = args;
333 a->linepos = pos;
334 a->func_call.linepos = pos;
336 return a;
339 a_Exp new_exp_from_member_dot( a_Exp base, c_str id, int pos )
341 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
342 a->s_type = ae_exp_dot_member;
343 a->s_meta = ae_meta_var;
344 a->dot_member.base = base;
345 a->dot_member.id = insert_symbol( id );
346 a->linepos = pos;
347 a->dot_member.linepos = pos;
349 return a;
352 a_Exp new_exp_from_postfix( a_Exp base, ae_Operator op, int pos )
354 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
355 a->s_type = ae_exp_postfix;
356 a->s_meta = ae_meta_var;
357 a->postfix.exp = base;
358 a->postfix.op = op;
359 a->linepos = pos;
360 a->postfix.linepos = pos;
362 return a;
365 a_Exp new_exp_from_dur( a_Exp base, a_Exp unit, int pos )
367 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
368 a->s_type = ae_exp_dur;
369 a->s_meta = ae_meta_value;
370 a->dur.base = base;
371 a->dur.unit = unit;
372 a->linepos = pos;
373 a->dur.linepos = pos;
375 return a;
378 a_Exp new_exp_from_id( c_str id, int pos )
380 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
381 a->s_type = ae_exp_primary;
382 a->s_meta = ae_meta_var;
383 a->primary.s_type = ae_primary_var;
384 a->primary.var = insert_symbol( id );
385 a->linepos = pos;
386 a->primary.linepos = pos;
388 return a;
391 a_Exp new_exp_from_int( long num, int pos )
393 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
394 a->s_type = ae_exp_primary;
395 a->s_meta = ae_meta_value;
396 a->primary.s_type = ae_primary_num;
397 a->primary.num = num;
398 a->linepos = pos;
399 a->primary.linepos = pos;
401 return a;
404 a_Exp new_exp_from_uint( unsigned long num, int pos )
406 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
407 a->s_type = ae_exp_primary;
408 a->s_meta = ae_meta_value;
409 a->primary.s_type = ae_primary_uint;
410 a->primary.num2 = num;
411 a->linepos = pos;
412 a->primary.linepos = pos;
414 return a;
417 a_Exp new_exp_from_float( double num, int pos )
419 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
420 a->s_type = ae_exp_primary;
421 a->s_meta = ae_meta_value;
422 a->primary.s_type = ae_primary_float;
423 a->primary.fnum = num;
424 a->linepos = pos;
425 a->primary.linepos = pos;
427 return a;
430 a_Exp new_exp_from_str( c_str str, int pos )
432 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
433 a->s_type = ae_exp_primary;
434 a->s_meta = ae_meta_value;
435 a->primary.s_type = ae_primary_str;
436 a->primary.str = str;
437 a->linepos = pos;
438 a->primary.linepos = pos;
440 return a;
443 a_Exp new_exp_from_if( a_Exp cond, a_Exp if_exp, a_Exp else_exp, int pos )
445 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
446 a->s_type = ae_exp_if;
447 a->s_meta = ( ( if_exp->s_meta == ae_meta_var &&
448 else_exp->s_meta == ae_meta_var ) ? ae_meta_var : ae_meta_value );
449 a->exp_if.cond = cond;
450 a->exp_if.if_exp = if_exp;
451 a->exp_if.else_exp = else_exp;
452 a->linepos = pos;
453 a->exp_if.linepos = pos;
455 return a;
458 a_Exp new_exp_decl( c_str type, a_Var_Decl_List var_decl_list, int pos )
460 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
461 a->s_type = ae_exp_decl;
462 a->s_meta = ae_meta_var;
463 a->decl.type = insert_symbol( type );
464 a->decl.var_decl_list = var_decl_list;
465 a->linepos = pos;
466 a->decl.linepos = pos;
468 return a;
471 a_Exp new_exp_from_namespace( c_str name, int pos )
473 a_Exp a = (a_Exp)checked_malloc( sizeof( struct a_Exp_ ) );
474 a->s_type = ae_exp_namespace;
475 a->s_meta = ae_meta_value;
476 a->name_space.name = insert_symbol( name );
477 a->name_space.linepos = pos;
478 a->linepos = pos;
480 return a;
483 a_Var_Decl new_var_decl( c_str name, int pos )
485 a_Var_Decl a = (a_Var_Decl)checked_malloc( sizeof( struct a_Var_Decl_ ) );
486 a->id = insert_symbol( name );
487 a->linepos = pos;
489 return a;
492 a_Var_Decl new_var_decl2( a_Var_Decl var_decl, int isarray, a_Exp exp, int pos )
494 a_Var_Decl a = (a_Var_Decl)checked_malloc( sizeof( struct a_Var_Decl_ ) );
495 a->var_decl = var_decl;
496 a->isarray = isarray;
497 a->exp = exp;
498 a->linepos = pos;
500 return a;
503 a_Var_Decl_List new_var_decl_list( a_Var_Decl var_decl, int pos )
505 a_Var_Decl_List a = (a_Var_Decl_List)checked_malloc(
506 sizeof( struct a_Var_Decl_List_ ) );
507 a->var_decl = var_decl;
508 a->linepos = pos;
510 return a;
513 a_Var_Decl_List prepend_var_decl_list( a_Var_Decl var_decl, a_Var_Decl_List list, int pos )
515 a_Var_Decl_List a = new_var_decl_list( var_decl, pos );
516 a->next = list;
518 return a;
521 a_Type_Decl new_type_decl( c_str type, int pos )
523 a_Type_Decl a = (a_Type_Decl)checked_malloc(
524 sizeof( struct a_Type_Decl_ ) );
525 a->id = insert_symbol( type );
526 a->array = 0;
527 a->linepos = pos;
529 return a;
532 a_Type_Decl new_type_decl_array( a_Type_Decl decl, int pos )
534 decl->array++;
535 return decl;
538 a_Arg_List new_arg_list( a_Type_Decl type_decl, c_str name, int pos )
540 a_Arg_List a = (a_Arg_List)checked_malloc(
541 sizeof( struct a_Arg_List_ ) );
542 a->type_decl = type_decl;
543 a->id = insert_symbol( name );
544 a->next = NULL;
545 a->linepos = pos;
547 return a;
550 a_Arg_List prepend_arg_list( a_Type_Decl type_decl, c_str name, a_Arg_List arg_list, int pos )
552 a_Arg_List a = new_arg_list( type_decl, name, pos );
553 a->next = arg_list;
554 a->linepos = pos;
556 return a;
559 a_Func_Def new_func_def( ae_Keyword func_decl, a_Type_Decl type_decl, c_str name,
560 a_Arg_List arg_list, a_Stmt code, int pos )
562 a_Func_Def a = (a_Func_Def)checked_malloc(
563 sizeof( struct a_Func_Def_ ) );
564 a->func_decl = func_decl;
565 a->type_decl = type_decl;
566 a->name = insert_symbol( name );
567 a->arg_list = arg_list;
568 a->s_type = ae_func_user;
569 a->code = code;
570 a->linepos = pos;
572 return a;
575 a_Class_Def new_class_def( c_str name, a_Class_Ext ext, a_Class_Body body, int pos )
577 a_Class_Def a = (a_Class_Def)checked_malloc( sizeof( struct a_Class_Def_ ) );
578 a->name = insert_symbol( name );
579 a->ext = ext;
580 a->body = body;
581 a->linepos = pos;
583 return a;
586 a_Class_Body new_class_body( a_Section section, int pos )
588 a_Class_Body a = (a_Class_Body)checked_malloc( sizeof( struct a_Class_Body_ ) );
589 a->section = section;
590 a->linepos = pos;
592 return a;
595 a_Class_Body prepend_class_body( a_Section section, a_Class_Body body, int pos )
597 a_Class_Body a = new_class_body( section, pos );
598 a->next = body;
599 a->linepos = pos;
601 return a;
604 a_Class_Ext new_class_ext( c_str extend_id, a_Id_List impl_list, int pos )
606 a_Class_Ext a = (a_Class_Ext)checked_malloc( sizeof( struct a_Class_Ext_ ) );
607 a->extend_id = insert_symbol( extend_id );
608 a->impl_list = impl_list;
609 a->linepos = pos;
611 return a;
614 a_Id_List new_id_list( c_str id, int pos )
616 a_Id_List a = (a_Id_List)checked_malloc( sizeof( struct a_Id_List_ ) );
617 a->id = insert_symbol( id );
618 a->next = NULL;
619 a->linepos = pos;
621 return a;
624 a_Id_List prepend_id_list( c_str id, a_Id_List list, int pos )
626 a_Id_List a = new_id_list( id, pos );
627 a->next = list;
628 a->linepos = pos;
630 return a;
634 static const char * op_str[] = {
635 "+",
636 "-",
637 "*",
638 "/",
639 "==",
640 "!=",
641 "<",
642 "<=",
643 ">",
644 ">=",
645 "&&",
646 "||",
647 "|",
648 "&",
649 "<<",
650 ">>",
651 "%",
652 "^",
653 "=>",
654 "+=>",
655 "-=>",
656 "*=>",
657 "/=>",
658 "&=>",
659 "|=>",
660 "^=>",
661 ">>=>",
662 "<<=>",
663 "%=>",
664 "->",
665 "++",
666 "--",
667 "~",
668 "!",
669 "@=>",
670 "=<"
674 const char * op2str( ae_Operator op )
676 return op_str[op];