*** empty log message ***
[chuck-blob.git] / v1 / chuck.y
blobb441d1e6d7359c152b3ea89dbaf2a37c3edd35dc
1 %{
3 /*----------------------------------------------------------------------------
4 ChucK Concurrent, On-the-fly Audio Programming Language
5 Compiler and Virtual Machine
7 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
8 http://chuck.cs.princeton.edu/
9 http://soundlab.cs.princeton.edu/
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 U.S.A.
25 -----------------------------------------------------------------------------*/
27 //-----------------------------------------------------------------------------
28 // file: chuck.tab.c
29 // desc: chuck parser
31 // author: Ge Wang (gewang@cs.princeton.edu) - generated by yacc
32 // Perry R. Cook (prc@cs.princeton.edu)
34 // based in part on the ansi C grammar by Jeff Lee, maintained by Jutta Degener
36 // date: Summer 2002
37 //-----------------------------------------------------------------------------
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include "chuck_utils.h"
42 #include "chuck_errmsg.h"
43 #include "chuck_absyn.h"
45 // function
46 int yylex( void );
48 void yyerror( char *s )
50 EM_error( EM_tokPos, "%s", s );
53 a_Program g_program = NULL;
58 %union
60 int pos;
61 int ival;
62 double fval;
63 c_str sval;
65 a_Program program;
66 a_Section program_section;
67 a_Stmt_List stmt_list;
68 a_Class_Def class_def;
69 a_Class_Ext class_ext;
70 a_Class_Body class_body;
71 a_Stmt stmt;
72 a_Exp exp;
73 a_Func_Def func_def;
74 a_Var_Decl_List var_decl_list;
75 a_Var_Decl var_decl;
76 a_Type_Decl type_decl;
77 a_Arg_List arg_list;
78 a_Id_List id_list;
81 // expect 26 shift/reduce conflicts
82 %expect 26
84 %token <sval> ID STRING_LIT
85 %token <ival> NUM
86 %token <fval> FLOAT
88 %token
89 POUND COMMA COLON SEMICOLON LPAREN RPAREN
90 LBRACK RBRACK LBRACE RBRACE DOT
91 PLUS MINUS TIMES DIVIDE PERCENT
92 EQ NEQ LT LE GT GE AND OR ASSIGN
93 IF THEN ELSE WHILE FOR DO
94 BREAK NULL_TOK FUNCTION RETURN
95 QUESTION EXCLAMATION S_OR S_AND S_XOR
96 PLUSPLUS MINUSMINUS
97 SIMULT PATTERN CODE TRANSPORT HOST
98 TIME WHENEVER NEXT UNTIL EVERY BEFORE
99 AFTER AT AT_SYM ATAT_SYM NEW
100 PLUS_CHUCK MINUS_CHUCK TIMES_CHUCK
101 DIVIDE_CHUCK S_AND_CHUCK S_OR_CHUCK
102 S_XOR_CHUCK SHIFT_RIGHT_CHUCK
103 SHIFT_LEFT_CHUCK PERCENT_CHUCK
104 SHIFT_RIGHT SHIFT_LEFT TILDA CHUCK
105 COLONCOLON S_CHUCK AT_CHUCK LEFT_S_CHUCK
106 UNCHUCK CLASS EXTENDS IMPLEMENTS PUBLIC
107 PROTECTED PRIVATE STATIC CONST SPORK
108 L_NSPC R_NSPC
111 %type <program> program
112 %type <program_section> program_section
113 %type <stmt> code_segment
114 %type <func_def> function_definition
115 %type <class_def> class_definition
116 %type <class_body> class_body
117 %type <class_ext> class_ext
118 %type <program_section> class_section
119 %type <stmt_list> statement_list
120 %type <stmt> statement
121 //%type <stmt_label> label_statement
122 %type <stmt> loop_statement
123 %type <stmt> selection_statement
124 %type <stmt> jump_statement
125 %type <stmt> expression_statement
126 %type <exp> expression
127 %type <exp> chuck_expression
128 %type <exp> conditional_expression
129 %type <exp> logical_or_expression
130 %type <exp> logical_and_expression
131 %type <exp> inclusive_or_expression
132 %type <exp> exclusive_or_expression
133 %type <exp> and_expression
134 %type <exp> equality_expression
135 %type <exp> relational_expression
136 %type <exp> shift_expression
137 %type <exp> additive_expression
138 %type <exp> multiplicative_expression
139 %type <exp> tilda_expression
140 %type <exp> cast_expression
141 %type <exp> unary_expression
142 %type <exp> postfix_expression
143 %type <exp> primary_expression
144 %type <exp> decl_expression
145 %type <ival> unary_operator
146 %type <ival> chuck_operator
147 %type <var_decl_list> var_decl_list
148 %type <var_decl> var_decl
149 %type <type_decl> type_decl
150 %type <ival> function_decl
151 %type <arg_list> arg_list
152 %type <id_list> id_list
154 %start program
158 program
159 : program_section { $$ = g_program = new_program( $1, EM_lineNum ); }
160 | program_section program { $$ = g_program = prepend_program( $1, $2, EM_lineNum ); }
163 program_section
164 : statement_list { $$ = new_section_stmt( $1, EM_lineNum ); }
165 | function_definition { $$ = new_section_func_def( $1, EM_lineNum ); }
166 | class_definition { $$ = new_section_class_def( $1, EM_lineNum ); }
169 class_definition
170 : CLASS ID LBRACE class_body RBRACE
171 { $$ = new_class_def( $2, NULL, $4, EM_lineNum ); }
172 | CLASS ID class_ext LBRACE class_body RBRACE
173 { $$ = new_class_def( $2, $3, $5, EM_lineNum ); }
176 class_ext
177 : IMPLEMENTS id_list { $$ = new_class_ext( NULL, $2, EM_lineNum ); }
178 | IMPLEMENTS id_list EXTENDS ID { $$ = new_class_ext( $4, $2, EM_lineNum ); }
179 | EXTENDS ID { $$ = new_class_ext( $2, NULL, EM_lineNum ); }
180 | EXTENDS ID IMPLEMENTS id_list { $$ = new_class_ext( $2, $4, EM_lineNum ); }
183 class_body
184 : class_section { $$ = new_class_body( $1, EM_lineNum ); }
185 | class_section class_body { $$ = prepend_class_body( $1, $2, EM_lineNum ); }
188 class_section
189 : statement_list { $$ = new_section_stmt( $1, EM_lineNum ); }
190 | function_definition { $$ = new_section_func_def( $1, EM_lineNum ); }
193 id_list
194 : ID { $$ = new_id_list( $1, EM_lineNum ); }
195 | ID COMMA id_list { $$ = prepend_id_list( $1, $3, EM_lineNum ); }
198 function_definition
199 : function_decl type_decl ID LPAREN arg_list RPAREN code_segment
200 { $$ = new_func_def( $1, $2, $3, $5, $7, EM_lineNum ); }
201 | function_decl type_decl ID LPAREN RPAREN code_segment
202 { $$ = new_func_def( $1, $2, $3, NULL, $6, EM_lineNum ); }
205 function_decl
206 : FUNCTION { $$ = ae_func_func; }
207 | PUBLIC { $$ = ae_func_public; }
208 | PROTECTED { $$ = ae_func_protected; }
209 | PRIVATE { $$ = ae_func_private; }
212 type_decl
213 : ID { $$ = new_type_decl( $1, EM_lineNum ); }
214 | type_decl LBRACK RBRACK { $$ = new_type_decl_array( $1, EM_lineNum ); }
217 arg_list
218 : type_decl ID { $$ = new_arg_list( $1, $2, EM_lineNum ); }
219 | type_decl ID COMMA arg_list { $$ = prepend_arg_list( $1, $2, $4, EM_lineNum ); }
222 statement_list
223 : statement { $$ = new_stmt_list( $1, EM_lineNum ); }
224 | statement statement_list { $$ = prepend_stmt_list( $1, $2, EM_lineNum ); }
227 statement
228 : expression_statement { $$ = $1; }
229 | loop_statement { $$ = $1; }
230 | selection_statement { $$ = $1; }
231 | jump_statement { $$ = $1; }
232 //| label_statement { }
233 | code_segment { $$ = $1; }
236 jump_statement
237 : RETURN SEMICOLON { $$ = new_stmt_from_return( NULL, EM_lineNum ); }
238 | RETURN expression SEMICOLON { $$ = new_stmt_from_return( $2, EM_lineNum ); }
241 selection_statement
242 : IF LPAREN expression RPAREN statement
243 { $$ = new_stmt_from_if( $3, $5, NULL, EM_lineNum ); }
244 | IF LPAREN expression RPAREN statement ELSE statement
245 { $$ = new_stmt_from_if( $3, $5, $7, EM_lineNum ); }
248 loop_statement
249 : WHILE LPAREN expression RPAREN statement
250 { $$ = new_stmt_from_while( $3, $5, EM_lineNum ); }
251 | DO statement WHILE LPAREN expression RPAREN SEMICOLON
252 { $$ = new_stmt_from_do_while( $5, $2, EM_lineNum ); }
253 | FOR LPAREN expression_statement expression_statement RPAREN statement
254 { $$ = new_stmt_from_for( $3, $4, NULL, $6, EM_lineNum ); }
255 | FOR LPAREN expression_statement expression_statement expression RPAREN statement
256 { $$ = new_stmt_from_for( $3, $4, $5, $7, EM_lineNum ); }
257 | UNTIL LPAREN expression RPAREN statement
258 { $$ = new_stmt_from_until( $3, $5, EM_lineNum ); }
259 | DO statement UNTIL LPAREN expression RPAREN SEMICOLON
260 { $$ = new_stmt_from_do_until( $5, $2, EM_lineNum ); }
263 code_segment
264 : LBRACE RBRACE { $$ = NULL; }
265 | LBRACE statement_list RBRACE { $$ = new_stmt_from_code( $2, EM_lineNum ); }
268 expression_statement
269 : SEMICOLON { $$ = NULL; }
270 | expression SEMICOLON { $$ = new_stmt_from_expression( $1, EM_lineNum ); }
273 expression
274 : chuck_expression { $$ = $1; }
275 | chuck_expression COMMA expression { $$ = prepend_expression( $1, $3, EM_lineNum ); }
278 chuck_expression
279 : decl_expression { $$ = $1; }
280 // | rassign_expression { $$ = $1; }
281 | chuck_expression chuck_operator decl_expression
282 { $$ = new_exp_from_binary( $1, $2, $3, EM_lineNum ); }
285 decl_expression
286 : conditional_expression { $$ = $1; }
287 | ID var_decl_list { $$ = new_exp_decl( $1, $2, EM_lineNum ); }
288 | AT_SYM var_decl_list { $$ = new_exp_decl( NULL, $2, EM_lineNum ); }
291 var_decl_list
292 : var_decl { $$ = new_var_decl_list( $1, EM_lineNum ); }
293 // | var_decl COMMA var_decl_list { $$ = prepend_var_decl_list( $1, $3, EM_lineNum ); }
296 var_decl
297 : ID { $$ = new_var_decl( $1, EM_lineNum ); }
298 | var_decl LBRACK RBRACK { $$ = new_var_decl2( $1, 1, NULL, EM_lineNum ); }
299 | var_decl LBRACK expression RBRACK { $$ = new_var_decl2( $1, 1, $3, EM_lineNum ); }
302 chuck_operator
303 : CHUCK { $$ = ae_op_chuck; }
304 | S_CHUCK { $$ = ae_op_s_chuck; }
305 | AT_CHUCK { $$ = ae_op_at_chuck; }
306 | PLUS_CHUCK { $$ = ae_op_plus_chuck; }
307 | MINUS_CHUCK { $$ = ae_op_minus_chuck; }
308 | TIMES_CHUCK { $$ = ae_op_times_chuck; }
309 | DIVIDE_CHUCK { $$ = ae_op_divide_chuck; }
310 | S_AND_CHUCK { $$ = ae_op_s_and_chuck; }
311 | S_OR_CHUCK { $$ = ae_op_s_or_chuck; }
312 | S_XOR_CHUCK { $$ = ae_op_s_xor_chuck; }
313 | SHIFT_RIGHT_CHUCK { $$ = ae_op_shift_right_chuck; }
314 | SHIFT_LEFT_CHUCK { $$ = ae_op_shift_left_chuck; }
315 | PERCENT_CHUCK { $$ = ae_op_percent_chuck; }
316 | UNCHUCK { $$ = ae_op_unchuck; }
319 conditional_expression
320 : logical_or_expression { $$ = $1; }
321 | logical_or_expression QUESTION expression COLON conditional_expression
322 { $$ = new_exp_from_if( $1, $3, $5, EM_lineNum ); }
325 logical_or_expression
326 : logical_and_expression { $$ = $1; }
327 | logical_or_expression OR logical_and_expression
328 { $$ = new_exp_from_binary( $1, ae_op_or, $3, EM_lineNum ); }
331 logical_and_expression
332 : inclusive_or_expression { $$ = $1; }
333 | logical_and_expression AND inclusive_or_expression
334 { $$ = new_exp_from_binary( $1, ae_op_and, $3, EM_lineNum ); }
337 inclusive_or_expression
338 : exclusive_or_expression { $$ = $1; }
339 | inclusive_or_expression S_OR exclusive_or_expression
340 { $$ = new_exp_from_binary( $1, ae_op_s_or, $3, EM_lineNum ); }
343 exclusive_or_expression
344 : and_expression { $$ = $1; }
345 | exclusive_or_expression S_XOR and_expression
346 { $$ = new_exp_from_binary( $1, ae_op_s_xor, $3, EM_lineNum ); }
349 and_expression
350 : equality_expression { $$ = $1; }
351 | and_expression S_AND equality_expression
352 { $$ = new_exp_from_binary( $1, ae_op_s_and, $3, EM_lineNum ); }
355 equality_expression
356 : relational_expression { $$ = $1; }
357 | equality_expression EQ relational_expression
358 { $$ = new_exp_from_binary( $1, ae_op_eq, $3, EM_lineNum ); }
359 | equality_expression NEQ relational_expression
360 { $$ = new_exp_from_binary( $1, ae_op_neq, $3, EM_lineNum ); }
363 relational_expression
364 : shift_expression { $$ = $1; }
365 | relational_expression LT shift_expression
366 { $$ = new_exp_from_binary( $1, ae_op_lt, $3, EM_lineNum ); }
367 | relational_expression GT shift_expression
368 { $$ = new_exp_from_binary( $1, ae_op_gt, $3, EM_lineNum ); }
369 | relational_expression LE shift_expression
370 { $$ = new_exp_from_binary( $1, ae_op_le, $3, EM_lineNum ); }
371 | relational_expression GE shift_expression
372 { $$ = new_exp_from_binary( $1, ae_op_ge, $3, EM_lineNum ); }
375 shift_expression
376 : additive_expression { $$ = $1; }
377 | shift_expression SHIFT_LEFT additive_expression
378 { $$ = new_exp_from_binary( $1, ae_op_shift_left, $3, EM_lineNum ); }
379 | shift_expression SHIFT_RIGHT additive_expression
380 { $$ = new_exp_from_binary( $1, ae_op_shift_right, $3, EM_lineNum ); }
383 additive_expression
384 : multiplicative_expression { $$ = $1; }
385 | additive_expression PLUS multiplicative_expression
386 { $$ = new_exp_from_binary( $1, ae_op_plus, $3, EM_lineNum ); }
387 | additive_expression MINUS multiplicative_expression
388 { $$ = new_exp_from_binary( $1, ae_op_minus, $3, EM_lineNum ); }
391 multiplicative_expression
392 : tilda_expression { $$ = $1; }
393 | multiplicative_expression TIMES tilda_expression
394 { $$ = new_exp_from_binary( $1, ae_op_times, $3, EM_lineNum ); }
395 | multiplicative_expression DIVIDE tilda_expression
396 { $$ = new_exp_from_binary( $1, ae_op_divide, $3, EM_lineNum ); }
397 | multiplicative_expression PERCENT tilda_expression
398 { $$ = new_exp_from_binary( $1, ae_op_percent, $3, EM_lineNum ); }
401 tilda_expression
402 : cast_expression { $$ = $1; }
403 | tilda_expression TILDA cast_expression
404 { $$ = new_exp_from_binary( $1, ae_op_tilda, $3, EM_lineNum ); }
407 cast_expression
408 : unary_expression { $$ = $1; }
409 | LPAREN ID RPAREN cast_expression { $$ = new_exp_from_cast( $2, $4, EM_lineNum ); }
410 //| LT ID GT cast_expression { $$ = new_exp_from_cast( $2, $4, EM_lineNum ); }
413 unary_expression
414 : postfix_expression { $$ = $1; }
415 | PLUSPLUS unary_expression
416 { $$ = new_exp_from_unary( ae_op_plusplus, $2, EM_lineNum ); }
417 | MINUSMINUS unary_expression
418 { $$ = new_exp_from_unary( ae_op_minusminus, $2, EM_lineNum ); }
419 | unary_operator cast_expression
420 { $$ = new_exp_from_unary( $1, $2, EM_lineNum ); }
421 //| SIZEOF unary_expression { }
424 unary_operator
425 : PLUS { $$ = ae_op_plus; }
426 | MINUS { $$ = ae_op_minus; }
427 | TILDA { $$ = ae_op_tilda; }
428 | EXCLAMATION { $$ = ae_op_exclamation; }
429 | S_AND { $$ = ae_op_s_and; }
430 | TIMES { $$ = ae_op_times; }
431 | SPORK TILDA { $$ = ae_op_spork; }
434 postfix_expression
435 : primary_expression { $$ = $1; }
436 | postfix_expression LBRACK expression RBRACK
437 { $$ = new_exp_from_array( $1, $3, EM_lineNum ); }
438 | postfix_expression LPAREN RPAREN
439 { $$ = new_exp_from_func_call( $1, NULL, EM_lineNum ); }
440 | postfix_expression LPAREN expression RPAREN
441 { $$ = new_exp_from_func_call( $1, $3, EM_lineNum ); }
442 | postfix_expression DOT ID
443 { $$ = new_exp_from_member_dot( $1, $3, EM_lineNum ); }
444 | postfix_expression PLUSPLUS
445 { $$ = new_exp_from_postfix( $1, ae_op_plusplus, EM_lineNum ); }
446 | postfix_expression MINUSMINUS
447 { $$ = new_exp_from_postfix( $1, ae_op_minusminus, EM_lineNum ); }
448 | postfix_expression COLONCOLON primary_expression
449 { $$ = new_exp_from_dur( $1, $3, EM_lineNum ); }
450 //| postfix_expression RARROW ID { }
453 primary_expression
454 : ID { $$ = new_exp_from_id( $1, EM_lineNum ); }
455 | NUM { $$ = new_exp_from_int( $1, EM_lineNum ); }
456 | FLOAT { $$ = new_exp_from_float( $1, EM_lineNum ); }
457 | STRING_LIT { $$ = new_exp_from_str( $1, EM_lineNum ); }
458 | L_NSPC R_NSPC { $$ = new_exp_from_namespace( "", EM_lineNum ); }
459 | L_NSPC ID R_NSPC { $$ = new_exp_from_namespace( $2, EM_lineNum ); }
460 | LPAREN expression RPAREN { $$ = $2; }