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