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.h
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
32 //-----------------------------------------------------------------------------
33 #ifndef __CHUCK_ABSYN_H__
34 #define __CHUCK_ABSYN_H__
36 #include "chuck_symbol.h"
38 #if defined(_cplusplus) || defined(__cplusplus)
47 ae_op_plus
, ae_op_minus
, ae_op_times
, ae_op_divide
,
48 ae_op_eq
, ae_op_neq
, ae_op_lt
, ae_op_le
, ae_op_gt
,
49 ae_op_ge
, ae_op_and
, ae_op_or
, ae_op_s_or
, ae_op_s_and
,
50 ae_op_shift_left
, ae_op_shift_right
, ae_op_percent
,
51 ae_op_s_xor
, ae_op_chuck
, ae_op_plus_chuck
, ae_op_minus_chuck
,
52 ae_op_times_chuck
, ae_op_divide_chuck
, ae_op_s_and_chuck
,
53 ae_op_s_or_chuck
, ae_op_s_xor_chuck
, ae_op_shift_right_chuck
,
54 ae_op_shift_left_chuck
, ae_op_percent_chuck
, ae_op_s_chuck
,
55 ae_op_plusplus
, ae_op_minusminus
, ae_op_tilda
, ae_op_exclamation
,
56 ae_op_at_chuck
, ae_op_unchuck
, ae_op_upchuck
, ae_op_spork
,
57 ae_op_typeof
, ae_op_sizeof
, ae_op_new
, ae_op_arrow_left
, ae_op_arrow_right
60 const char * op2str( ae_Operator op
);
64 ae_key_this
, ae_key_me
, ae_key_func
, ae_key_public
, ae_key_protected
,
65 ae_key_private
, ae_key_static
, ae_key_instance
, ae_key_abstract
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 typedef struct a_Program_
* a_Program
;
75 typedef struct a_Section_
* a_Section
;
76 typedef struct a_Stmt_List_
* a_Stmt_List
;
77 typedef struct a_Class_Def_
* a_Class_Def
;
78 typedef struct a_Func_Def_
* a_Func_Def
;
79 typedef struct a_Code_Segment_
* a_Code_Segment
;
80 typedef struct a_Stmt_
* a_Stmt
;
81 typedef struct a_Exp_
* a_Exp
;
82 typedef struct a_Exp_Chuck_
* a_Exp_Chuck
;
83 typedef struct a_Exp_Binary_
* a_Exp_Binary
;
84 typedef struct a_Exp_Cast_
* a_Exp_Cast
;
85 typedef struct a_Exp_Unary_
* a_Exp_Unary
;
86 typedef struct a_Exp_Postfix_
* a_Exp_Postfix
;
87 typedef struct a_Exp_Primary_
* a_Exp_Primary
;
88 typedef struct a_Exp_Dur_
* a_Exp_Dur
;
89 typedef struct a_Exp_Array_
* a_Exp_Array
;
90 typedef struct a_Exp_Func_Call_
* a_Exp_Func_Call
;
91 typedef struct a_Exp_Dot_Member_
* a_Exp_Dot_Member
;
92 typedef struct a_Exp_If_
* a_Exp_If
;
93 typedef struct a_Exp_Decl_
* a_Exp_Decl
;
94 typedef struct a_Exp_Hack_
* a_Exp_Hack
;
95 typedef struct a_Stmt_Code_
* a_Stmt_Code
;
96 typedef struct a_Stmt_If_
* a_Stmt_If
;
97 typedef struct a_Stmt_While_
* a_Stmt_While
;
98 typedef struct a_Stmt_Until_
* a_Stmt_Until
;
99 typedef struct a_Stmt_For_
* a_Stmt_For
;
100 typedef struct a_Stmt_Loop_
* a_Stmt_Loop
;
101 typedef struct a_Stmt_Switch_
* a_Stmt_Switch
;
102 typedef struct a_Stmt_Break_
* a_Stmt_Break
;
103 typedef struct a_Stmt_Continue_
* a_Stmt_Continue
;
104 typedef struct a_Stmt_Return_
* a_Stmt_Return
;
105 typedef struct a_Stmt_Case_
* a_Stmt_Case
;
106 typedef struct a_Stmt_GotoLabel_
* a_Stmt_GotoLabel
;
107 typedef struct a_Decl_
* a_Decl
;
108 typedef struct a_Var_Decl_
* a_Var_Decl
;
109 typedef struct a_Var_Decl_List_
* a_Var_Decl_List
;
110 typedef struct a_Type_Decl_
* a_Type_Decl
;
111 typedef struct a_Arg_List_
* a_Arg_List
;
112 typedef struct a_Id_List_
* a_Id_List
;
113 typedef struct a_Class_Ext_
* a_Class_Ext
;
114 typedef struct a_Class_Body_
* a_Class_Body
;
115 typedef struct a_Array_Sub_
* a_Array_Sub
;
116 typedef struct a_Complex_
* a_Complex
;
117 typedef struct a_Polar_
* a_Polar
;
119 // forward reference for type
120 typedef struct Chuck_Type
* t_CKTYPE
;
121 typedef struct Chuck_Value
* t_CKVALUE
;
122 typedef struct Chuck_Func
* t_CKFUNC
;
123 typedef struct Chuck_Namespace
* t_CKNSPC
;
124 typedef struct Chuck_VM_Code
* t_CKVMCODE
;
129 //------------------------------------------------------------------------------
131 //------------------------------------------------------------------------------
132 a_Program
new_program( a_Section section
, int pos
);
133 a_Program
prepend_program( a_Section section
, a_Program program
, int pos
);
134 a_Section
new_section_stmt( a_Stmt_List stmt_list
, int pos
);
135 a_Section
new_section_func_def( a_Func_Def func_def
, int pos
);
136 a_Section
new_section_class_def( a_Class_Def class_def
, int pos
);
137 a_Stmt_List
new_stmt_list( a_Stmt stmt
, int pos
);
138 a_Stmt_List
prepend_stmt_list( a_Stmt stmt
, a_Stmt_List stmt_list
, int pos
);
139 a_Stmt
new_stmt_from_expression( a_Exp exp
, int pos
);
140 a_Stmt
new_stmt_from_code( a_Stmt_List code
, int pos
);
141 a_Stmt
new_stmt_from_while( a_Exp cond
, a_Stmt body
, int pos
);
142 a_Stmt
new_stmt_from_do_while( a_Exp cond
, a_Stmt body
, int pos
);
143 a_Stmt
new_stmt_from_until( a_Exp cond
, a_Stmt body
, int pos
);
144 a_Stmt
new_stmt_from_do_until( a_Exp cond
, a_Stmt body
, int pos
);
145 a_Stmt
new_stmt_from_for( a_Stmt c1
, a_Stmt c2
, a_Exp c3
, a_Stmt body
, int pos
);
146 a_Stmt
new_stmt_from_loop( a_Exp cond
, a_Stmt body
, int pos
);
147 a_Stmt
new_stmt_from_if( a_Exp cond
, a_Stmt if_body
, a_Stmt else_body
, int pos
);
148 a_Stmt
new_stmt_from_switch( a_Exp exp
, int pos
);
149 a_Stmt
new_stmt_from_break( int pos
);
150 a_Stmt
new_stmt_from_continue( int pos
);
151 a_Stmt
new_stmt_from_return( a_Exp exp
, int pos
);
152 a_Stmt
new_stmt_from_label( c_str xid
, int pos
);
153 a_Stmt
new_stmt_from_case( a_Exp exp
, int pos
);
154 a_Exp
prepend_expression( a_Exp exp
, a_Exp list
, int pos
);
155 a_Exp
new_exp_from_binary( a_Exp lhs
, ae_Operator oper
, a_Exp rhs
, int pos
);
156 a_Exp
new_exp_from_unary( ae_Operator oper
, a_Exp exp
, int pos
);
157 a_Exp
new_exp_from_unary2( ae_Operator oper
, a_Type_Decl type
, a_Array_Sub array
, int pos
);
158 a_Exp
new_exp_from_unary3( ae_Operator oper
, a_Stmt code
, int pos
);
159 a_Exp
new_exp_from_cast( a_Type_Decl type
, a_Exp exp
, int pos
);
160 a_Exp
new_exp_from_array( a_Exp base
, a_Array_Sub indices
, int pos
);
161 a_Exp
new_exp_from_array_lit( a_Array_Sub exp_list
, int pos
);
162 a_Exp
new_exp_from_func_call( a_Exp base
, a_Exp args
, int pos
);
163 a_Exp
new_exp_from_member_dot( a_Exp base
, c_str member
, int pos
);
164 a_Exp
new_exp_from_postfix( a_Exp base
, ae_Operator op
, int pos
);
165 a_Exp
new_exp_from_dur( a_Exp base
, a_Exp unit
, int pos
);
166 a_Exp
new_exp_from_id( c_str xid
, int pos
);
167 a_Exp
new_exp_from_int( long num
, int pos
);
168 a_Exp
new_exp_from_uint( unsigned long num
, int pos
);
169 a_Exp
new_exp_from_float( double num
, int pos
);
170 a_Exp
new_exp_from_str( c_str str
, int pos
);
171 a_Exp
new_exp_from_if( a_Exp cond
, a_Exp lhs
, a_Exp rhs
, int pos
);
172 a_Exp
new_exp_from_complex( a_Complex
, int pos
);
173 a_Exp
new_exp_from_polar( a_Polar
, int pos
);
174 a_Exp
new_exp_decl( a_Type_Decl type_decl
, a_Var_Decl_List var_decl_list
, int is_static
, int pos
);
175 a_Exp
new_exp_from_hack( a_Exp exp
, int pos
);
176 a_Exp
new_exp_from_nil( int pos
);
177 a_Var_Decl_List
new_var_decl_list( a_Var_Decl var_decl
, int pos
);
178 a_Var_Decl_List
prepend_var_decl_list( a_Var_Decl var_decl
, a_Var_Decl_List list
, int pos
);
179 a_Var_Decl
new_var_decl( c_constr xid
, a_Array_Sub array
, int pos
);
180 a_Type_Decl
new_type_decl( a_Id_List xid
, int ref
, int pos
);
181 a_Type_Decl
add_type_decl_array( a_Type_Decl type_decl
, a_Array_Sub array
, int pos
);
182 a_Arg_List
new_arg_list( a_Type_Decl type_decl
, a_Var_Decl var_decl
, int pos
);
183 a_Arg_List
prepend_arg_list( a_Type_Decl type_decl
, a_Var_Decl var_decl
, a_Arg_List arg_list
, int pos
);
184 a_Array_Sub
new_array_sub( a_Exp exp
, int pos
);
185 a_Array_Sub
prepend_array_sub( a_Array_Sub array
, a_Exp exp
, int pos
);
186 a_Complex
new_complex( a_Exp re
, int pos
);
187 a_Polar
new_polar( a_Exp mod
, int pos
);
188 a_Class_Def
new_class_def( ae_Keyword class_decl
, a_Id_List xid
, a_Class_Ext ext
, a_Class_Body body
, int pos
);
189 a_Class_Body
new_class_body( a_Section section
, int pos
);
190 a_Class_Body
prepend_class_body( a_Section section
, a_Class_Body body
, int pos
);
191 a_Class_Ext
new_class_ext( a_Id_List extend_id
, a_Id_List impl_list
, int pos
);
192 a_Class_Def
new_iface_def( ae_Keyword class_decl
, a_Id_List xid
, a_Class_Ext ext
, a_Class_Body body
, int pos
);
193 a_Id_List
new_id_list( c_str xid
, int pos
);
194 a_Id_List
prepend_id_list( c_str xid
, a_Id_List list
, int pos
);
195 void clean_exp( a_Exp exp
);
196 a_Func_Def
new_func_def( ae_Keyword func_decl
, ae_Keyword static_decl
,
197 a_Type_Decl type_decl
, c_str name
,
198 a_Arg_List arg_list
, a_Stmt code
, int pos
);
200 void delete_id_list( a_Id_List x
);
205 //------------------------------------------------------------------------------
207 //------------------------------------------------------------------------------
208 struct a_Exp_Binary_
{ a_Exp lhs
; ae_Operator op
; a_Exp rhs
; t_CKFUNC ck_func
; int linepos
; a_Exp self
; };
209 struct a_Exp_Cast_
{ a_Type_Decl type
; a_Exp exp
; int linepos
; a_Exp self
; };
210 struct a_Exp_Unary_
{ ae_Operator op
; a_Exp exp
; a_Type_Decl type
; a_Array_Sub array
;
211 a_Stmt code
; int linepos
; a_Exp self
; };
212 struct a_Exp_Postfix_
{ a_Exp exp
; ae_Operator op
; int linepos
; a_Exp self
; };
213 struct a_Exp_Dur_
{ a_Exp base
; a_Exp unit
; int linepos
; a_Exp self
; };
214 struct a_Exp_Array_
{ a_Exp base
; a_Array_Sub indices
; int linepos
; a_Exp self
; };
215 struct a_Exp_Func_Call_
{ a_Exp func
; a_Exp args
; t_CKTYPE ret_type
;
216 t_CKFUNC ck_func
; t_CKVMCODE ck_vm_code
; int linepos
; a_Exp self
; };
217 struct a_Exp_Dot_Member_
{ a_Exp base
; t_CKTYPE t_base
; S_Symbol xid
; int linepos
; a_Exp self
; };
218 struct a_Exp_If_
{ a_Exp cond
; a_Exp if_exp
; a_Exp else_exp
; int linepos
; a_Exp self
; };
219 struct a_Exp_Decl_
{ a_Type_Decl type
; a_Var_Decl_List var_decl_list
; int num_var_decls
; int is_static
;
220 t_CKTYPE ck_type
; int linepos
; a_Exp self
; };
221 struct a_Exp_Hack_
{ a_Exp exp
; int linepos
; a_Exp self
; };
222 struct a_Var_Decl_List_
{ a_Var_Decl var_decl
; a_Var_Decl_List next
; int linepos
; a_Exp self
; };
223 struct a_Var_Decl_
{ S_Symbol xid
; a_Var_Decl var_decl
; a_Array_Sub array
; t_CKVALUE value
;
224 void * addr
; int linepos
; a_Exp self
; };
225 struct a_Type_Decl_
{ a_Id_List xid
; a_Array_Sub array
; int ref
; int linepos
; /*a_Exp self;*/ };
226 struct a_Array_Sub_
{ t_CKUINT depth
; a_Exp exp_list
; int linepos
; a_Exp self
;
227 int err_num
; int err_pos
; };
228 struct a_Arg_List_
{ a_Type_Decl type_decl
; a_Var_Decl var_decl
; t_CKTYPE type
;
229 a_Arg_List next
; int linepos
; a_Exp self
; };
230 struct a_Complex_
{ a_Exp re
; a_Exp im
; int linepos
; a_Exp self
; };
231 struct a_Polar_
{ a_Exp mod
; a_Exp phase
; int linepos
; a_Exp self
; };
233 // enum primary exp type
234 typedef enum { ae_primary_var
, ae_primary_num
, ae_primary_float
,
235 ae_primary_str
, ae_primary_array
, ae_primary_exp
,
236 ae_primary_hack
, ae_primary_complex
, ae_primary_polar
, ae_primary_nil
237 } ae_Exp_Primary_Type
;
239 struct a_Exp_Primary_
241 ae_Exp_Primary_Type s_type
;
261 typedef enum { ae_exp_binary
= 0, ae_exp_unary
, ae_exp_cast
, ae_exp_postfix
,
262 ae_exp_dur
, ae_exp_primary
, ae_exp_array
, ae_exp_func_call
,
263 ae_exp_dot_member
, ae_exp_if
, ae_exp_decl
266 typedef enum { ae_meta_value
= 0, ae_meta_var
} ae_Exp_Meta
;
278 t_CKUINT emit_var
; // 1 = emit var, 2 = emit var and value
282 struct a_Exp_Binary_ binary
;
283 struct a_Exp_Unary_ unary
;
284 struct a_Exp_Cast_ cast
;
285 struct a_Exp_Postfix_ postfix
;
286 struct a_Exp_Dur_ dur
;
287 struct a_Exp_Primary_ primary
;
288 struct a_Exp_Array_ array
;
289 struct a_Exp_Func_Call_ func_call
;
290 struct a_Exp_Dot_Member_ dot_member
;
291 struct a_Exp_If_ exp_if
;
292 struct a_Exp_Decl_ decl
;
298 struct a_Stmt_While_
{ int is_do
; a_Exp cond
; a_Stmt body
; int linepos
; a_Stmt self
; };
299 struct a_Stmt_Until_
{ int is_do
; a_Exp cond
; a_Stmt body
; int linepos
; a_Stmt self
; };
300 struct a_Stmt_For_
{ a_Stmt c1
; a_Stmt c2
; a_Exp c3
; a_Stmt body
; int linepos
; a_Stmt self
; };
301 struct a_Stmt_Loop_
{ a_Exp cond
; a_Stmt body
; int linepos
; a_Stmt self
; };
302 struct a_Stmt_Code_
{ a_Stmt_List stmt_list
; int linepos
; a_Stmt self
; };
303 struct a_Stmt_If_
{ a_Exp cond
; a_Stmt if_body
; a_Stmt else_body
; int linepos
; a_Stmt self
; };
304 struct a_Stmt_Switch_
{ a_Exp val
; int linepos
; a_Stmt self
; };
305 struct a_Stmt_Break_
{ int linepos
; a_Stmt self
; };
306 struct a_Stmt_Continue_
{ int linepos
; a_Stmt self
; };
307 struct a_Stmt_Return_
{ a_Exp val
; int linepos
; a_Stmt self
; };
308 struct a_Stmt_Case_
{ a_Exp exp
; int linepos
; a_Stmt self
; };
309 struct a_Stmt_GotoLabel_
{ S_Symbol name
; int linepos
; a_Stmt self
; };
311 // enum values for stmt type
312 typedef enum { ae_stmt_exp
, ae_stmt_while
, ae_stmt_until
, ae_stmt_for
, ae_stmt_loop
,
313 ae_stmt_if
, ae_stmt_code
, ae_stmt_switch
, ae_stmt_break
,
314 ae_stmt_continue
, ae_stmt_return
, ae_stmt_case
, ae_stmt_gotolabel
325 struct a_Stmt_Code_ stmt_code
;
326 struct a_Stmt_While_ stmt_while
;
327 struct a_Stmt_Until_ stmt_until
;
328 struct a_Stmt_Loop_ stmt_loop
;
329 struct a_Stmt_For_ stmt_for
;
330 struct a_Stmt_If_ stmt_if
;
331 struct a_Stmt_Switch_ stmt_switch
;
332 struct a_Stmt_Break_ stmt_break
;
333 struct a_Stmt_Continue_ stmt_continue
;
334 struct a_Stmt_Return_ stmt_return
;
335 struct a_Stmt_Case_ stmt_case
;
336 struct a_Stmt_GotoLabel_ stmt_gotolabel
;
342 struct a_Stmt_List_
{ a_Stmt stmt
; a_Stmt_List next
; int linepos
; };
343 struct a_Class_Def_
{ ae_Keyword decl
; a_Id_List name
; a_Class_Ext ext
;
344 a_Class_Body body
; t_CKTYPE type
; int iface
; t_CKNSPC home
;
346 struct a_Class_Ext_
{ a_Id_List extend_id
; a_Id_List impl_list
; int linepos
; };
347 struct a_Class_Body_
{ a_Section section
; a_Class_Body next
; int linepos
; };
348 struct a_Id_List_
{ S_Symbol xid
; a_Id_List next
; int linepos
; };
350 typedef enum { ae_func_user
, ae_func_builtin
} ae_Func_Type
;
351 // struct t_Func_User_ { int linepos; };
352 // typedef unsigned int (* builtin_func_ptr)( unsigned int arg );
353 // struct t_Func_BuiltIn_ { builtin_func_ptr func_ptr; int linepos; };
355 ae_Keyword func_decl
;
356 ae_Keyword static_decl
;
357 a_Type_Decl type_decl
;
365 unsigned int stack_depth
;
366 void * dl_func_ptr
; // should be not NULL iff s_type == ae_func_builtin
370 // enum values for section types
371 typedef enum { ae_section_stmt
, ae_section_func
, ae_section_class
376 ae_Section_Type s_type
;
380 a_Stmt_List stmt_list
;
381 a_Class_Def class_def
;
388 struct a_Program_
{ a_Section section
; a_Program next
; int linepos
; };
391 #if defined(_cplusplus) || defined(__cplusplus)