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"
38 a_Program
new_program( a_Section section
, int pos
)
40 a_Program a
= (a_Program
)checked_malloc( sizeof( struct a_Program_
) );
47 a_Program
prepend_program( a_Section section
, a_Program program
, int pos
)
49 a_Program a
= new_program( section
, pos
);
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
;
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
;
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
;
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_
) );
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
);
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
;
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
;
121 a
->stmt_code
.linepos
= pos
;
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
;
134 a
->stmt_if
.linepos
= pos
;
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
;
147 a
->stmt_while
.linepos
= pos
;
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
;
160 a
->stmt_while
.linepos
= pos
;
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
;
173 a
->stmt_until
.linepos
= pos
;
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
;
186 a
->stmt_until
.linepos
= pos
;
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
;
198 a
->stmt_for
.body
= body
;
200 a
->stmt_for
.linepos
= pos
;
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
;
211 a
->stmt_switch
.linepos
= pos
;
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
;
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
;
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
;
240 a
->stmt_return
.linepos
= pos
;
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
);
251 a
->stmt_gotolabel
.linepos
= pos
;
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
;
262 a
->stmt_case
.linepos
= pos
;
267 a_Exp
prepend_expression( a_Exp exp
, a_Exp list
, int pos
)
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
;
282 a
->binary
.linepos
= pos
;
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
;
295 a
->unary
.linepos
= pos
;
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
);
308 a
->cast
.linepos
= pos
;
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
;
321 a
->array
.linepos
= pos
;
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
;
334 a
->func_call
.linepos
= pos
;
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
);
347 a
->dot_member
.linepos
= pos
;
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
;
360 a
->postfix
.linepos
= pos
;
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
;
373 a
->dur
.linepos
= pos
;
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
);
386 a
->primary
.linepos
= pos
;
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
;
399 a
->primary
.linepos
= pos
;
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
;
412 a
->primary
.linepos
= pos
;
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
;
425 a
->primary
.linepos
= pos
;
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
;
438 a
->primary
.linepos
= pos
;
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
;
453 a
->exp_if
.linepos
= pos
;
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
;
466 a
->decl
.linepos
= pos
;
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
;
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
);
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
;
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
;
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
);
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
);
532 a_Type_Decl
new_type_decl_array( a_Type_Decl decl
, int pos
)
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
);
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
);
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
;
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
);
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
;
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
);
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
;
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
);
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
);
634 static const char * op_str
[] = {
674 const char * op2str( ae_Operator op
)