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