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