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