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