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