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