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