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