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