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