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