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