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