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