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