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