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