1829
|
1 /* |
1
|
2 |
2847
|
3 Copyright (C) 1996, 1997 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 |
3156
|
34 #include <cstdio> |
|
35 |
2427
|
36 #ifdef YYBYACC |
|
37 #include <cstdlib> |
|
38 #endif |
|
39 |
581
|
40 #include <strstream.h> |
|
41 |
1
|
42 #include "Matrix.h" |
3021
|
43 #include "cmd-edit.h" |
|
44 #include "cmd-hist.h" |
|
45 #include "file-ops.h" |
|
46 #include "file-stat.h" |
1
|
47 |
2166
|
48 #include "defun.h" |
3021
|
49 #include "dynamic-ld.h" |
1351
|
50 #include "error.h" |
|
51 #include "input.h" |
|
52 #include "lex.h" |
1743
|
53 #include "oct-hist.h" |
2970
|
54 #include "ov-usr-fcn.h" |
1670
|
55 #include "toplev.h" |
1351
|
56 #include "pager.h" |
|
57 #include "parse.h" |
2987
|
58 #include "pt-all.h" |
1351
|
59 #include "symtab.h" |
|
60 #include "token.h" |
3021
|
61 #include "unwind-prot.h" |
1
|
62 #include "utils.h" |
1351
|
63 #include "variables.h" |
1
|
64 |
3021
|
65 // TRUE means we print |
|
66 static bool Vdefault_eval_print_flag = true; |
|
67 |
2166
|
68 // If TRUE, generate a warning for the assignment in things like |
|
69 // |
|
70 // octave> if (a = 2 < n) |
|
71 // |
|
72 // but not |
|
73 // |
|
74 // octave> if ((a = 2) < n) |
|
75 // |
|
76 static bool Vwarn_assign_as_truth_value; |
|
77 |
2764
|
78 // If TRUE, generate a warning for variable swich labels. |
|
79 static bool Vwarn_variable_switch_label; |
|
80 |
2166
|
81 // If TRUE, generate warning if declared function name disagrees with |
|
82 // the name of the file in which it is defined. |
|
83 static bool Vwarn_function_name_clash; |
|
84 |
3162
|
85 // TRUE means warn about function files that have time stamps in the future. |
|
86 bool Vwarn_future_time_stamp; |
|
87 |
2166
|
88 // If TRUE, generate warning if a statement in a function is not |
|
89 // terminated with a semicolon. Useful for checking functions that |
|
90 // should only produce output using explicit printing statements. |
|
91 static bool Vwarn_missing_semicolon; |
|
92 |
1
|
93 // Temporary symbol table pointer used to cope with bogus function syntax. |
522
|
94 symbol_table *tmp_local_sym_tab = 0; |
1
|
95 |
|
96 // The current input line number. |
|
97 int input_line_number = 0; |
|
98 |
|
99 // The column of the current token. |
143
|
100 int current_input_column = 1; |
1
|
101 |
338
|
102 // Buffer for help text snagged from function files. |
1755
|
103 string help_buf; |
1
|
104 |
3021
|
105 // TRUE means we are using readline. |
|
106 // (--no-line-editing) |
|
107 bool line_editing = true; |
|
108 |
|
109 // TRUE means we printed messages about reading startup files. |
|
110 bool reading_startup_message_printed = false; |
|
111 |
|
112 // TRUE means input is coming from startup file. |
|
113 bool input_from_startup_file = false; |
|
114 |
|
115 // TRUE means that input is coming from a file that was named on |
|
116 // the command line. |
|
117 bool input_from_command_line_file = true; |
|
118 |
496
|
119 // Forward declarations for some functions defined at the bottom of |
|
120 // the file. |
|
121 |
|
122 // Generic error messages. |
2970
|
123 static void |
|
124 yyerror (const char *s); |
1
|
125 |
578
|
126 // Error mesages for mismatched end tokens. |
2970
|
127 static void |
|
128 end_error (const char *type, token::end_tok_type ettype, int l, int c); |
1
|
129 |
578
|
130 // Check to see that end tokens are properly matched. |
2970
|
131 static bool |
|
132 end_token_ok (token *tok, token::end_tok_type expected); |
496
|
133 |
|
134 // Maybe print a warning if an assignment expression is used as the |
|
135 // test in a logical expression. |
2970
|
136 static void |
|
137 maybe_warn_assign_as_truth_value (tree_expression *expr); |
1
|
138 |
2764
|
139 // Maybe print a warning about switch labels that aren't constants. |
2970
|
140 static void |
|
141 maybe_warn_variable_switch_label (tree_expression *expr); |
2764
|
142 |
1623
|
143 // Create a plot command. |
2970
|
144 static tree_plot_command * |
|
145 make_plot_command (token *tok, plot_limits *range, subplot_list *list); |
1623
|
146 |
|
147 // Finish building a range. |
2970
|
148 static tree_expression * |
|
149 finish_colon_expression (tree_colon_expression *e); |
1623
|
150 |
1607
|
151 // Build a constant. |
2970
|
152 static tree_constant * |
|
153 make_constant (int op, token *tok_val); |
1607
|
154 |
578
|
155 // Build a binary expression. |
2970
|
156 static tree_expression * |
|
157 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
158 tree_expression *op2); |
578
|
159 |
2375
|
160 // Build a boolean expression. |
2970
|
161 static tree_expression * |
|
162 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
163 tree_expression *op2); |
2375
|
164 |
578
|
165 // Build a prefix expression. |
2970
|
166 static tree_expression * |
|
167 make_prefix_op (int op, tree_expression *op1, token *tok_val); |
578
|
168 |
|
169 // Build a postfix expression. |
2970
|
170 static tree_expression * |
|
171 make_postfix_op (int op, tree_expression *op1, token *tok_val); |
578
|
172 |
1623
|
173 // Build an unwind-protect command. |
2970
|
174 static tree_command * |
|
175 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
|
176 tree_statement_list *cleanup, token *end_tok); |
1623
|
177 |
|
178 // Build a try-catch command. |
2970
|
179 static tree_command * |
|
180 make_try_command (token *try_tok, tree_statement_list *body, |
|
181 tree_statement_list *cleanup, token *end_tok); |
1623
|
182 |
|
183 // Build a while command. |
2970
|
184 static tree_command * |
|
185 make_while_command (token *while_tok, tree_expression *expr, |
|
186 tree_statement_list *body, token *end_tok); |
1623
|
187 |
|
188 // Build a for command. |
2970
|
189 static tree_command * |
|
190 make_for_command (token *for_tok, tree_argument_list *lhs, |
|
191 tree_expression *expr, tree_statement_list *body, |
|
192 token *end_tok); |
1623
|
193 |
|
194 // Build a break command. |
2970
|
195 static tree_command * |
|
196 make_break_command (token *break_tok); |
1623
|
197 |
|
198 // Build a continue command. |
2970
|
199 static tree_command * |
|
200 make_continue_command (token *continue_tok); |
1623
|
201 |
|
202 // Build a return command. |
2970
|
203 static tree_command * |
|
204 make_return_command (token *return_tok); |
1623
|
205 |
|
206 // Start an if command. |
2970
|
207 static tree_if_command_list * |
|
208 start_if_command (tree_expression *expr, tree_statement_list *list); |
1623
|
209 |
|
210 // Finish an if command. |
2970
|
211 static tree_if_command * |
|
212 finish_if_command (token *if_tok, tree_if_command_list *list, token *end_tok); |
1623
|
213 |
|
214 // Build an elseif clause. |
2970
|
215 static tree_if_clause * |
|
216 make_elseif_clause (tree_expression *expr, tree_statement_list *list); |
1623
|
217 |
2764
|
218 // Finish a switch command. |
2970
|
219 static tree_switch_command * |
|
220 finish_switch_command (token *switch_tok, tree_expression *expr, |
|
221 tree_switch_case_list *list, token *end_tok); |
2764
|
222 |
|
223 // Build a switch case. |
2970
|
224 static tree_switch_case * |
|
225 make_switch_case (tree_expression *expr, tree_statement_list *list); |
2764
|
226 |
1623
|
227 // Build an assignment to a variable. |
2970
|
228 static tree_expression * |
|
229 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
230 tree_expression *rhs); |
1623
|
231 |
|
232 // Begin defining a function. |
2970
|
233 static octave_user_function * |
|
234 start_function (tree_parameter_list *param_list, tree_statement_list *body); |
1623
|
235 |
|
236 // Do most of the work for defining a function. |
2970
|
237 static octave_user_function * |
|
238 frob_function (tree_identifier *id, octave_user_function *fcn); |
1623
|
239 |
|
240 // Finish defining a function. |
2970
|
241 static octave_user_function * |
|
242 finish_function (tree_identifier *id, octave_user_function *fcn); |
1623
|
243 |
|
244 // Finish defining a function a different way. |
2970
|
245 static octave_user_function * |
|
246 finish_function (tree_parameter_list *ret_list, octave_user_function *fcn); |
751
|
247 |
2883
|
248 // Reset state after parsing function. |
2970
|
249 static void |
|
250 recover_from_parsing_function (void); |
2883
|
251 |
751
|
252 // Make an index expression. |
2970
|
253 static tree_index_expression * |
|
254 make_index_expression (tree_expression *expr, tree_argument_list *args); |
|
255 |
|
256 // Make an indirect reference expression. |
|
257 static tree_indirect_ref * |
|
258 make_indirect_ref (tree_expression *expr, const string&); |
666
|
259 |
2846
|
260 // Make a declaration command. |
2970
|
261 static tree_decl_command * |
|
262 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst); |
2846
|
263 |
1623
|
264 // Finish building a matrix list. |
2970
|
265 static tree_expression * |
|
266 finish_matrix (tree_matrix *m); |
1623
|
267 |
1511
|
268 // Maybe print a warning. Duh. |
2970
|
269 static void |
|
270 maybe_warn_missing_semi (tree_statement_list *); |
1511
|
271 |
2525
|
272 // Set the print flag for a statement based on the separator type. |
2970
|
273 static void |
|
274 set_stmt_print_flag (tree_statement_list *, char, bool); |
2525
|
275 |
1
|
276 #define ABORT_PARSE \ |
|
277 do \ |
|
278 { \ |
522
|
279 global_command = 0; \ |
1
|
280 yyerrok; \ |
2865
|
281 if (interactive || forced_interactive) \ |
1
|
282 YYACCEPT; \ |
|
283 else \ |
|
284 YYABORT; \ |
|
285 } \ |
|
286 while (0) |
|
287 |
|
288 %} |
|
289 |
666
|
290 // Bison declarations. |
|
291 |
1
|
292 %union |
|
293 { |
2891
|
294 // The type of the basic tokens returned by the lexer. |
143
|
295 token *tok_val; |
|
296 |
2891
|
297 // Types for the nonterminals we generate. |
2525
|
298 char sep_type; |
1
|
299 tree *tree_type; |
1829
|
300 tree_matrix *tree_matrix_type; |
496
|
301 tree_expression *tree_expression_type; |
2375
|
302 tree_constant *tree_constant_type; |
1
|
303 tree_identifier *tree_identifier_type; |
|
304 tree_index_expression *tree_index_expression_type; |
|
305 tree_colon_expression *tree_colon_expression_type; |
|
306 tree_argument_list *tree_argument_list_type; |
|
307 tree_parameter_list *tree_parameter_list_type; |
|
308 tree_command *tree_command_type; |
|
309 tree_if_command *tree_if_command_type; |
578
|
310 tree_if_clause *tree_if_clause_type; |
|
311 tree_if_command_list *tree_if_command_list_type; |
2764
|
312 tree_switch_command *tree_switch_command_type; |
|
313 tree_switch_case *tree_switch_case_type; |
|
314 tree_switch_case_list *tree_switch_case_list_type; |
2846
|
315 tree_decl_elt *tree_decl_elt_type; |
|
316 tree_decl_init_list *tree_decl_init_list_type; |
|
317 tree_decl_command *tree_decl_command_type; |
578
|
318 tree_statement *tree_statement_type; |
|
319 tree_statement_list *tree_statement_list_type; |
1
|
320 tree_plot_command *tree_plot_command_type; |
578
|
321 subplot *subplot_type; |
|
322 subplot_list *subplot_list_type; |
|
323 plot_limits *plot_limits_type; |
|
324 plot_range *plot_range_type; |
|
325 subplot_using *subplot_using_type; |
|
326 subplot_style *subplot_style_type; |
3165
|
327 subplot_axes *subplot_axes_type; |
2891
|
328 octave_user_function *octave_user_function_type; |
1
|
329 } |
|
330 |
143
|
331 // Tokens with line and column information. |
|
332 %token <tok_val> '=' ':' '-' '+' '*' '/' |
2883
|
333 %token <tok_val> ADD_EQ SUB_EQ MUL_EQ DIV_EQ EMUL_EQ EDIV_EQ AND_EQ OR_EQ |
2899
|
334 %token <tok_val> LSHIFT_EQ RSHIFT_EQ LSHIFT RSHIFT |
428
|
335 %token <tok_val> EXPR_AND_AND EXPR_OR_OR |
143
|
336 %token <tok_val> EXPR_AND EXPR_OR EXPR_NOT |
|
337 %token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
1276
|
338 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS |
|
339 %token <tok_val> QUOTE TRANSPOSE |
143
|
340 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW |
|
341 %token <tok_val> NUM IMAG_NUM |
2970
|
342 %token <tok_val> STRUCT_ELT |
2883
|
343 %token <tok_val> NAME |
143
|
344 %token <tok_val> END |
|
345 %token <tok_val> PLOT |
3165
|
346 %token <tok_val> TEXT STYLE AXES_TAG |
1491
|
347 %token <tok_val> FOR WHILE |
|
348 %token <tok_val> IF ELSEIF ELSE |
2764
|
349 %token <tok_val> SWITCH CASE OTHERWISE |
1491
|
350 %token <tok_val> BREAK CONTINUE FUNC_RET |
924
|
351 %token <tok_val> UNWIND CLEANUP |
1489
|
352 %token <tok_val> TRY CATCH |
2846
|
353 %token <tok_val> GLOBAL STATIC |
1
|
354 |
143
|
355 // Other tokens. |
2970
|
356 %token END_OF_INPUT LEXICAL_ERROR |
|
357 %token FCN ELLIPSIS ALL_VA_ARGS |
3165
|
358 %token USING TITLE WITH AXES COLON OPEN_BRACE CLOSE_BRACE CLEAR |
1
|
359 |
143
|
360 // Nonterminals we construct. |
2525
|
361 %type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep |
578
|
362 %type <tree_type> input |
2960
|
363 %type <tree_constant_type> constant magic_colon |
1829
|
364 %type <tree_matrix_type> rows rows1 |
2970
|
365 %type <tree_expression_type> title matrix |
|
366 %type <tree_expression_type> primary_expr postfix_expr prefix_expr binary_expr |
|
367 %type <tree_expression_type> simple_expr colon_expr assign_expr expression |
1
|
368 %type <tree_identifier_type> identifier |
2891
|
369 %type <octave_user_function_type> function1 function2 function3 |
2970
|
370 %type <tree_index_expression_type> word_list_cmd |
|
371 %type <tree_colon_expression_type> colon_expr1 |
|
372 %type <tree_argument_list_type> arg_list word_list assign_lhs matrix_row |
723
|
373 %type <tree_parameter_list_type> param_list param_list1 |
|
374 %type <tree_parameter_list_type> return_list return_list1 |
2970
|
375 %type <tree_command_type> command select_command loop_command |
|
376 %type <tree_command_type> jump_command except_command function |
578
|
377 %type <tree_if_command_type> if_command |
|
378 %type <tree_if_clause_type> elseif_clause else_clause |
|
379 %type <tree_if_command_list_type> if_cmd_list1 if_cmd_list |
2764
|
380 %type <tree_switch_command_type> switch_command |
|
381 %type <tree_switch_case_type> switch_case default_case |
|
382 %type <tree_switch_case_list_type> case_list1 case_list |
2846
|
383 %type <tree_decl_elt_type> decl2 |
|
384 %type <tree_decl_init_list_type> decl1 |
|
385 %type <tree_decl_command_type> declaration |
578
|
386 %type <tree_statement_type> statement |
627
|
387 %type <tree_statement_list_type> simple_list simple_list1 list list1 |
2891
|
388 %type <tree_statement_list_type> opt_list input1 function4 |
1
|
389 %type <tree_plot_command_type> plot_command |
578
|
390 %type <subplot_type> plot_command2 plot_options |
|
391 %type <subplot_list_type> plot_command1 |
|
392 %type <plot_limits_type> ranges |
|
393 %type <plot_range_type> ranges1 |
|
394 %type <subplot_using_type> using using1 |
|
395 %type <subplot_style_type> style |
3165
|
396 %type <subplot_axes_type> axes |
1
|
397 |
143
|
398 // Precedence and associativity. |
1
|
399 %left ';' ',' '\n' |
2899
|
400 %right '=' ADD_EQ SUB_EQ MUL_EQ DIV_EQ EMUL_EQ EDIV_EQ OR_EQ AND_EQ LSHIFT_EQ RSHIFT_EQ |
428
|
401 %left EXPR_AND_AND EXPR_OR_OR |
1
|
402 %left EXPR_AND EXPR_OR |
|
403 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
2899
|
404 %left LSHIFT RSHIFT |
1
|
405 %left ':' |
1276
|
406 %left '-' '+' EPLUS EMINUS |
1
|
407 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
408 %left QUOTE TRANSPOSE |
|
409 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
410 %right POW EPOW |
2970
|
411 %left '(' '.' |
1
|
412 |
143
|
413 // Where to start. |
1
|
414 %start input |
|
415 |
|
416 %% |
|
417 |
2970
|
418 // ============================== |
|
419 // Statements and statement lists |
|
420 // ============================== |
|
421 |
627
|
422 input : input1 |
1
|
423 { |
627
|
424 global_command = $1; |
1
|
425 promptflag = 1; |
|
426 YYACCEPT; |
|
427 } |
|
428 | END_OF_INPUT |
|
429 { |
522
|
430 global_command = 0; |
1
|
431 promptflag = 1; |
|
432 YYABORT; |
|
433 } |
627
|
434 | simple_list parse_error |
1091
|
435 { ABORT_PARSE; } |
627
|
436 | parse_error |
1091
|
437 { ABORT_PARSE; } |
627
|
438 ; |
|
439 |
|
440 input1 : '\n' |
|
441 { $$ = 0; } |
327
|
442 | simple_list |
627
|
443 { $$ = $1; } |
1
|
444 | simple_list '\n' |
627
|
445 { $$ = $1; } |
1
|
446 | simple_list END_OF_INPUT |
627
|
447 { $$ = $1; } |
|
448 ; |
|
449 |
2525
|
450 simple_list : simple_list1 opt_sep_no_nl |
1
|
451 { |
2525
|
452 set_stmt_print_flag ($1, $2, false); |
1511
|
453 $$ = $1; |
1
|
454 } |
|
455 ; |
|
456 |
578
|
457 simple_list1 : statement |
|
458 { $$ = new tree_statement_list ($1); } |
2525
|
459 | simple_list1 sep_no_nl statement |
1
|
460 { |
2525
|
461 set_stmt_print_flag ($1, $2, false); |
578
|
462 $1->append ($3); |
1511
|
463 $$ = $1; |
1
|
464 } |
|
465 ; |
|
466 |
|
467 opt_list : // empty |
578
|
468 { $$ = new tree_statement_list (); } |
1
|
469 | list |
|
470 { $$ = $1; } |
496
|
471 ; |
1
|
472 |
2525
|
473 list : list1 opt_sep |
1511
|
474 { |
2525
|
475 set_stmt_print_flag ($1, $2, true); |
1829
|
476 $$ = $1; |
1
|
477 } |
|
478 ; |
|
479 |
578
|
480 list1 : statement |
440
|
481 { |
2857
|
482 lexer_flags.beginning_of_function = false; |
578
|
483 $$ = new tree_statement_list ($1); |
440
|
484 } |
2525
|
485 | list1 sep statement |
1511
|
486 { |
2525
|
487 set_stmt_print_flag ($1, $2, true); |
578
|
488 $1->append ($3); |
1511
|
489 $$ = $1; |
1
|
490 } |
|
491 ; |
|
492 |
2970
|
493 statement : expression |
578
|
494 { $$ = new tree_statement ($1); } |
2970
|
495 | command |
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 |
2970
|
505 // =========== |
|
506 // Expressions |
|
507 // =========== |
|
508 |
|
509 identifier : NAME |
|
510 { |
|
511 $$ = new tree_identifier |
|
512 ($1->sym_rec (), $1->line (), $1->column ()); |
|
513 } |
|
514 ; |
|
515 |
|
516 constant : NUM |
|
517 { $$ = make_constant (NUM, $1); } |
|
518 | IMAG_NUM |
|
519 { $$ = make_constant (IMAG_NUM, $1); } |
|
520 | TEXT |
|
521 { $$ = make_constant (TEXT, $1); } |
|
522 ; |
|
523 |
|
524 matrix : '[' ']' |
|
525 { $$ = new tree_constant (octave_value (Matrix ())); } |
|
526 | '[' ';' ']' |
|
527 { $$ = new tree_constant (octave_value (Matrix ())); } |
|
528 | '[' rows ']' |
|
529 { $$ = finish_matrix ($2); } |
|
530 ; |
|
531 |
|
532 rows : rows1 |
|
533 { $$ = $1; } |
|
534 | rows1 ';' // Ignore trailing semicolon. |
|
535 { $$ = $1; } |
|
536 ; |
|
537 |
|
538 rows1 : matrix_row |
|
539 { $$ = new tree_matrix ($1); } |
|
540 | rows1 ';' matrix_row |
|
541 { |
|
542 $1->append ($3); |
|
543 $$ = $1; |
|
544 } |
|
545 ; |
|
546 |
|
547 matrix_row : arg_list |
|
548 { $$ = $1; } |
|
549 | arg_list ',' // Ignore trailing comma. |
|
550 { $$ = $1; } |
|
551 ; |
|
552 |
|
553 primary_expr : identifier |
|
554 { $$ = $1; } |
|
555 | constant |
|
556 { $$ = $1; } |
|
557 | matrix |
|
558 { $$ = $1; } |
|
559 | '(' expression ')' |
|
560 { $$ = $2->mark_in_parens (); } |
|
561 ; |
|
562 |
|
563 magic_colon : ':' |
|
564 { |
|
565 octave_value tmp (octave_value::magic_colon_t); |
|
566 $$ = new tree_constant (tmp); |
|
567 } |
|
568 ; |
|
569 |
|
570 arg_list : expression |
|
571 { $$ = new tree_argument_list ($1); } |
|
572 | magic_colon |
|
573 { $$ = new tree_argument_list ($1); } |
|
574 | ALL_VA_ARGS |
|
575 { |
|
576 octave_value tmp (octave_value::all_va_args_t); |
|
577 tree_constant *all_va_args = new tree_constant (tmp); |
|
578 $$ = new tree_argument_list (all_va_args); |
|
579 } |
|
580 | arg_list ',' magic_colon |
|
581 { |
|
582 $1->append ($3); |
|
583 $$ = $1; |
|
584 } |
|
585 | arg_list ',' expression |
|
586 { |
|
587 $1->append ($3); |
|
588 $$ = $1; |
|
589 } |
|
590 | arg_list ',' ALL_VA_ARGS |
|
591 { |
|
592 octave_value tmp (octave_value::all_va_args_t); |
|
593 tree_constant *all_va_args = new tree_constant (tmp); |
|
594 $1->append (all_va_args); |
|
595 $$ = $1; |
|
596 } |
|
597 ; |
|
598 |
|
599 parsing_indir : // empty |
|
600 { lexer_flags.looking_at_indirect_ref = true; } |
|
601 ; |
|
602 |
|
603 postfix_expr : primary_expr |
|
604 { $$ = $1; } |
|
605 | postfix_expr '(' ')' |
|
606 { $$ = make_index_expression ($1, 0); } |
|
607 | postfix_expr '(' arg_list ')' |
|
608 { $$ = make_index_expression ($1, $3); } |
|
609 | postfix_expr PLUS_PLUS |
|
610 { $$ = make_postfix_op (PLUS_PLUS, $1, $2); } |
|
611 | postfix_expr MINUS_MINUS |
|
612 { $$ = make_postfix_op (MINUS_MINUS, $1, $2); } |
|
613 | postfix_expr QUOTE |
|
614 { $$ = make_postfix_op (QUOTE, $1, $2); } |
|
615 | postfix_expr TRANSPOSE |
|
616 { $$ = make_postfix_op (TRANSPOSE, $1, $2); } |
|
617 | postfix_expr '.' parsing_indir STRUCT_ELT |
|
618 { $$ = make_indirect_ref ($1, $4->text ()); } |
|
619 ; |
|
620 |
|
621 prefix_expr : postfix_expr |
|
622 { $$ = $1; } |
|
623 | PLUS_PLUS prefix_expr %prec UNARY |
|
624 { $$ = make_prefix_op (PLUS_PLUS, $2, $1); } |
|
625 | MINUS_MINUS prefix_expr %prec UNARY |
|
626 { $$ = make_prefix_op (MINUS_MINUS, $2, $1); } |
|
627 | EXPR_NOT prefix_expr %prec UNARY |
|
628 { $$ = make_prefix_op (EXPR_NOT, $2, $1); } |
|
629 | '+' prefix_expr %prec UNARY |
|
630 { $$ = $2; } |
|
631 | '-' prefix_expr %prec UNARY |
|
632 { $$ = make_prefix_op ('-', $2, $1); } |
|
633 ; |
|
634 |
|
635 binary_expr : prefix_expr |
|
636 { $$ = $1; } |
|
637 | binary_expr POW binary_expr |
|
638 { $$ = make_binary_op (POW, $1, $2, $3); } |
|
639 | binary_expr EPOW binary_expr |
|
640 { $$ = make_binary_op (EPOW, $1, $2, $3); } |
|
641 | binary_expr '+' binary_expr |
|
642 { $$ = make_binary_op ('+', $1, $2, $3); } |
|
643 | binary_expr '-' binary_expr |
|
644 { $$ = make_binary_op ('-', $1, $2, $3); } |
|
645 | binary_expr '*' binary_expr |
|
646 { $$ = make_binary_op ('*', $1, $2, $3); } |
|
647 | binary_expr '/' binary_expr |
|
648 { $$ = make_binary_op ('/', $1, $2, $3); } |
|
649 | binary_expr EPLUS binary_expr |
|
650 { $$ = make_binary_op ('+', $1, $2, $3); } |
|
651 | binary_expr EMINUS binary_expr |
|
652 { $$ = make_binary_op ('-', $1, $2, $3); } |
|
653 | binary_expr EMUL binary_expr |
|
654 { $$ = make_binary_op (EMUL, $1, $2, $3); } |
|
655 | binary_expr EDIV binary_expr |
|
656 { $$ = make_binary_op (EDIV, $1, $2, $3); } |
|
657 | binary_expr LEFTDIV binary_expr |
|
658 { $$ = make_binary_op (LEFTDIV, $1, $2, $3); } |
|
659 | binary_expr ELEFTDIV binary_expr |
|
660 { $$ = make_binary_op (ELEFTDIV, $1, $2, $3); } |
|
661 ; |
|
662 |
|
663 colon_expr : colon_expr1 |
|
664 { $$ = finish_colon_expression ($1); } |
|
665 ; |
|
666 |
|
667 colon_expr1 : binary_expr |
|
668 { $$ = new tree_colon_expression ($1); } |
|
669 | colon_expr1 ':' binary_expr |
|
670 { |
|
671 if (! ($$ = $1->append ($3))) |
|
672 ABORT_PARSE; |
|
673 } |
|
674 ; |
|
675 |
|
676 simple_expr : colon_expr |
|
677 { $$ = $1; } |
|
678 | simple_expr LSHIFT simple_expr |
|
679 { $$ = make_binary_op (LSHIFT, $1, $2, $3); } |
|
680 | simple_expr RSHIFT simple_expr |
|
681 { $$ = make_binary_op (RSHIFT, $1, $2, $3); } |
|
682 | simple_expr EXPR_LT simple_expr |
|
683 { $$ = make_binary_op (EXPR_LT, $1, $2, $3); } |
|
684 | simple_expr EXPR_LE simple_expr |
|
685 { $$ = make_binary_op (EXPR_LE, $1, $2, $3); } |
|
686 | simple_expr EXPR_EQ simple_expr |
|
687 { $$ = make_binary_op (EXPR_EQ, $1, $2, $3); } |
|
688 | simple_expr EXPR_GE simple_expr |
|
689 { $$ = make_binary_op (EXPR_GE, $1, $2, $3); } |
|
690 | simple_expr EXPR_GT simple_expr |
|
691 { $$ = make_binary_op (EXPR_GT, $1, $2, $3); } |
|
692 | simple_expr EXPR_NE simple_expr |
|
693 { $$ = make_binary_op (EXPR_NE, $1, $2, $3); } |
|
694 | simple_expr EXPR_AND simple_expr |
|
695 { $$ = make_binary_op (EXPR_AND, $1, $2, $3); } |
|
696 | simple_expr EXPR_OR simple_expr |
|
697 { $$ = make_binary_op (EXPR_OR, $1, $2, $3); } |
|
698 | simple_expr EXPR_AND_AND simple_expr |
|
699 { $$ = make_boolean_op (EXPR_AND_AND, $1, $2, $3); } |
|
700 | simple_expr EXPR_OR_OR simple_expr |
|
701 { $$ = make_boolean_op (EXPR_OR_OR, $1, $2, $3); } |
|
702 ; |
|
703 |
|
704 // Arrange for the lexer to return CLOSE_BRACE for `]' by looking ahead |
|
705 // one token for an assignment op. |
|
706 |
|
707 assign_lhs : simple_expr |
|
708 { $$ = new tree_argument_list ($1); } |
|
709 | '[' arg_list CLOSE_BRACE |
|
710 { $$ = $2; } |
|
711 ; |
|
712 |
|
713 assign_expr : assign_lhs '=' expression |
|
714 { $$ = make_assign_op ('=', $1, $2, $3); } |
|
715 | assign_lhs ADD_EQ expression |
|
716 { $$ = make_assign_op (ADD_EQ, $1, $2, $3); } |
|
717 | assign_lhs SUB_EQ expression |
|
718 { $$ = make_assign_op (SUB_EQ, $1, $2, $3); } |
|
719 | assign_lhs MUL_EQ expression |
|
720 { $$ = make_assign_op (MUL_EQ, $1, $2, $3); } |
|
721 | assign_lhs DIV_EQ expression |
|
722 { $$ = make_assign_op (DIV_EQ, $1, $2, $3); } |
|
723 | assign_lhs LSHIFT_EQ expression |
|
724 { $$ = make_assign_op (LSHIFT_EQ, $1, $2, $3); } |
|
725 | assign_lhs RSHIFT_EQ expression |
|
726 { $$ = make_assign_op (RSHIFT_EQ, $1, $2, $3); } |
|
727 | assign_lhs EMUL_EQ expression |
|
728 { $$ = make_assign_op (EMUL_EQ, $1, $2, $3); } |
|
729 | assign_lhs EDIV_EQ expression |
|
730 { $$ = make_assign_op (EDIV_EQ, $1, $2, $3); } |
|
731 | assign_lhs AND_EQ expression |
|
732 { $$ = make_assign_op (AND_EQ, $1, $2, $3); } |
|
733 | assign_lhs OR_EQ expression |
|
734 { $$ = make_assign_op (OR_EQ, $1, $2, $3); } |
|
735 ; |
|
736 |
|
737 word_list_cmd : identifier word_list |
|
738 { $$ = make_index_expression ($1, $2); } |
|
739 ; |
|
740 |
|
741 word_list : TEXT |
|
742 { |
|
743 tree_constant *tmp = make_constant (TEXT, $1); |
|
744 $$ = new tree_argument_list (tmp); |
|
745 } |
|
746 | word_list TEXT |
|
747 { |
|
748 tree_constant *tmp = make_constant (TEXT, $2); |
|
749 $1->append (tmp); |
|
750 $$ = $1; |
|
751 } |
|
752 ; |
|
753 |
|
754 expression : simple_expr |
|
755 { $$ = $1; } |
|
756 | word_list_cmd |
|
757 { $$ = $1; } |
|
758 | assign_expr |
|
759 { $$ = $1; } |
|
760 ; |
|
761 |
|
762 // ================================================ |
|
763 // Commands, declarations, and function definitions |
|
764 // ================================================ |
|
765 |
|
766 command : declaration |
|
767 { $$ = $1; } |
|
768 | select_command |
|
769 { $$ = $1; } |
|
770 | loop_command |
|
771 { $$ = $1; } |
|
772 | jump_command |
|
773 { $$ = $1; } |
|
774 | except_command |
|
775 { $$ = $1; } |
|
776 | function |
|
777 { $$ = $1; } |
|
778 | plot_command |
|
779 { $$ = $1; } |
|
780 ; |
|
781 |
|
782 // ===================== |
|
783 // Declaration statemnts |
|
784 // ===================== |
|
785 |
|
786 declaration : GLOBAL decl1 |
|
787 { $$ = make_decl_command (GLOBAL, $1, $2); } |
|
788 | STATIC decl1 |
|
789 { $$ = make_decl_command (STATIC, $1, $2); } |
|
790 ; |
|
791 |
|
792 decl1 : decl2 |
|
793 { $$ = new tree_decl_init_list ($1); } |
|
794 | decl1 decl2 |
|
795 { |
|
796 $1->append ($2); |
|
797 $$ = $1; |
|
798 } |
|
799 ; |
|
800 |
|
801 decl2 : identifier |
|
802 { $$ = new tree_decl_elt ($1); } |
|
803 | identifier '=' expression |
|
804 { $$ = new tree_decl_elt ($1, $3); } |
|
805 ; |
|
806 |
|
807 // ==================== |
|
808 // Selection statements |
|
809 // ==================== |
|
810 |
|
811 select_command : if_command |
|
812 { $$ = $1; } |
|
813 | switch_command |
|
814 { $$ = $1; } |
|
815 ; |
|
816 |
|
817 // ============ |
|
818 // If statement |
|
819 // ============ |
|
820 |
|
821 if_command : IF if_cmd_list END |
|
822 { |
|
823 if (! ($$ = finish_if_command ($1, $2, $3))) |
|
824 ABORT_PARSE; |
|
825 } |
|
826 ; |
|
827 |
|
828 if_cmd_list : if_cmd_list1 |
|
829 { $$ = $1; } |
|
830 | if_cmd_list1 else_clause |
|
831 { |
|
832 $1->append ($2); |
|
833 $$ = $1; |
|
834 } |
|
835 ; |
|
836 |
|
837 if_cmd_list1 : expression opt_sep opt_list |
|
838 { $$ = start_if_command ($1, $3); } |
|
839 | if_cmd_list1 elseif_clause |
|
840 { |
|
841 $1->append ($2); |
|
842 $$ = $1; |
|
843 } |
|
844 ; |
|
845 |
|
846 elseif_clause : ELSEIF opt_sep expression opt_sep opt_list |
|
847 { $$ = make_elseif_clause ($3, $5); } |
|
848 ; |
|
849 |
|
850 else_clause : ELSE opt_sep opt_list |
|
851 { $$ = new tree_if_clause ($3); } |
|
852 ; |
|
853 |
|
854 // ================ |
|
855 // Switch statement |
|
856 // ================ |
|
857 |
|
858 switch_command : SWITCH expression opt_sep case_list END |
|
859 { |
|
860 if (! ($$ = finish_switch_command ($1, $2, $4, $5))) |
|
861 ABORT_PARSE; |
|
862 } |
|
863 ; |
|
864 |
|
865 case_list : case_list1 |
|
866 { $$ = $1; } |
|
867 | case_list1 default_case |
|
868 { |
|
869 $1->append ($2); |
|
870 $$ = $1; |
|
871 } |
|
872 ; |
|
873 |
|
874 case_list1 : switch_case |
|
875 { $$ = new tree_switch_case_list ($1); } |
|
876 | case_list1 switch_case |
|
877 { |
|
878 $1->append ($2); |
|
879 $$ = $1; |
|
880 } |
|
881 ; |
|
882 |
|
883 switch_case : CASE opt_sep expression opt_sep list |
|
884 { $$ = make_switch_case ($3, $5); } |
|
885 ; |
|
886 |
|
887 default_case : OTHERWISE opt_sep opt_list |
|
888 { $$ = new tree_switch_case ($3); } |
|
889 ; |
|
890 |
|
891 // ======= |
|
892 // Looping |
|
893 // ======= |
|
894 |
|
895 loop_command : WHILE expression opt_sep opt_list END |
|
896 { |
|
897 if (! ($$ = make_while_command ($1, $2, $4, $5))) |
|
898 ABORT_PARSE; |
|
899 } |
|
900 | FOR assign_lhs '=' expression opt_sep opt_list END |
|
901 { |
|
902 if (! ($$ = make_for_command ($1, $2, $4, $6, $7))) |
|
903 ABORT_PARSE; |
|
904 } |
|
905 ; |
|
906 |
|
907 // ======= |
|
908 // Jumping |
|
909 // ======= |
|
910 |
|
911 jump_command : BREAK |
|
912 { |
|
913 if (! ($$ = make_break_command ($1))) |
|
914 ABORT_PARSE; |
|
915 } |
|
916 | CONTINUE |
|
917 { |
|
918 if (! ($$ = make_continue_command ($1))) |
|
919 ABORT_PARSE; |
|
920 } |
|
921 | FUNC_RET |
|
922 { |
|
923 if (! ($$ = make_return_command ($1))) |
|
924 ABORT_PARSE; |
|
925 } |
|
926 ; |
|
927 |
|
928 // ========== |
|
929 // Exceptions |
|
930 // ========== |
|
931 |
|
932 except_command : UNWIND opt_sep opt_list CLEANUP opt_sep opt_list END |
|
933 { |
|
934 if (! ($$ = make_unwind_command ($1, $3, $6, $7))) |
|
935 ABORT_PARSE; |
|
936 } |
|
937 | TRY opt_sep opt_list CATCH opt_sep opt_list END |
|
938 { |
|
939 if (! ($$ = make_try_command ($1, $3, $6, $7))) |
|
940 ABORT_PARSE; |
|
941 } |
|
942 ; |
|
943 |
|
944 // =========================================== |
|
945 // Some `subroutines' for function definitions |
|
946 // =========================================== |
|
947 |
|
948 global_symtab : // empty |
|
949 { curr_sym_tab = global_sym_tab; } |
|
950 ; |
|
951 |
|
952 local_symtab : // empty |
|
953 { curr_sym_tab = tmp_local_sym_tab; } |
|
954 ; |
|
955 |
|
956 in_return_list : // empty |
|
957 { lexer_flags.looking_at_return_list = true; } |
|
958 ; |
|
959 |
|
960 parsed_fcn_name : // empty |
|
961 { lexer_flags.parsed_function_name = true; } |
|
962 ; |
|
963 |
|
964 // =========================== |
|
965 // List of function parameters |
|
966 // =========================== |
|
967 |
|
968 param_list_beg : '(' |
|
969 { lexer_flags.looking_at_parameter_list = true; } |
|
970 ; |
|
971 |
|
972 param_list_end : ')' |
|
973 { lexer_flags.looking_at_parameter_list = false; } |
|
974 ; |
|
975 |
|
976 param_list : param_list_beg param_list_end |
|
977 { |
|
978 lexer_flags.quote_is_transpose = false; |
|
979 $$ = 0; |
|
980 } |
|
981 | param_list_beg ELLIPSIS param_list_end |
|
982 { |
|
983 lexer_flags.quote_is_transpose = false; |
|
984 tree_parameter_list *tmp = new tree_parameter_list (); |
|
985 tmp->mark_varargs_only (); |
|
986 $$ = tmp; |
|
987 } |
|
988 | param_list1 param_list_end |
|
989 { |
|
990 lexer_flags.quote_is_transpose = false; |
|
991 $1->mark_as_formal_parameters (); |
|
992 $$ = $1; |
|
993 } |
|
994 | param_list1 ',' ELLIPSIS param_list_end |
|
995 { |
|
996 lexer_flags.quote_is_transpose = false; |
|
997 $1->mark_as_formal_parameters (); |
|
998 $1->mark_varargs (); |
|
999 $$ = $1; |
|
1000 } |
|
1001 ; |
|
1002 |
|
1003 param_list1 : param_list_beg identifier |
|
1004 { $$ = new tree_parameter_list ($2); } |
|
1005 | param_list1 ',' identifier |
|
1006 { |
|
1007 $1->append ($3); |
|
1008 $$ = $1; |
|
1009 } |
|
1010 | param_list_beg error |
|
1011 { |
|
1012 yyerror ("invalid parameter list"); |
|
1013 $$ = 0; |
|
1014 ABORT_PARSE; |
|
1015 } |
|
1016 | param_list1 ',' error |
|
1017 { |
|
1018 yyerror ("invalid parameter list"); |
|
1019 $$ = 0; |
|
1020 ABORT_PARSE; |
|
1021 } |
|
1022 ; |
|
1023 |
|
1024 // =================================== |
|
1025 // List of function return value names |
|
1026 // =================================== |
|
1027 |
|
1028 return_list_beg : '[' local_symtab in_return_list |
|
1029 ; |
|
1030 |
|
1031 return_list : return_list_beg return_list_end |
|
1032 { |
|
1033 lexer_flags.looking_at_return_list = false; |
|
1034 $$ = new tree_parameter_list (); |
|
1035 } |
|
1036 | return_list_beg ELLIPSIS return_list_end |
|
1037 { |
|
1038 lexer_flags.looking_at_return_list = false; |
|
1039 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1040 tmp->mark_varargs_only (); |
|
1041 $$ = tmp; |
|
1042 } |
|
1043 | return_list_beg return_list1 return_list_end |
|
1044 { |
|
1045 lexer_flags.looking_at_return_list = false; |
|
1046 $$ = $2; |
|
1047 } |
|
1048 | return_list_beg return_list1 ',' ELLIPSIS return_list_end |
|
1049 { |
|
1050 lexer_flags.looking_at_return_list = false; |
|
1051 $2->mark_varargs (); |
|
1052 $$ = $2; |
|
1053 } |
|
1054 ; |
|
1055 |
|
1056 return_list1 : identifier |
|
1057 { $$ = new tree_parameter_list ($1); } |
|
1058 | return_list1 ',' identifier |
|
1059 { |
|
1060 $1->append ($3); |
|
1061 $$ = $1; |
|
1062 } |
|
1063 ; |
|
1064 |
|
1065 return_list_end : global_symtab ']' |
|
1066 ; |
|
1067 |
|
1068 // =================== |
|
1069 // Function definition |
|
1070 // =================== |
|
1071 |
|
1072 function_beg : FCN global_symtab |
|
1073 ; |
|
1074 |
|
1075 function : function_beg function2 |
|
1076 { |
|
1077 recover_from_parsing_function (); |
|
1078 $$ = 0; |
|
1079 } |
|
1080 | function_beg identifier function1 |
|
1081 { |
|
1082 finish_function ($2, $3); |
|
1083 recover_from_parsing_function (); |
|
1084 $$ = 0; |
|
1085 } |
|
1086 | function_beg return_list function1 |
|
1087 { |
|
1088 finish_function ($2, $3); |
|
1089 recover_from_parsing_function (); |
|
1090 $$ = 0; |
|
1091 } |
|
1092 ; |
|
1093 |
|
1094 function1 : global_symtab '=' function2 |
|
1095 { $$ = $3; } |
|
1096 ; |
|
1097 |
|
1098 function2 : identifier local_symtab parsed_fcn_name function3 |
|
1099 { |
|
1100 if (! ($$ = frob_function ($1, $4))) |
|
1101 ABORT_PARSE; |
|
1102 } |
|
1103 ; |
|
1104 |
|
1105 function3 : param_list function4 |
|
1106 { $$ = start_function ($1, $2); } |
|
1107 | function4 |
|
1108 { $$ = start_function (0, $1); } |
|
1109 ; |
|
1110 |
|
1111 function4 : opt_sep opt_list function_end |
|
1112 { $$ = $2; } |
|
1113 ; |
|
1114 |
|
1115 function_end : END |
|
1116 { |
|
1117 if (end_token_ok ($1, token::function_end)) |
|
1118 { |
|
1119 if (reading_fcn_file) |
|
1120 check_for_garbage_after_fcn_def (); |
|
1121 } |
|
1122 else |
|
1123 ABORT_PARSE; |
|
1124 } |
|
1125 | END_OF_INPUT |
|
1126 { |
|
1127 if (! (reading_fcn_file || reading_script_file)) |
|
1128 YYABORT; |
|
1129 } |
|
1130 ; |
|
1131 |
|
1132 // ======== |
|
1133 // Plotting |
|
1134 // ======== |
|
1135 |
1
|
1136 plot_command : PLOT plot_command1 |
|
1137 { |
1623
|
1138 if (! ($$ = make_plot_command ($1, 0, $2))) |
|
1139 ABORT_PARSE; |
1
|
1140 } |
|
1141 | PLOT ranges plot_command1 |
|
1142 { |
1623
|
1143 if (! ($$ = make_plot_command ($1, $2, $3))) |
|
1144 ABORT_PARSE; |
1
|
1145 } |
|
1146 ; |
|
1147 |
|
1148 ranges : ranges1 |
578
|
1149 { $$ = new plot_limits ($1); } |
1
|
1150 | ranges1 ranges1 |
578
|
1151 { $$ = new plot_limits ($1, $2); } |
1
|
1152 | ranges1 ranges1 ranges1 |
578
|
1153 { $$ = new plot_limits ($1, $2, $3); } |
1
|
1154 ; |
|
1155 |
|
1156 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
578
|
1157 { $$ = new plot_range ($2, $4); } |
1
|
1158 | OPEN_BRACE COLON expression CLOSE_BRACE |
578
|
1159 { $$ = new plot_range (0, $3); } |
1
|
1160 | OPEN_BRACE expression COLON CLOSE_BRACE |
578
|
1161 { $$ = new plot_range ($2, 0); } |
1
|
1162 | OPEN_BRACE COLON CLOSE_BRACE |
578
|
1163 { $$ = new plot_range (); } |
1
|
1164 | OPEN_BRACE CLOSE_BRACE |
578
|
1165 { $$ = new plot_range (); } |
1
|
1166 ; |
|
1167 |
478
|
1168 plot_command1 : // empty |
522
|
1169 { $$ = 0; } |
478
|
1170 | plot_command2 |
578
|
1171 { $$ = new subplot_list ($1); } |
1
|
1172 | plot_command1 ',' plot_command2 |
1829
|
1173 { |
|
1174 $1->append ($3); |
|
1175 $$ = $1; |
|
1176 } |
1
|
1177 ; |
|
1178 |
|
1179 plot_command2 : expression |
578
|
1180 { $$ = new subplot ($1); } |
1
|
1181 | expression plot_options |
3165
|
1182 { $$ = $2->add_data ($1); } |
1
|
1183 ; |
|
1184 |
|
1185 plot_options : using |
3165
|
1186 { |
|
1187 subplot *tmp = new subplot (); |
|
1188 $$ = tmp->add_clause ($1); |
|
1189 } |
1
|
1190 | title |
3165
|
1191 { |
|
1192 subplot *tmp = new subplot (); |
|
1193 $$ = tmp->add_clause ($1); |
|
1194 } |
1
|
1195 | style |
3165
|
1196 { |
|
1197 subplot *tmp = new subplot (); |
|
1198 $$ = tmp->add_clause ($1); |
|
1199 } |
|
1200 | axes |
|
1201 { |
|
1202 subplot *tmp = new subplot (); |
|
1203 $$ = tmp->add_clause ($1); |
|
1204 } |
|
1205 | plot_options using |
|
1206 { |
|
1207 if (! ($$ = $1->add_clause ($2))) |
|
1208 { |
|
1209 yyerror ("only one using option may be specified"); |
|
1210 ABORT_PARSE; |
|
1211 } |
|
1212 } |
|
1213 | plot_options title |
|
1214 { |
|
1215 if (! ($$ = $1->add_clause ($2))) |
|
1216 { |
|
1217 yyerror ("only one title option my be specified"); |
|
1218 ABORT_PARSE; |
|
1219 } |
|
1220 } |
|
1221 | plot_options style |
|
1222 { |
|
1223 if (! ($$ = $1->add_clause ($2))) |
|
1224 { |
|
1225 yyerror ("only one style option my be specified"); |
|
1226 ABORT_PARSE; |
|
1227 } |
|
1228 } |
|
1229 | plot_options axes |
|
1230 { |
|
1231 if (! ($$ = $1->add_clause ($2))) |
|
1232 { |
|
1233 yyerror ("only one axes option may be specified"); |
|
1234 ABORT_PARSE; |
|
1235 } |
|
1236 } |
|
1237 ; |
|
1238 |
|
1239 axes : AXES AXES_TAG |
|
1240 { |
|
1241 lexer_flags.in_plot_axes = false; |
|
1242 $$ = new subplot_axes ($2->text ()); |
|
1243 } |
1
|
1244 ; |
|
1245 |
|
1246 using : using1 |
|
1247 { |
2857
|
1248 lexer_flags.in_plot_using = false; |
1
|
1249 $$ = $1; |
|
1250 } |
|
1251 | using1 expression |
|
1252 { |
2857
|
1253 lexer_flags.in_plot_using = false; |
1
|
1254 $$ = $1->set_format ($2); |
|
1255 } |
|
1256 ; |
|
1257 |
|
1258 using1 : USING expression |
|
1259 { |
578
|
1260 subplot_using *tmp = new subplot_using (); |
1
|
1261 $$ = tmp->add_qualifier ($2); |
|
1262 } |
|
1263 | using1 COLON expression |
|
1264 { $$ = $1->add_qualifier ($3); } |
|
1265 ; |
|
1266 |
|
1267 title : TITLE expression |
|
1268 { $$ = $2; } |
|
1269 ; |
|
1270 |
|
1271 style : WITH STYLE |
1755
|
1272 { $$ = new subplot_style ($2->text ()); } |
1
|
1273 | WITH STYLE expression |
1755
|
1274 { $$ = new subplot_style ($2->text (), $3); } |
2542
|
1275 | WITH STYLE expression expression |
|
1276 { $$ = new subplot_style ($2->text (), $3, $4); } |
1
|
1277 ; |
|
1278 |
2970
|
1279 // ============= |
|
1280 // Miscellaneous |
|
1281 // ============= |
|
1282 |
|
1283 parse_error : LEXICAL_ERROR |
|
1284 { yyerror ("parse error"); } |
|
1285 | error |
1
|
1286 ; |
|
1287 |
2525
|
1288 sep_no_nl : ',' |
|
1289 { $$ = ','; } |
|
1290 | ';' |
|
1291 { $$ = ';'; } |
|
1292 | sep_no_nl ',' |
|
1293 { $$ = $1; } |
|
1294 | sep_no_nl ';' |
|
1295 { $$ = $1; } |
|
1296 ; |
|
1297 |
|
1298 opt_sep_no_nl : // empty |
|
1299 { $$ = 0; } |
|
1300 | sep_no_nl |
|
1301 { $$ = $1; } |
|
1302 ; |
|
1303 |
|
1304 sep : ',' |
|
1305 { $$ = ','; } |
|
1306 | ';' |
|
1307 { $$ = ';'; } |
|
1308 | '\n' |
|
1309 { $$ = '\n'; } |
|
1310 | sep ',' |
|
1311 { $$ = $1; } |
|
1312 | sep ';' |
|
1313 { $$ = $1; } |
|
1314 | sep '\n' |
|
1315 { $$ = $1; } |
|
1316 ; |
|
1317 |
|
1318 opt_sep : // empty |
|
1319 { $$ = 0; } |
|
1320 | sep |
|
1321 { $$ = $1; } |
|
1322 ; |
|
1323 |
1
|
1324 %% |
|
1325 |
666
|
1326 // Generic error messages. |
|
1327 |
1
|
1328 static void |
2805
|
1329 yyerror (const char *s) |
1
|
1330 { |
143
|
1331 int err_col = current_input_column - 1; |
1
|
1332 |
581
|
1333 ostrstream output_buf; |
1
|
1334 |
1005
|
1335 if (reading_fcn_file || reading_script_file) |
|
1336 output_buf << "parse error near line " << input_line_number |
1607
|
1337 << " of file " << curr_fcn_file_full_name; |
1005
|
1338 else |
|
1339 output_buf << "parse error:"; |
581
|
1340 |
1005
|
1341 if (s && strcmp (s, "parse error") != 0) |
|
1342 output_buf << "\n\n " << s; |
|
1343 |
|
1344 output_buf << "\n\n"; |
1
|
1345 |
1755
|
1346 if (! current_input_line.empty ()) |
1
|
1347 { |
1755
|
1348 size_t len = current_input_line.length (); |
1060
|
1349 |
1755
|
1350 if (current_input_line[len-1] == '\n') |
|
1351 current_input_line.resize (len-1); |
1005
|
1352 |
335
|
1353 // Print the line, maybe with a pointer near the error token. |
1005
|
1354 |
1755
|
1355 output_buf << ">>> " << current_input_line << "\n"; |
1060
|
1356 |
|
1357 if (err_col == 0) |
|
1358 err_col = len; |
|
1359 |
|
1360 for (int i = 0; i < err_col + 3; i++) |
|
1361 output_buf << " "; |
|
1362 |
|
1363 output_buf << "^"; |
1
|
1364 } |
1005
|
1365 |
|
1366 output_buf << "\n" << ends; |
581
|
1367 |
1005
|
1368 char *msg = output_buf.str (); |
|
1369 |
1090
|
1370 parse_error ("%s", msg); |
1005
|
1371 |
|
1372 delete [] msg; |
1
|
1373 } |
|
1374 |
666
|
1375 // Error mesages for mismatched end tokens. |
|
1376 |
496
|
1377 static void |
2805
|
1378 end_error (const char *type, token::end_tok_type ettype, int l, int c) |
496
|
1379 { |
2805
|
1380 static const char *fmt |
|
1381 = "`%s' command matched by `%s' near line %d column %d"; |
496
|
1382 |
|
1383 switch (ettype) |
|
1384 { |
|
1385 case token::simple_end: |
|
1386 error (fmt, type, "end", l, c); |
|
1387 break; |
777
|
1388 |
496
|
1389 case token::for_end: |
|
1390 error (fmt, type, "endfor", l, c); |
|
1391 break; |
777
|
1392 |
496
|
1393 case token::function_end: |
|
1394 error (fmt, type, "endfunction", l, c); |
|
1395 break; |
777
|
1396 |
496
|
1397 case token::if_end: |
|
1398 error (fmt, type, "endif", l, c); |
|
1399 break; |
777
|
1400 |
496
|
1401 case token::while_end: |
|
1402 error (fmt, type, "endwhile", l, c); |
|
1403 break; |
777
|
1404 |
1371
|
1405 case token::unwind_protect_end: |
|
1406 error (fmt, type, "end_unwind_protect", l, c); |
|
1407 break; |
|
1408 |
496
|
1409 default: |
|
1410 panic_impossible (); |
|
1411 break; |
|
1412 } |
|
1413 } |
|
1414 |
666
|
1415 // Check to see that end tokens are properly matched. |
|
1416 |
2857
|
1417 static bool |
|
1418 end_token_ok (token *tok, token::end_tok_type expected) |
143
|
1419 { |
2857
|
1420 bool retval = true; |
|
1421 |
143
|
1422 token::end_tok_type ettype = tok->ettype (); |
2857
|
1423 |
143
|
1424 if (ettype != expected && ettype != token::simple_end) |
|
1425 { |
2857
|
1426 retval = false; |
|
1427 |
143
|
1428 yyerror ("parse error"); |
|
1429 |
|
1430 int l = tok->line (); |
|
1431 int c = tok->column (); |
|
1432 |
|
1433 switch (expected) |
|
1434 { |
|
1435 case token::for_end: |
|
1436 end_error ("for", ettype, l, c); |
|
1437 break; |
777
|
1438 |
143
|
1439 case token::function_end: |
|
1440 end_error ("function", ettype, l, c); |
|
1441 break; |
777
|
1442 |
143
|
1443 case token::if_end: |
|
1444 end_error ("if", ettype, l, c); |
|
1445 break; |
777
|
1446 |
1489
|
1447 case token::try_catch_end: |
|
1448 end_error ("try", ettype, l, c); |
|
1449 break; |
|
1450 |
2764
|
1451 case token::switch_end: |
|
1452 end_error ("switch", ettype, l, c); |
|
1453 break; |
|
1454 |
1489
|
1455 case token::unwind_protect_end: |
|
1456 end_error ("unwind_protect", ettype, l, c); |
|
1457 break; |
|
1458 |
143
|
1459 case token::while_end: |
|
1460 end_error ("while", ettype, l, c); |
|
1461 break; |
777
|
1462 |
143
|
1463 default: |
|
1464 panic_impossible (); |
|
1465 break; |
|
1466 } |
|
1467 } |
2857
|
1468 |
|
1469 return retval; |
143
|
1470 } |
|
1471 |
666
|
1472 // Maybe print a warning if an assignment expression is used as the |
|
1473 // test in a logical expression. |
|
1474 |
496
|
1475 static void |
|
1476 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1477 { |
2166
|
1478 if (Vwarn_assign_as_truth_value |
1
|
1479 && expr->is_assignment_expression () |
2961
|
1480 && expr->paren_count () < 2) |
1
|
1481 { |
|
1482 warning ("suggest parenthesis around assignment used as truth value"); |
|
1483 } |
|
1484 } |
578
|
1485 |
2764
|
1486 // Maybe print a warning about switch labels that aren't constants. |
|
1487 |
|
1488 static void |
|
1489 maybe_warn_variable_switch_label (tree_expression *expr) |
|
1490 { |
|
1491 if (Vwarn_variable_switch_label && ! expr->is_constant ()) |
|
1492 { |
|
1493 warning ("variable switch label"); |
|
1494 } |
|
1495 } |
|
1496 |
1623
|
1497 // Create a plot command. |
|
1498 |
|
1499 static tree_plot_command * |
|
1500 make_plot_command (token *tok, plot_limits *range, subplot_list *list) |
|
1501 { |
|
1502 if (range) |
|
1503 { |
|
1504 if (tok->pttype () == token::replot) |
|
1505 { |
|
1506 yyerror ("cannot specify new ranges with replot"); |
|
1507 return 0; |
|
1508 } |
|
1509 } |
|
1510 else if (! list && tok->pttype () != token::replot) |
|
1511 { |
|
1512 yyerror ("must have something to plot"); |
|
1513 return 0; |
|
1514 } |
|
1515 |
2857
|
1516 lexer_flags.plotting = false; |
|
1517 lexer_flags.past_plot_range = false; |
|
1518 lexer_flags.in_plot_range = false; |
|
1519 lexer_flags.in_plot_using = false; |
|
1520 lexer_flags.in_plot_style = false; |
1623
|
1521 |
|
1522 return new tree_plot_command (list, range, tok->pttype ()); |
|
1523 } |
|
1524 |
2533
|
1525 static tree_expression * |
|
1526 fold (tree_binary_expression *e) |
|
1527 { |
3110
|
1528 tree_expression *retval = e; |
|
1529 |
|
1530 unwind_protect::begin_frame ("fold"); |
|
1531 |
|
1532 unwind_protect_int (error_state); |
|
1533 |
|
1534 unwind_protect_bool (buffer_error_messages); |
|
1535 buffer_error_messages = true; |
|
1536 |
|
1537 unwind_protect::add (clear_global_error_variable, 0); |
2533
|
1538 |
|
1539 tree_expression *op1 = e->lhs (); |
|
1540 tree_expression *op2 = e->rhs (); |
|
1541 |
|
1542 if (op1->is_constant () && op2->is_constant ()) |
|
1543 { |
2970
|
1544 octave_value tmp = e->rvalue (); |
2533
|
1545 |
|
1546 if (! error_state) |
|
1547 { |
|
1548 tree_constant *tc_retval = new tree_constant (tmp); |
|
1549 |
|
1550 ostrstream buf; |
|
1551 |
|
1552 tree_print_code tpc (buf); |
|
1553 |
|
1554 e->accept (tpc); |
|
1555 |
|
1556 buf << ends; |
|
1557 |
|
1558 char *s = buf.str (); |
|
1559 |
|
1560 tc_retval->stash_original_text (s); |
|
1561 |
|
1562 delete [] s; |
|
1563 |
|
1564 delete e; |
|
1565 |
|
1566 retval = tc_retval; |
|
1567 } |
|
1568 } |
3110
|
1569 |
|
1570 unwind_protect::run_frame ("fold"); |
2533
|
1571 |
|
1572 return retval; |
|
1573 } |
|
1574 |
1623
|
1575 // Finish building a range. |
|
1576 |
|
1577 static tree_expression * |
|
1578 finish_colon_expression (tree_colon_expression *e) |
|
1579 { |
3110
|
1580 tree_expression *retval = e; |
|
1581 |
|
1582 unwind_protect::begin_frame ("finish_colon_expression"); |
|
1583 |
|
1584 unwind_protect_int (error_state); |
|
1585 |
|
1586 unwind_protect_bool (buffer_error_messages); |
|
1587 buffer_error_messages = true; |
|
1588 |
|
1589 unwind_protect::add (clear_global_error_variable, 0); |
1623
|
1590 |
2533
|
1591 tree_expression *base = e->base (); |
|
1592 tree_expression *limit = e->limit (); |
|
1593 tree_expression *incr = e->increment (); |
|
1594 |
2970
|
1595 if (base) |
1623
|
1596 { |
2970
|
1597 if (limit) |
2533
|
1598 { |
2970
|
1599 if (base->is_constant () && limit->is_constant () |
|
1600 && (! incr || (incr && incr->is_constant ()))) |
|
1601 { |
|
1602 octave_value tmp = e->rvalue (); |
|
1603 |
|
1604 if (! error_state) |
|
1605 { |
|
1606 tree_constant *tc_retval = new tree_constant (tmp); |
|
1607 |
|
1608 ostrstream buf; |
|
1609 |
|
1610 tree_print_code tpc (buf); |
|
1611 |
|
1612 e->accept (tpc); |
|
1613 |
|
1614 buf << ends; |
|
1615 |
|
1616 char *s = buf.str (); |
|
1617 |
|
1618 tc_retval->stash_original_text (s); |
|
1619 |
|
1620 delete [] s; |
|
1621 |
|
1622 delete e; |
|
1623 |
|
1624 retval = tc_retval; |
|
1625 } |
|
1626 } |
2533
|
1627 } |
|
1628 else |
2970
|
1629 { |
2990
|
1630 e->preserve_base (); |
|
1631 delete e; |
2970
|
1632 |
|
1633 // XXX FIXME XXX -- need to attempt constant folding here |
|
1634 // too (we need a generic way to do that). |
|
1635 retval = base; |
|
1636 } |
1623
|
1637 } |
|
1638 |
3110
|
1639 unwind_protect::run_frame ("finish_colon_expression"); |
|
1640 |
1623
|
1641 return retval; |
|
1642 } |
|
1643 |
1607
|
1644 // Make a constant. |
|
1645 |
2375
|
1646 static tree_constant * |
1607
|
1647 make_constant (int op, token *tok_val) |
|
1648 { |
|
1649 int l = tok_val->line (); |
|
1650 int c = tok_val->column (); |
|
1651 |
2375
|
1652 tree_constant *retval; |
1607
|
1653 |
|
1654 switch (op) |
|
1655 { |
|
1656 case NUM: |
2533
|
1657 { |
2883
|
1658 octave_value tmp (tok_val->number ()); |
|
1659 retval = new tree_constant (tmp, l, c); |
2533
|
1660 retval->stash_original_text (tok_val->text_rep ()); |
|
1661 } |
1607
|
1662 break; |
|
1663 |
|
1664 case IMAG_NUM: |
|
1665 { |
2883
|
1666 octave_value tmp (Complex (0.0, tok_val->number ())); |
|
1667 retval = new tree_constant (tmp, l, c); |
1607
|
1668 retval->stash_original_text (tok_val->text_rep ()); |
|
1669 } |
|
1670 break; |
|
1671 |
|
1672 case TEXT: |
2883
|
1673 { |
|
1674 octave_value tmp (tok_val->text ()); |
|
1675 retval = new tree_constant (tmp, l, c); |
|
1676 } |
1607
|
1677 break; |
|
1678 |
|
1679 default: |
|
1680 panic_impossible (); |
|
1681 break; |
|
1682 } |
|
1683 |
|
1684 return retval; |
|
1685 } |
|
1686 |
666
|
1687 // Build a binary expression. |
|
1688 |
578
|
1689 static tree_expression * |
|
1690 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1691 tree_expression *op2) |
|
1692 { |
2883
|
1693 octave_value::binary_op t = octave_value::unknown_binary_op; |
1623
|
1694 |
578
|
1695 switch (op) |
|
1696 { |
|
1697 case POW: |
2883
|
1698 t = octave_value::pow; |
578
|
1699 break; |
777
|
1700 |
578
|
1701 case EPOW: |
2883
|
1702 t = octave_value::el_pow; |
578
|
1703 break; |
777
|
1704 |
578
|
1705 case '+': |
2883
|
1706 t = octave_value::add; |
578
|
1707 break; |
777
|
1708 |
578
|
1709 case '-': |
2883
|
1710 t = octave_value::sub; |
578
|
1711 break; |
777
|
1712 |
578
|
1713 case '*': |
2883
|
1714 t = octave_value::mul; |
578
|
1715 break; |
777
|
1716 |
578
|
1717 case '/': |
2883
|
1718 t = octave_value::div; |
578
|
1719 break; |
777
|
1720 |
578
|
1721 case EMUL: |
2883
|
1722 t = octave_value::el_mul; |
578
|
1723 break; |
777
|
1724 |
578
|
1725 case EDIV: |
2883
|
1726 t = octave_value::el_div; |
578
|
1727 break; |
777
|
1728 |
578
|
1729 case LEFTDIV: |
2883
|
1730 t = octave_value::ldiv; |
578
|
1731 break; |
777
|
1732 |
578
|
1733 case ELEFTDIV: |
2883
|
1734 t = octave_value::el_ldiv; |
578
|
1735 break; |
777
|
1736 |
2899
|
1737 case LSHIFT: |
|
1738 t = octave_value::lshift; |
|
1739 break; |
|
1740 |
|
1741 case RSHIFT: |
|
1742 t = octave_value::rshift; |
|
1743 break; |
|
1744 |
578
|
1745 case EXPR_LT: |
2883
|
1746 t = octave_value::lt; |
578
|
1747 break; |
777
|
1748 |
578
|
1749 case EXPR_LE: |
2883
|
1750 t = octave_value::le; |
578
|
1751 break; |
777
|
1752 |
578
|
1753 case EXPR_EQ: |
2883
|
1754 t = octave_value::eq; |
578
|
1755 break; |
777
|
1756 |
578
|
1757 case EXPR_GE: |
2883
|
1758 t = octave_value::ge; |
578
|
1759 break; |
777
|
1760 |
578
|
1761 case EXPR_GT: |
2883
|
1762 t = octave_value::gt; |
578
|
1763 break; |
777
|
1764 |
578
|
1765 case EXPR_NE: |
2883
|
1766 t = octave_value::ne; |
578
|
1767 break; |
777
|
1768 |
578
|
1769 case EXPR_AND: |
2883
|
1770 t = octave_value::el_and; |
578
|
1771 break; |
777
|
1772 |
578
|
1773 case EXPR_OR: |
2883
|
1774 t = octave_value::el_or; |
578
|
1775 break; |
777
|
1776 |
578
|
1777 default: |
|
1778 panic_impossible (); |
|
1779 break; |
|
1780 } |
|
1781 |
|
1782 int l = tok_val->line (); |
|
1783 int c = tok_val->column (); |
|
1784 |
2533
|
1785 tree_binary_expression *e |
|
1786 = new tree_binary_expression (op1, op2, l, c, t); |
1623
|
1787 |
2533
|
1788 return fold (e); |
578
|
1789 } |
|
1790 |
2375
|
1791 // Build a boolean expression. |
666
|
1792 |
578
|
1793 static tree_expression * |
2375
|
1794 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
1795 tree_expression *op2) |
578
|
1796 { |
2375
|
1797 tree_boolean_expression::type t; |
|
1798 |
578
|
1799 switch (op) |
|
1800 { |
2375
|
1801 case EXPR_AND_AND: |
2805
|
1802 t = tree_boolean_expression::bool_and; |
578
|
1803 break; |
777
|
1804 |
2375
|
1805 case EXPR_OR_OR: |
2805
|
1806 t = tree_boolean_expression::bool_or; |
578
|
1807 break; |
777
|
1808 |
578
|
1809 default: |
|
1810 panic_impossible (); |
|
1811 break; |
|
1812 } |
|
1813 |
|
1814 int l = tok_val->line (); |
|
1815 int c = tok_val->column (); |
|
1816 |
2533
|
1817 tree_boolean_expression *e |
|
1818 = new tree_boolean_expression (op1, op2, l, c, t); |
2375
|
1819 |
2533
|
1820 return fold (e); |
578
|
1821 } |
|
1822 |
2375
|
1823 // Build a prefix expression. |
666
|
1824 |
578
|
1825 static tree_expression * |
2960
|
1826 make_prefix_op (int op, tree_expression *op1, token *tok_val) |
578
|
1827 { |
2375
|
1828 tree_prefix_expression::type t; |
|
1829 |
578
|
1830 switch (op) |
|
1831 { |
2960
|
1832 case EXPR_NOT: |
|
1833 t = tree_prefix_expression::unot; |
|
1834 break; |
|
1835 |
|
1836 case '-': |
|
1837 t = tree_prefix_expression::uminus; |
|
1838 break; |
|
1839 |
578
|
1840 case PLUS_PLUS: |
2375
|
1841 t = tree_prefix_expression::increment; |
578
|
1842 break; |
777
|
1843 |
578
|
1844 case MINUS_MINUS: |
2375
|
1845 t = tree_prefix_expression::decrement; |
578
|
1846 break; |
777
|
1847 |
578
|
1848 default: |
|
1849 panic_impossible (); |
|
1850 break; |
|
1851 } |
|
1852 |
|
1853 int l = tok_val->line (); |
|
1854 int c = tok_val->column (); |
|
1855 |
3110
|
1856 // XXX FIXME XXX -- what about constant folding here? |
|
1857 |
2960
|
1858 return new tree_prefix_expression (t, op1, l, c); |
578
|
1859 } |
|
1860 |
2375
|
1861 // Build a postfix expression. |
666
|
1862 |
578
|
1863 static tree_expression * |
2960
|
1864 make_postfix_op (int op, tree_expression *op1, token *tok_val) |
578
|
1865 { |
2375
|
1866 tree_postfix_expression::type t; |
1623
|
1867 |
578
|
1868 switch (op) |
|
1869 { |
2960
|
1870 case QUOTE: |
|
1871 t = tree_postfix_expression::hermitian; |
|
1872 break; |
|
1873 |
|
1874 case TRANSPOSE: |
|
1875 t = tree_postfix_expression::transpose; |
|
1876 break; |
|
1877 |
2375
|
1878 case PLUS_PLUS: |
|
1879 t = tree_postfix_expression::increment; |
578
|
1880 break; |
777
|
1881 |
2375
|
1882 case MINUS_MINUS: |
|
1883 t = tree_postfix_expression::decrement; |
578
|
1884 break; |
777
|
1885 |
578
|
1886 default: |
|
1887 panic_impossible (); |
|
1888 break; |
|
1889 } |
|
1890 |
|
1891 int l = tok_val->line (); |
|
1892 int c = tok_val->column (); |
|
1893 |
3110
|
1894 // XXX FIXME XXX -- what about constant folding here? |
|
1895 |
2960
|
1896 return new tree_postfix_expression (t, op1, l, c); |
1623
|
1897 } |
|
1898 |
|
1899 // Build an unwind-protect command. |
|
1900 |
|
1901 static tree_command * |
|
1902 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
|
1903 tree_statement_list *cleanup, token *end_tok) |
|
1904 { |
|
1905 tree_command *retval = 0; |
|
1906 |
2857
|
1907 if (end_token_ok (end_tok, token::unwind_protect_end)) |
1623
|
1908 { |
|
1909 int l = unwind_tok->line (); |
|
1910 int c = unwind_tok->column (); |
|
1911 |
|
1912 retval = new tree_unwind_protect_command (body, cleanup, l, c); |
|
1913 } |
|
1914 |
|
1915 return retval; |
|
1916 } |
|
1917 |
|
1918 // Build a try-catch command. |
|
1919 |
|
1920 static tree_command * |
|
1921 make_try_command (token *try_tok, tree_statement_list *body, |
|
1922 tree_statement_list *cleanup, token *end_tok) |
|
1923 { |
|
1924 tree_command *retval = 0; |
|
1925 |
2857
|
1926 if (end_token_ok (end_tok, token::try_catch_end)) |
1623
|
1927 { |
|
1928 int l = try_tok->line (); |
|
1929 int c = try_tok->column (); |
|
1930 |
|
1931 retval = new tree_try_catch_command (body, cleanup, l, c); |
|
1932 } |
|
1933 |
|
1934 return retval; |
|
1935 } |
|
1936 |
|
1937 // Build a while command. |
|
1938 |
|
1939 static tree_command * |
|
1940 make_while_command (token *while_tok, tree_expression *expr, |
|
1941 tree_statement_list *body, token *end_tok) |
|
1942 { |
|
1943 tree_command *retval = 0; |
|
1944 |
|
1945 maybe_warn_assign_as_truth_value (expr); |
|
1946 |
2857
|
1947 if (end_token_ok (end_tok, token::while_end)) |
1623
|
1948 { |
1826
|
1949 lexer_flags.looping--; |
1623
|
1950 |
|
1951 int l = while_tok->line (); |
|
1952 int c = while_tok->column (); |
|
1953 |
|
1954 retval = new tree_while_command (expr, body, l, c); |
|
1955 } |
|
1956 |
|
1957 return retval; |
|
1958 } |
|
1959 |
|
1960 // Build a for command. |
|
1961 |
|
1962 static tree_command * |
2970
|
1963 make_for_command (token *for_tok, tree_argument_list *lhs, |
1623
|
1964 tree_expression *expr, tree_statement_list *body, |
|
1965 token *end_tok) |
|
1966 { |
|
1967 tree_command *retval = 0; |
|
1968 |
2857
|
1969 if (end_token_ok (end_tok, token::for_end)) |
1623
|
1970 { |
1826
|
1971 lexer_flags.looping--; |
1623
|
1972 |
|
1973 int l = for_tok->line (); |
|
1974 int c = for_tok->column (); |
|
1975 |
2970
|
1976 if (lhs->length () == 1) |
|
1977 { |
|
1978 tree_expression *tmp = lhs->remove_front (); |
|
1979 |
|
1980 retval = new tree_simple_for_command (tmp, expr, body, l, c); |
|
1981 |
|
1982 delete lhs; |
|
1983 } |
|
1984 else |
|
1985 retval = new tree_complex_for_command (lhs, expr, body, l, c); |
1623
|
1986 } |
|
1987 |
|
1988 return retval; |
|
1989 } |
|
1990 |
|
1991 // Build a break command. |
|
1992 |
|
1993 static tree_command * |
|
1994 make_break_command (token *break_tok) |
|
1995 { |
|
1996 tree_command *retval = 0; |
|
1997 |
2620
|
1998 int l = break_tok->line (); |
|
1999 int c = break_tok->column (); |
|
2000 |
|
2001 if (lexer_flags.looping || lexer_flags.defining_func || reading_script_file) |
|
2002 retval = new tree_break_command (l, c); |
1623
|
2003 else |
2620
|
2004 retval = new tree_no_op_command ("break", l, c); |
1623
|
2005 |
|
2006 return retval; |
|
2007 } |
|
2008 |
|
2009 // Build a continue command. |
|
2010 |
|
2011 static tree_command * |
|
2012 make_continue_command (token *continue_tok) |
|
2013 { |
|
2014 tree_command *retval = 0; |
|
2015 |
2620
|
2016 int l = continue_tok->line (); |
|
2017 int c = continue_tok->column (); |
|
2018 |
|
2019 if (lexer_flags.looping) |
|
2020 retval = new tree_continue_command (l, c); |
1623
|
2021 else |
2620
|
2022 retval = new tree_no_op_command ("continue", l, c); |
1623
|
2023 |
|
2024 return retval; |
|
2025 } |
|
2026 |
|
2027 // Build a return command. |
|
2028 |
|
2029 static tree_command * |
|
2030 make_return_command (token *return_tok) |
|
2031 { |
|
2032 tree_command *retval = 0; |
|
2033 |
2620
|
2034 int l = return_tok->line (); |
|
2035 int c = return_tok->column (); |
|
2036 |
|
2037 if (lexer_flags.defining_func || reading_script_file) |
|
2038 retval = new tree_return_command (l, c); |
1623
|
2039 else |
2620
|
2040 retval = new tree_no_op_command ("return", l, c); |
1623
|
2041 |
|
2042 return retval; |
|
2043 } |
|
2044 |
|
2045 // Start an if command. |
|
2046 |
|
2047 static tree_if_command_list * |
|
2048 start_if_command (tree_expression *expr, tree_statement_list *list) |
|
2049 { |
|
2050 maybe_warn_assign_as_truth_value (expr); |
|
2051 |
|
2052 tree_if_clause *t = new tree_if_clause (expr, list); |
|
2053 |
|
2054 return new tree_if_command_list (t); |
|
2055 } |
|
2056 |
|
2057 // Finish an if command. |
|
2058 |
|
2059 static tree_if_command * |
|
2060 finish_if_command (token *if_tok, tree_if_command_list *list, |
|
2061 token *end_tok) |
|
2062 { |
|
2063 tree_if_command *retval = 0; |
|
2064 |
2857
|
2065 if (end_token_ok (end_tok, token::if_end)) |
1623
|
2066 { |
|
2067 int l = if_tok->line (); |
|
2068 int c = if_tok->column (); |
|
2069 |
|
2070 retval = new tree_if_command (list, l, c); |
|
2071 } |
|
2072 |
|
2073 return retval; |
|
2074 } |
|
2075 |
|
2076 // Build an elseif clause. |
|
2077 |
|
2078 static tree_if_clause * |
|
2079 make_elseif_clause (tree_expression *expr, tree_statement_list *list) |
|
2080 { |
|
2081 maybe_warn_assign_as_truth_value (expr); |
|
2082 |
|
2083 return new tree_if_clause (expr, list); |
|
2084 } |
|
2085 |
2764
|
2086 // Finish a switch command. |
|
2087 |
|
2088 static tree_switch_command * |
|
2089 finish_switch_command (token *switch_tok, tree_expression *expr, |
|
2090 tree_switch_case_list *list, token *end_tok) |
|
2091 { |
|
2092 tree_switch_command *retval = 0; |
|
2093 |
2857
|
2094 if (end_token_ok (end_tok, token::switch_end)) |
2764
|
2095 { |
|
2096 int l = switch_tok->line (); |
|
2097 int c = switch_tok->column (); |
|
2098 |
|
2099 retval = new tree_switch_command (expr, list, l, c); |
|
2100 } |
|
2101 |
|
2102 return retval; |
|
2103 } |
|
2104 |
|
2105 // Build a switch case. |
|
2106 |
|
2107 static tree_switch_case * |
|
2108 make_switch_case (tree_expression *expr, tree_statement_list *list) |
|
2109 { |
|
2110 maybe_warn_variable_switch_label (expr); |
|
2111 |
|
2112 return new tree_switch_case (expr, list); |
|
2113 } |
|
2114 |
1623
|
2115 // Build an assignment to a variable. |
|
2116 |
|
2117 static tree_expression * |
2970
|
2118 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
2119 tree_expression *rhs) |
1623
|
2120 { |
2970
|
2121 tree_expression *retval = 0; |
|
2122 |
2883
|
2123 octave_value::assign_op t = octave_value::unknown_assign_op; |
|
2124 |
|
2125 switch (op) |
|
2126 { |
|
2127 case '=': |
|
2128 t = octave_value::asn_eq; |
|
2129 break; |
|
2130 |
|
2131 case ADD_EQ: |
|
2132 t = octave_value::add_eq; |
|
2133 break; |
|
2134 |
|
2135 case SUB_EQ: |
|
2136 t = octave_value::sub_eq; |
|
2137 break; |
|
2138 |
|
2139 case MUL_EQ: |
|
2140 t = octave_value::mul_eq; |
|
2141 break; |
|
2142 |
|
2143 case DIV_EQ: |
|
2144 t = octave_value::div_eq; |
|
2145 break; |
|
2146 |
2899
|
2147 case LSHIFT_EQ: |
|
2148 t = octave_value::lshift_eq; |
|
2149 break; |
|
2150 |
|
2151 case RSHIFT_EQ: |
|
2152 t = octave_value::rshift_eq; |
|
2153 break; |
|
2154 |
2883
|
2155 case EMUL_EQ: |
|
2156 t = octave_value::el_mul_eq; |
|
2157 break; |
|
2158 |
|
2159 case EDIV_EQ: |
|
2160 t = octave_value::el_div_eq; |
|
2161 break; |
|
2162 |
|
2163 case AND_EQ: |
|
2164 t = octave_value::el_and_eq; |
|
2165 break; |
|
2166 |
|
2167 case OR_EQ: |
|
2168 t = octave_value::el_or_eq; |
|
2169 break; |
|
2170 |
|
2171 default: |
|
2172 panic_impossible (); |
|
2173 break; |
|
2174 } |
|
2175 |
1623
|
2176 int l = eq_tok->line (); |
|
2177 int c = eq_tok->column (); |
|
2178 |
2970
|
2179 if (lhs->length () == 1) |
666
|
2180 { |
2970
|
2181 tree_expression *tmp = lhs->remove_front (); |
|
2182 |
|
2183 retval = new tree_simple_assignment (tmp, rhs, false, l, c, t); |
|
2184 |
|
2185 delete lhs; |
666
|
2186 } |
|
2187 else |
2970
|
2188 return new tree_multi_assignment (lhs, rhs, 0, l, c); |
666
|
2189 |
|
2190 return retval; |
|
2191 } |
751
|
2192 |
1623
|
2193 // Begin defining a function. |
|
2194 |
2891
|
2195 static octave_user_function * |
|
2196 start_function (tree_parameter_list *param_list, tree_statement_list *body) |
1623
|
2197 { |
|
2198 body->mark_as_function_body (); |
|
2199 |
2891
|
2200 // We'll fill in the return list later. |
|
2201 |
|
2202 octave_user_function *fcn |
|
2203 = new octave_user_function (param_list, 0, body, curr_sym_tab); |
1623
|
2204 |
|
2205 return fcn; |
|
2206 } |
|
2207 |
|
2208 // Do most of the work for defining a function. |
|
2209 |
2891
|
2210 static octave_user_function * |
|
2211 frob_function (tree_identifier *id, octave_user_function *fcn) |
1623
|
2212 { |
1755
|
2213 string id_name = id->name (); |
1623
|
2214 |
|
2215 // If input is coming from a file, issue a warning if the name of |
|
2216 // the file does not match the name of the function stated in the |
|
2217 // file. Matlab doesn't provide a diagnostic (it ignores the stated |
|
2218 // name). |
|
2219 |
|
2220 fcn->stash_function_name (id_name); |
|
2221 |
|
2222 if (reading_fcn_file) |
|
2223 { |
1755
|
2224 if (curr_fcn_file_name != id_name) |
1623
|
2225 { |
2166
|
2226 if (Vwarn_function_name_clash) |
1623
|
2227 warning ("function name `%s' does not agree with function\ |
1755
|
2228 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2229 |
|
2230 global_sym_tab->rename (id_name, curr_fcn_file_name); |
|
2231 |
|
2232 if (error_state) |
|
2233 return 0; |
|
2234 |
|
2235 id_name = id->name (); |
|
2236 } |
|
2237 |
3162
|
2238 time_t now = time (0); |
|
2239 |
1623
|
2240 fcn->stash_function_name (id_name); |
|
2241 fcn->stash_fcn_file_name (); |
3162
|
2242 fcn->stash_fcn_file_time (now); |
1623
|
2243 fcn->mark_as_system_fcn_file (); |
3162
|
2244 |
|
2245 if (Vwarn_future_time_stamp) |
|
2246 { |
|
2247 string nm = fcn->fcn_file_name (); |
|
2248 |
|
2249 file_stat fs (nm); |
|
2250 |
|
2251 if (fs && fs.is_newer (now)) |
|
2252 warning ("time stamp for `%s' is in the future", nm.c_str ()); |
|
2253 } |
1623
|
2254 } |
|
2255 else if (! (input_from_tmp_history_file || input_from_startup_file) |
|
2256 && reading_script_file |
1755
|
2257 && curr_fcn_file_name == id_name) |
1623
|
2258 { |
|
2259 warning ("function `%s' defined within script file `%s'", |
1755
|
2260 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2261 } |
|
2262 |
|
2263 top_level_sym_tab->clear (id_name); |
|
2264 |
2856
|
2265 symbol_record *sr = global_sym_tab->lookup (id_name); |
2791
|
2266 |
|
2267 if (sr) |
|
2268 fcn->stash_symtab_ptr (sr); |
|
2269 else |
|
2270 panic_impossible (); |
|
2271 |
3016
|
2272 id->define (fcn, symbol_record::USER_FUNCTION); |
1755
|
2273 |
1623
|
2274 id->document (help_buf); |
|
2275 |
|
2276 return fcn; |
|
2277 } |
|
2278 |
|
2279 // Finish defining a function. |
|
2280 |
2891
|
2281 static octave_user_function * |
|
2282 finish_function (tree_identifier *id, octave_user_function *fcn) |
1623
|
2283 { |
2883
|
2284 tree_parameter_list *tpl = new tree_parameter_list (id); |
1623
|
2285 |
|
2286 tpl->mark_as_formal_parameters (); |
|
2287 |
|
2288 return fcn->define_ret_list (tpl); |
|
2289 } |
|
2290 |
|
2291 // Finish defining a function a different way. |
|
2292 |
2891
|
2293 static octave_user_function * |
|
2294 finish_function (tree_parameter_list *ret_list, octave_user_function *fcn) |
1623
|
2295 { |
|
2296 ret_list->mark_as_formal_parameters (); |
|
2297 |
|
2298 return fcn->define_ret_list (ret_list); |
|
2299 } |
|
2300 |
2883
|
2301 static void |
|
2302 recover_from_parsing_function (void) |
|
2303 { |
|
2304 curr_sym_tab = top_level_sym_tab; |
|
2305 |
|
2306 lexer_flags.defining_func = false; |
|
2307 lexer_flags.beginning_of_function = false; |
|
2308 lexer_flags.parsed_function_name = false; |
|
2309 lexer_flags.looking_at_return_list = false; |
|
2310 lexer_flags.looking_at_parameter_list = false; |
|
2311 } |
|
2312 |
2846
|
2313 // Make an index expression. |
|
2314 |
751
|
2315 static tree_index_expression * |
2970
|
2316 make_index_expression (tree_expression *expr, tree_argument_list *args) |
751
|
2317 { |
|
2318 tree_index_expression *retval = 0; |
|
2319 |
2970
|
2320 int l = expr->line (); |
|
2321 int c = expr->column (); |
|
2322 |
|
2323 expr->mark_postfix_indexed (); |
|
2324 |
|
2325 retval = new tree_index_expression (expr, args, l, c); |
|
2326 |
|
2327 return retval; |
|
2328 } |
|
2329 |
|
2330 // Make an indirect reference expression. |
|
2331 |
|
2332 static tree_indirect_ref * |
|
2333 make_indirect_ref (tree_expression *expr, const string& elt) |
|
2334 { |
|
2335 tree_indirect_ref *retval = 0; |
|
2336 |
|
2337 int l = expr->line (); |
|
2338 int c = expr->column (); |
|
2339 |
|
2340 retval = new tree_indirect_ref (expr, elt, l, c); |
|
2341 |
|
2342 lexer_flags.looking_at_indirect_ref = false; |
751
|
2343 |
|
2344 return retval; |
|
2345 } |
1511
|
2346 |
2846
|
2347 // Make a declaration command. |
|
2348 |
|
2349 static tree_decl_command * |
|
2350 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) |
|
2351 { |
|
2352 tree_decl_command *retval = 0; |
|
2353 |
|
2354 int l = tok_val->line (); |
|
2355 int c = tok_val->column (); |
|
2356 |
|
2357 switch (tok) |
|
2358 { |
|
2359 case GLOBAL: |
|
2360 retval = new tree_global_command (lst, l, c); |
|
2361 break; |
|
2362 |
|
2363 case STATIC: |
|
2364 if (lexer_flags.defining_func) |
|
2365 retval = new tree_static_command (lst, l, c); |
|
2366 else |
|
2367 { |
|
2368 if (reading_script_file) |
|
2369 warning ("ignoring static declaration near line %d of file `%s'", |
|
2370 l, curr_fcn_file_full_name.c_str ()); |
|
2371 else |
|
2372 warning ("ignoring static declaration near line %d", l); |
|
2373 } |
|
2374 break; |
|
2375 |
|
2376 default: |
|
2377 panic_impossible (); |
|
2378 break; |
|
2379 } |
|
2380 |
|
2381 return retval; |
|
2382 } |
|
2383 |
1623
|
2384 // Finish building a matrix list. |
|
2385 |
|
2386 static tree_expression * |
1829
|
2387 finish_matrix (tree_matrix *m) |
1623
|
2388 { |
3110
|
2389 tree_expression *retval = m; |
|
2390 |
|
2391 unwind_protect::begin_frame ("finish_matrix"); |
|
2392 |
|
2393 unwind_protect_int (error_state); |
|
2394 |
|
2395 unwind_protect_bool (buffer_error_messages); |
|
2396 buffer_error_messages = true; |
|
2397 |
|
2398 unwind_protect::add (clear_global_error_variable, 0); |
1623
|
2399 |
2533
|
2400 if (m->all_elements_are_constant ()) |
1829
|
2401 { |
2970
|
2402 octave_value tmp = m->rvalue (); |
1623
|
2403 |
2533
|
2404 if (! error_state) |
|
2405 { |
|
2406 tree_constant *tc_retval = new tree_constant (tmp); |
|
2407 |
|
2408 ostrstream buf; |
|
2409 |
|
2410 tree_print_code tpc (buf); |
|
2411 |
|
2412 m->accept (tpc); |
|
2413 |
|
2414 buf << ends; |
1623
|
2415 |
2533
|
2416 char *s = buf.str (); |
|
2417 |
|
2418 tc_retval->stash_original_text (s); |
|
2419 |
|
2420 delete [] s; |
|
2421 |
|
2422 delete m; |
|
2423 |
|
2424 retval = tc_retval; |
|
2425 } |
1623
|
2426 } |
3110
|
2427 |
|
2428 unwind_protect::run_frame ("finish_matrix"); |
1623
|
2429 |
|
2430 return retval; |
|
2431 } |
|
2432 |
1511
|
2433 static void |
|
2434 maybe_warn_missing_semi (tree_statement_list *t) |
|
2435 { |
2166
|
2436 if (lexer_flags.defining_func && Vwarn_missing_semicolon) |
1511
|
2437 { |
|
2438 tree_statement *tmp = t->rear(); |
1607
|
2439 |
1511
|
2440 if (tmp->is_expression ()) |
1607
|
2441 warning ("missing semicolon near line %d, column %d in file `%s'", |
1755
|
2442 tmp->line (), tmp->column (), |
|
2443 curr_fcn_file_full_name.c_str ()); |
1511
|
2444 } |
|
2445 } |
1994
|
2446 |
2525
|
2447 static void |
|
2448 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2449 bool warn_missing_semi) |
|
2450 { |
|
2451 switch (sep) |
|
2452 { |
|
2453 case ';': |
|
2454 { |
|
2455 tree_statement *tmp = list->rear (); |
|
2456 tmp->set_print_flag (0); |
|
2457 } |
|
2458 break; |
|
2459 |
|
2460 case 0: |
|
2461 case ',': |
|
2462 case '\n': |
|
2463 if (warn_missing_semi) |
|
2464 maybe_warn_missing_semi (list); |
|
2465 break; |
|
2466 |
|
2467 default: |
|
2468 warning ("unrecognized separator type!"); |
|
2469 break; |
|
2470 } |
|
2471 } |
|
2472 |
3021
|
2473 void |
|
2474 parse_and_execute (FILE *f) |
|
2475 { |
|
2476 unwind_protect::begin_frame ("parse_and_execute"); |
|
2477 |
|
2478 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2479 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
2480 |
|
2481 unwind_protect::add (restore_input_buffer, old_buf); |
|
2482 unwind_protect::add (delete_input_buffer, new_buf); |
|
2483 |
|
2484 switch_to_buffer (new_buf); |
|
2485 |
|
2486 unwind_protect_bool (line_editing); |
|
2487 unwind_protect_bool (input_from_command_line_file); |
|
2488 |
|
2489 line_editing = false; |
|
2490 input_from_command_line_file = false; |
|
2491 |
|
2492 unwind_protect_ptr (curr_sym_tab); |
|
2493 |
|
2494 int retval; |
|
2495 do |
|
2496 { |
|
2497 reset_parser (); |
|
2498 |
|
2499 retval = yyparse (); |
|
2500 |
|
2501 if (retval == 0 && global_command) |
|
2502 { |
|
2503 global_command->eval (); |
|
2504 |
|
2505 delete global_command; |
|
2506 |
|
2507 global_command = 0; |
|
2508 |
|
2509 bool quit = (tree_return_command::returning |
|
2510 || tree_break_command::breaking); |
|
2511 |
|
2512 if (tree_return_command::returning) |
|
2513 tree_return_command::returning = 0; |
|
2514 |
|
2515 if (tree_break_command::breaking) |
|
2516 tree_break_command::breaking--; |
|
2517 |
|
2518 if (error_state) |
|
2519 { |
|
2520 error ("near line %d of file `%s'", input_line_number, |
|
2521 curr_fcn_file_full_name.c_str ()); |
|
2522 |
|
2523 break; |
|
2524 } |
|
2525 |
|
2526 if (quit) |
|
2527 break; |
|
2528 } |
|
2529 } |
|
2530 while (retval == 0); |
|
2531 |
|
2532 unwind_protect::run_frame ("parse_and_execute"); |
|
2533 } |
|
2534 |
|
2535 static void |
|
2536 safe_fclose (void *f) |
|
2537 { |
|
2538 if (f) |
|
2539 fclose (static_cast<FILE *> (f)); |
|
2540 } |
|
2541 |
|
2542 void |
|
2543 parse_and_execute (const string& s, bool verbose, const char *warn_for) |
|
2544 { |
|
2545 unwind_protect::begin_frame ("parse_and_execute_2"); |
|
2546 |
|
2547 unwind_protect_bool (reading_script_file); |
|
2548 unwind_protect_str (curr_fcn_file_full_name); |
|
2549 |
|
2550 reading_script_file = true; |
|
2551 curr_fcn_file_full_name = s; |
|
2552 |
|
2553 FILE *f = get_input_from_file (s, 0); |
|
2554 |
|
2555 if (f) |
|
2556 { |
|
2557 unwind_protect::add (safe_fclose, f); |
|
2558 |
|
2559 unwind_protect_int (input_line_number); |
|
2560 unwind_protect_int (current_input_column); |
|
2561 |
|
2562 input_line_number = 0; |
|
2563 current_input_column = 1; |
|
2564 |
|
2565 if (verbose) |
|
2566 { |
|
2567 cout << "reading commands from " << s << " ... "; |
|
2568 reading_startup_message_printed = true; |
|
2569 cout.flush (); |
|
2570 } |
|
2571 |
|
2572 parse_and_execute (f); |
|
2573 |
|
2574 if (verbose) |
|
2575 cout << "done." << endl; |
|
2576 } |
|
2577 else if (warn_for) |
|
2578 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
|
2579 |
|
2580 unwind_protect::run_frame ("parse_and_execute_2"); |
|
2581 } |
|
2582 |
|
2583 static bool |
|
2584 looks_like_octave_copyright (const string& s) |
|
2585 { |
|
2586 bool retval = false; |
|
2587 |
|
2588 string t = s.substr (0, 15); |
|
2589 |
|
2590 if (t == " Copyright (C) ") |
|
2591 { |
|
2592 size_t pos = s.find ('\n'); |
|
2593 |
|
2594 if (pos != NPOS) |
|
2595 { |
|
2596 pos = s.find ('\n', pos + 1); |
|
2597 |
|
2598 if (pos != NPOS) |
|
2599 { |
|
2600 pos++; |
|
2601 |
|
2602 t = s.substr (pos, 29); |
|
2603 |
|
2604 if (t == " This file is part of Octave." |
|
2605 || t == " This program is free softwar") |
|
2606 retval = true; |
|
2607 } |
|
2608 } |
|
2609 } |
|
2610 |
|
2611 return retval; |
|
2612 } |
|
2613 |
|
2614 // Eat whitespace and comments from FFILE, returning the text of the |
|
2615 // comments read if it doesn't look like a copyright notice. If |
|
2616 // IN_PARTS, consider each block of comments separately; otherwise, |
|
2617 // grab them all at once. If UPDATE_POS is TRUE, line and column |
|
2618 // number information is updated. |
|
2619 |
|
2620 // XXX FIXME XXX -- grab_help_text() in lex.l duplicates some of this |
|
2621 // code! |
|
2622 |
|
2623 static string |
|
2624 gobble_leading_white_space (FILE *ffile, bool in_parts, bool update_pos) |
|
2625 { |
|
2626 string help_txt; |
|
2627 |
|
2628 bool first_comments_seen = false; |
|
2629 bool begin_comment = false; |
|
2630 bool have_help_text = false; |
|
2631 bool in_comment = false; |
|
2632 int c; |
|
2633 |
|
2634 while ((c = getc (ffile)) != EOF) |
|
2635 { |
|
2636 if (update_pos) |
|
2637 current_input_column++; |
|
2638 |
|
2639 if (begin_comment) |
|
2640 { |
|
2641 if (c == '%' || c == '#') |
|
2642 continue; |
|
2643 else |
|
2644 begin_comment = false; |
|
2645 } |
|
2646 |
|
2647 if (in_comment) |
|
2648 { |
|
2649 if (! have_help_text) |
|
2650 { |
|
2651 first_comments_seen = true; |
|
2652 help_txt += (char) c; |
|
2653 } |
|
2654 |
|
2655 if (c == '\n') |
|
2656 { |
|
2657 if (update_pos) |
|
2658 { |
|
2659 input_line_number++; |
|
2660 current_input_column = 0; |
|
2661 } |
|
2662 in_comment = false; |
|
2663 |
|
2664 if (in_parts) |
|
2665 { |
|
2666 if ((c = getc (ffile)) != EOF) |
|
2667 { |
|
2668 if (update_pos) |
|
2669 current_input_column--; |
|
2670 ungetc (c, ffile); |
|
2671 if (c == '\n') |
|
2672 break; |
|
2673 } |
|
2674 else |
|
2675 break; |
|
2676 } |
|
2677 } |
|
2678 } |
|
2679 else |
|
2680 { |
|
2681 switch (c) |
|
2682 { |
|
2683 case ' ': |
|
2684 case '\t': |
|
2685 if (first_comments_seen) |
|
2686 have_help_text = true; |
|
2687 break; |
|
2688 |
|
2689 case '\n': |
|
2690 if (first_comments_seen) |
|
2691 have_help_text = true; |
|
2692 if (update_pos) |
|
2693 { |
|
2694 input_line_number++; |
|
2695 current_input_column = 0; |
|
2696 } |
|
2697 continue; |
|
2698 |
|
2699 case '%': |
|
2700 case '#': |
|
2701 begin_comment = true; |
|
2702 in_comment = true; |
|
2703 break; |
|
2704 |
|
2705 default: |
|
2706 if (update_pos) |
|
2707 current_input_column--; |
|
2708 ungetc (c, ffile); |
|
2709 goto done; |
|
2710 } |
|
2711 } |
|
2712 } |
|
2713 |
|
2714 done: |
|
2715 |
|
2716 if (! help_txt.empty ()) |
|
2717 { |
|
2718 if (looks_like_octave_copyright (help_txt)) |
|
2719 help_txt.resize (0); |
|
2720 |
|
2721 if (in_parts && help_txt.empty ()) |
|
2722 help_txt = gobble_leading_white_space (ffile, in_parts, update_pos); |
|
2723 } |
|
2724 |
|
2725 return help_txt; |
|
2726 } |
|
2727 |
|
2728 string |
|
2729 get_help_from_file (const string& path) |
|
2730 { |
|
2731 string retval; |
|
2732 |
|
2733 if (! path.empty ()) |
|
2734 { |
|
2735 FILE *fptr = fopen (path.c_str (), "r"); |
|
2736 |
|
2737 if (fptr) |
|
2738 { |
|
2739 unwind_protect::add (safe_fclose, (void *) fptr); |
|
2740 |
|
2741 retval = gobble_leading_white_space (fptr, true, true); |
|
2742 |
|
2743 unwind_protect::run (); |
|
2744 } |
|
2745 } |
|
2746 |
|
2747 return retval; |
|
2748 } |
|
2749 |
|
2750 static int |
|
2751 is_function_file (FILE *ffile) |
|
2752 { |
|
2753 int status = 0; |
|
2754 |
|
2755 long pos = ftell (ffile); |
|
2756 |
|
2757 gobble_leading_white_space (ffile, false, false); |
|
2758 |
|
2759 char buf [10]; |
|
2760 fgets (buf, 10, ffile); |
|
2761 int len = strlen (buf); |
|
2762 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
2763 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
2764 status = 1; |
|
2765 |
|
2766 fseek (ffile, pos, SEEK_SET); |
|
2767 |
|
2768 return status; |
|
2769 } |
|
2770 |
|
2771 static void |
|
2772 restore_command_history (void *) |
|
2773 { |
|
2774 command_history::ignore_entries (! Vsaving_history); |
|
2775 } |
|
2776 |
|
2777 static void |
|
2778 restore_input_stream (void *f) |
|
2779 { |
|
2780 command_editor::set_input_stream (static_cast<FILE *> (f)); |
|
2781 } |
|
2782 |
|
2783 static bool |
3165
|
2784 parse_fcn_file (const string& ff, bool exec_script, bool force_script = false) |
3021
|
2785 { |
|
2786 unwind_protect::begin_frame ("parse_fcn_file"); |
|
2787 |
|
2788 int script_file_executed = false; |
|
2789 |
|
2790 // Open function file and parse. |
|
2791 |
|
2792 bool old_reading_fcn_file_state = reading_fcn_file; |
|
2793 |
|
2794 FILE *in_stream = command_editor::get_input_stream (); |
|
2795 |
|
2796 unwind_protect::add (restore_input_stream, in_stream); |
|
2797 |
|
2798 unwind_protect_ptr (ff_instream); |
|
2799 |
|
2800 unwind_protect_int (input_line_number); |
|
2801 unwind_protect_int (current_input_column); |
|
2802 unwind_protect_bool (reading_fcn_file); |
|
2803 unwind_protect_bool (line_editing); |
|
2804 |
|
2805 input_line_number = 0; |
|
2806 current_input_column = 1; |
|
2807 reading_fcn_file = true; |
|
2808 line_editing = false; |
|
2809 |
|
2810 FILE *ffile = get_input_from_file (ff, 0); |
|
2811 |
|
2812 unwind_protect::add (safe_fclose, ffile); |
|
2813 |
|
2814 if (ffile) |
|
2815 { |
|
2816 // Check to see if this file defines a function or is just a |
|
2817 // list of commands. |
|
2818 |
3165
|
2819 if (! force_script && is_function_file (ffile)) |
3021
|
2820 { |
|
2821 // XXX FIXME XXX -- we shouldn't need both the |
|
2822 // command_history object and the |
|
2823 // Vsaving_history variable... |
|
2824 command_history::ignore_entries (); |
|
2825 |
|
2826 unwind_protect::add (restore_command_history, 0); |
|
2827 |
|
2828 unwind_protect_int (Vecho_executing_commands); |
|
2829 unwind_protect_bool (Vsaving_history); |
|
2830 unwind_protect_bool (reading_fcn_file); |
|
2831 unwind_protect_bool (input_from_command_line_file); |
|
2832 |
|
2833 Vecho_executing_commands = ECHO_OFF; |
|
2834 Vsaving_history = false; |
|
2835 reading_fcn_file = true; |
|
2836 input_from_command_line_file = false; |
|
2837 |
|
2838 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2839 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
2840 |
|
2841 unwind_protect::add (restore_input_buffer, (void *) old_buf); |
|
2842 unwind_protect::add (delete_input_buffer, (void *) new_buf); |
|
2843 |
|
2844 switch_to_buffer (new_buf); |
|
2845 |
|
2846 unwind_protect_ptr (curr_sym_tab); |
|
2847 |
|
2848 reset_parser (); |
|
2849 |
|
2850 help_buf = gobble_leading_white_space (ffile, true, true); |
|
2851 |
|
2852 // XXX FIXME XXX -- this should not be necessary. |
|
2853 gobble_leading_white_space (ffile, false, true); |
|
2854 |
|
2855 int status = yyparse (); |
|
2856 |
|
2857 if (status != 0) |
|
2858 { |
|
2859 error ("parse error while reading function file %s", |
|
2860 ff.c_str ()); |
|
2861 global_sym_tab->clear (curr_fcn_file_name); |
|
2862 } |
|
2863 } |
|
2864 else if (exec_script) |
|
2865 { |
|
2866 // The value of `reading_fcn_file' will be restored to the |
|
2867 // proper value when we unwind from this frame. |
|
2868 reading_fcn_file = old_reading_fcn_file_state; |
|
2869 |
|
2870 // XXX FIXME XXX -- we shouldn't need both the |
|
2871 // command_history object and the |
|
2872 // Vsaving_history variable... |
|
2873 command_history::ignore_entries (); |
|
2874 |
|
2875 unwind_protect::add (restore_command_history, 0); |
|
2876 |
|
2877 unwind_protect_bool (Vsaving_history); |
|
2878 unwind_protect_bool (reading_script_file); |
|
2879 |
|
2880 Vsaving_history = false; |
|
2881 reading_script_file = true; |
|
2882 |
|
2883 parse_and_execute (ffile); |
|
2884 |
|
2885 script_file_executed = true; |
|
2886 } |
|
2887 } |
|
2888 |
|
2889 unwind_protect::run_frame ("parse_fcn_file"); |
|
2890 |
|
2891 return script_file_executed; |
|
2892 } |
|
2893 |
|
2894 bool |
|
2895 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
|
2896 { |
|
2897 bool script_file_executed = false; |
|
2898 |
|
2899 string nm = sym_rec->name (); |
|
2900 |
|
2901 if (octave_dynamic_loader::load_fcn_from_dot_oct_file (nm)) |
|
2902 { |
|
2903 force_link_to_function (nm); |
|
2904 } |
|
2905 else |
|
2906 { |
|
2907 string ff = fcn_file_in_path (nm); |
|
2908 |
|
2909 // These are needed by yyparse. |
|
2910 |
|
2911 unwind_protect::begin_frame ("load_fcn_from_file"); |
|
2912 |
|
2913 unwind_protect_str (curr_fcn_file_name); |
|
2914 unwind_protect_str (curr_fcn_file_full_name); |
|
2915 |
|
2916 curr_fcn_file_name = nm; |
|
2917 curr_fcn_file_full_name = ff; |
|
2918 |
|
2919 if (ff.length () > 0) |
3165
|
2920 script_file_executed = parse_fcn_file (ff, exec_script); |
3021
|
2921 |
|
2922 if (! (error_state || script_file_executed)) |
|
2923 force_link_to_function (nm); |
|
2924 |
|
2925 unwind_protect::run_frame ("load_fcn_from_file"); |
|
2926 } |
|
2927 |
|
2928 return script_file_executed; |
|
2929 } |
|
2930 |
|
2931 DEFUN (source, args, , |
|
2932 "source (FILE)\n\ |
|
2933 \n\ |
|
2934 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
2935 script file but without requiring the file to be named `FILE.m'.") |
|
2936 { |
|
2937 octave_value_list retval; |
|
2938 |
|
2939 int nargin = args.length (); |
|
2940 |
|
2941 if (nargin == 1) |
|
2942 { |
|
2943 string file = args(0).string_value (); |
|
2944 |
|
2945 if (! error_state) |
|
2946 { |
|
2947 file = file_ops::tilde_expand (file); |
|
2948 |
3165
|
2949 parse_fcn_file (file, true, true); |
3021
|
2950 |
|
2951 if (error_state) |
|
2952 error ("source: error sourcing file `%s'", file.c_str ()); |
|
2953 } |
|
2954 else |
|
2955 error ("source: expecting file name as argument"); |
|
2956 } |
|
2957 else |
|
2958 print_usage ("source"); |
|
2959 |
|
2960 return retval; |
|
2961 } |
|
2962 |
|
2963 octave_value_list |
3156
|
2964 feval (const string& name, const octave_value_list& args, int nargout) |
|
2965 { |
|
2966 octave_value_list retval; |
|
2967 |
|
2968 octave_function *fcn = is_valid_function (name, "feval", 1); |
|
2969 |
|
2970 if (fcn) |
|
2971 retval = fcn->do_index_op (nargout, args); |
|
2972 |
|
2973 return retval; |
|
2974 } |
|
2975 |
|
2976 octave_value_list |
3021
|
2977 feval (const octave_value_list& args, int nargout) |
|
2978 { |
|
2979 octave_value_list retval; |
|
2980 |
3156
|
2981 if (args.length () > 0) |
3021
|
2982 { |
3156
|
2983 string name = args(0).string_value (); |
|
2984 |
|
2985 if (! error_state) |
3021
|
2986 { |
3156
|
2987 string_vector arg_names = args.name_tags (); |
|
2988 |
|
2989 int tmp_nargin = args.length () - 1; |
|
2990 |
|
2991 octave_value_list tmp_args (tmp_nargin, octave_value ()); |
|
2992 |
|
2993 string_vector tmp_arg_names (tmp_nargin); |
|
2994 |
|
2995 for (int i = 0; i < tmp_nargin; i++) |
|
2996 { |
|
2997 tmp_args(i) = args(i+1); |
|
2998 tmp_arg_names(i) = arg_names(i+1); |
|
2999 } |
|
3000 |
|
3001 tmp_args.stash_name_tags (tmp_arg_names); |
|
3002 |
|
3003 retval = feval (name, tmp_args, nargout); |
3021
|
3004 } |
|
3005 } |
|
3006 |
|
3007 return retval; |
|
3008 } |
|
3009 |
|
3010 DEFUN (feval, args, nargout, |
|
3011 "feval (NAME, ARGS, ...)\n\ |
|
3012 \n\ |
|
3013 evaluate NAME as a function, passing ARGS as its arguments") |
|
3014 { |
|
3015 octave_value_list retval; |
|
3016 |
|
3017 int nargin = args.length (); |
|
3018 |
|
3019 if (nargin > 0) |
|
3020 retval = feval (args, nargout); |
|
3021 else |
|
3022 print_usage ("feval"); |
|
3023 |
|
3024 return retval; |
|
3025 } |
|
3026 |
3099
|
3027 octave_value_list |
3021
|
3028 eval_string (const string& s, bool silent, int& parse_status, int nargout) |
|
3029 { |
|
3030 unwind_protect::begin_frame ("eval_string"); |
|
3031 |
|
3032 unwind_protect_bool (get_input_from_eval_string); |
|
3033 unwind_protect_bool (input_from_command_line_file); |
|
3034 unwind_protect_ptr (global_command); |
|
3035 unwind_protect_str (current_eval_string); |
|
3036 |
|
3037 get_input_from_eval_string = true; |
|
3038 input_from_command_line_file = false; |
|
3039 current_eval_string = s; |
|
3040 |
|
3041 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3042 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
3043 |
|
3044 unwind_protect::add (restore_input_buffer, old_buf); |
|
3045 unwind_protect::add (delete_input_buffer, new_buf); |
|
3046 |
|
3047 switch_to_buffer (new_buf); |
|
3048 |
|
3049 unwind_protect_ptr (curr_sym_tab); |
|
3050 |
|
3051 reset_parser (); |
|
3052 |
|
3053 parse_status = yyparse (); |
|
3054 |
|
3055 // Important to reset the idea of where input is coming from before |
|
3056 // trying to eval the command we just parsed -- it might contain the |
|
3057 // name of an function file that still needs to be parsed! |
|
3058 |
|
3059 tree_statement_list *command = global_command; |
|
3060 |
|
3061 unwind_protect::run_frame ("eval_string"); |
|
3062 |
|
3063 octave_value_list retval; |
|
3064 |
|
3065 if (parse_status == 0 && command) |
|
3066 { |
|
3067 retval = command->eval (silent, nargout); |
|
3068 delete command; |
|
3069 } |
|
3070 |
|
3071 return retval; |
|
3072 } |
|
3073 |
|
3074 octave_value |
|
3075 eval_string (const string& s, bool silent, int& parse_status) |
|
3076 { |
|
3077 octave_value retval; |
|
3078 |
|
3079 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
|
3080 |
|
3081 if (! tmp.empty ()) |
|
3082 retval = tmp(0); |
|
3083 |
|
3084 return retval; |
|
3085 } |
|
3086 |
|
3087 static octave_value_list |
|
3088 eval_string (const octave_value& arg, bool silent, int& parse_status, |
|
3089 int nargout) |
|
3090 { |
|
3091 string s = arg.string_value (); |
|
3092 |
|
3093 if (error_state) |
|
3094 { |
|
3095 error ("eval: expecting string argument"); |
|
3096 return -1.0; |
|
3097 } |
|
3098 |
|
3099 return eval_string (s, silent, parse_status, nargout); |
|
3100 } |
|
3101 |
|
3102 DEFUN (eval, args, nargout, |
|
3103 "eval (TRY, CATCH)\n\ |
|
3104 \n\ |
|
3105 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
3106 string CATCH.") |
|
3107 { |
|
3108 octave_value_list retval; |
|
3109 |
|
3110 int nargin = args.length (); |
|
3111 |
|
3112 if (nargin > 0) |
|
3113 { |
|
3114 unwind_protect::begin_frame ("Feval"); |
|
3115 |
|
3116 if (nargin > 1) |
|
3117 { |
|
3118 unwind_protect_bool (buffer_error_messages); |
|
3119 buffer_error_messages = true; |
|
3120 } |
|
3121 |
|
3122 int parse_status = 0; |
|
3123 |
|
3124 retval = eval_string (args(0), ! Vdefault_eval_print_flag, |
|
3125 parse_status, nargout); |
|
3126 |
|
3127 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
3128 { |
|
3129 error_state = 0; |
|
3130 |
|
3131 // Set up for letting the user print any messages from |
|
3132 // errors that occurred in the first part of this eval(). |
|
3133 |
|
3134 buffer_error_messages = false; |
|
3135 bind_global_error_variable (); |
|
3136 unwind_protect::add (clear_global_error_variable, 0); |
|
3137 |
|
3138 eval_string (args(1), 0, parse_status, nargout); |
|
3139 |
|
3140 retval = octave_value_list (); |
|
3141 } |
|
3142 |
|
3143 unwind_protect::run_frame ("Feval"); |
|
3144 } |
|
3145 else |
|
3146 print_usage ("eval"); |
|
3147 |
|
3148 return retval; |
|
3149 } |
|
3150 |
|
3151 static int |
|
3152 default_eval_print_flag (void) |
|
3153 { |
|
3154 Vdefault_eval_print_flag = check_preference ("default_eval_print_flag"); |
|
3155 |
|
3156 return 0; |
|
3157 } |
|
3158 |
2166
|
3159 static int |
|
3160 warn_assign_as_truth_value (void) |
|
3161 { |
|
3162 Vwarn_assign_as_truth_value |
|
3163 = check_preference ("warn_assign_as_truth_value"); |
|
3164 |
|
3165 return 0; |
|
3166 } |
|
3167 |
|
3168 static int |
|
3169 warn_function_name_clash (void) |
|
3170 { |
|
3171 Vwarn_function_name_clash = check_preference ("warn_function_name_clash"); |
|
3172 |
|
3173 return 0; |
|
3174 } |
|
3175 |
|
3176 static int |
3162
|
3177 warn_future_time_stamp (void) |
|
3178 { |
|
3179 Vwarn_future_time_stamp = check_preference ("warn_future_time_stamp"); |
|
3180 |
|
3181 return 0; |
|
3182 } |
|
3183 |
|
3184 static int |
2166
|
3185 warn_missing_semicolon (void) |
|
3186 { |
|
3187 Vwarn_missing_semicolon = check_preference ("warn_missing_semicolon"); |
|
3188 |
|
3189 return 0; |
|
3190 } |
|
3191 |
2764
|
3192 static int |
|
3193 warn_variable_switch_label (void) |
|
3194 { |
|
3195 Vwarn_variable_switch_label |
|
3196 = check_preference ("warn_variable_switch_label"); |
|
3197 |
|
3198 return 0; |
|
3199 } |
|
3200 |
2166
|
3201 void |
|
3202 symbols_of_parse (void) |
|
3203 { |
3021
|
3204 DEFVAR (default_eval_print_flag, 1.0, 0, default_eval_print_flag, |
|
3205 "If the value of this variable is nonzero, Octave will print the\n\ |
|
3206 results of commands executed by eval() that do not end with semicolons."); |
|
3207 |
2166
|
3208 DEFVAR (warn_assign_as_truth_value, 1.0, 0, warn_assign_as_truth_value, |
|
3209 "produce warning for assignments used as truth values"); |
|
3210 |
|
3211 DEFVAR (warn_function_name_clash, 1.0, 0, warn_function_name_clash, |
|
3212 "produce warning if function name conflicts with file name"); |
|
3213 |
3162
|
3214 DEFVAR (warn_future_time_stamp, 1.0, 0, warn_future_time_stamp, |
|
3215 "warn if a function file has a time stamp that is in the future"); |
|
3216 |
2166
|
3217 DEFVAR (warn_missing_semicolon, 0.0, 0, warn_missing_semicolon, |
2764
|
3218 "produce a warning if a statement in a function file is not\n\ |
2166
|
3219 terminated with a semicolon"); |
2764
|
3220 |
|
3221 DEFVAR (warn_variable_switch_label, 0.0, 0, warn_variable_switch_label, |
|
3222 "produce warning for variables used as switch labels"); |
2166
|
3223 } |
|
3224 |
1994
|
3225 /* |
|
3226 ;;; Local Variables: *** |
|
3227 ;;; mode: text *** |
|
3228 ;;; End: *** |
|
3229 */ |