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