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
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_absyn.cpp
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
32 //-----------------------------------------------------------------------------
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_
) );
48 a_Program
prepend_program( a_Section section
, a_Program program
, int pos
)
50 a_Program a
= new_program( section
, pos
);
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
;
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
;
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
;
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_
) );
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
);
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
;
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
;
122 a
->stmt_code
.linepos
= pos
;
123 a
->stmt_code
.self
= 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
;
136 a
->stmt_if
.linepos
= pos
;
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
;
150 a
->stmt_while
.linepos
= pos
;
151 a
->stmt_while
.self
= 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
;
164 a
->stmt_while
.linepos
= pos
;
165 a
->stmt_while
.self
= 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
;
178 a
->stmt_until
.linepos
= pos
;
179 a
->stmt_until
.self
= 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
;
192 a
->stmt_until
.linepos
= pos
;
193 a
->stmt_until
.self
= 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
;
205 a
->stmt_for
.body
= body
;
207 a
->stmt_for
.linepos
= pos
;
208 a
->stmt_for
.self
= 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
;
220 a
->stmt_loop
.linepos
= pos
;
221 a
->stmt_loop
.self
= 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
;
232 a
->stmt_switch
.linepos
= pos
;
233 a
->stmt_switch
.self
= 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
;
243 a
->stmt_break
.linepos
= pos
;
244 a
->stmt_break
.self
= 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
;
254 a
->stmt_continue
.linepos
= pos
;
255 a
->stmt_continue
.self
= 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
;
266 a
->stmt_return
.linepos
= pos
;
267 a
->stmt_return
.self
= 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
);
278 a
->stmt_gotolabel
.linepos
= pos
;
279 a
->stmt_gotolabel
.self
= 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
;
290 a
->stmt_case
.linepos
= pos
;
291 a
->stmt_case
.self
= a
;
296 a_Exp
prepend_expression( a_Exp exp
, a_Exp list
, int pos
)
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
;
311 a
->binary
.linepos
= pos
;
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
;
325 a
->unary
.linepos
= pos
;
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
;
338 a
->unary
.type
= type
;
339 a
->unary
.array
= array
;
341 a
->unary
.linepos
= pos
;
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
;
353 a
->unary
.code
= code
;
355 a
->unary
.linepos
= pos
;
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
;
369 a
->cast
.linepos
= pos
;
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
;
383 a
->array
.linepos
= pos
;
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
;
397 a
->func_call
.linepos
= pos
;
398 a
->func_call
.self
= 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
);
411 a
->dot_member
.linepos
= pos
;
412 a
->dot_member
.self
= 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
;
425 a
->postfix
.linepos
= pos
;
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
;
439 a
->dur
.linepos
= pos
;
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
);
453 a
->primary
.linepos
= pos
;
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
;
467 a
->primary
.linepos
= pos
;
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
;
481 a
->primary
.linepos
= pos
;
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
;
495 a
->primary
.linepos
= pos
;
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
;
509 a
->primary
.linepos
= pos
;
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
;
525 a
->exp_if
.linepos
= pos
;
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
;
537 a
->decl
.var_decl_list
= var_decl_list
;
539 a
->decl
.is_static
= is_static
;
540 a
->decl
.linepos
= pos
;
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
;
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
;
569 a
->primary
.complex->self
= 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
;
584 a
->primary
.polar
->self
= 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
;
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
);
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
;
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
);
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_
) );
642 a_Type_Decl
add_type_decl_array( a_Type_Decl a
, a_Array_Sub array
, int pos
)
644 assert( a
->array
== NULL
);
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
;
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
);
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
;
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
;
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
);
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
;
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
);
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
;
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
);
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
);
759 void clean_exp( a_Exp exp
)
762 clean_exp( exp
->next
);
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_
) );
773 // make sure no multi
774 /* if( exp && exp->next )
784 a->err_num = 1; // multi
785 a->err_pos = exp->linepos; // set error for type-checker
798 a_Array_Sub
prepend_array_sub( a_Array_Sub a
, a_Exp exp
, int pos
)
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
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
822 exp
->next
= a
->exp_list
;
835 a_Complex
new_complex( a_Exp re
, int pos
)
837 a_Complex a
= (a_Complex
)checked_malloc( sizeof( struct a_Complex_
) );
839 if( re
) a
->im
= re
->next
;
845 a_Polar
new_polar( a_Exp mod
, int pos
)
847 a_Polar a
= (a_Polar
)checked_malloc( sizeof( struct a_Polar_
) );
849 if( mod
) a
->phase
= mod
->next
;
855 void delete_id_list( a_Id_List x
)
859 a_Id_List curr
= x
, next
= x
->next
, temp
;
871 static const char * op_str
[] = {
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
];