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