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