1829
|
1 /* |
1
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
|
23 // Parser for Octave. |
|
24 |
767
|
25 // C decarations. |
|
26 |
1
|
27 %{ |
|
28 #define YYDEBUG 1 |
|
29 |
240
|
30 #ifdef HAVE_CONFIG_H |
1229
|
31 #include <config.h> |
240
|
32 #endif |
|
33 |
2427
|
34 #ifdef YYBYACC |
|
35 #include <cstdlib> |
|
36 #endif |
|
37 |
581
|
38 #include <strstream.h> |
|
39 |
1
|
40 #include "Matrix.h" |
|
41 |
2166
|
42 #include "defun.h" |
1351
|
43 #include "error.h" |
|
44 #include "input.h" |
|
45 #include "lex.h" |
1743
|
46 #include "oct-hist.h" |
1670
|
47 #include "toplev.h" |
1351
|
48 #include "pager.h" |
|
49 #include "parse.h" |
1740
|
50 #include "pt-cmd.h" |
|
51 #include "pt-const.h" |
|
52 #include "pt-fcn.h" |
|
53 #include "pt-fvc.h" |
|
54 #include "pt-mat.h" |
|
55 #include "pt-mvr.h" |
|
56 #include "pt-exp.h" |
|
57 #include "pt-misc.h" |
|
58 #include "pt-plot.h" |
2533
|
59 #include "pt-pr-code.h" |
1351
|
60 #include "symtab.h" |
|
61 #include "token.h" |
1
|
62 #include "utils.h" |
1351
|
63 #include "variables.h" |
1
|
64 |
2166
|
65 // If TRUE, generate a warning for the assignment in things like |
|
66 // |
|
67 // octave> if (a = 2 < n) |
|
68 // |
|
69 // but not |
|
70 // |
|
71 // octave> if ((a = 2) < n) |
|
72 // |
|
73 static bool Vwarn_assign_as_truth_value; |
|
74 |
2764
|
75 // If TRUE, generate a warning for variable swich labels. |
|
76 static bool Vwarn_variable_switch_label; |
|
77 |
2166
|
78 // If TRUE, generate a warning for the comma in things like |
|
79 // |
|
80 // octave> global a, b = 2 |
|
81 // |
|
82 static bool Vwarn_comma_in_global_decl; |
|
83 |
|
84 // If TRUE, generate warning if declared function name disagrees with |
|
85 // the name of the file in which it is defined. |
|
86 static bool Vwarn_function_name_clash; |
|
87 |
|
88 // If TRUE, generate warning if a statement in a function is not |
|
89 // terminated with a semicolon. Useful for checking functions that |
|
90 // should only produce output using explicit printing statements. |
|
91 static bool Vwarn_missing_semicolon; |
|
92 |
1
|
93 // Temporary symbol table pointer used to cope with bogus function syntax. |
522
|
94 symbol_table *tmp_local_sym_tab = 0; |
1
|
95 |
|
96 // The current input line number. |
|
97 int input_line_number = 0; |
|
98 |
|
99 // The column of the current token. |
143
|
100 int current_input_column = 1; |
1
|
101 |
338
|
102 // Buffer for help text snagged from function files. |
1755
|
103 string help_buf; |
1
|
104 |
496
|
105 // Forward declarations for some functions defined at the bottom of |
|
106 // the file. |
|
107 |
|
108 // Generic error messages. |
2805
|
109 static void yyerror (const char *s); |
1
|
110 |
578
|
111 // Error mesages for mismatched end tokens. |
2805
|
112 static void end_error |
|
113 (const char *type, token::end_tok_type ettype, int l, int c); |
1
|
114 |
578
|
115 // Check to see that end tokens are properly matched. |
496
|
116 static int check_end (token *tok, token::end_tok_type expected); |
1
|
117 |
496
|
118 // Try to figure out early if an expression should become an |
1623
|
119 // assignment to the built-in variable ans. |
496
|
120 static tree_expression *maybe_convert_to_ans_assign (tree_expression *expr); |
|
121 |
|
122 // Maybe print a warning if an assignment expression is used as the |
|
123 // test in a logical expression. |
|
124 static void maybe_warn_assign_as_truth_value (tree_expression *expr); |
1
|
125 |
2764
|
126 // Maybe print a warning about switch labels that aren't constants. |
|
127 static void maybe_warn_variable_switch_label (tree_expression *expr); |
|
128 |
1623
|
129 // Create a plot command. |
|
130 static tree_plot_command *make_plot_command |
|
131 (token *tok, plot_limits *range, subplot_list *list); |
|
132 |
|
133 // Finish building a range. |
|
134 static tree_expression *finish_colon_expression (tree_colon_expression *e); |
|
135 |
1607
|
136 // Build a constant. |
2375
|
137 static tree_constant *make_constant (int op, token *tok_val); |
1607
|
138 |
578
|
139 // Build a binary expression. |
751
|
140 static tree_expression *make_binary_op |
|
141 (int op, tree_expression *op1, token *tok_val, tree_expression *op2); |
578
|
142 |
2375
|
143 // Build a boolean expression. |
|
144 static tree_expression *make_boolean_op |
|
145 (int op, tree_expression *op1, token *tok_val, tree_expression *op2); |
|
146 |
578
|
147 // Build a prefix expression. |
751
|
148 static tree_expression *make_prefix_op |
|
149 (int op, tree_identifier *op1, token *tok_val); |
578
|
150 |
|
151 // Build a postfix expression. |
751
|
152 static tree_expression *make_postfix_op |
|
153 (int op, tree_identifier *op1, token *tok_val); |
578
|
154 |
|
155 // Build a binary expression. |
751
|
156 static tree_expression *make_unary_op |
|
157 (int op, tree_expression *op1, token *tok_val); |
578
|
158 |
1623
|
159 // Build an unwind-protect command. |
|
160 static tree_command *make_unwind_command |
|
161 (token *unwind_tok, tree_statement_list *body, |
|
162 tree_statement_list *cleanup, token *end_tok); |
|
163 |
|
164 // Build a try-catch command. |
|
165 static tree_command *make_try_command |
|
166 (token *try_tok, tree_statement_list *body, |
|
167 tree_statement_list *cleanup, token *end_tok); |
|
168 |
|
169 // Build a while command. |
|
170 static tree_command *make_while_command |
|
171 (token *while_tok, tree_expression *expr, |
|
172 tree_statement_list *body, token *end_tok); |
|
173 |
|
174 // Build a for command. |
|
175 static tree_command *make_for_command |
|
176 (token *for_tok, tree_index_expression *var, |
|
177 tree_expression *expr, tree_statement_list *body, |
|
178 token *end_tok); |
|
179 |
|
180 // Build a for command a different way. |
|
181 static tree_command *make_for_command |
1829
|
182 (token *for_tok, tree_matrix_row *mr, tree_expression *expr, |
1623
|
183 tree_statement_list *body, token *end_tok); |
|
184 |
|
185 // Build a break command. |
|
186 static tree_command *make_break_command (token *break_tok); |
|
187 |
|
188 // Build a continue command. |
|
189 static tree_command *make_continue_command (token *continue_tok); |
|
190 |
|
191 // Build a return command. |
|
192 static tree_command *make_return_command (token *return_tok); |
|
193 |
|
194 // Start an if command. |
|
195 static tree_if_command_list *start_if_command |
|
196 (tree_expression *expr, tree_statement_list *list); |
|
197 |
|
198 // Finish an if command. |
|
199 static tree_if_command *finish_if_command |
|
200 (token *if_tok, tree_if_command_list *list, token *end_tok); |
|
201 |
|
202 // Build an elseif clause. |
|
203 static tree_if_clause *make_elseif_clause |
|
204 (tree_expression *expr, tree_statement_list *list); |
|
205 |
2764
|
206 // Finish a switch command. |
|
207 static tree_switch_command *finish_switch_command |
|
208 (token *switch_tok, tree_expression *expr, |
|
209 tree_switch_case_list *list, token *end_tok); |
|
210 |
|
211 // Build a switch case. |
|
212 static tree_switch_case *make_switch_case |
|
213 (tree_expression *expr, tree_statement_list *list); |
|
214 |
1623
|
215 // Build an assignment to a variable. |
|
216 static tree_expression *make_simple_assignment |
|
217 (tree_index_expression *var, token *eq_tok, tree_expression *expr); |
|
218 |
666
|
219 // Make an expression that handles assignment of multiple values. |
751
|
220 static tree_expression *make_multi_val_ret |
1829
|
221 (tree_matrix_row *mr, tree_expression *rhs, token *eq_tok); |
1623
|
222 |
|
223 // Begin defining a function. |
|
224 static tree_function *start_function_def |
|
225 (tree_parameter_list *param_list, tree_statement_list *body); |
|
226 |
|
227 // Do most of the work for defining a function. |
|
228 static tree_function *frob_function_def |
|
229 (tree_identifier *id, tree_function *fcn); |
|
230 |
|
231 // Finish defining a function. |
|
232 static tree_function *finish_function_def (token *var, tree_function *fcn); |
|
233 |
|
234 // Finish defining a function a different way. |
|
235 static tree_function *finish_function_def |
|
236 (tree_parameter_list *ret_list, tree_function *fcn); |
751
|
237 |
|
238 // Make an index expression. |
|
239 static tree_index_expression *make_index_expression |
|
240 (tree_indirect_ref *indir, tree_argument_list *args); |
666
|
241 |
1623
|
242 // Finish building a matrix list. |
1829
|
243 static tree_expression *finish_matrix (tree_matrix *m); |
1623
|
244 |
1511
|
245 // Maybe print a warning. Duh. |
|
246 static void maybe_warn_missing_semi (tree_statement_list *); |
|
247 |
2525
|
248 // Set the print flag for a statement based on the separator type. |
|
249 static void set_stmt_print_flag (tree_statement_list *, char, bool); |
|
250 |
1
|
251 #define ABORT_PARSE \ |
|
252 do \ |
|
253 { \ |
522
|
254 global_command = 0; \ |
1
|
255 yyerrok; \ |
|
256 if (interactive) \ |
|
257 YYACCEPT; \ |
|
258 else \ |
|
259 YYABORT; \ |
|
260 } \ |
|
261 while (0) |
|
262 |
|
263 %} |
|
264 |
666
|
265 // Bison declarations. |
|
266 |
1
|
267 %union |
|
268 { |
143
|
269 // The type of the basic tokens returned by the lexer. |
|
270 token *tok_val; |
|
271 |
|
272 // Types for the nonterminals we generate. |
2525
|
273 char sep_type; |
1
|
274 tree *tree_type; |
1829
|
275 tree_matrix *tree_matrix_type; |
|
276 tree_matrix_row *tree_matrix_row_type; |
496
|
277 tree_expression *tree_expression_type; |
2375
|
278 tree_constant *tree_constant_type; |
1
|
279 tree_identifier *tree_identifier_type; |
751
|
280 tree_indirect_ref *tree_indirect_ref_type; |
1
|
281 tree_function *tree_function_type; |
|
282 tree_index_expression *tree_index_expression_type; |
|
283 tree_colon_expression *tree_colon_expression_type; |
|
284 tree_argument_list *tree_argument_list_type; |
|
285 tree_parameter_list *tree_parameter_list_type; |
|
286 tree_command *tree_command_type; |
|
287 tree_if_command *tree_if_command_type; |
578
|
288 tree_if_clause *tree_if_clause_type; |
|
289 tree_if_command_list *tree_if_command_list_type; |
2764
|
290 tree_switch_command *tree_switch_command_type; |
|
291 tree_switch_case *tree_switch_case_type; |
|
292 tree_switch_case_list *tree_switch_case_list_type; |
578
|
293 tree_global *tree_global_type; |
|
294 tree_global_init_list *tree_global_init_list_type; |
195
|
295 tree_global_command *tree_global_command_type; |
578
|
296 tree_statement *tree_statement_type; |
|
297 tree_statement_list *tree_statement_list_type; |
1
|
298 tree_plot_command *tree_plot_command_type; |
578
|
299 subplot *subplot_type; |
|
300 subplot_list *subplot_list_type; |
|
301 plot_limits *plot_limits_type; |
|
302 plot_range *plot_range_type; |
|
303 subplot_using *subplot_using_type; |
|
304 subplot_style *subplot_style_type; |
1
|
305 } |
|
306 |
143
|
307 // Tokens with line and column information. |
|
308 %token <tok_val> '=' ':' '-' '+' '*' '/' |
428
|
309 %token <tok_val> EXPR_AND_AND EXPR_OR_OR |
143
|
310 %token <tok_val> EXPR_AND EXPR_OR EXPR_NOT |
|
311 %token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
1276
|
312 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS |
|
313 %token <tok_val> QUOTE TRANSPOSE |
143
|
314 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW |
|
315 %token <tok_val> NUM IMAG_NUM |
169
|
316 %token <tok_val> NAME SCREW |
143
|
317 %token <tok_val> END |
|
318 %token <tok_val> PLOT |
|
319 %token <tok_val> TEXT STYLE |
1491
|
320 %token <tok_val> FOR WHILE |
|
321 %token <tok_val> IF ELSEIF ELSE |
2764
|
322 %token <tok_val> SWITCH CASE OTHERWISE |
1491
|
323 %token <tok_val> BREAK CONTINUE FUNC_RET |
924
|
324 %token <tok_val> UNWIND CLEANUP |
1489
|
325 %token <tok_val> TRY CATCH |
578
|
326 %token <tok_val> GLOBAL |
751
|
327 %token <tok_val> TEXT_ID |
1
|
328 |
143
|
329 // Other tokens. |
627
|
330 %token LEXICAL_ERROR |
191
|
331 %token FCN SCREW_TWO |
206
|
332 %token ELLIPSIS |
922
|
333 %token ALL_VA_ARGS |
206
|
334 %token END_OF_INPUT |
883
|
335 %token USING TITLE WITH COLON OPEN_BRACE CLOSE_BRACE CLEAR |
1
|
336 |
143
|
337 // Nonterminals we construct. |
2525
|
338 %type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep |
578
|
339 %type <tree_type> input |
1829
|
340 %type <tree_matrix_type> rows rows1 |
|
341 %type <tree_matrix_row_type> matrix_row matrix_row1 |
496
|
342 %type <tree_expression_type> expression simple_expr simple_expr1 |
1623
|
343 %type <tree_expression_type> ans_expression title matrix |
1
|
344 %type <tree_identifier_type> identifier |
751
|
345 %type <tree_indirect_ref_type> indirect_ref indirect_ref1 |
578
|
346 %type <tree_function_type> func_def1 func_def2 func_def3 |
482
|
347 %type <tree_index_expression_type> variable word_list_cmd |
1
|
348 %type <tree_colon_expression_type> colon_expr |
578
|
349 %type <tree_argument_list_type> arg_list word_list |
723
|
350 %type <tree_parameter_list_type> param_list param_list1 |
|
351 %type <tree_parameter_list_type> return_list return_list1 |
578
|
352 %type <tree_command_type> command func_def |
|
353 %type <tree_if_command_type> if_command |
|
354 %type <tree_if_clause_type> elseif_clause else_clause |
|
355 %type <tree_if_command_list_type> if_cmd_list1 if_cmd_list |
2764
|
356 %type <tree_switch_command_type> switch_command |
|
357 %type <tree_switch_case_type> switch_case default_case |
|
358 %type <tree_switch_case_list_type> case_list1 case_list |
578
|
359 %type <tree_global_type> global_decl2 |
|
360 %type <tree_global_init_list_type> global_decl1 |
|
361 %type <tree_global_command_type> global_decl |
|
362 %type <tree_statement_type> statement |
627
|
363 %type <tree_statement_list_type> simple_list simple_list1 list list1 |
|
364 %type <tree_statement_list_type> opt_list input1 |
1
|
365 %type <tree_plot_command_type> plot_command |
578
|
366 %type <subplot_type> plot_command2 plot_options |
|
367 %type <subplot_list_type> plot_command1 |
|
368 %type <plot_limits_type> ranges |
|
369 %type <plot_range_type> ranges1 |
|
370 %type <subplot_using_type> using using1 |
|
371 %type <subplot_style_type> style |
1
|
372 |
143
|
373 // Precedence and associativity. |
1
|
374 %left ';' ',' '\n' |
|
375 %right '=' |
428
|
376 %left EXPR_AND_AND EXPR_OR_OR |
1
|
377 %left EXPR_AND EXPR_OR |
|
378 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
|
379 %left ':' |
1276
|
380 %left '-' '+' EPLUS EMINUS |
1
|
381 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
382 %left QUOTE TRANSPOSE |
|
383 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
384 %right POW EPOW |
|
385 |
2524
|
386 // There are 18 shift/reduce conflicts, ok? But this only works with |
2427
|
387 // bison... |
2524
|
388 // %expect 18 |
143
|
389 |
|
390 // Where to start. |
1
|
391 %start input |
|
392 |
666
|
393 // Grammar rules. |
|
394 |
1
|
395 %% |
|
396 |
627
|
397 input : input1 |
1
|
398 { |
627
|
399 global_command = $1; |
1
|
400 promptflag = 1; |
|
401 YYACCEPT; |
|
402 } |
|
403 | END_OF_INPUT |
|
404 { |
522
|
405 global_command = 0; |
1
|
406 promptflag = 1; |
|
407 YYABORT; |
|
408 } |
627
|
409 | simple_list parse_error |
1091
|
410 { ABORT_PARSE; } |
627
|
411 | parse_error |
1091
|
412 { ABORT_PARSE; } |
627
|
413 ; |
|
414 |
|
415 input1 : '\n' |
|
416 { $$ = 0; } |
327
|
417 | simple_list |
627
|
418 { $$ = $1; } |
1
|
419 | simple_list '\n' |
627
|
420 { $$ = $1; } |
1
|
421 | simple_list END_OF_INPUT |
627
|
422 { $$ = $1; } |
|
423 ; |
|
424 |
|
425 parse_error : LEXICAL_ERROR |
1091
|
426 { yyerror ("parse error"); } |
1
|
427 | error |
|
428 ; |
|
429 |
2525
|
430 simple_list : simple_list1 opt_sep_no_nl |
1
|
431 { |
2525
|
432 set_stmt_print_flag ($1, $2, false); |
1511
|
433 $$ = $1; |
1
|
434 } |
|
435 ; |
|
436 |
578
|
437 simple_list1 : statement |
|
438 { $$ = new tree_statement_list ($1); } |
2525
|
439 | simple_list1 sep_no_nl statement |
1
|
440 { |
2525
|
441 set_stmt_print_flag ($1, $2, false); |
578
|
442 $1->append ($3); |
1511
|
443 $$ = $1; |
1
|
444 } |
|
445 ; |
|
446 |
|
447 opt_list : // empty |
578
|
448 { $$ = new tree_statement_list (); } |
1
|
449 | list |
|
450 { $$ = $1; } |
496
|
451 ; |
1
|
452 |
2525
|
453 list : list1 opt_sep |
1511
|
454 { |
2525
|
455 set_stmt_print_flag ($1, $2, true); |
1829
|
456 $$ = $1; |
1
|
457 } |
|
458 ; |
|
459 |
578
|
460 list1 : statement |
440
|
461 { |
1826
|
462 lexer_flags.beginning_of_function = 0; |
578
|
463 $$ = new tree_statement_list ($1); |
440
|
464 } |
2525
|
465 | list1 sep statement |
1511
|
466 { |
2525
|
467 set_stmt_print_flag ($1, $2, true); |
578
|
468 $1->append ($3); |
1511
|
469 $$ = $1; |
1
|
470 } |
|
471 ; |
|
472 |
578
|
473 statement : command |
|
474 { $$ = new tree_statement ($1); } |
1
|
475 | ans_expression |
578
|
476 { $$ = new tree_statement ($1); } |
883
|
477 | PLOT CLEAR |
|
478 { |
|
479 symbol_record *sr = lookup_by_name ("clearplot", 0); |
|
480 tree_identifier *id = new tree_identifier (sr); |
|
481 $$ = new tree_statement (id); |
|
482 } |
1
|
483 ; |
|
484 |
|
485 plot_command : PLOT plot_command1 |
|
486 { |
1623
|
487 if (! ($$ = make_plot_command ($1, 0, $2))) |
|
488 ABORT_PARSE; |
1
|
489 } |
|
490 | PLOT ranges plot_command1 |
|
491 { |
1623
|
492 if (! ($$ = make_plot_command ($1, $2, $3))) |
|
493 ABORT_PARSE; |
1
|
494 } |
|
495 ; |
|
496 |
|
497 ranges : ranges1 |
578
|
498 { $$ = new plot_limits ($1); } |
1
|
499 | ranges1 ranges1 |
578
|
500 { $$ = new plot_limits ($1, $2); } |
1
|
501 | ranges1 ranges1 ranges1 |
578
|
502 { $$ = new plot_limits ($1, $2, $3); } |
1
|
503 ; |
|
504 |
|
505 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
578
|
506 { $$ = new plot_range ($2, $4); } |
1
|
507 | OPEN_BRACE COLON expression CLOSE_BRACE |
578
|
508 { $$ = new plot_range (0, $3); } |
1
|
509 | OPEN_BRACE expression COLON CLOSE_BRACE |
578
|
510 { $$ = new plot_range ($2, 0); } |
1
|
511 | OPEN_BRACE COLON CLOSE_BRACE |
578
|
512 { $$ = new plot_range (); } |
1
|
513 | OPEN_BRACE CLOSE_BRACE |
578
|
514 { $$ = new plot_range (); } |
1
|
515 ; |
|
516 |
478
|
517 plot_command1 : // empty |
522
|
518 { $$ = 0; } |
478
|
519 | plot_command2 |
578
|
520 { $$ = new subplot_list ($1); } |
1
|
521 | plot_command1 ',' plot_command2 |
1829
|
522 { |
|
523 $1->append ($3); |
|
524 $$ = $1; |
|
525 } |
1
|
526 ; |
|
527 |
|
528 plot_command2 : expression |
578
|
529 { $$ = new subplot ($1); } |
1
|
530 | expression plot_options |
1623
|
531 { $$ = $2->set_data ($1); } |
1
|
532 ; |
|
533 |
|
534 plot_options : using |
578
|
535 { $$ = new subplot ($1, 0, 0); } |
1
|
536 | title |
578
|
537 { $$ = new subplot (0, $1, 0); } |
1
|
538 | style |
578
|
539 { $$ = new subplot (0, 0, $1); } |
1
|
540 | using title |
578
|
541 { $$ = new subplot ($1, $2, 0); } |
|
542 | title using |
|
543 { $$ = new subplot ($2, $1, 0); } |
|
544 | using style |
|
545 { $$ = new subplot ($1, 0, $2); } |
|
546 | style using |
|
547 { $$ = new subplot ($2, 0, $1); } |
|
548 | title style |
|
549 { $$ = new subplot (0, $1, $2); } |
|
550 | style title |
|
551 { $$ = new subplot (0, $2, $1); } |
|
552 | using title style |
|
553 { $$ = new subplot ($1, $2, $3); } |
|
554 | using style title |
|
555 { $$ = new subplot ($1, $3, $2); } |
|
556 | title using style |
|
557 { $$ = new subplot ($2, $1, $3); } |
|
558 | title style using |
|
559 { $$ = new subplot ($3, $1, $2); } |
|
560 | style using title |
|
561 { $$ = new subplot ($2, $3, $1); } |
|
562 | style title using |
|
563 { $$ = new subplot ($3, $2, $1); } |
1
|
564 ; |
|
565 |
|
566 using : using1 |
|
567 { |
1826
|
568 lexer_flags.in_plot_using = 0; |
1
|
569 $$ = $1; |
|
570 } |
|
571 | using1 expression |
|
572 { |
1826
|
573 lexer_flags.in_plot_using = 0; |
1
|
574 $$ = $1->set_format ($2); |
|
575 } |
|
576 ; |
|
577 |
|
578 using1 : USING expression |
|
579 { |
578
|
580 subplot_using *tmp = new subplot_using (); |
1
|
581 $$ = tmp->add_qualifier ($2); |
|
582 } |
|
583 | using1 COLON expression |
|
584 { $$ = $1->add_qualifier ($3); } |
|
585 ; |
|
586 |
|
587 title : TITLE expression |
|
588 { $$ = $2; } |
|
589 ; |
|
590 |
|
591 style : WITH STYLE |
1755
|
592 { $$ = new subplot_style ($2->text ()); } |
1
|
593 | WITH STYLE expression |
1755
|
594 { $$ = new subplot_style ($2->text (), $3); } |
2542
|
595 | WITH STYLE expression expression |
|
596 { $$ = new subplot_style ($2->text (), $3, $4); } |
1
|
597 ; |
|
598 |
|
599 ans_expression : expression |
|
600 { $$ = maybe_convert_to_ans_assign ($1); } |
|
601 ; |
|
602 |
|
603 global_decl : GLOBAL global_decl1 |
578
|
604 { |
|
605 $$ = new tree_global_command ($2, $1->line (), |
|
606 $1->column ()); |
|
607 } |
1
|
608 ; |
|
609 |
578
|
610 global_decl1 : global_decl2 |
|
611 { $$ = new tree_global_init_list ($1); } |
|
612 | global_decl1 optcomma global_decl2 |
1829
|
613 { |
|
614 $1->append ($3); |
|
615 $$ = $1; |
|
616 } |
578
|
617 |
|
618 global_decl2 : identifier |
|
619 { $$ = new tree_global ($1); } |
|
620 | identifier '=' expression |
1
|
621 { |
578
|
622 tree_simple_assignment_expression *tmp_ass; |
|
623 tmp_ass = new tree_simple_assignment_expression |
581
|
624 ($1, $3, 0, 0, $2->line (), $2->column ()); |
578
|
625 $$ = new tree_global (tmp_ass); |
1
|
626 } |
|
627 ; |
|
628 |
|
629 optcomma : // empty |
|
630 | ',' |
|
631 { |
2166
|
632 if (Vwarn_comma_in_global_decl) |
1
|
633 warning ("comma in global declaration not\ |
|
634 interpreted as a command separator"); |
|
635 } |
|
636 ; |
|
637 |
578
|
638 command : plot_command |
|
639 { $$ = $1; } |
|
640 | func_def |
|
641 { $$ = $1; } |
|
642 | global_decl |
|
643 { $$ = $1; } |
2764
|
644 | switch_command |
|
645 { $$ = $1; } |
578
|
646 | if_command |
2764
|
647 { $$ = $1; } |
2525
|
648 | UNWIND opt_sep opt_list CLEANUP opt_sep opt_list END |
916
|
649 { |
1623
|
650 if (! ($$ = make_unwind_command ($1, $3, $6, $7))) |
916
|
651 ABORT_PARSE; |
|
652 } |
2525
|
653 | TRY opt_sep opt_list CATCH opt_sep opt_list END |
1489
|
654 { |
1623
|
655 if (! ($$ = make_try_command ($1, $3, $6, $7))) |
1489
|
656 ABORT_PARSE; |
|
657 } |
2525
|
658 | WHILE expression opt_sep opt_list END |
1
|
659 { |
1623
|
660 if (! ($$ = make_while_command ($1, $2, $4, $5))) |
143
|
661 ABORT_PARSE; |
1
|
662 } |
2525
|
663 | FOR variable '=' expression opt_sep opt_list END |
1
|
664 { |
1623
|
665 if (! ($$ = make_for_command ($1, $2, $4, $6, $7))) |
143
|
666 ABORT_PARSE; |
1
|
667 } |
1229
|
668 | FOR '[' screwed_again matrix_row SCREW_TWO '=' |
2525
|
669 expression opt_sep opt_list END |
1229
|
670 { |
1829
|
671 if (! ($$ = make_for_command ($1, $4, $7, $9, $10))) |
1229
|
672 ABORT_PARSE; |
|
673 } |
1
|
674 | BREAK |
|
675 { |
1623
|
676 if (! ($$ = make_break_command ($1))) |
|
677 ABORT_PARSE; |
1
|
678 } |
|
679 | CONTINUE |
|
680 { |
1623
|
681 if (! ($$ = make_continue_command ($1))) |
|
682 ABORT_PARSE; |
1
|
683 } |
|
684 | FUNC_RET |
|
685 { |
1623
|
686 if (! ($$ = make_return_command ($1))) |
|
687 ABORT_PARSE; |
1
|
688 } |
|
689 ; |
|
690 |
578
|
691 if_command : IF if_cmd_list END |
|
692 { |
1623
|
693 if (! ($$ = finish_if_command ($1, $2, $3))) |
578
|
694 ABORT_PARSE; |
|
695 } |
|
696 ; |
|
697 |
|
698 if_cmd_list : if_cmd_list1 |
2427
|
699 { $$ = $1; } |
578
|
700 | if_cmd_list1 else_clause |
1829
|
701 { |
|
702 $1->append ($2); |
|
703 $$ = $1; |
|
704 } |
578
|
705 ; |
|
706 |
2525
|
707 if_cmd_list1 : expression opt_sep opt_list |
1623
|
708 { $$ = start_if_command ($1, $3); } |
578
|
709 | if_cmd_list1 elseif_clause |
1829
|
710 { |
|
711 $1->append ($2); |
|
712 $$ = $1; |
|
713 } |
578
|
714 ; |
|
715 |
2525
|
716 elseif_clause : ELSEIF opt_sep expression opt_sep opt_list |
1623
|
717 { $$ = make_elseif_clause ($3, $5); } |
578
|
718 ; |
|
719 |
2525
|
720 else_clause : ELSE opt_sep opt_list |
578
|
721 { $$ = new tree_if_clause ($3); } |
1
|
722 ; |
|
723 |
2764
|
724 switch_command : SWITCH expression opt_sep case_list END |
|
725 { |
|
726 if (! ($$ = finish_switch_command ($1, $2, $4, $5))) |
|
727 ABORT_PARSE; |
|
728 } |
|
729 ; |
|
730 |
|
731 case_list : case_list1 |
|
732 { $$ = $1; } |
|
733 | case_list1 default_case |
|
734 { |
|
735 $1->append ($2); |
|
736 $$ = $1; |
|
737 } |
|
738 ; |
|
739 |
|
740 case_list1 : switch_case |
|
741 { $$ = new tree_switch_case_list ($1); } |
|
742 | case_list1 switch_case |
|
743 { |
|
744 $1->append ($2); |
|
745 $$ = $1; |
|
746 } |
|
747 ; |
|
748 |
|
749 switch_case : CASE opt_sep expression opt_sep list |
|
750 { $$ = make_switch_case ($3, $5); } |
|
751 ; |
|
752 |
|
753 default_case : OTHERWISE opt_sep opt_list |
|
754 { $$ = new tree_switch_case ($3); } |
|
755 ; |
|
756 |
1
|
757 screwed_again : // empty |
1826
|
758 { lexer_flags.maybe_screwed_again++; } |
1
|
759 ; |
|
760 |
1623
|
761 expression : simple_expr |
|
762 { $$ = $1; } |
1
|
763 | NUM '=' expression |
|
764 { |
1005
|
765 yyerror ("invalid assignment to a number"); |
522
|
766 $$ = 0; |
1
|
767 ABORT_PARSE; |
|
768 } |
1623
|
769 ; |
|
770 |
|
771 // Now that we do some simple constant folding, we have to make sure |
|
772 // that we get something valid back make_binary_op and make_unary_op. |
|
773 |
|
774 simple_expr : simple_expr1 |
666
|
775 { |
1623
|
776 if (! ($$ = $1)) |
666
|
777 ABORT_PARSE; |
|
778 } |
1
|
779 ; |
|
780 |
1623
|
781 simple_expr1 : NUM |
|
782 { $$ = make_constant (NUM, $1); } |
|
783 | IMAG_NUM |
|
784 { $$ = make_constant (IMAG_NUM, $1); } |
|
785 | TEXT |
|
786 { $$ = make_constant (TEXT, $1); } |
|
787 | '(' simple_expr ')' |
|
788 { |
2124
|
789 $2->mark_in_parens (); |
1623
|
790 $$ = $2; |
|
791 } |
|
792 | word_list_cmd |
|
793 { $$ = $1; } |
|
794 | variable |
|
795 { $$ = $1; } |
2533
|
796 | colon_expr |
|
797 { $$ = finish_colon_expression ($1); } |
1623
|
798 | matrix |
1
|
799 { $$ = $1; } |
1623
|
800 | '[' ']' |
2375
|
801 { $$ = new tree_constant (Matrix ()); } |
1623
|
802 | '[' ';' ']' |
2375
|
803 { $$ = new tree_constant (Matrix ()); } |
1623
|
804 | PLUS_PLUS identifier %prec UNARY |
|
805 { $$ = make_prefix_op (PLUS_PLUS, $2, $1); } |
|
806 | MINUS_MINUS identifier %prec UNARY |
|
807 { $$ = make_prefix_op (MINUS_MINUS, $2, $1); } |
|
808 | EXPR_NOT simple_expr |
|
809 { $$ = make_unary_op (EXPR_NOT, $2, $1); } |
|
810 | '+' simple_expr %prec UNARY |
|
811 { $$ = $2; } |
|
812 | '-' simple_expr %prec UNARY |
|
813 { $$ = make_unary_op ('-', $2, $1); } |
|
814 | variable '=' simple_expr |
|
815 { $$ = make_simple_assignment ($1, $2, $3); } |
|
816 | '[' screwed_again matrix_row SCREW_TWO '=' simple_expr |
|
817 { |
1829
|
818 if (! ($$ = make_multi_val_ret ($3, $6, $5))) |
1623
|
819 ABORT_PARSE; |
|
820 } |
1
|
821 | identifier PLUS_PLUS |
578
|
822 { $$ = make_postfix_op (PLUS_PLUS, $1, $2); } |
1
|
823 | identifier MINUS_MINUS |
578
|
824 { $$ = make_postfix_op (MINUS_MINUS, $1, $2); } |
1
|
825 | simple_expr QUOTE |
578
|
826 { $$ = make_unary_op (QUOTE, $1, $2); } |
1
|
827 | simple_expr TRANSPOSE |
578
|
828 { $$ = make_unary_op (TRANSPOSE, $1, $2); } |
1
|
829 | simple_expr POW simple_expr |
578
|
830 { $$ = make_binary_op (POW, $1, $2, $3); } |
1
|
831 | simple_expr EPOW simple_expr |
578
|
832 { $$ = make_binary_op (EPOW, $1, $2, $3); } |
1
|
833 | simple_expr '+' simple_expr |
578
|
834 { $$ = make_binary_op ('+', $1, $2, $3); } |
1
|
835 | simple_expr '-' simple_expr |
578
|
836 { $$ = make_binary_op ('-', $1, $2, $3); } |
1
|
837 | simple_expr '*' simple_expr |
578
|
838 { $$ = make_binary_op ('*', $1, $2, $3); } |
1
|
839 | simple_expr '/' simple_expr |
578
|
840 { $$ = make_binary_op ('/', $1, $2, $3); } |
1276
|
841 | simple_expr EPLUS simple_expr |
|
842 { $$ = make_binary_op ('+', $1, $2, $3); } |
|
843 | simple_expr EMINUS simple_expr |
|
844 { $$ = make_binary_op ('-', $1, $2, $3); } |
1
|
845 | simple_expr EMUL simple_expr |
578
|
846 { $$ = make_binary_op (EMUL, $1, $2, $3); } |
1
|
847 | simple_expr EDIV simple_expr |
578
|
848 { $$ = make_binary_op (EDIV, $1, $2, $3); } |
1
|
849 | simple_expr LEFTDIV simple_expr |
578
|
850 { $$ = make_binary_op (LEFTDIV, $1, $2, $3); } |
1
|
851 | simple_expr ELEFTDIV simple_expr |
578
|
852 { $$ = make_binary_op (ELEFTDIV, $1, $2, $3); } |
1
|
853 | simple_expr EXPR_LT simple_expr |
578
|
854 { $$ = make_binary_op (EXPR_LT, $1, $2, $3); } |
1
|
855 | simple_expr EXPR_LE simple_expr |
578
|
856 { $$ = make_binary_op (EXPR_LE, $1, $2, $3); } |
1
|
857 | simple_expr EXPR_EQ simple_expr |
578
|
858 { $$ = make_binary_op (EXPR_EQ, $1, $2, $3); } |
1
|
859 | simple_expr EXPR_GE simple_expr |
578
|
860 { $$ = make_binary_op (EXPR_GE, $1, $2, $3); } |
1
|
861 | simple_expr EXPR_GT simple_expr |
578
|
862 { $$ = make_binary_op (EXPR_GT, $1, $2, $3); } |
1
|
863 | simple_expr EXPR_NE simple_expr |
578
|
864 { $$ = make_binary_op (EXPR_NE, $1, $2, $3); } |
1
|
865 | simple_expr EXPR_AND simple_expr |
578
|
866 { $$ = make_binary_op (EXPR_AND, $1, $2, $3); } |
1
|
867 | simple_expr EXPR_OR simple_expr |
578
|
868 { $$ = make_binary_op (EXPR_OR, $1, $2, $3); } |
2375
|
869 | simple_expr EXPR_AND_AND simple_expr |
|
870 { $$ = make_boolean_op (EXPR_AND_AND, $1, $2, $3); } |
|
871 | simple_expr EXPR_OR_OR simple_expr |
|
872 { $$ = make_boolean_op (EXPR_OR_OR, $1, $2, $3); } |
1
|
873 ; |
|
874 |
1623
|
875 colon_expr : simple_expr ':' simple_expr |
1
|
876 { |
1623
|
877 $$ = new tree_colon_expression |
|
878 ($1, $3, $2->line (), $2->column ()); |
240
|
879 } |
1
|
880 | colon_expr ':' simple_expr |
|
881 { |
1623
|
882 if (! ($$ = $1->chain ($3))) |
1005
|
883 ABORT_PARSE; |
1
|
884 } |
|
885 ; |
|
886 |
|
887 word_list_cmd : identifier word_list |
482
|
888 { |
|
889 $$ = new tree_index_expression |
1623
|
890 ($1, $2, $1->line (), $1->column ()); |
482
|
891 } |
1
|
892 ; |
|
893 |
578
|
894 word_list : TEXT |
482
|
895 { |
2375
|
896 tree_constant *tmp = make_constant (TEXT, $1); |
482
|
897 $$ = new tree_argument_list (tmp); |
|
898 } |
578
|
899 | word_list TEXT |
482
|
900 { |
2375
|
901 tree_constant *tmp = make_constant (TEXT, $2); |
578
|
902 $1->append (tmp); |
1829
|
903 $$ = $1; |
482
|
904 } |
1
|
905 ; |
|
906 |
|
907 // This is truly disgusting. |
|
908 |
|
909 g_symtab : // empty |
|
910 { curr_sym_tab = global_sym_tab; } |
|
911 ; |
|
912 |
2745
|
913 in_return_list : // empty |
|
914 { lexer_flags.looking_at_return_list = 1; } |
|
915 ; |
|
916 |
1
|
917 local_symtab : // empty |
|
918 { curr_sym_tab = tmp_local_sym_tab; } |
|
919 ; |
|
920 |
|
921 safe : // empty |
1826
|
922 { lexer_flags.maybe_screwed = 0; } |
1
|
923 ; |
|
924 |
|
925 are_we_screwed : // empty |
1826
|
926 { lexer_flags.maybe_screwed = 1; } |
1
|
927 ; |
|
928 |
|
929 func_def : FCN g_symtab are_we_screwed func_def1 |
|
930 { |
|
931 curr_sym_tab = top_level_sym_tab; |
1826
|
932 lexer_flags.defining_func = 0; |
522
|
933 $$ = 0; |
1
|
934 } |
|
935 | FCN g_symtab are_we_screwed func_def2 |
|
936 { |
|
937 curr_sym_tab = top_level_sym_tab; |
1826
|
938 lexer_flags.defining_func = 0; |
522
|
939 $$ = 0; |
1
|
940 } |
|
941 ; |
|
942 |
|
943 func_def1 : SCREW safe g_symtab '=' func_def2 |
1623
|
944 { $$ = finish_function_def ($1, $5); } |
723
|
945 | return_list g_symtab '=' func_def2 |
1623
|
946 { $$ = finish_function_def ($1, $4); } |
1
|
947 ; |
|
948 |
2745
|
949 return_list_x : '[' safe local_symtab in_return_list |
723
|
950 ; |
|
951 |
|
952 return_list : return_list_x ']' |
2745
|
953 { |
|
954 lexer_flags.looking_at_return_list = 0; |
|
955 $$ = new tree_parameter_list (); |
|
956 } |
723
|
957 | return_list_x ELLIPSIS ']' |
|
958 { |
2745
|
959 lexer_flags.looking_at_return_list = 0; |
723
|
960 tree_parameter_list *tmp = new tree_parameter_list (); |
|
961 tmp->mark_varargs_only (); |
|
962 $$ = tmp; |
|
963 } |
|
964 | return_list1 ']' |
2745
|
965 { |
|
966 lexer_flags.looking_at_return_list = 0; |
|
967 $$ = $1; |
|
968 } |
723
|
969 | return_list1 ',' ELLIPSIS ']' |
|
970 { |
2745
|
971 lexer_flags.looking_at_return_list = 0; |
723
|
972 $1->mark_varargs (); |
|
973 $$ = $1; |
|
974 } |
|
975 ; |
|
976 |
|
977 return_list1 : return_list_x identifier |
|
978 { $$ = new tree_parameter_list ($2); } |
|
979 | return_list_x error |
|
980 { |
1005
|
981 yyerror ("invalid function return list"); |
1829
|
982 $$ = 0; |
723
|
983 ABORT_PARSE; |
|
984 } |
|
985 | return_list1 ',' identifier |
1829
|
986 { |
|
987 $1->append ($3); |
|
988 $$ = $1; |
|
989 } |
1
|
990 ; |
|
991 |
|
992 func_def2 : identifier safe local_symtab func_def3 |
|
993 { |
1623
|
994 if (! ($$ = frob_function_def ($1, $4))) |
|
995 ABORT_PARSE; |
1
|
996 } |
|
997 ; |
|
998 |
2525
|
999 func_def3 : param_list opt_sep opt_list fcn_end_or_eof |
1623
|
1000 { $$ = start_function_def ($1, $3); } |
2525
|
1001 | opt_sep opt_list fcn_end_or_eof |
1623
|
1002 { $$ = start_function_def (0, $2); } |
1
|
1003 ; |
|
1004 |
|
1005 fcn_end_or_eof : END |
|
1006 { |
143
|
1007 if (check_end ($1, token::function_end)) |
1829
|
1008 ABORT_PARSE; |
1
|
1009 |
338
|
1010 if (reading_fcn_file) |
1
|
1011 check_for_garbage_after_fcn_def (); |
|
1012 } |
|
1013 | END_OF_INPUT |
|
1014 { |
338
|
1015 if (! (reading_fcn_file || reading_script_file)) |
1
|
1016 YYABORT; |
|
1017 } |
|
1018 ; |
|
1019 |
751
|
1020 indirect_ref : indirect_ref1 |
191
|
1021 { |
1826
|
1022 lexer_flags.looking_at_indirect_ref = 0; |
751
|
1023 $$ = $1; |
191
|
1024 } |
751
|
1025 |
|
1026 indirect_ref1 : identifier |
191
|
1027 { |
751
|
1028 $$ = new tree_indirect_ref ($1, $1->line (), |
|
1029 $1->column ()); |
191
|
1030 } |
1826
|
1031 | indirect_ref1 '.' |
|
1032 { lexer_flags.looking_at_indirect_ref = 1; } TEXT_ID |
2375
|
1033 { $$ = new tree_indirect_ref ($1, $4->text ()); } |
751
|
1034 ; |
|
1035 |
|
1036 variable : indirect_ref |
|
1037 { $$ = make_index_expression ($1, 0); } |
|
1038 | indirect_ref '(' ')' |
|
1039 { $$ = make_index_expression ($1, 0); } |
|
1040 | indirect_ref '(' arg_list ')' |
|
1041 { $$ = make_index_expression ($1, $3); } |
|
1042 | indirect_ref '[' |
1
|
1043 { |
1005
|
1044 yyerror ("use `(\' and `)\' as index operators, not\ |
1
|
1045 `[\' and `]\'"); |
522
|
1046 $$ = 0; |
1
|
1047 ABORT_PARSE; |
|
1048 } |
|
1049 ; |
|
1050 |
2818
|
1051 param_list_beg : '(' |
2745
|
1052 { lexer_flags.looking_at_parameter_list = 1; } |
|
1053 ; |
|
1054 |
2818
|
1055 param_list_end : ')' |
2825
|
1056 { lexer_flags.looking_at_parameter_list = 0; } |
2818
|
1057 ; |
|
1058 |
|
1059 param_list : param_list_beg param_list_end |
1
|
1060 { |
1826
|
1061 lexer_flags.quote_is_transpose = 0; |
522
|
1062 $$ = 0; |
240
|
1063 } |
2818
|
1064 | param_list_beg ELLIPSIS param_list_end |
504
|
1065 { |
1826
|
1066 lexer_flags.quote_is_transpose = 0; |
504
|
1067 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1068 tmp->mark_varargs_only (); |
|
1069 $$ = tmp; |
|
1070 } |
2818
|
1071 | param_list1 param_list_end |
240
|
1072 { |
1826
|
1073 lexer_flags.quote_is_transpose = 0; |
578
|
1074 $1->mark_as_formal_parameters (); |
1829
|
1075 $$ = $1; |
1
|
1076 } |
2818
|
1077 | param_list1 ',' ELLIPSIS param_list_end |
206
|
1078 { |
1826
|
1079 lexer_flags.quote_is_transpose = 0; |
578
|
1080 $1->mark_as_formal_parameters (); |
|
1081 $1->mark_varargs (); |
1829
|
1082 $$ = $1; |
206
|
1083 } |
504
|
1084 ; |
1
|
1085 |
2818
|
1086 param_list1 : param_list_beg identifier |
|
1087 { $$ = new tree_parameter_list ($2); } |
1
|
1088 | param_list1 ',' identifier |
1829
|
1089 { |
|
1090 $1->append ($3); |
|
1091 $$ = $1; |
|
1092 } |
2818
|
1093 | param_list_beg error |
1
|
1094 { |
1005
|
1095 yyerror ("invalid parameter list"); |
1829
|
1096 $$ = 0; |
206
|
1097 ABORT_PARSE; |
1
|
1098 } |
|
1099 | param_list1 ',' error |
|
1100 { |
1005
|
1101 yyerror ("invalid parameter list"); |
1829
|
1102 $$ = 0; |
206
|
1103 ABORT_PARSE; |
1
|
1104 } |
|
1105 ; |
|
1106 |
|
1107 identifier : NAME |
504
|
1108 { |
|
1109 $$ = new tree_identifier |
|
1110 ($1->sym_rec (), $1->line (), $1->column ()); |
|
1111 } |
|
1112 ; |
1
|
1113 |
578
|
1114 arg_list : ':' |
1
|
1115 { |
2375
|
1116 tree_constant *colon = |
|
1117 new tree_constant (tree_constant::magic_colon_t); |
1
|
1118 $$ = new tree_argument_list (colon); |
|
1119 } |
922
|
1120 | expression |
|
1121 { $$ = new tree_argument_list ($1); } |
|
1122 | ALL_VA_ARGS |
|
1123 { |
2375
|
1124 tree_constant *all_va_args = |
|
1125 new tree_constant (tree_constant::all_va_args_t); |
922
|
1126 $$ = new tree_argument_list (all_va_args); |
|
1127 } |
578
|
1128 | arg_list ',' ':' |
1
|
1129 { |
2375
|
1130 tree_constant *colon = |
|
1131 new tree_constant (tree_constant::magic_colon_t); |
578
|
1132 $1->append (colon); |
1829
|
1133 $$ = $1; |
1
|
1134 } |
578
|
1135 | arg_list ',' expression |
1829
|
1136 { |
|
1137 $1->append ($3); |
|
1138 $$ = $1; |
|
1139 } |
922
|
1140 | arg_list ',' ALL_VA_ARGS |
|
1141 { |
2375
|
1142 tree_constant *all_va_args = |
|
1143 new tree_constant (tree_constant::all_va_args_t); |
922
|
1144 $1->append (all_va_args); |
1829
|
1145 $$ = $1; |
922
|
1146 } |
1
|
1147 ; |
|
1148 |
240
|
1149 matrix : '[' screwed_again rows ']' |
1829
|
1150 { $$ = finish_matrix ($3); } |
1
|
1151 ; |
|
1152 |
1084
|
1153 rows : rows1 |
1829
|
1154 { $$ = $1; } |
1084
|
1155 | rows1 ';' // Ignore trailing semicolon. |
1829
|
1156 { $$ = $1; } |
1
|
1157 ; |
|
1158 |
1084
|
1159 rows1 : matrix_row |
1829
|
1160 { $$ = new tree_matrix ($1); } |
1084
|
1161 | rows1 ';' matrix_row |
1829
|
1162 { |
|
1163 $1->append ($3); |
|
1164 $$ = $1; |
|
1165 } |
1084
|
1166 ; |
|
1167 |
|
1168 matrix_row : matrix_row1 |
1829
|
1169 { $$ = $1; } |
1084
|
1170 | matrix_row1 ',' // Ignore trailing comma. |
1829
|
1171 { $$ = $1; } |
1084
|
1172 ; |
|
1173 |
|
1174 matrix_row1 : expression // First element on row. |
1829
|
1175 { $$ = new tree_matrix_row ($1); } |
1084
|
1176 | matrix_row1 ',' expression |
1
|
1177 { |
1829
|
1178 $1->append ($3); |
|
1179 $$ = $1; |
1
|
1180 } |
|
1181 ; |
|
1182 |
2525
|
1183 sep_no_nl : ',' |
|
1184 { $$ = ','; } |
|
1185 | ';' |
|
1186 { $$ = ';'; } |
|
1187 | sep_no_nl ',' |
|
1188 { $$ = $1; } |
|
1189 | sep_no_nl ';' |
|
1190 { $$ = $1; } |
|
1191 ; |
|
1192 |
|
1193 opt_sep_no_nl : // empty |
|
1194 { $$ = 0; } |
|
1195 | sep_no_nl |
|
1196 { $$ = $1; } |
|
1197 ; |
|
1198 |
|
1199 sep : ',' |
|
1200 { $$ = ','; } |
|
1201 | ';' |
|
1202 { $$ = ';'; } |
|
1203 | '\n' |
|
1204 { $$ = '\n'; } |
|
1205 | sep ',' |
|
1206 { $$ = $1; } |
|
1207 | sep ';' |
|
1208 { $$ = $1; } |
|
1209 | sep '\n' |
|
1210 { $$ = $1; } |
|
1211 ; |
|
1212 |
|
1213 opt_sep : // empty |
|
1214 { $$ = 0; } |
|
1215 | sep |
|
1216 { $$ = $1; } |
|
1217 ; |
|
1218 |
1
|
1219 %% |
|
1220 |
666
|
1221 // Generic error messages. |
|
1222 |
1
|
1223 static void |
2805
|
1224 yyerror (const char *s) |
1
|
1225 { |
143
|
1226 int err_col = current_input_column - 1; |
1
|
1227 |
581
|
1228 ostrstream output_buf; |
1
|
1229 |
1005
|
1230 if (reading_fcn_file || reading_script_file) |
|
1231 output_buf << "parse error near line " << input_line_number |
1607
|
1232 << " of file " << curr_fcn_file_full_name; |
1005
|
1233 else |
|
1234 output_buf << "parse error:"; |
581
|
1235 |
1005
|
1236 if (s && strcmp (s, "parse error") != 0) |
|
1237 output_buf << "\n\n " << s; |
|
1238 |
|
1239 output_buf << "\n\n"; |
1
|
1240 |
1755
|
1241 if (! current_input_line.empty ()) |
1
|
1242 { |
1755
|
1243 size_t len = current_input_line.length (); |
1060
|
1244 |
1755
|
1245 if (current_input_line[len-1] == '\n') |
|
1246 current_input_line.resize (len-1); |
1005
|
1247 |
335
|
1248 // Print the line, maybe with a pointer near the error token. |
1005
|
1249 |
1755
|
1250 output_buf << ">>> " << current_input_line << "\n"; |
1060
|
1251 |
|
1252 if (err_col == 0) |
|
1253 err_col = len; |
|
1254 |
|
1255 for (int i = 0; i < err_col + 3; i++) |
|
1256 output_buf << " "; |
|
1257 |
|
1258 output_buf << "^"; |
1
|
1259 } |
1005
|
1260 |
|
1261 output_buf << "\n" << ends; |
581
|
1262 |
1005
|
1263 char *msg = output_buf.str (); |
|
1264 |
1090
|
1265 parse_error ("%s", msg); |
1005
|
1266 |
|
1267 delete [] msg; |
1
|
1268 } |
|
1269 |
666
|
1270 // Error mesages for mismatched end tokens. |
|
1271 |
496
|
1272 static void |
2805
|
1273 end_error (const char *type, token::end_tok_type ettype, int l, int c) |
496
|
1274 { |
2805
|
1275 static const char *fmt |
|
1276 = "`%s' command matched by `%s' near line %d column %d"; |
496
|
1277 |
|
1278 switch (ettype) |
|
1279 { |
|
1280 case token::simple_end: |
|
1281 error (fmt, type, "end", l, c); |
|
1282 break; |
777
|
1283 |
496
|
1284 case token::for_end: |
|
1285 error (fmt, type, "endfor", l, c); |
|
1286 break; |
777
|
1287 |
496
|
1288 case token::function_end: |
|
1289 error (fmt, type, "endfunction", l, c); |
|
1290 break; |
777
|
1291 |
496
|
1292 case token::if_end: |
|
1293 error (fmt, type, "endif", l, c); |
|
1294 break; |
777
|
1295 |
496
|
1296 case token::while_end: |
|
1297 error (fmt, type, "endwhile", l, c); |
|
1298 break; |
777
|
1299 |
1371
|
1300 case token::unwind_protect_end: |
|
1301 error (fmt, type, "end_unwind_protect", l, c); |
|
1302 break; |
|
1303 |
496
|
1304 default: |
|
1305 panic_impossible (); |
|
1306 break; |
|
1307 } |
|
1308 } |
|
1309 |
666
|
1310 // Check to see that end tokens are properly matched. |
|
1311 |
143
|
1312 static int |
|
1313 check_end (token *tok, token::end_tok_type expected) |
|
1314 { |
|
1315 token::end_tok_type ettype = tok->ettype (); |
|
1316 if (ettype != expected && ettype != token::simple_end) |
|
1317 { |
|
1318 yyerror ("parse error"); |
|
1319 |
|
1320 int l = tok->line (); |
|
1321 int c = tok->column (); |
|
1322 |
|
1323 switch (expected) |
|
1324 { |
|
1325 case token::for_end: |
|
1326 end_error ("for", ettype, l, c); |
|
1327 break; |
777
|
1328 |
143
|
1329 case token::function_end: |
|
1330 end_error ("function", ettype, l, c); |
|
1331 break; |
777
|
1332 |
143
|
1333 case token::if_end: |
|
1334 end_error ("if", ettype, l, c); |
|
1335 break; |
777
|
1336 |
1489
|
1337 case token::try_catch_end: |
|
1338 end_error ("try", ettype, l, c); |
|
1339 break; |
|
1340 |
2764
|
1341 case token::switch_end: |
|
1342 end_error ("switch", ettype, l, c); |
|
1343 break; |
|
1344 |
1489
|
1345 case token::unwind_protect_end: |
|
1346 end_error ("unwind_protect", ettype, l, c); |
|
1347 break; |
|
1348 |
143
|
1349 case token::while_end: |
|
1350 end_error ("while", ettype, l, c); |
|
1351 break; |
777
|
1352 |
143
|
1353 default: |
|
1354 panic_impossible (); |
|
1355 break; |
|
1356 } |
|
1357 return 1; |
|
1358 } |
|
1359 else |
|
1360 return 0; |
|
1361 } |
|
1362 |
666
|
1363 // Try to figure out early if an expression should become an |
1623
|
1364 // assignment to the built-in variable ans. |
666
|
1365 // |
2375
|
1366 // Need to make sure that the expression is not already an identifier |
666
|
1367 // that has a name, or an assignment expression. |
|
1368 // |
2375
|
1369 // Note that an expression can not be just an identifier now -- it |
666
|
1370 // must at least be an index expression (see the definition of the |
2375
|
1371 // non-terminal variable above). |
666
|
1372 |
496
|
1373 static tree_expression * |
|
1374 maybe_convert_to_ans_assign (tree_expression *expr) |
1
|
1375 { |
|
1376 if (expr->is_index_expression ()) |
|
1377 { |
195
|
1378 expr->mark_for_possible_ans_assign (); |
|
1379 return expr; |
|
1380 } |
|
1381 else if (expr->is_assignment_expression () |
|
1382 || expr->is_prefix_expression ()) |
|
1383 { |
|
1384 return expr; |
|
1385 } |
|
1386 else |
|
1387 { |
1266
|
1388 // XXX FIXME XXX -- making ans_id static, passing its address to |
|
1389 // tree_simple_assignment_expression along with a flag to not |
|
1390 // delete it seems to create a memory leak. Hmm. |
|
1391 |
1162
|
1392 static symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
1266
|
1393 tree_identifier *ans_id = new tree_identifier (sr); |
1
|
1394 |
1607
|
1395 int l = expr->line (); |
|
1396 int c = expr->column (); |
|
1397 |
|
1398 return new tree_simple_assignment_expression (ans_id, expr, 0, 1, l, c); |
1
|
1399 } |
|
1400 } |
|
1401 |
666
|
1402 // Maybe print a warning if an assignment expression is used as the |
|
1403 // test in a logical expression. |
|
1404 |
496
|
1405 static void |
|
1406 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1407 { |
2166
|
1408 if (Vwarn_assign_as_truth_value |
1
|
1409 && expr->is_assignment_expression () |
2124
|
1410 && expr->is_in_parens () < 2) |
1
|
1411 { |
|
1412 warning ("suggest parenthesis around assignment used as truth value"); |
|
1413 } |
|
1414 } |
578
|
1415 |
2764
|
1416 // Maybe print a warning about switch labels that aren't constants. |
|
1417 |
|
1418 static void |
|
1419 maybe_warn_variable_switch_label (tree_expression *expr) |
|
1420 { |
|
1421 if (Vwarn_variable_switch_label && ! expr->is_constant ()) |
|
1422 { |
|
1423 warning ("variable switch label"); |
|
1424 } |
|
1425 } |
|
1426 |
1623
|
1427 // Create a plot command. |
|
1428 |
|
1429 static tree_plot_command * |
|
1430 make_plot_command (token *tok, plot_limits *range, subplot_list *list) |
|
1431 { |
|
1432 if (range) |
|
1433 { |
|
1434 if (tok->pttype () == token::replot) |
|
1435 { |
|
1436 yyerror ("cannot specify new ranges with replot"); |
|
1437 return 0; |
|
1438 } |
|
1439 } |
|
1440 else if (! list && tok->pttype () != token::replot) |
|
1441 { |
|
1442 yyerror ("must have something to plot"); |
|
1443 return 0; |
|
1444 } |
|
1445 |
1826
|
1446 lexer_flags.plotting = 0; |
|
1447 lexer_flags.past_plot_range = 0; |
|
1448 lexer_flags.in_plot_range = 0; |
|
1449 lexer_flags.in_plot_using = 0; |
|
1450 lexer_flags.in_plot_style = 0; |
1623
|
1451 |
|
1452 return new tree_plot_command (list, range, tok->pttype ()); |
|
1453 } |
|
1454 |
2533
|
1455 static tree_expression * |
|
1456 fold (tree_binary_expression *e) |
|
1457 { |
|
1458 tree_expression *retval = 0; |
|
1459 |
|
1460 tree_expression *op1 = e->lhs (); |
|
1461 tree_expression *op2 = e->rhs (); |
|
1462 |
|
1463 if (op1->is_constant () && op2->is_constant ()) |
|
1464 { |
|
1465 octave_value tmp = e->eval (0); |
|
1466 |
|
1467 if (! error_state) |
|
1468 { |
|
1469 tree_constant *tc_retval = new tree_constant (tmp); |
|
1470 |
|
1471 ostrstream buf; |
|
1472 |
|
1473 tree_print_code tpc (buf); |
|
1474 |
|
1475 e->accept (tpc); |
|
1476 |
|
1477 buf << ends; |
|
1478 |
|
1479 char *s = buf.str (); |
|
1480 |
|
1481 tc_retval->stash_original_text (s); |
|
1482 |
|
1483 delete [] s; |
|
1484 |
|
1485 delete e; |
|
1486 |
|
1487 retval = tc_retval; |
|
1488 } |
|
1489 else |
|
1490 delete e; |
|
1491 } |
|
1492 else |
|
1493 retval = e; |
|
1494 |
|
1495 return retval; |
|
1496 } |
|
1497 |
|
1498 static tree_expression * |
|
1499 fold (tree_unary_expression *e) |
|
1500 { |
|
1501 tree_expression *retval = 0; |
|
1502 |
|
1503 tree_expression *op1 = e->operand (); |
|
1504 |
|
1505 if (op1->is_constant ()) |
|
1506 { |
|
1507 octave_value tmp = e->eval (0); |
|
1508 |
|
1509 if (! error_state) |
|
1510 { |
|
1511 tree_constant *tc_retval = new tree_constant (tmp); |
|
1512 |
|
1513 ostrstream buf; |
|
1514 |
|
1515 tree_print_code tpc (buf); |
|
1516 |
|
1517 e->accept (tpc); |
|
1518 |
|
1519 buf << ends; |
|
1520 |
|
1521 char *s = buf.str (); |
|
1522 |
|
1523 tc_retval->stash_original_text (s); |
|
1524 |
|
1525 delete [] s; |
|
1526 |
|
1527 delete e; |
|
1528 |
|
1529 retval = tc_retval; |
|
1530 } |
|
1531 else |
|
1532 delete e; |
|
1533 } |
|
1534 else |
|
1535 retval = e; |
|
1536 |
|
1537 return retval; |
|
1538 } |
|
1539 |
1623
|
1540 // Finish building a range. |
|
1541 |
|
1542 static tree_expression * |
|
1543 finish_colon_expression (tree_colon_expression *e) |
|
1544 { |
|
1545 tree_expression *retval = 0; |
|
1546 |
2533
|
1547 tree_expression *base = e->base (); |
|
1548 tree_expression *limit = e->limit (); |
|
1549 tree_expression *incr = e->increment (); |
|
1550 |
|
1551 if (base->is_constant () && limit->is_constant () |
|
1552 && (! incr || (incr && incr->is_constant ()))) |
1623
|
1553 { |
2086
|
1554 octave_value tmp = e->eval (0); |
1623
|
1555 |
2533
|
1556 if (! error_state) |
|
1557 { |
|
1558 tree_constant *tc_retval = new tree_constant (tmp); |
|
1559 |
|
1560 ostrstream buf; |
|
1561 |
|
1562 tree_print_code tpc (buf); |
|
1563 |
|
1564 e->accept (tpc); |
|
1565 |
|
1566 buf << ends; |
1623
|
1567 |
2533
|
1568 char *s = buf.str (); |
|
1569 |
|
1570 tc_retval->stash_original_text (s); |
|
1571 |
|
1572 delete [] s; |
|
1573 |
|
1574 delete e; |
|
1575 |
|
1576 retval = tc_retval; |
|
1577 } |
|
1578 else |
|
1579 delete e; |
1623
|
1580 } |
|
1581 else |
|
1582 retval = e; |
|
1583 |
|
1584 return retval; |
|
1585 } |
|
1586 |
1607
|
1587 // Make a constant. |
|
1588 |
2375
|
1589 static tree_constant * |
1607
|
1590 make_constant (int op, token *tok_val) |
|
1591 { |
|
1592 int l = tok_val->line (); |
|
1593 int c = tok_val->column (); |
|
1594 |
2375
|
1595 tree_constant *retval; |
1607
|
1596 |
|
1597 switch (op) |
|
1598 { |
|
1599 case NUM: |
2533
|
1600 { |
|
1601 retval = new tree_constant (tok_val->number (), l, c); |
|
1602 retval->stash_original_text (tok_val->text_rep ()); |
|
1603 } |
1607
|
1604 break; |
|
1605 |
|
1606 case IMAG_NUM: |
|
1607 { |
|
1608 Complex C (0.0, tok_val->number ()); |
2375
|
1609 retval = new tree_constant (C, l, c); |
1607
|
1610 retval->stash_original_text (tok_val->text_rep ()); |
|
1611 } |
|
1612 break; |
|
1613 |
|
1614 case TEXT: |
2375
|
1615 retval = new tree_constant (tok_val->text (), l, c); |
1607
|
1616 break; |
|
1617 |
|
1618 default: |
|
1619 panic_impossible (); |
|
1620 break; |
|
1621 } |
|
1622 |
|
1623 return retval; |
|
1624 } |
|
1625 |
666
|
1626 // Build a binary expression. |
|
1627 |
578
|
1628 static tree_expression * |
|
1629 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1630 tree_expression *op2) |
|
1631 { |
2375
|
1632 tree_binary_expression::type t; |
1623
|
1633 |
578
|
1634 switch (op) |
|
1635 { |
|
1636 case POW: |
2375
|
1637 t = tree_binary_expression::power; |
578
|
1638 break; |
777
|
1639 |
578
|
1640 case EPOW: |
2375
|
1641 t = tree_binary_expression::elem_pow; |
578
|
1642 break; |
777
|
1643 |
578
|
1644 case '+': |
2375
|
1645 t = tree_binary_expression::add; |
578
|
1646 break; |
777
|
1647 |
578
|
1648 case '-': |
2375
|
1649 t = tree_binary_expression::subtract; |
578
|
1650 break; |
777
|
1651 |
578
|
1652 case '*': |
2375
|
1653 t = tree_binary_expression::multiply; |
578
|
1654 break; |
777
|
1655 |
578
|
1656 case '/': |
2375
|
1657 t = tree_binary_expression::divide; |
578
|
1658 break; |
777
|
1659 |
578
|
1660 case EMUL: |
2375
|
1661 t = tree_binary_expression::el_mul; |
578
|
1662 break; |
777
|
1663 |
578
|
1664 case EDIV: |
2375
|
1665 t = tree_binary_expression::el_div; |
578
|
1666 break; |
777
|
1667 |
578
|
1668 case LEFTDIV: |
2375
|
1669 t = tree_binary_expression::leftdiv; |
578
|
1670 break; |
777
|
1671 |
578
|
1672 case ELEFTDIV: |
2375
|
1673 t = tree_binary_expression::el_leftdiv; |
578
|
1674 break; |
777
|
1675 |
578
|
1676 case EXPR_LT: |
2375
|
1677 t = tree_binary_expression::cmp_lt; |
578
|
1678 break; |
777
|
1679 |
578
|
1680 case EXPR_LE: |
2375
|
1681 t = tree_binary_expression::cmp_le; |
578
|
1682 break; |
777
|
1683 |
578
|
1684 case EXPR_EQ: |
2375
|
1685 t = tree_binary_expression::cmp_eq; |
578
|
1686 break; |
777
|
1687 |
578
|
1688 case EXPR_GE: |
2375
|
1689 t = tree_binary_expression::cmp_ge; |
578
|
1690 break; |
777
|
1691 |
578
|
1692 case EXPR_GT: |
2375
|
1693 t = tree_binary_expression::cmp_gt; |
578
|
1694 break; |
777
|
1695 |
578
|
1696 case EXPR_NE: |
2375
|
1697 t = tree_binary_expression::cmp_ne; |
578
|
1698 break; |
777
|
1699 |
578
|
1700 case EXPR_AND: |
2805
|
1701 t = tree_binary_expression::el_and; |
578
|
1702 break; |
777
|
1703 |
578
|
1704 case EXPR_OR: |
2805
|
1705 t = tree_binary_expression::el_or; |
578
|
1706 break; |
777
|
1707 |
578
|
1708 default: |
|
1709 panic_impossible (); |
|
1710 break; |
|
1711 } |
|
1712 |
|
1713 int l = tok_val->line (); |
|
1714 int c = tok_val->column (); |
|
1715 |
2533
|
1716 tree_binary_expression *e |
|
1717 = new tree_binary_expression (op1, op2, l, c, t); |
1623
|
1718 |
2533
|
1719 return fold (e); |
578
|
1720 } |
|
1721 |
2375
|
1722 // Build a boolean expression. |
666
|
1723 |
578
|
1724 static tree_expression * |
2375
|
1725 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
1726 tree_expression *op2) |
578
|
1727 { |
2375
|
1728 tree_boolean_expression::type t; |
|
1729 |
578
|
1730 switch (op) |
|
1731 { |
2375
|
1732 case EXPR_AND_AND: |
2805
|
1733 t = tree_boolean_expression::bool_and; |
578
|
1734 break; |
777
|
1735 |
2375
|
1736 case EXPR_OR_OR: |
2805
|
1737 t = tree_boolean_expression::bool_or; |
578
|
1738 break; |
777
|
1739 |
578
|
1740 default: |
|
1741 panic_impossible (); |
|
1742 break; |
|
1743 } |
|
1744 |
|
1745 int l = tok_val->line (); |
|
1746 int c = tok_val->column (); |
|
1747 |
2533
|
1748 tree_boolean_expression *e |
|
1749 = new tree_boolean_expression (op1, op2, l, c, t); |
2375
|
1750 |
2533
|
1751 return fold (e); |
578
|
1752 } |
|
1753 |
2375
|
1754 // Build a prefix expression. |
666
|
1755 |
578
|
1756 static tree_expression * |
2375
|
1757 make_prefix_op (int op, tree_identifier *op1, token *tok_val) |
578
|
1758 { |
2375
|
1759 tree_prefix_expression::type t; |
|
1760 |
578
|
1761 switch (op) |
|
1762 { |
|
1763 case PLUS_PLUS: |
2375
|
1764 t = tree_prefix_expression::increment; |
578
|
1765 break; |
777
|
1766 |
578
|
1767 case MINUS_MINUS: |
2375
|
1768 t = tree_prefix_expression::decrement; |
578
|
1769 break; |
777
|
1770 |
578
|
1771 default: |
|
1772 panic_impossible (); |
|
1773 break; |
|
1774 } |
|
1775 |
|
1776 int l = tok_val->line (); |
|
1777 int c = tok_val->column (); |
|
1778 |
2375
|
1779 return new tree_prefix_expression (op1, l, c, t); |
578
|
1780 } |
|
1781 |
2375
|
1782 // Build a postfix expression. |
666
|
1783 |
578
|
1784 static tree_expression * |
2375
|
1785 make_postfix_op (int op, tree_identifier *op1, token *tok_val) |
578
|
1786 { |
2375
|
1787 tree_postfix_expression::type t; |
1623
|
1788 |
578
|
1789 switch (op) |
|
1790 { |
2375
|
1791 case PLUS_PLUS: |
|
1792 t = tree_postfix_expression::increment; |
578
|
1793 break; |
777
|
1794 |
2375
|
1795 case MINUS_MINUS: |
|
1796 t = tree_postfix_expression::decrement; |
578
|
1797 break; |
777
|
1798 |
578
|
1799 default: |
|
1800 panic_impossible (); |
|
1801 break; |
|
1802 } |
|
1803 |
|
1804 int l = tok_val->line (); |
|
1805 int c = tok_val->column (); |
|
1806 |
2375
|
1807 return new tree_postfix_expression (op1, l, c, t); |
|
1808 } |
|
1809 |
|
1810 // Build a unary expression. |
|
1811 |
|
1812 static tree_expression * |
|
1813 make_unary_op (int op, tree_expression *op1, token *tok_val) |
|
1814 { |
|
1815 tree_unary_expression::type t; |
|
1816 |
|
1817 switch (op) |
|
1818 { |
|
1819 case QUOTE: |
|
1820 t = tree_unary_expression::hermitian; |
|
1821 break; |
|
1822 |
|
1823 case TRANSPOSE: |
|
1824 t = tree_unary_expression::transpose; |
|
1825 break; |
|
1826 |
|
1827 case EXPR_NOT: |
2805
|
1828 t = tree_unary_expression::unot; |
2375
|
1829 break; |
|
1830 |
|
1831 case '-': |
|
1832 t = tree_unary_expression::uminus; |
|
1833 break; |
|
1834 |
|
1835 default: |
|
1836 panic_impossible (); |
|
1837 break; |
|
1838 } |
|
1839 |
|
1840 int l = tok_val->line (); |
|
1841 int c = tok_val->column (); |
|
1842 |
2533
|
1843 tree_unary_expression *e |
|
1844 = new tree_unary_expression (op1, l, c, t); |
1623
|
1845 |
2533
|
1846 return fold (e); |
1623
|
1847 } |
|
1848 |
|
1849 // Build an unwind-protect command. |
|
1850 |
|
1851 static tree_command * |
|
1852 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
|
1853 tree_statement_list *cleanup, token *end_tok) |
|
1854 { |
|
1855 tree_command *retval = 0; |
|
1856 |
|
1857 if (! check_end (end_tok, token::unwind_protect_end)) |
|
1858 { |
|
1859 int l = unwind_tok->line (); |
|
1860 int c = unwind_tok->column (); |
|
1861 |
|
1862 retval = new tree_unwind_protect_command (body, cleanup, l, c); |
|
1863 } |
|
1864 |
|
1865 return retval; |
|
1866 } |
|
1867 |
|
1868 // Build a try-catch command. |
|
1869 |
|
1870 static tree_command * |
|
1871 make_try_command (token *try_tok, tree_statement_list *body, |
|
1872 tree_statement_list *cleanup, token *end_tok) |
|
1873 { |
|
1874 tree_command *retval = 0; |
|
1875 |
|
1876 if (! check_end (end_tok, token::try_catch_end)) |
|
1877 { |
|
1878 int l = try_tok->line (); |
|
1879 int c = try_tok->column (); |
|
1880 |
|
1881 retval = new tree_try_catch_command (body, cleanup, l, c); |
|
1882 } |
|
1883 |
|
1884 return retval; |
|
1885 } |
|
1886 |
|
1887 // Build a while command. |
|
1888 |
|
1889 static tree_command * |
|
1890 make_while_command (token *while_tok, tree_expression *expr, |
|
1891 tree_statement_list *body, token *end_tok) |
|
1892 { |
|
1893 tree_command *retval = 0; |
|
1894 |
|
1895 maybe_warn_assign_as_truth_value (expr); |
|
1896 |
|
1897 if (! check_end (end_tok, token::while_end)) |
|
1898 { |
1826
|
1899 lexer_flags.looping--; |
1623
|
1900 |
|
1901 int l = while_tok->line (); |
|
1902 int c = while_tok->column (); |
|
1903 |
|
1904 retval = new tree_while_command (expr, body, l, c); |
|
1905 } |
|
1906 |
|
1907 return retval; |
|
1908 } |
|
1909 |
|
1910 // Build a for command. |
|
1911 |
|
1912 static tree_command * |
|
1913 make_for_command (token *for_tok, tree_index_expression *var, |
|
1914 tree_expression *expr, tree_statement_list *body, |
|
1915 token *end_tok) |
|
1916 { |
|
1917 tree_command *retval = 0; |
|
1918 |
|
1919 if (! check_end (end_tok, token::for_end)) |
|
1920 { |
1826
|
1921 lexer_flags.looping--; |
1623
|
1922 |
|
1923 int l = for_tok->line (); |
|
1924 int c = for_tok->column (); |
|
1925 |
|
1926 retval = new tree_for_command (var, expr, body, l, c); |
|
1927 } |
|
1928 |
|
1929 return retval; |
|
1930 } |
|
1931 |
|
1932 // Build a for command a different way. |
|
1933 |
|
1934 static tree_command * |
1829
|
1935 make_for_command (token *for_tok, tree_matrix_row *mr, |
|
1936 tree_expression *expr, tree_statement_list *body, |
|
1937 token *end_tok) |
1623
|
1938 { |
|
1939 tree_command *retval = 0; |
|
1940 |
|
1941 if (! check_end (end_tok, token::for_end)) |
|
1942 { |
1826
|
1943 lexer_flags.looping--; |
1623
|
1944 |
1829
|
1945 tree_return_list *id_list = mr->to_return_list (); |
1623
|
1946 |
|
1947 int l = for_tok->line (); |
|
1948 int c = for_tok->column (); |
|
1949 |
|
1950 retval = new tree_for_command (id_list, expr, body, l, c); |
|
1951 } |
|
1952 |
|
1953 return retval; |
|
1954 } |
|
1955 |
|
1956 // Build a break command. |
|
1957 |
|
1958 static tree_command * |
|
1959 make_break_command (token *break_tok) |
|
1960 { |
|
1961 tree_command *retval = 0; |
|
1962 |
2620
|
1963 int l = break_tok->line (); |
|
1964 int c = break_tok->column (); |
|
1965 |
|
1966 if (lexer_flags.looping || lexer_flags.defining_func || reading_script_file) |
|
1967 retval = new tree_break_command (l, c); |
1623
|
1968 else |
2620
|
1969 retval = new tree_no_op_command ("break", l, c); |
1623
|
1970 |
|
1971 return retval; |
|
1972 } |
|
1973 |
|
1974 // Build a continue command. |
|
1975 |
|
1976 static tree_command * |
|
1977 make_continue_command (token *continue_tok) |
|
1978 { |
|
1979 tree_command *retval = 0; |
|
1980 |
2620
|
1981 int l = continue_tok->line (); |
|
1982 int c = continue_tok->column (); |
|
1983 |
|
1984 if (lexer_flags.looping) |
|
1985 retval = new tree_continue_command (l, c); |
1623
|
1986 else |
2620
|
1987 retval = new tree_no_op_command ("continue", l, c); |
1623
|
1988 |
|
1989 return retval; |
|
1990 } |
|
1991 |
|
1992 // Build a return command. |
|
1993 |
|
1994 static tree_command * |
|
1995 make_return_command (token *return_tok) |
|
1996 { |
|
1997 tree_command *retval = 0; |
|
1998 |
2620
|
1999 int l = return_tok->line (); |
|
2000 int c = return_tok->column (); |
|
2001 |
|
2002 if (lexer_flags.defining_func || reading_script_file) |
|
2003 retval = new tree_return_command (l, c); |
1623
|
2004 else |
2620
|
2005 retval = new tree_no_op_command ("return", l, c); |
1623
|
2006 |
|
2007 return retval; |
|
2008 } |
|
2009 |
|
2010 // Start an if command. |
|
2011 |
|
2012 static tree_if_command_list * |
|
2013 start_if_command (tree_expression *expr, tree_statement_list *list) |
|
2014 { |
|
2015 maybe_warn_assign_as_truth_value (expr); |
|
2016 |
|
2017 tree_if_clause *t = new tree_if_clause (expr, list); |
|
2018 |
|
2019 return new tree_if_command_list (t); |
|
2020 } |
|
2021 |
|
2022 // Finish an if command. |
|
2023 |
|
2024 static tree_if_command * |
|
2025 finish_if_command (token *if_tok, tree_if_command_list *list, |
|
2026 token *end_tok) |
|
2027 { |
|
2028 tree_if_command *retval = 0; |
|
2029 |
|
2030 if (! check_end (end_tok, token::if_end)) |
|
2031 { |
|
2032 int l = if_tok->line (); |
|
2033 int c = if_tok->column (); |
|
2034 |
|
2035 retval = new tree_if_command (list, l, c); |
|
2036 } |
|
2037 |
|
2038 return retval; |
|
2039 } |
|
2040 |
|
2041 // Build an elseif clause. |
|
2042 |
|
2043 static tree_if_clause * |
|
2044 make_elseif_clause (tree_expression *expr, tree_statement_list *list) |
|
2045 { |
|
2046 maybe_warn_assign_as_truth_value (expr); |
|
2047 |
|
2048 return new tree_if_clause (expr, list); |
|
2049 } |
|
2050 |
2764
|
2051 // Finish a switch command. |
|
2052 |
|
2053 static tree_switch_command * |
|
2054 finish_switch_command (token *switch_tok, tree_expression *expr, |
|
2055 tree_switch_case_list *list, token *end_tok) |
|
2056 { |
|
2057 tree_switch_command *retval = 0; |
|
2058 |
|
2059 if (! check_end (end_tok, token::switch_end)) |
|
2060 { |
|
2061 int l = switch_tok->line (); |
|
2062 int c = switch_tok->column (); |
|
2063 |
|
2064 retval = new tree_switch_command (expr, list, l, c); |
|
2065 } |
|
2066 |
|
2067 return retval; |
|
2068 } |
|
2069 |
|
2070 // Build a switch case. |
|
2071 |
|
2072 static tree_switch_case * |
|
2073 make_switch_case (tree_expression *expr, tree_statement_list *list) |
|
2074 { |
|
2075 maybe_warn_variable_switch_label (expr); |
|
2076 |
|
2077 return new tree_switch_case (expr, list); |
|
2078 } |
|
2079 |
1623
|
2080 // Build an assignment to a variable. |
|
2081 |
|
2082 static tree_expression * |
|
2083 make_simple_assignment (tree_index_expression *var, token *eq_tok, |
|
2084 tree_expression *expr) |
|
2085 { |
|
2086 int l = eq_tok->line (); |
|
2087 int c = eq_tok->column (); |
|
2088 |
|
2089 return new tree_simple_assignment_expression (var, expr, 0, 0, l, c); |
578
|
2090 } |
666
|
2091 |
|
2092 // Make an expression that handles assignment of multiple values. |
|
2093 |
|
2094 static tree_expression * |
1829
|
2095 make_multi_val_ret (tree_matrix_row *mr, tree_expression *rhs, token *eq_tok) |
666
|
2096 { |
|
2097 // Convert the matrix list to a list of identifiers. If that fails, |
|
2098 // we can abort here, without losing anything -- no other possible |
|
2099 // syntax is valid if we've seen the equals sign as the next token |
|
2100 // after the `]'. |
|
2101 |
|
2102 tree_expression *retval = 0; |
|
2103 |
1826
|
2104 lexer_flags.maybe_screwed_again--; |
666
|
2105 |
1829
|
2106 tree_return_list *id_list = mr->to_return_list (); |
666
|
2107 |
|
2108 if (id_list) |
|
2109 { |
|
2110 int list_len = id_list->length (); |
|
2111 |
|
2112 if (list_len == 1) |
|
2113 { |
|
2114 tree_index_expression *lhs = id_list->remove_front (); |
1623
|
2115 |
|
2116 int l = eq_tok->line (); |
|
2117 int c = eq_tok->column (); |
|
2118 |
947
|
2119 retval = new tree_simple_assignment_expression (lhs, rhs, |
|
2120 0, 0, l, c); |
666
|
2121 } |
|
2122 else if (list_len > 1) |
|
2123 { |
|
2124 if (rhs->is_multi_val_ret_expression ()) |
|
2125 { |
|
2126 tree_multi_val_ret *t = (tree_multi_val_ret *) rhs; |
1623
|
2127 |
|
2128 int l = eq_tok->line (); |
|
2129 int c = eq_tok->column (); |
|
2130 |
1229
|
2131 retval = new tree_multi_assignment_expression (id_list, t, |
|
2132 0, l, c); |
666
|
2133 } |
|
2134 else |
1005
|
2135 yyerror ("RHS must be an expression that returns multiple values"); |
666
|
2136 } |
|
2137 else |
|
2138 panic_impossible (); |
|
2139 } |
|
2140 else |
1005
|
2141 yyerror ("invalid identifier list for assignment"); |
666
|
2142 |
|
2143 return retval; |
|
2144 } |
751
|
2145 |
1623
|
2146 // Begin defining a function. |
|
2147 |
|
2148 static tree_function * |
|
2149 start_function_def (tree_parameter_list *param_list, |
|
2150 tree_statement_list *body) |
|
2151 { |
|
2152 body->mark_as_function_body (); |
|
2153 |
|
2154 tree_function *fcn = new tree_function (body, curr_sym_tab); |
|
2155 |
|
2156 fcn->define_param_list (param_list); |
|
2157 |
|
2158 return fcn; |
|
2159 } |
|
2160 |
|
2161 // Do most of the work for defining a function. |
|
2162 |
|
2163 static tree_function * |
|
2164 frob_function_def (tree_identifier *id, tree_function *fcn) |
|
2165 { |
1755
|
2166 string id_name = id->name (); |
1623
|
2167 |
|
2168 // If input is coming from a file, issue a warning if the name of |
|
2169 // the file does not match the name of the function stated in the |
|
2170 // file. Matlab doesn't provide a diagnostic (it ignores the stated |
|
2171 // name). |
|
2172 |
|
2173 fcn->stash_function_name (id_name); |
|
2174 |
|
2175 if (reading_fcn_file) |
|
2176 { |
1755
|
2177 if (curr_fcn_file_name != id_name) |
1623
|
2178 { |
2166
|
2179 if (Vwarn_function_name_clash) |
1623
|
2180 warning ("function name `%s' does not agree with function\ |
1755
|
2181 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2182 |
|
2183 global_sym_tab->rename (id_name, curr_fcn_file_name); |
|
2184 |
|
2185 if (error_state) |
|
2186 return 0; |
|
2187 |
|
2188 id_name = id->name (); |
|
2189 } |
|
2190 |
|
2191 fcn->stash_function_name (id_name); |
|
2192 fcn->stash_fcn_file_name (); |
|
2193 fcn->stash_fcn_file_time (time (0)); |
|
2194 fcn->mark_as_system_fcn_file (); |
|
2195 } |
|
2196 else if (! (input_from_tmp_history_file || input_from_startup_file) |
|
2197 && reading_script_file |
1755
|
2198 && curr_fcn_file_name == id_name) |
1623
|
2199 { |
|
2200 warning ("function `%s' defined within script file `%s'", |
1755
|
2201 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2202 } |
|
2203 |
|
2204 top_level_sym_tab->clear (id_name); |
|
2205 |
2791
|
2206 symbol_record *sr = global_sym_tab->lookup (id_name, 0, 0); |
|
2207 |
|
2208 if (sr) |
|
2209 fcn->stash_symtab_ptr (sr); |
|
2210 else |
|
2211 panic_impossible (); |
|
2212 |
1623
|
2213 id->define (fcn); |
1755
|
2214 |
1623
|
2215 id->document (help_buf); |
|
2216 |
|
2217 return fcn; |
|
2218 } |
|
2219 |
|
2220 // Finish defining a function. |
|
2221 |
|
2222 static tree_function * |
|
2223 finish_function_def (token *var, tree_function *fcn) |
|
2224 { |
|
2225 symbol_record *sr = var->sym_rec (); |
|
2226 |
|
2227 int l = var->line (); |
|
2228 int c = var->column (); |
|
2229 |
|
2230 tree_identifier *tmp = new tree_identifier (sr, l, c); |
|
2231 |
|
2232 tree_parameter_list *tpl = new tree_parameter_list (tmp); |
|
2233 |
|
2234 tpl->mark_as_formal_parameters (); |
|
2235 |
|
2236 return fcn->define_ret_list (tpl); |
|
2237 } |
|
2238 |
|
2239 // Finish defining a function a different way. |
|
2240 |
|
2241 static tree_function * |
|
2242 finish_function_def (tree_parameter_list *ret_list, tree_function *fcn) |
|
2243 { |
|
2244 ret_list->mark_as_formal_parameters (); |
|
2245 |
|
2246 return fcn->define_ret_list (ret_list); |
|
2247 } |
|
2248 |
751
|
2249 static tree_index_expression * |
|
2250 make_index_expression (tree_indirect_ref *indir, tree_argument_list *args) |
|
2251 { |
|
2252 tree_index_expression *retval = 0; |
|
2253 |
|
2254 int l = indir->line (); |
|
2255 int c = indir->column (); |
|
2256 |
|
2257 if (indir->is_identifier_only ()) |
|
2258 { |
|
2259 indir->preserve_identifier (); |
|
2260 retval = new tree_index_expression (indir->ident (), args, l, c); |
|
2261 delete indir; |
|
2262 } |
|
2263 else |
|
2264 retval = new tree_index_expression (indir, args, l, c); |
|
2265 |
|
2266 return retval; |
|
2267 } |
1511
|
2268 |
1623
|
2269 // Finish building a matrix list. |
|
2270 |
|
2271 static tree_expression * |
1829
|
2272 finish_matrix (tree_matrix *m) |
1623
|
2273 { |
|
2274 tree_expression *retval = 0; |
|
2275 |
1826
|
2276 lexer_flags.maybe_screwed_again--; |
1623
|
2277 |
2533
|
2278 if (m->all_elements_are_constant ()) |
1829
|
2279 { |
2086
|
2280 octave_value tmp = m->eval (0); |
1623
|
2281 |
2533
|
2282 if (! error_state) |
|
2283 { |
|
2284 tree_constant *tc_retval = new tree_constant (tmp); |
|
2285 |
|
2286 ostrstream buf; |
|
2287 |
|
2288 tree_print_code tpc (buf); |
|
2289 |
|
2290 m->accept (tpc); |
|
2291 |
|
2292 buf << ends; |
1623
|
2293 |
2533
|
2294 char *s = buf.str (); |
|
2295 |
|
2296 tc_retval->stash_original_text (s); |
|
2297 |
|
2298 delete [] s; |
|
2299 |
|
2300 delete m; |
|
2301 |
|
2302 retval = tc_retval; |
|
2303 } |
|
2304 else |
|
2305 delete m; |
1623
|
2306 } |
|
2307 else |
1829
|
2308 retval = m; |
1623
|
2309 |
|
2310 return retval; |
|
2311 } |
|
2312 |
1511
|
2313 static void |
|
2314 maybe_warn_missing_semi (tree_statement_list *t) |
|
2315 { |
2166
|
2316 if (lexer_flags.defining_func && Vwarn_missing_semicolon) |
1511
|
2317 { |
|
2318 tree_statement *tmp = t->rear(); |
1607
|
2319 |
1511
|
2320 if (tmp->is_expression ()) |
1607
|
2321 warning ("missing semicolon near line %d, column %d in file `%s'", |
1755
|
2322 tmp->line (), tmp->column (), |
|
2323 curr_fcn_file_full_name.c_str ()); |
1511
|
2324 } |
|
2325 } |
1994
|
2326 |
2525
|
2327 static void |
|
2328 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2329 bool warn_missing_semi) |
|
2330 { |
|
2331 switch (sep) |
|
2332 { |
|
2333 case ';': |
|
2334 { |
|
2335 tree_statement *tmp = list->rear (); |
|
2336 tmp->set_print_flag (0); |
|
2337 } |
|
2338 break; |
|
2339 |
|
2340 case 0: |
|
2341 case ',': |
|
2342 case '\n': |
|
2343 if (warn_missing_semi) |
|
2344 maybe_warn_missing_semi (list); |
|
2345 break; |
|
2346 |
|
2347 default: |
|
2348 warning ("unrecognized separator type!"); |
|
2349 break; |
|
2350 } |
|
2351 } |
|
2352 |
2166
|
2353 static int |
|
2354 warn_assign_as_truth_value (void) |
|
2355 { |
|
2356 Vwarn_assign_as_truth_value |
|
2357 = check_preference ("warn_assign_as_truth_value"); |
|
2358 |
|
2359 return 0; |
|
2360 } |
|
2361 |
|
2362 static int |
|
2363 warn_comma_in_global_decl (void) |
|
2364 { |
|
2365 Vwarn_comma_in_global_decl = check_preference ("warn_comma_in_global_decl"); |
|
2366 |
|
2367 return 0; |
|
2368 } |
|
2369 |
|
2370 static int |
|
2371 warn_function_name_clash (void) |
|
2372 { |
|
2373 Vwarn_function_name_clash = check_preference ("warn_function_name_clash"); |
|
2374 |
|
2375 return 0; |
|
2376 } |
|
2377 |
|
2378 static int |
|
2379 warn_missing_semicolon (void) |
|
2380 { |
|
2381 Vwarn_missing_semicolon = check_preference ("warn_missing_semicolon"); |
|
2382 |
|
2383 return 0; |
|
2384 } |
|
2385 |
2764
|
2386 static int |
|
2387 warn_variable_switch_label (void) |
|
2388 { |
|
2389 Vwarn_variable_switch_label |
|
2390 = check_preference ("warn_variable_switch_label"); |
|
2391 |
|
2392 return 0; |
|
2393 } |
|
2394 |
2166
|
2395 void |
|
2396 symbols_of_parse (void) |
|
2397 { |
|
2398 DEFVAR (warn_assign_as_truth_value, 1.0, 0, warn_assign_as_truth_value, |
|
2399 "produce warning for assignments used as truth values"); |
|
2400 |
|
2401 DEFVAR (warn_comma_in_global_decl, 1.0, 0, warn_comma_in_global_decl, |
|
2402 "produce warning for commas in global declarations"); |
|
2403 |
|
2404 DEFVAR (warn_function_name_clash, 1.0, 0, warn_function_name_clash, |
|
2405 "produce warning if function name conflicts with file name"); |
|
2406 |
|
2407 DEFVAR (warn_missing_semicolon, 0.0, 0, warn_missing_semicolon, |
2764
|
2408 "produce a warning if a statement in a function file is not\n\ |
2166
|
2409 terminated with a semicolon"); |
2764
|
2410 |
|
2411 DEFVAR (warn_variable_switch_label, 0.0, 0, warn_variable_switch_label, |
|
2412 "produce warning for variables used as switch labels"); |
2166
|
2413 } |
|
2414 |
1994
|
2415 /* |
|
2416 ;;; Local Variables: *** |
|
2417 ;;; mode: text *** |
|
2418 ;;; End: *** |
|
2419 */ |