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