1829
|
1 /* |
1
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
|
23 // Parser for Octave. |
|
24 |
767
|
25 // C decarations. |
|
26 |
1
|
27 %{ |
|
28 #define YYDEBUG 1 |
|
29 |
240
|
30 #ifdef HAVE_CONFIG_H |
1229
|
31 #include <config.h> |
240
|
32 #endif |
|
33 |
3178
|
34 #include <cassert> |
3156
|
35 #include <cstdio> |
|
36 |
2427
|
37 #ifdef YYBYACC |
|
38 #include <cstdlib> |
|
39 #endif |
|
40 |
581
|
41 #include <strstream.h> |
|
42 |
1
|
43 #include "Matrix.h" |
3021
|
44 #include "cmd-edit.h" |
|
45 #include "cmd-hist.h" |
|
46 #include "file-ops.h" |
|
47 #include "file-stat.h" |
1
|
48 |
3665
|
49 #include "comment-list.h" |
2166
|
50 #include "defun.h" |
3021
|
51 #include "dynamic-ld.h" |
1351
|
52 #include "error.h" |
|
53 #include "input.h" |
|
54 #include "lex.h" |
1743
|
55 #include "oct-hist.h" |
2970
|
56 #include "ov-usr-fcn.h" |
1670
|
57 #include "toplev.h" |
1351
|
58 #include "pager.h" |
|
59 #include "parse.h" |
2987
|
60 #include "pt-all.h" |
1351
|
61 #include "symtab.h" |
|
62 #include "token.h" |
3021
|
63 #include "unwind-prot.h" |
1
|
64 #include "utils.h" |
1351
|
65 #include "variables.h" |
1
|
66 |
3021
|
67 // TRUE means we print |
|
68 static bool Vdefault_eval_print_flag = true; |
|
69 |
2166
|
70 // If TRUE, generate a warning for the assignment in things like |
|
71 // |
|
72 // octave> if (a = 2 < n) |
|
73 // |
|
74 // but not |
|
75 // |
|
76 // octave> if ((a = 2) < n) |
|
77 // |
|
78 static bool Vwarn_assign_as_truth_value; |
|
79 |
2764
|
80 // If TRUE, generate a warning for variable swich labels. |
|
81 static bool Vwarn_variable_switch_label; |
|
82 |
2166
|
83 // If TRUE, generate warning if declared function name disagrees with |
|
84 // the name of the file in which it is defined. |
|
85 static bool Vwarn_function_name_clash; |
|
86 |
3162
|
87 // TRUE means warn about function files that have time stamps in the future. |
|
88 bool Vwarn_future_time_stamp; |
|
89 |
2166
|
90 // If TRUE, generate warning if a statement in a function is not |
|
91 // terminated with a semicolon. Useful for checking functions that |
|
92 // should only produce output using explicit printing statements. |
|
93 static bool Vwarn_missing_semicolon; |
|
94 |
1
|
95 // Temporary symbol table pointer used to cope with bogus function syntax. |
522
|
96 symbol_table *tmp_local_sym_tab = 0; |
1
|
97 |
|
98 // The current input line number. |
|
99 int input_line_number = 0; |
|
100 |
|
101 // The column of the current token. |
143
|
102 int current_input_column = 1; |
1
|
103 |
338
|
104 // Buffer for help text snagged from function files. |
3523
|
105 std::string help_buf; |
1
|
106 |
3665
|
107 // Buffer for comments appearing before a function statement. |
|
108 static std::string fcn_comment_header; |
|
109 |
3021
|
110 // TRUE means we are using readline. |
|
111 // (--no-line-editing) |
|
112 bool line_editing = true; |
|
113 |
|
114 // TRUE means we printed messages about reading startup files. |
|
115 bool reading_startup_message_printed = false; |
|
116 |
|
117 // TRUE means input is coming from startup file. |
|
118 bool input_from_startup_file = false; |
|
119 |
|
120 // TRUE means that input is coming from a file that was named on |
|
121 // the command line. |
|
122 bool input_from_command_line_file = true; |
|
123 |
3489
|
124 // TRUE means that we are in the process of evaluating a function |
|
125 // body. The parser might be called in that case if we are looking at |
|
126 // an eval() statement. |
|
127 bool evaluating_function_body = false; |
|
128 |
496
|
129 // Forward declarations for some functions defined at the bottom of |
|
130 // the file. |
|
131 |
|
132 // Generic error messages. |
2970
|
133 static void |
|
134 yyerror (const char *s); |
1
|
135 |
578
|
136 // Error mesages for mismatched end tokens. |
2970
|
137 static void |
|
138 end_error (const char *type, token::end_tok_type ettype, int l, int c); |
1
|
139 |
578
|
140 // Check to see that end tokens are properly matched. |
2970
|
141 static bool |
|
142 end_token_ok (token *tok, token::end_tok_type expected); |
496
|
143 |
|
144 // Maybe print a warning if an assignment expression is used as the |
|
145 // test in a logical expression. |
2970
|
146 static void |
|
147 maybe_warn_assign_as_truth_value (tree_expression *expr); |
1
|
148 |
2764
|
149 // Maybe print a warning about switch labels that aren't constants. |
2970
|
150 static void |
|
151 maybe_warn_variable_switch_label (tree_expression *expr); |
2764
|
152 |
1623
|
153 // Create a plot command. |
2970
|
154 static tree_plot_command * |
|
155 make_plot_command (token *tok, plot_limits *range, subplot_list *list); |
1623
|
156 |
|
157 // Finish building a range. |
2970
|
158 static tree_expression * |
|
159 finish_colon_expression (tree_colon_expression *e); |
1623
|
160 |
1607
|
161 // Build a constant. |
2970
|
162 static tree_constant * |
|
163 make_constant (int op, token *tok_val); |
1607
|
164 |
578
|
165 // Build a binary expression. |
2970
|
166 static tree_expression * |
|
167 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
168 tree_expression *op2); |
578
|
169 |
2375
|
170 // Build a boolean expression. |
2970
|
171 static tree_expression * |
|
172 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
173 tree_expression *op2); |
2375
|
174 |
578
|
175 // Build a prefix expression. |
2970
|
176 static tree_expression * |
|
177 make_prefix_op (int op, tree_expression *op1, token *tok_val); |
578
|
178 |
|
179 // Build a postfix expression. |
2970
|
180 static tree_expression * |
|
181 make_postfix_op (int op, tree_expression *op1, token *tok_val); |
578
|
182 |
1623
|
183 // Build an unwind-protect command. |
2970
|
184 static tree_command * |
|
185 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
3665
|
186 tree_statement_list *cleanup, token *end_tok, |
|
187 octave_comment_list *lc, octave_comment_list *mc); |
1623
|
188 |
|
189 // Build a try-catch command. |
2970
|
190 static tree_command * |
|
191 make_try_command (token *try_tok, tree_statement_list *body, |
3665
|
192 tree_statement_list *cleanup, token *end_tok, |
|
193 octave_comment_list *lc, octave_comment_list *mc); |
1623
|
194 |
|
195 // Build a while command. |
2970
|
196 static tree_command * |
|
197 make_while_command (token *while_tok, tree_expression *expr, |
3665
|
198 tree_statement_list *body, token *end_tok, |
|
199 octave_comment_list *lc); |
1623
|
200 |
3484
|
201 // Build a do-until command. |
|
202 static tree_command * |
|
203 make_do_until_command (token *do_tok, tree_statement_list *body, |
3665
|
204 tree_expression *expr, octave_comment_list *lc); |
3484
|
205 |
1623
|
206 // Build a for command. |
2970
|
207 static tree_command * |
|
208 make_for_command (token *for_tok, tree_argument_list *lhs, |
|
209 tree_expression *expr, tree_statement_list *body, |
3665
|
210 token *end_tok, octave_comment_list *lc); |
1623
|
211 |
|
212 // Build a break command. |
2970
|
213 static tree_command * |
|
214 make_break_command (token *break_tok); |
1623
|
215 |
|
216 // Build a continue command. |
2970
|
217 static tree_command * |
|
218 make_continue_command (token *continue_tok); |
1623
|
219 |
|
220 // Build a return command. |
2970
|
221 static tree_command * |
|
222 make_return_command (token *return_tok); |
1623
|
223 |
|
224 // Start an if command. |
2970
|
225 static tree_if_command_list * |
|
226 start_if_command (tree_expression *expr, tree_statement_list *list); |
1623
|
227 |
|
228 // Finish an if command. |
2970
|
229 static tree_if_command * |
3665
|
230 finish_if_command (token *if_tok, tree_if_command_list *list, |
|
231 token *end_tok, octave_comment_list *lc); |
1623
|
232 |
|
233 // Build an elseif clause. |
2970
|
234 static tree_if_clause * |
3665
|
235 make_elseif_clause (tree_expression *expr, tree_statement_list *list, |
|
236 octave_comment_list *lc); |
1623
|
237 |
2764
|
238 // Finish a switch command. |
2970
|
239 static tree_switch_command * |
|
240 finish_switch_command (token *switch_tok, tree_expression *expr, |
3665
|
241 tree_switch_case_list *list, token *end_tok, |
|
242 octave_comment_list *lc); |
2764
|
243 |
|
244 // Build a switch case. |
2970
|
245 static tree_switch_case * |
3665
|
246 make_switch_case (tree_expression *expr, tree_statement_list *list, |
|
247 octave_comment_list *lc); |
2764
|
248 |
1623
|
249 // Build an assignment to a variable. |
2970
|
250 static tree_expression * |
|
251 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
252 tree_expression *rhs); |
1623
|
253 |
|
254 // Begin defining a function. |
2970
|
255 static octave_user_function * |
|
256 start_function (tree_parameter_list *param_list, tree_statement_list *body); |
1623
|
257 |
|
258 // Do most of the work for defining a function. |
2970
|
259 static octave_user_function * |
|
260 frob_function (tree_identifier *id, octave_user_function *fcn); |
1623
|
261 |
|
262 // Finish defining a function. |
2970
|
263 static octave_user_function * |
3665
|
264 finish_function (tree_identifier *id, octave_user_function *fcn, |
|
265 octave_comment_list *lc); |
1623
|
266 |
|
267 // Finish defining a function a different way. |
2970
|
268 static octave_user_function * |
3665
|
269 finish_function (tree_parameter_list *ret_list, |
|
270 octave_user_function *fcn, octave_comment_list *lc); |
751
|
271 |
2883
|
272 // Reset state after parsing function. |
2970
|
273 static void |
|
274 recover_from_parsing_function (void); |
2883
|
275 |
751
|
276 // Make an index expression. |
2970
|
277 static tree_index_expression * |
|
278 make_index_expression (tree_expression *expr, tree_argument_list *args); |
|
279 |
|
280 // Make an indirect reference expression. |
|
281 static tree_indirect_ref * |
3523
|
282 make_indirect_ref (tree_expression *expr, const std::string&); |
666
|
283 |
2846
|
284 // Make a declaration command. |
2970
|
285 static tree_decl_command * |
|
286 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst); |
2846
|
287 |
1623
|
288 // Finish building a matrix list. |
2970
|
289 static tree_expression * |
|
290 finish_matrix (tree_matrix *m); |
1623
|
291 |
3351
|
292 // Finish building a cell list. |
|
293 static tree_expression * |
|
294 finish_cell (tree_cell *c); |
|
295 |
1511
|
296 // Maybe print a warning. Duh. |
2970
|
297 static void |
|
298 maybe_warn_missing_semi (tree_statement_list *); |
1511
|
299 |
2525
|
300 // Set the print flag for a statement based on the separator type. |
2970
|
301 static void |
|
302 set_stmt_print_flag (tree_statement_list *, char, bool); |
2525
|
303 |
1
|
304 #define ABORT_PARSE \ |
|
305 do \ |
|
306 { \ |
522
|
307 global_command = 0; \ |
1
|
308 yyerrok; \ |
2865
|
309 if (interactive || forced_interactive) \ |
1
|
310 YYACCEPT; \ |
|
311 else \ |
|
312 YYABORT; \ |
|
313 } \ |
|
314 while (0) |
|
315 |
|
316 %} |
|
317 |
666
|
318 // Bison declarations. |
|
319 |
1
|
320 %union |
|
321 { |
2891
|
322 // The type of the basic tokens returned by the lexer. |
143
|
323 token *tok_val; |
|
324 |
3665
|
325 // Comment strings that we need to deal with mid-rule. |
|
326 octave_comment_list *comment_type; |
|
327 |
2891
|
328 // Types for the nonterminals we generate. |
2525
|
329 char sep_type; |
1
|
330 tree *tree_type; |
1829
|
331 tree_matrix *tree_matrix_type; |
3351
|
332 tree_cell *tree_cell_type; |
496
|
333 tree_expression *tree_expression_type; |
2375
|
334 tree_constant *tree_constant_type; |
1
|
335 tree_identifier *tree_identifier_type; |
|
336 tree_index_expression *tree_index_expression_type; |
|
337 tree_colon_expression *tree_colon_expression_type; |
|
338 tree_argument_list *tree_argument_list_type; |
|
339 tree_parameter_list *tree_parameter_list_type; |
|
340 tree_command *tree_command_type; |
|
341 tree_if_command *tree_if_command_type; |
578
|
342 tree_if_clause *tree_if_clause_type; |
|
343 tree_if_command_list *tree_if_command_list_type; |
2764
|
344 tree_switch_command *tree_switch_command_type; |
|
345 tree_switch_case *tree_switch_case_type; |
|
346 tree_switch_case_list *tree_switch_case_list_type; |
2846
|
347 tree_decl_elt *tree_decl_elt_type; |
|
348 tree_decl_init_list *tree_decl_init_list_type; |
|
349 tree_decl_command *tree_decl_command_type; |
578
|
350 tree_statement *tree_statement_type; |
|
351 tree_statement_list *tree_statement_list_type; |
1
|
352 tree_plot_command *tree_plot_command_type; |
578
|
353 subplot *subplot_type; |
|
354 subplot_list *subplot_list_type; |
|
355 plot_limits *plot_limits_type; |
|
356 plot_range *plot_range_type; |
|
357 subplot_using *subplot_using_type; |
|
358 subplot_style *subplot_style_type; |
3165
|
359 subplot_axes *subplot_axes_type; |
2891
|
360 octave_user_function *octave_user_function_type; |
1
|
361 } |
|
362 |
143
|
363 // Tokens with line and column information. |
|
364 %token <tok_val> '=' ':' '-' '+' '*' '/' |
3204
|
365 %token <tok_val> ADD_EQ SUB_EQ MUL_EQ DIV_EQ LEFTDIV_EQ |
|
366 %token <tok_val> EMUL_EQ EDIV_EQ ELEFTDIV_EQ AND_EQ OR_EQ |
2899
|
367 %token <tok_val> LSHIFT_EQ RSHIFT_EQ LSHIFT RSHIFT |
428
|
368 %token <tok_val> EXPR_AND_AND EXPR_OR_OR |
143
|
369 %token <tok_val> EXPR_AND EXPR_OR EXPR_NOT |
|
370 %token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
1276
|
371 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS |
|
372 %token <tok_val> QUOTE TRANSPOSE |
143
|
373 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW |
|
374 %token <tok_val> NUM IMAG_NUM |
2970
|
375 %token <tok_val> STRUCT_ELT |
2883
|
376 %token <tok_val> NAME |
143
|
377 %token <tok_val> END |
|
378 %token <tok_val> PLOT |
3165
|
379 %token <tok_val> TEXT STYLE AXES_TAG |
3484
|
380 %token <tok_val> FOR WHILE DO UNTIL |
1491
|
381 %token <tok_val> IF ELSEIF ELSE |
2764
|
382 %token <tok_val> SWITCH CASE OTHERWISE |
1491
|
383 %token <tok_val> BREAK CONTINUE FUNC_RET |
924
|
384 %token <tok_val> UNWIND CLEANUP |
1489
|
385 %token <tok_val> TRY CATCH |
2846
|
386 %token <tok_val> GLOBAL STATIC |
1
|
387 |
143
|
388 // Other tokens. |
2970
|
389 %token END_OF_INPUT LEXICAL_ERROR |
|
390 %token FCN ELLIPSIS ALL_VA_ARGS |
3165
|
391 %token USING TITLE WITH AXES COLON OPEN_BRACE CLOSE_BRACE CLEAR |
1
|
392 |
143
|
393 // Nonterminals we construct. |
3665
|
394 %type <comment_type> stash_comment function_beg |
2525
|
395 %type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep |
578
|
396 %type <tree_type> input |
2960
|
397 %type <tree_constant_type> constant magic_colon |
3351
|
398 %type <tree_matrix_type> matrix_rows matrix_rows1 |
|
399 %type <tree_cell_type> cell_rows cell_rows1 |
|
400 %type <tree_expression_type> title matrix cell |
2970
|
401 %type <tree_expression_type> primary_expr postfix_expr prefix_expr binary_expr |
|
402 %type <tree_expression_type> simple_expr colon_expr assign_expr expression |
1
|
403 %type <tree_identifier_type> identifier |
2891
|
404 %type <octave_user_function_type> function1 function2 function3 |
2970
|
405 %type <tree_index_expression_type> word_list_cmd |
|
406 %type <tree_colon_expression_type> colon_expr1 |
3351
|
407 %type <tree_argument_list_type> arg_list word_list assign_lhs |
|
408 %type <tree_argument_list_type> cell_or_matrix_row |
723
|
409 %type <tree_parameter_list_type> param_list param_list1 |
|
410 %type <tree_parameter_list_type> return_list return_list1 |
2970
|
411 %type <tree_command_type> command select_command loop_command |
|
412 %type <tree_command_type> jump_command except_command function |
578
|
413 %type <tree_if_command_type> if_command |
|
414 %type <tree_if_clause_type> elseif_clause else_clause |
|
415 %type <tree_if_command_list_type> if_cmd_list1 if_cmd_list |
2764
|
416 %type <tree_switch_command_type> switch_command |
|
417 %type <tree_switch_case_type> switch_case default_case |
|
418 %type <tree_switch_case_list_type> case_list1 case_list |
2846
|
419 %type <tree_decl_elt_type> decl2 |
|
420 %type <tree_decl_init_list_type> decl1 |
|
421 %type <tree_decl_command_type> declaration |
578
|
422 %type <tree_statement_type> statement |
627
|
423 %type <tree_statement_list_type> simple_list simple_list1 list list1 |
2891
|
424 %type <tree_statement_list_type> opt_list input1 function4 |
1
|
425 %type <tree_plot_command_type> plot_command |
578
|
426 %type <subplot_type> plot_command2 plot_options |
|
427 %type <subplot_list_type> plot_command1 |
|
428 %type <plot_limits_type> ranges |
|
429 %type <plot_range_type> ranges1 |
|
430 %type <subplot_using_type> using using1 |
|
431 %type <subplot_style_type> style |
3165
|
432 %type <subplot_axes_type> axes |
1
|
433 |
143
|
434 // Precedence and associativity. |
1
|
435 %left ';' ',' '\n' |
3204
|
436 %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
|
437 %left EXPR_AND_AND EXPR_OR_OR |
1
|
438 %left EXPR_AND EXPR_OR |
|
439 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
2899
|
440 %left LSHIFT RSHIFT |
1
|
441 %left ':' |
1276
|
442 %left '-' '+' EPLUS EMINUS |
1
|
443 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
444 %left QUOTE TRANSPOSE |
|
445 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
446 %right POW EPOW |
3351
|
447 %left '(' '.' '{' |
1
|
448 |
143
|
449 // Where to start. |
1
|
450 %start input |
|
451 |
|
452 %% |
|
453 |
2970
|
454 // ============================== |
|
455 // Statements and statement lists |
|
456 // ============================== |
|
457 |
627
|
458 input : input1 |
1
|
459 { |
627
|
460 global_command = $1; |
1
|
461 promptflag = 1; |
|
462 YYACCEPT; |
|
463 } |
|
464 | END_OF_INPUT |
|
465 { |
522
|
466 global_command = 0; |
1
|
467 promptflag = 1; |
|
468 YYABORT; |
|
469 } |
627
|
470 | simple_list parse_error |
1091
|
471 { ABORT_PARSE; } |
627
|
472 | parse_error |
1091
|
473 { ABORT_PARSE; } |
627
|
474 ; |
|
475 |
|
476 input1 : '\n' |
|
477 { $$ = 0; } |
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 |
|
1656 unwind_protect_bool (buffer_error_messages); |
|
1657 buffer_error_messages = true; |
|
1658 |
|
1659 unwind_protect::add (clear_global_error_variable, 0); |
2533
|
1660 |
|
1661 tree_expression *op1 = e->lhs (); |
|
1662 tree_expression *op2 = e->rhs (); |
|
1663 |
|
1664 if (op1->is_constant () && op2->is_constant ()) |
|
1665 { |
2970
|
1666 octave_value tmp = e->rvalue (); |
2533
|
1667 |
3489
|
1668 if (! (error_state || warning_state)) |
2533
|
1669 { |
|
1670 tree_constant *tc_retval = new tree_constant (tmp); |
|
1671 |
3523
|
1672 std::ostrstream buf; |
2533
|
1673 |
|
1674 tree_print_code tpc (buf); |
|
1675 |
|
1676 e->accept (tpc); |
|
1677 |
3538
|
1678 buf << std::ends; |
2533
|
1679 |
|
1680 char *s = buf.str (); |
|
1681 |
|
1682 tc_retval->stash_original_text (s); |
|
1683 |
|
1684 delete [] s; |
|
1685 |
|
1686 delete e; |
|
1687 |
|
1688 retval = tc_retval; |
|
1689 } |
|
1690 } |
3110
|
1691 |
3292
|
1692 unwind_protect::run_frame ("fold_binary_expression"); |
|
1693 |
|
1694 return retval; |
|
1695 } |
|
1696 |
|
1697 static tree_expression * |
|
1698 fold (tree_unary_expression *e) |
|
1699 { |
|
1700 tree_expression *retval = e; |
|
1701 |
|
1702 unwind_protect::begin_frame ("fold_unary_expression"); |
|
1703 |
|
1704 unwind_protect_int (error_state); |
|
1705 |
|
1706 unwind_protect_bool (buffer_error_messages); |
|
1707 buffer_error_messages = true; |
|
1708 |
|
1709 unwind_protect::add (clear_global_error_variable, 0); |
|
1710 |
|
1711 tree_expression *op = e->operand (); |
|
1712 |
|
1713 if (op->is_constant ()) |
|
1714 { |
|
1715 octave_value tmp = e->rvalue (); |
|
1716 |
3489
|
1717 if (! (error_state || warning_state)) |
3292
|
1718 { |
|
1719 tree_constant *tc_retval = new tree_constant (tmp); |
|
1720 |
3523
|
1721 std::ostrstream buf; |
3292
|
1722 |
|
1723 tree_print_code tpc (buf); |
|
1724 |
|
1725 e->accept (tpc); |
|
1726 |
3538
|
1727 buf << std::ends; |
3292
|
1728 |
|
1729 char *s = buf.str (); |
|
1730 |
|
1731 tc_retval->stash_original_text (s); |
|
1732 |
|
1733 delete [] s; |
|
1734 |
|
1735 delete e; |
|
1736 |
|
1737 retval = tc_retval; |
|
1738 } |
|
1739 } |
|
1740 |
|
1741 unwind_protect::run_frame ("fold_unary_expression"); |
2533
|
1742 |
|
1743 return retval; |
|
1744 } |
|
1745 |
1623
|
1746 // Finish building a range. |
|
1747 |
|
1748 static tree_expression * |
|
1749 finish_colon_expression (tree_colon_expression *e) |
|
1750 { |
3110
|
1751 tree_expression *retval = e; |
|
1752 |
|
1753 unwind_protect::begin_frame ("finish_colon_expression"); |
|
1754 |
|
1755 unwind_protect_int (error_state); |
|
1756 |
|
1757 unwind_protect_bool (buffer_error_messages); |
|
1758 buffer_error_messages = true; |
|
1759 |
|
1760 unwind_protect::add (clear_global_error_variable, 0); |
1623
|
1761 |
2533
|
1762 tree_expression *base = e->base (); |
|
1763 tree_expression *limit = e->limit (); |
|
1764 tree_expression *incr = e->increment (); |
|
1765 |
2970
|
1766 if (base) |
1623
|
1767 { |
2970
|
1768 if (limit) |
2533
|
1769 { |
2970
|
1770 if (base->is_constant () && limit->is_constant () |
|
1771 && (! incr || (incr && incr->is_constant ()))) |
|
1772 { |
|
1773 octave_value tmp = e->rvalue (); |
|
1774 |
3489
|
1775 if (! (error_state || warning_state)) |
2970
|
1776 { |
|
1777 tree_constant *tc_retval = new tree_constant (tmp); |
|
1778 |
3523
|
1779 std::ostrstream buf; |
2970
|
1780 |
|
1781 tree_print_code tpc (buf); |
|
1782 |
|
1783 e->accept (tpc); |
|
1784 |
3538
|
1785 buf << std::ends; |
2970
|
1786 |
|
1787 char *s = buf.str (); |
|
1788 |
|
1789 tc_retval->stash_original_text (s); |
|
1790 |
|
1791 delete [] s; |
|
1792 |
|
1793 delete e; |
|
1794 |
|
1795 retval = tc_retval; |
|
1796 } |
|
1797 } |
2533
|
1798 } |
|
1799 else |
2970
|
1800 { |
2990
|
1801 e->preserve_base (); |
|
1802 delete e; |
2970
|
1803 |
|
1804 // XXX FIXME XXX -- need to attempt constant folding here |
|
1805 // too (we need a generic way to do that). |
|
1806 retval = base; |
|
1807 } |
1623
|
1808 } |
|
1809 |
3110
|
1810 unwind_protect::run_frame ("finish_colon_expression"); |
|
1811 |
1623
|
1812 return retval; |
|
1813 } |
|
1814 |
1607
|
1815 // Make a constant. |
|
1816 |
2375
|
1817 static tree_constant * |
1607
|
1818 make_constant (int op, token *tok_val) |
|
1819 { |
|
1820 int l = tok_val->line (); |
|
1821 int c = tok_val->column (); |
|
1822 |
3216
|
1823 tree_constant *retval = 0; |
1607
|
1824 |
|
1825 switch (op) |
|
1826 { |
|
1827 case NUM: |
2533
|
1828 { |
2883
|
1829 octave_value tmp (tok_val->number ()); |
|
1830 retval = new tree_constant (tmp, l, c); |
2533
|
1831 retval->stash_original_text (tok_val->text_rep ()); |
|
1832 } |
1607
|
1833 break; |
|
1834 |
|
1835 case IMAG_NUM: |
|
1836 { |
2883
|
1837 octave_value tmp (Complex (0.0, tok_val->number ())); |
|
1838 retval = new tree_constant (tmp, l, c); |
1607
|
1839 retval->stash_original_text (tok_val->text_rep ()); |
|
1840 } |
|
1841 break; |
|
1842 |
|
1843 case TEXT: |
2883
|
1844 { |
|
1845 octave_value tmp (tok_val->text ()); |
|
1846 retval = new tree_constant (tmp, l, c); |
|
1847 } |
1607
|
1848 break; |
|
1849 |
|
1850 default: |
|
1851 panic_impossible (); |
|
1852 break; |
|
1853 } |
|
1854 |
|
1855 return retval; |
|
1856 } |
|
1857 |
666
|
1858 // Build a binary expression. |
|
1859 |
578
|
1860 static tree_expression * |
|
1861 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1862 tree_expression *op2) |
|
1863 { |
2883
|
1864 octave_value::binary_op t = octave_value::unknown_binary_op; |
1623
|
1865 |
578
|
1866 switch (op) |
|
1867 { |
|
1868 case POW: |
3525
|
1869 t = octave_value::op_pow; |
578
|
1870 break; |
777
|
1871 |
578
|
1872 case EPOW: |
3525
|
1873 t = octave_value::op_el_pow; |
578
|
1874 break; |
777
|
1875 |
578
|
1876 case '+': |
3525
|
1877 t = octave_value::op_add; |
578
|
1878 break; |
777
|
1879 |
578
|
1880 case '-': |
3525
|
1881 t = octave_value::op_sub; |
578
|
1882 break; |
777
|
1883 |
578
|
1884 case '*': |
3525
|
1885 t = octave_value::op_mul; |
578
|
1886 break; |
777
|
1887 |
578
|
1888 case '/': |
3525
|
1889 t = octave_value::op_div; |
578
|
1890 break; |
777
|
1891 |
578
|
1892 case EMUL: |
3525
|
1893 t = octave_value::op_el_mul; |
578
|
1894 break; |
777
|
1895 |
578
|
1896 case EDIV: |
3525
|
1897 t = octave_value::op_el_div; |
578
|
1898 break; |
777
|
1899 |
578
|
1900 case LEFTDIV: |
3525
|
1901 t = octave_value::op_ldiv; |
578
|
1902 break; |
777
|
1903 |
578
|
1904 case ELEFTDIV: |
3525
|
1905 t = octave_value::op_el_ldiv; |
578
|
1906 break; |
777
|
1907 |
2899
|
1908 case LSHIFT: |
3525
|
1909 t = octave_value::op_lshift; |
2899
|
1910 break; |
|
1911 |
|
1912 case RSHIFT: |
3525
|
1913 t = octave_value::op_rshift; |
2899
|
1914 break; |
|
1915 |
578
|
1916 case EXPR_LT: |
3525
|
1917 t = octave_value::op_lt; |
578
|
1918 break; |
777
|
1919 |
578
|
1920 case EXPR_LE: |
3525
|
1921 t = octave_value::op_le; |
578
|
1922 break; |
777
|
1923 |
578
|
1924 case EXPR_EQ: |
3525
|
1925 t = octave_value::op_eq; |
578
|
1926 break; |
777
|
1927 |
578
|
1928 case EXPR_GE: |
3525
|
1929 t = octave_value::op_ge; |
578
|
1930 break; |
777
|
1931 |
578
|
1932 case EXPR_GT: |
3525
|
1933 t = octave_value::op_gt; |
578
|
1934 break; |
777
|
1935 |
578
|
1936 case EXPR_NE: |
3525
|
1937 t = octave_value::op_ne; |
578
|
1938 break; |
777
|
1939 |
578
|
1940 case EXPR_AND: |
3525
|
1941 t = octave_value::op_el_and; |
578
|
1942 break; |
777
|
1943 |
578
|
1944 case EXPR_OR: |
3525
|
1945 t = octave_value::op_el_or; |
578
|
1946 break; |
777
|
1947 |
578
|
1948 default: |
|
1949 panic_impossible (); |
|
1950 break; |
|
1951 } |
|
1952 |
|
1953 int l = tok_val->line (); |
|
1954 int c = tok_val->column (); |
|
1955 |
2533
|
1956 tree_binary_expression *e |
|
1957 = new tree_binary_expression (op1, op2, l, c, t); |
1623
|
1958 |
2533
|
1959 return fold (e); |
578
|
1960 } |
|
1961 |
2375
|
1962 // Build a boolean expression. |
666
|
1963 |
578
|
1964 static tree_expression * |
2375
|
1965 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
1966 tree_expression *op2) |
578
|
1967 { |
2375
|
1968 tree_boolean_expression::type t; |
|
1969 |
578
|
1970 switch (op) |
|
1971 { |
2375
|
1972 case EXPR_AND_AND: |
2805
|
1973 t = tree_boolean_expression::bool_and; |
578
|
1974 break; |
777
|
1975 |
2375
|
1976 case EXPR_OR_OR: |
2805
|
1977 t = tree_boolean_expression::bool_or; |
578
|
1978 break; |
777
|
1979 |
578
|
1980 default: |
|
1981 panic_impossible (); |
|
1982 break; |
|
1983 } |
|
1984 |
|
1985 int l = tok_val->line (); |
|
1986 int c = tok_val->column (); |
|
1987 |
2533
|
1988 tree_boolean_expression *e |
|
1989 = new tree_boolean_expression (op1, op2, l, c, t); |
2375
|
1990 |
2533
|
1991 return fold (e); |
578
|
1992 } |
|
1993 |
2375
|
1994 // Build a prefix expression. |
666
|
1995 |
578
|
1996 static tree_expression * |
2960
|
1997 make_prefix_op (int op, tree_expression *op1, token *tok_val) |
578
|
1998 { |
3203
|
1999 octave_value::unary_op t = octave_value::unknown_unary_op; |
2375
|
2000 |
578
|
2001 switch (op) |
|
2002 { |
2960
|
2003 case EXPR_NOT: |
3525
|
2004 t = octave_value::op_not; |
2960
|
2005 break; |
|
2006 |
|
2007 case '-': |
3525
|
2008 t = octave_value::op_uminus; |
2960
|
2009 break; |
|
2010 |
578
|
2011 case PLUS_PLUS: |
3525
|
2012 t = octave_value::op_incr; |
578
|
2013 break; |
777
|
2014 |
578
|
2015 case MINUS_MINUS: |
3525
|
2016 t = octave_value::op_decr; |
578
|
2017 break; |
777
|
2018 |
578
|
2019 default: |
|
2020 panic_impossible (); |
|
2021 break; |
|
2022 } |
|
2023 |
|
2024 int l = tok_val->line (); |
|
2025 int c = tok_val->column (); |
|
2026 |
3292
|
2027 tree_prefix_expression *e |
|
2028 = new tree_prefix_expression (op1, l, c, t); |
|
2029 |
|
2030 return fold (e); |
578
|
2031 } |
|
2032 |
2375
|
2033 // Build a postfix expression. |
666
|
2034 |
578
|
2035 static tree_expression * |
2960
|
2036 make_postfix_op (int op, tree_expression *op1, token *tok_val) |
578
|
2037 { |
3203
|
2038 octave_value::unary_op t = octave_value::unknown_unary_op; |
1623
|
2039 |
578
|
2040 switch (op) |
|
2041 { |
2960
|
2042 case QUOTE: |
3525
|
2043 t = octave_value::op_hermitian; |
2960
|
2044 break; |
|
2045 |
|
2046 case TRANSPOSE: |
3525
|
2047 t = octave_value::op_transpose; |
2960
|
2048 break; |
|
2049 |
2375
|
2050 case PLUS_PLUS: |
3525
|
2051 t = octave_value::op_incr; |
578
|
2052 break; |
777
|
2053 |
2375
|
2054 case MINUS_MINUS: |
3525
|
2055 t = octave_value::op_decr; |
578
|
2056 break; |
777
|
2057 |
578
|
2058 default: |
|
2059 panic_impossible (); |
|
2060 break; |
|
2061 } |
|
2062 |
|
2063 int l = tok_val->line (); |
|
2064 int c = tok_val->column (); |
|
2065 |
3292
|
2066 tree_postfix_expression *e |
|
2067 = new tree_postfix_expression (op1, l, c, t); |
|
2068 |
|
2069 return fold (e); |
1623
|
2070 } |
|
2071 |
|
2072 // Build an unwind-protect command. |
|
2073 |
|
2074 static tree_command * |
|
2075 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
3665
|
2076 tree_statement_list *cleanup, token *end_tok, |
|
2077 octave_comment_list *lc, octave_comment_list *mc) |
1623
|
2078 { |
|
2079 tree_command *retval = 0; |
|
2080 |
2857
|
2081 if (end_token_ok (end_tok, token::unwind_protect_end)) |
1623
|
2082 { |
3665
|
2083 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2084 |
1623
|
2085 int l = unwind_tok->line (); |
|
2086 int c = unwind_tok->column (); |
|
2087 |
3665
|
2088 retval = new tree_unwind_protect_command (body, cleanup, |
|
2089 lc, mc, tc, l, c); |
1623
|
2090 } |
|
2091 |
|
2092 return retval; |
|
2093 } |
|
2094 |
|
2095 // Build a try-catch command. |
|
2096 |
|
2097 static tree_command * |
|
2098 make_try_command (token *try_tok, tree_statement_list *body, |
3665
|
2099 tree_statement_list *cleanup, token *end_tok, |
|
2100 octave_comment_list *lc, octave_comment_list *mc) |
1623
|
2101 { |
|
2102 tree_command *retval = 0; |
|
2103 |
2857
|
2104 if (end_token_ok (end_tok, token::try_catch_end)) |
1623
|
2105 { |
3665
|
2106 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2107 |
1623
|
2108 int l = try_tok->line (); |
|
2109 int c = try_tok->column (); |
|
2110 |
3665
|
2111 retval = new tree_try_catch_command (body, cleanup, |
|
2112 lc, mc, tc, l, c); |
1623
|
2113 } |
|
2114 |
|
2115 return retval; |
|
2116 } |
|
2117 |
|
2118 // Build a while command. |
|
2119 |
|
2120 static tree_command * |
|
2121 make_while_command (token *while_tok, tree_expression *expr, |
3665
|
2122 tree_statement_list *body, token *end_tok, |
|
2123 octave_comment_list *lc) |
1623
|
2124 { |
|
2125 tree_command *retval = 0; |
|
2126 |
|
2127 maybe_warn_assign_as_truth_value (expr); |
|
2128 |
2857
|
2129 if (end_token_ok (end_tok, token::while_end)) |
1623
|
2130 { |
3665
|
2131 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2132 |
1826
|
2133 lexer_flags.looping--; |
1623
|
2134 |
|
2135 int l = while_tok->line (); |
|
2136 int c = while_tok->column (); |
|
2137 |
3665
|
2138 retval = new tree_while_command (expr, body, lc, tc, l, c); |
1623
|
2139 } |
|
2140 |
|
2141 return retval; |
|
2142 } |
|
2143 |
3484
|
2144 // Build a do-until command. |
|
2145 |
|
2146 static tree_command * |
|
2147 make_do_until_command (token *do_tok, tree_statement_list *body, |
3665
|
2148 tree_expression *expr, octave_comment_list *lc) |
3484
|
2149 { |
|
2150 tree_command *retval = 0; |
|
2151 |
|
2152 maybe_warn_assign_as_truth_value (expr); |
|
2153 |
3665
|
2154 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2155 |
3484
|
2156 lexer_flags.looping--; |
|
2157 |
|
2158 int l = do_tok->line (); |
|
2159 int c = do_tok->column (); |
|
2160 |
3665
|
2161 retval = new tree_do_until_command (expr, body, lc, tc, l, c); |
3484
|
2162 |
|
2163 return retval; |
|
2164 } |
|
2165 |
1623
|
2166 // Build a for command. |
|
2167 |
|
2168 static tree_command * |
2970
|
2169 make_for_command (token *for_tok, tree_argument_list *lhs, |
1623
|
2170 tree_expression *expr, tree_statement_list *body, |
3665
|
2171 token *end_tok, octave_comment_list *lc) |
1623
|
2172 { |
|
2173 tree_command *retval = 0; |
|
2174 |
2857
|
2175 if (end_token_ok (end_tok, token::for_end)) |
1623
|
2176 { |
3665
|
2177 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2178 |
1826
|
2179 lexer_flags.looping--; |
1623
|
2180 |
|
2181 int l = for_tok->line (); |
|
2182 int c = for_tok->column (); |
|
2183 |
2970
|
2184 if (lhs->length () == 1) |
|
2185 { |
|
2186 tree_expression *tmp = lhs->remove_front (); |
|
2187 |
3665
|
2188 retval = new tree_simple_for_command (tmp, expr, body, |
|
2189 lc, tc, l, c); |
2970
|
2190 |
|
2191 delete lhs; |
|
2192 } |
|
2193 else |
3665
|
2194 retval = new tree_complex_for_command (lhs, expr, body, |
|
2195 lc, tc, l, c); |
1623
|
2196 } |
|
2197 |
|
2198 return retval; |
|
2199 } |
|
2200 |
|
2201 // Build a break command. |
|
2202 |
|
2203 static tree_command * |
|
2204 make_break_command (token *break_tok) |
|
2205 { |
|
2206 tree_command *retval = 0; |
|
2207 |
2620
|
2208 int l = break_tok->line (); |
|
2209 int c = break_tok->column (); |
|
2210 |
3489
|
2211 if (lexer_flags.looping || lexer_flags.defining_func |
|
2212 || reading_script_file || evaluating_function_body) |
2620
|
2213 retval = new tree_break_command (l, c); |
1623
|
2214 else |
2620
|
2215 retval = new tree_no_op_command ("break", l, c); |
1623
|
2216 |
|
2217 return retval; |
|
2218 } |
|
2219 |
|
2220 // Build a continue command. |
|
2221 |
|
2222 static tree_command * |
|
2223 make_continue_command (token *continue_tok) |
|
2224 { |
|
2225 tree_command *retval = 0; |
|
2226 |
2620
|
2227 int l = continue_tok->line (); |
|
2228 int c = continue_tok->column (); |
|
2229 |
|
2230 if (lexer_flags.looping) |
|
2231 retval = new tree_continue_command (l, c); |
1623
|
2232 else |
2620
|
2233 retval = new tree_no_op_command ("continue", l, c); |
1623
|
2234 |
|
2235 return retval; |
|
2236 } |
|
2237 |
|
2238 // Build a return command. |
|
2239 |
|
2240 static tree_command * |
|
2241 make_return_command (token *return_tok) |
|
2242 { |
|
2243 tree_command *retval = 0; |
|
2244 |
2620
|
2245 int l = return_tok->line (); |
|
2246 int c = return_tok->column (); |
|
2247 |
3489
|
2248 if (lexer_flags.defining_func || reading_script_file |
|
2249 || evaluating_function_body) |
2620
|
2250 retval = new tree_return_command (l, c); |
1623
|
2251 else |
2620
|
2252 retval = new tree_no_op_command ("return", l, c); |
1623
|
2253 |
|
2254 return retval; |
|
2255 } |
|
2256 |
|
2257 // Start an if command. |
|
2258 |
|
2259 static tree_if_command_list * |
|
2260 start_if_command (tree_expression *expr, tree_statement_list *list) |
|
2261 { |
|
2262 maybe_warn_assign_as_truth_value (expr); |
|
2263 |
|
2264 tree_if_clause *t = new tree_if_clause (expr, list); |
|
2265 |
|
2266 return new tree_if_command_list (t); |
|
2267 } |
|
2268 |
|
2269 // Finish an if command. |
|
2270 |
|
2271 static tree_if_command * |
|
2272 finish_if_command (token *if_tok, tree_if_command_list *list, |
3665
|
2273 token *end_tok, octave_comment_list *lc) |
1623
|
2274 { |
|
2275 tree_if_command *retval = 0; |
|
2276 |
2857
|
2277 if (end_token_ok (end_tok, token::if_end)) |
1623
|
2278 { |
3665
|
2279 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2280 |
1623
|
2281 int l = if_tok->line (); |
|
2282 int c = if_tok->column (); |
|
2283 |
3665
|
2284 retval = new tree_if_command (list, lc, tc, l, c); |
1623
|
2285 } |
|
2286 |
|
2287 return retval; |
|
2288 } |
|
2289 |
|
2290 // Build an elseif clause. |
|
2291 |
|
2292 static tree_if_clause * |
3665
|
2293 make_elseif_clause (tree_expression *expr, tree_statement_list *list, |
|
2294 octave_comment_list *lc) |
1623
|
2295 { |
|
2296 maybe_warn_assign_as_truth_value (expr); |
|
2297 |
3665
|
2298 return new tree_if_clause (expr, list, lc); |
1623
|
2299 } |
|
2300 |
2764
|
2301 // Finish a switch command. |
|
2302 |
|
2303 static tree_switch_command * |
|
2304 finish_switch_command (token *switch_tok, tree_expression *expr, |
3665
|
2305 tree_switch_case_list *list, token *end_tok, |
|
2306 octave_comment_list *lc) |
2764
|
2307 { |
|
2308 tree_switch_command *retval = 0; |
|
2309 |
2857
|
2310 if (end_token_ok (end_tok, token::switch_end)) |
2764
|
2311 { |
3665
|
2312 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2313 |
2764
|
2314 int l = switch_tok->line (); |
|
2315 int c = switch_tok->column (); |
|
2316 |
3665
|
2317 retval = new tree_switch_command (expr, list, lc, tc, l, c); |
2764
|
2318 } |
|
2319 |
|
2320 return retval; |
|
2321 } |
|
2322 |
|
2323 // Build a switch case. |
|
2324 |
|
2325 static tree_switch_case * |
3665
|
2326 make_switch_case (tree_expression *expr, tree_statement_list *list, |
|
2327 octave_comment_list *lc) |
2764
|
2328 { |
|
2329 maybe_warn_variable_switch_label (expr); |
|
2330 |
3665
|
2331 return new tree_switch_case (expr, list, lc); |
2764
|
2332 } |
|
2333 |
1623
|
2334 // Build an assignment to a variable. |
|
2335 |
|
2336 static tree_expression * |
2970
|
2337 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
2338 tree_expression *rhs) |
1623
|
2339 { |
2970
|
2340 tree_expression *retval = 0; |
|
2341 |
2883
|
2342 octave_value::assign_op t = octave_value::unknown_assign_op; |
|
2343 |
|
2344 switch (op) |
|
2345 { |
|
2346 case '=': |
3525
|
2347 t = octave_value::op_asn_eq; |
2883
|
2348 break; |
|
2349 |
|
2350 case ADD_EQ: |
3525
|
2351 t = octave_value::op_add_eq; |
2883
|
2352 break; |
|
2353 |
|
2354 case SUB_EQ: |
3525
|
2355 t = octave_value::op_sub_eq; |
2883
|
2356 break; |
|
2357 |
|
2358 case MUL_EQ: |
3525
|
2359 t = octave_value::op_mul_eq; |
2883
|
2360 break; |
|
2361 |
|
2362 case DIV_EQ: |
3525
|
2363 t = octave_value::op_div_eq; |
2883
|
2364 break; |
|
2365 |
3204
|
2366 case LEFTDIV_EQ: |
3525
|
2367 t = octave_value::op_ldiv_eq; |
3204
|
2368 break; |
|
2369 |
2899
|
2370 case LSHIFT_EQ: |
3525
|
2371 t = octave_value::op_lshift_eq; |
2899
|
2372 break; |
|
2373 |
|
2374 case RSHIFT_EQ: |
3525
|
2375 t = octave_value::op_rshift_eq; |
2899
|
2376 break; |
|
2377 |
2883
|
2378 case EMUL_EQ: |
3525
|
2379 t = octave_value::op_el_mul_eq; |
2883
|
2380 break; |
|
2381 |
|
2382 case EDIV_EQ: |
3525
|
2383 t = octave_value::op_el_div_eq; |
2883
|
2384 break; |
|
2385 |
3204
|
2386 case ELEFTDIV_EQ: |
3525
|
2387 t = octave_value::op_el_ldiv_eq; |
3204
|
2388 break; |
|
2389 |
2883
|
2390 case AND_EQ: |
3525
|
2391 t = octave_value::op_el_and_eq; |
2883
|
2392 break; |
|
2393 |
|
2394 case OR_EQ: |
3525
|
2395 t = octave_value::op_el_or_eq; |
2883
|
2396 break; |
|
2397 |
|
2398 default: |
|
2399 panic_impossible (); |
|
2400 break; |
|
2401 } |
|
2402 |
1623
|
2403 int l = eq_tok->line (); |
|
2404 int c = eq_tok->column (); |
|
2405 |
2970
|
2406 if (lhs->length () == 1) |
666
|
2407 { |
2970
|
2408 tree_expression *tmp = lhs->remove_front (); |
|
2409 |
|
2410 retval = new tree_simple_assignment (tmp, rhs, false, l, c, t); |
|
2411 |
|
2412 delete lhs; |
666
|
2413 } |
|
2414 else |
3208
|
2415 return new tree_multi_assignment (lhs, rhs, false, l, c, t); |
666
|
2416 |
|
2417 return retval; |
|
2418 } |
751
|
2419 |
1623
|
2420 // Begin defining a function. |
|
2421 |
2891
|
2422 static octave_user_function * |
|
2423 start_function (tree_parameter_list *param_list, tree_statement_list *body) |
1623
|
2424 { |
|
2425 body->mark_as_function_body (); |
|
2426 |
2891
|
2427 // We'll fill in the return list later. |
|
2428 |
|
2429 octave_user_function *fcn |
|
2430 = new octave_user_function (param_list, 0, body, curr_sym_tab); |
1623
|
2431 |
3665
|
2432 if (fcn) |
|
2433 { |
|
2434 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2435 |
|
2436 fcn->stash_trailing_comment (tc); |
|
2437 } |
|
2438 |
1623
|
2439 return fcn; |
|
2440 } |
|
2441 |
|
2442 // Do most of the work for defining a function. |
|
2443 |
2891
|
2444 static octave_user_function * |
|
2445 frob_function (tree_identifier *id, octave_user_function *fcn) |
1623
|
2446 { |
3523
|
2447 std::string id_name = id->name (); |
1623
|
2448 |
|
2449 // If input is coming from a file, issue a warning if the name of |
|
2450 // the file does not match the name of the function stated in the |
|
2451 // file. Matlab doesn't provide a diagnostic (it ignores the stated |
|
2452 // name). |
|
2453 |
|
2454 fcn->stash_function_name (id_name); |
|
2455 |
|
2456 if (reading_fcn_file) |
|
2457 { |
1755
|
2458 if (curr_fcn_file_name != id_name) |
1623
|
2459 { |
2166
|
2460 if (Vwarn_function_name_clash) |
1623
|
2461 warning ("function name `%s' does not agree with function\ |
1755
|
2462 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2463 |
|
2464 global_sym_tab->rename (id_name, curr_fcn_file_name); |
|
2465 |
|
2466 if (error_state) |
|
2467 return 0; |
|
2468 |
|
2469 id_name = id->name (); |
|
2470 } |
|
2471 |
3162
|
2472 time_t now = time (0); |
|
2473 |
1623
|
2474 fcn->stash_function_name (id_name); |
|
2475 fcn->stash_fcn_file_name (); |
3162
|
2476 fcn->stash_fcn_file_time (now); |
1623
|
2477 fcn->mark_as_system_fcn_file (); |
3162
|
2478 |
|
2479 if (Vwarn_future_time_stamp) |
|
2480 { |
3523
|
2481 std::string nm = fcn->fcn_file_name (); |
3162
|
2482 |
|
2483 file_stat fs (nm); |
|
2484 |
|
2485 if (fs && fs.is_newer (now)) |
|
2486 warning ("time stamp for `%s' is in the future", nm.c_str ()); |
|
2487 } |
1623
|
2488 } |
|
2489 else if (! (input_from_tmp_history_file || input_from_startup_file) |
|
2490 && reading_script_file |
1755
|
2491 && curr_fcn_file_name == id_name) |
1623
|
2492 { |
|
2493 warning ("function `%s' defined within script file `%s'", |
1755
|
2494 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2495 } |
|
2496 |
|
2497 top_level_sym_tab->clear (id_name); |
|
2498 |
2856
|
2499 symbol_record *sr = global_sym_tab->lookup (id_name); |
2791
|
2500 |
|
2501 if (sr) |
|
2502 fcn->stash_symtab_ptr (sr); |
|
2503 else |
|
2504 panic_impossible (); |
|
2505 |
3016
|
2506 id->define (fcn, symbol_record::USER_FUNCTION); |
1755
|
2507 |
1623
|
2508 id->document (help_buf); |
|
2509 |
|
2510 return fcn; |
|
2511 } |
|
2512 |
|
2513 // Finish defining a function. |
|
2514 |
2891
|
2515 static octave_user_function * |
3665
|
2516 finish_function (tree_identifier *id, octave_user_function *fcn, |
|
2517 octave_comment_list *lc) |
1623
|
2518 { |
2883
|
2519 tree_parameter_list *tpl = new tree_parameter_list (id); |
1623
|
2520 |
|
2521 tpl->mark_as_formal_parameters (); |
|
2522 |
3665
|
2523 fcn->stash_leading_comment (lc); |
|
2524 |
1623
|
2525 return fcn->define_ret_list (tpl); |
|
2526 } |
|
2527 |
|
2528 // Finish defining a function a different way. |
|
2529 |
2891
|
2530 static octave_user_function * |
3665
|
2531 finish_function (tree_parameter_list *ret_list, |
|
2532 octave_user_function *fcn, octave_comment_list *lc) |
1623
|
2533 { |
|
2534 ret_list->mark_as_formal_parameters (); |
|
2535 |
3665
|
2536 fcn->stash_leading_comment (lc); |
|
2537 |
1623
|
2538 return fcn->define_ret_list (ret_list); |
|
2539 } |
|
2540 |
2883
|
2541 static void |
|
2542 recover_from_parsing_function (void) |
|
2543 { |
|
2544 curr_sym_tab = top_level_sym_tab; |
|
2545 |
|
2546 lexer_flags.defining_func = false; |
|
2547 lexer_flags.beginning_of_function = false; |
|
2548 lexer_flags.parsed_function_name = false; |
|
2549 lexer_flags.looking_at_return_list = false; |
|
2550 lexer_flags.looking_at_parameter_list = false; |
|
2551 } |
|
2552 |
2846
|
2553 // Make an index expression. |
|
2554 |
751
|
2555 static tree_index_expression * |
2970
|
2556 make_index_expression (tree_expression *expr, tree_argument_list *args) |
751
|
2557 { |
|
2558 tree_index_expression *retval = 0; |
|
2559 |
2970
|
2560 int l = expr->line (); |
|
2561 int c = expr->column (); |
|
2562 |
|
2563 expr->mark_postfix_indexed (); |
|
2564 |
|
2565 retval = new tree_index_expression (expr, args, l, c); |
|
2566 |
|
2567 return retval; |
|
2568 } |
|
2569 |
|
2570 // Make an indirect reference expression. |
|
2571 |
|
2572 static tree_indirect_ref * |
3523
|
2573 make_indirect_ref (tree_expression *expr, const std::string& elt) |
2970
|
2574 { |
|
2575 tree_indirect_ref *retval = 0; |
|
2576 |
|
2577 int l = expr->line (); |
|
2578 int c = expr->column (); |
|
2579 |
|
2580 retval = new tree_indirect_ref (expr, elt, l, c); |
|
2581 |
|
2582 lexer_flags.looking_at_indirect_ref = false; |
751
|
2583 |
|
2584 return retval; |
|
2585 } |
1511
|
2586 |
2846
|
2587 // Make a declaration command. |
|
2588 |
|
2589 static tree_decl_command * |
|
2590 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) |
|
2591 { |
|
2592 tree_decl_command *retval = 0; |
|
2593 |
|
2594 int l = tok_val->line (); |
|
2595 int c = tok_val->column (); |
|
2596 |
|
2597 switch (tok) |
|
2598 { |
|
2599 case GLOBAL: |
|
2600 retval = new tree_global_command (lst, l, c); |
|
2601 break; |
|
2602 |
|
2603 case STATIC: |
|
2604 if (lexer_flags.defining_func) |
|
2605 retval = new tree_static_command (lst, l, c); |
|
2606 else |
|
2607 { |
|
2608 if (reading_script_file) |
|
2609 warning ("ignoring static declaration near line %d of file `%s'", |
|
2610 l, curr_fcn_file_full_name.c_str ()); |
|
2611 else |
|
2612 warning ("ignoring static declaration near line %d", l); |
|
2613 } |
|
2614 break; |
|
2615 |
|
2616 default: |
|
2617 panic_impossible (); |
|
2618 break; |
|
2619 } |
|
2620 |
|
2621 return retval; |
|
2622 } |
|
2623 |
1623
|
2624 // Finish building a matrix list. |
|
2625 |
|
2626 static tree_expression * |
1829
|
2627 finish_matrix (tree_matrix *m) |
1623
|
2628 { |
3110
|
2629 tree_expression *retval = m; |
|
2630 |
|
2631 unwind_protect::begin_frame ("finish_matrix"); |
|
2632 |
|
2633 unwind_protect_int (error_state); |
|
2634 |
|
2635 unwind_protect_bool (buffer_error_messages); |
|
2636 buffer_error_messages = true; |
|
2637 |
|
2638 unwind_protect::add (clear_global_error_variable, 0); |
1623
|
2639 |
2533
|
2640 if (m->all_elements_are_constant ()) |
1829
|
2641 { |
2970
|
2642 octave_value tmp = m->rvalue (); |
1623
|
2643 |
3489
|
2644 if (! (error_state || warning_state)) |
2533
|
2645 { |
|
2646 tree_constant *tc_retval = new tree_constant (tmp); |
|
2647 |
3523
|
2648 std::ostrstream buf; |
2533
|
2649 |
|
2650 tree_print_code tpc (buf); |
|
2651 |
|
2652 m->accept (tpc); |
|
2653 |
3538
|
2654 buf << std::ends; |
1623
|
2655 |
2533
|
2656 char *s = buf.str (); |
|
2657 |
|
2658 tc_retval->stash_original_text (s); |
|
2659 |
|
2660 delete [] s; |
|
2661 |
|
2662 delete m; |
|
2663 |
|
2664 retval = tc_retval; |
|
2665 } |
1623
|
2666 } |
3110
|
2667 |
|
2668 unwind_protect::run_frame ("finish_matrix"); |
1623
|
2669 |
|
2670 return retval; |
|
2671 } |
|
2672 |
3351
|
2673 // Finish building a cell list. |
|
2674 |
|
2675 static tree_expression * |
|
2676 finish_cell (tree_cell *c) |
|
2677 { |
|
2678 // For now, this doesn't do anything. |
|
2679 |
|
2680 return c; |
|
2681 } |
|
2682 |
1511
|
2683 static void |
|
2684 maybe_warn_missing_semi (tree_statement_list *t) |
|
2685 { |
2166
|
2686 if (lexer_flags.defining_func && Vwarn_missing_semicolon) |
1511
|
2687 { |
|
2688 tree_statement *tmp = t->rear(); |
1607
|
2689 |
1511
|
2690 if (tmp->is_expression ()) |
1607
|
2691 warning ("missing semicolon near line %d, column %d in file `%s'", |
1755
|
2692 tmp->line (), tmp->column (), |
|
2693 curr_fcn_file_full_name.c_str ()); |
1511
|
2694 } |
|
2695 } |
1994
|
2696 |
2525
|
2697 static void |
|
2698 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2699 bool warn_missing_semi) |
|
2700 { |
|
2701 switch (sep) |
|
2702 { |
|
2703 case ';': |
|
2704 { |
|
2705 tree_statement *tmp = list->rear (); |
|
2706 tmp->set_print_flag (0); |
|
2707 } |
|
2708 break; |
|
2709 |
|
2710 case 0: |
|
2711 case ',': |
|
2712 case '\n': |
|
2713 if (warn_missing_semi) |
|
2714 maybe_warn_missing_semi (list); |
|
2715 break; |
|
2716 |
|
2717 default: |
|
2718 warning ("unrecognized separator type!"); |
|
2719 break; |
|
2720 } |
|
2721 } |
|
2722 |
3021
|
2723 void |
|
2724 parse_and_execute (FILE *f) |
|
2725 { |
|
2726 unwind_protect::begin_frame ("parse_and_execute"); |
|
2727 |
|
2728 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2729 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
2730 |
|
2731 unwind_protect::add (restore_input_buffer, old_buf); |
|
2732 unwind_protect::add (delete_input_buffer, new_buf); |
|
2733 |
|
2734 switch_to_buffer (new_buf); |
|
2735 |
|
2736 unwind_protect_bool (line_editing); |
|
2737 unwind_protect_bool (input_from_command_line_file); |
|
2738 |
|
2739 line_editing = false; |
|
2740 input_from_command_line_file = false; |
|
2741 |
|
2742 unwind_protect_ptr (curr_sym_tab); |
|
2743 |
|
2744 int retval; |
|
2745 do |
|
2746 { |
|
2747 reset_parser (); |
|
2748 |
|
2749 retval = yyparse (); |
|
2750 |
|
2751 if (retval == 0 && global_command) |
|
2752 { |
|
2753 global_command->eval (); |
|
2754 |
|
2755 delete global_command; |
|
2756 |
|
2757 global_command = 0; |
|
2758 |
|
2759 bool quit = (tree_return_command::returning |
|
2760 || tree_break_command::breaking); |
|
2761 |
|
2762 if (tree_return_command::returning) |
|
2763 tree_return_command::returning = 0; |
|
2764 |
|
2765 if (tree_break_command::breaking) |
|
2766 tree_break_command::breaking--; |
|
2767 |
|
2768 if (error_state) |
|
2769 { |
|
2770 error ("near line %d of file `%s'", input_line_number, |
|
2771 curr_fcn_file_full_name.c_str ()); |
|
2772 |
|
2773 break; |
|
2774 } |
|
2775 |
|
2776 if (quit) |
|
2777 break; |
|
2778 } |
|
2779 } |
|
2780 while (retval == 0); |
|
2781 |
|
2782 unwind_protect::run_frame ("parse_and_execute"); |
|
2783 } |
|
2784 |
|
2785 static void |
|
2786 safe_fclose (void *f) |
|
2787 { |
|
2788 if (f) |
|
2789 fclose (static_cast<FILE *> (f)); |
|
2790 } |
|
2791 |
|
2792 void |
3523
|
2793 parse_and_execute (const std::string& s, bool verbose, const char *warn_for) |
3021
|
2794 { |
|
2795 unwind_protect::begin_frame ("parse_and_execute_2"); |
|
2796 |
|
2797 unwind_protect_bool (reading_script_file); |
|
2798 unwind_protect_str (curr_fcn_file_full_name); |
|
2799 |
|
2800 reading_script_file = true; |
|
2801 curr_fcn_file_full_name = s; |
|
2802 |
|
2803 FILE *f = get_input_from_file (s, 0); |
|
2804 |
|
2805 if (f) |
|
2806 { |
|
2807 unwind_protect::add (safe_fclose, f); |
|
2808 |
|
2809 unwind_protect_int (input_line_number); |
|
2810 unwind_protect_int (current_input_column); |
|
2811 |
|
2812 input_line_number = 0; |
|
2813 current_input_column = 1; |
|
2814 |
|
2815 if (verbose) |
|
2816 { |
3531
|
2817 std::cout << "reading commands from " << s << " ... "; |
3021
|
2818 reading_startup_message_printed = true; |
3531
|
2819 std::cout.flush (); |
3021
|
2820 } |
|
2821 |
|
2822 parse_and_execute (f); |
|
2823 |
|
2824 if (verbose) |
3538
|
2825 std::cout << "done." << std::endl; |
3021
|
2826 } |
|
2827 else if (warn_for) |
|
2828 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
|
2829 |
|
2830 unwind_protect::run_frame ("parse_and_execute_2"); |
|
2831 } |
|
2832 |
|
2833 static bool |
3523
|
2834 looks_like_octave_copyright (const std::string& s) |
3021
|
2835 { |
|
2836 bool retval = false; |
|
2837 |
3523
|
2838 std::string t = s.substr (0, 14); |
3427
|
2839 |
|
2840 if (t == "Copyright (C) ") |
3021
|
2841 { |
|
2842 size_t pos = s.find ('\n'); |
|
2843 |
|
2844 if (pos != NPOS) |
|
2845 { |
|
2846 pos = s.find ('\n', pos + 1); |
|
2847 |
|
2848 if (pos != NPOS) |
|
2849 { |
|
2850 pos++; |
|
2851 |
3427
|
2852 t = s.substr (pos, 28); |
|
2853 |
|
2854 if (t == "This file is part of Octave." |
|
2855 || t == "This program is free softwar") |
3021
|
2856 retval = true; |
|
2857 } |
|
2858 } |
|
2859 } |
|
2860 |
|
2861 return retval; |
|
2862 } |
|
2863 |
|
2864 // Eat whitespace and comments from FFILE, returning the text of the |
|
2865 // comments read if it doesn't look like a copyright notice. If |
|
2866 // IN_PARTS, consider each block of comments separately; otherwise, |
|
2867 // grab them all at once. If UPDATE_POS is TRUE, line and column |
3665
|
2868 // number information is updated. If SAVE_COPYRIGHT is TRUE, then |
|
2869 // comments that are recognized as a copyright notice are saved in the |
|
2870 // comment buffer. |
3021
|
2871 |
|
2872 // XXX FIXME XXX -- grab_help_text() in lex.l duplicates some of this |
|
2873 // code! |
|
2874 |
3536
|
2875 static std::string |
3665
|
2876 gobble_leading_white_space (FILE *ffile, bool in_parts, |
|
2877 bool update_pos, bool save_copyright) |
3021
|
2878 { |
3523
|
2879 std::string help_txt; |
3021
|
2880 |
|
2881 bool first_comments_seen = false; |
|
2882 bool begin_comment = false; |
|
2883 bool have_help_text = false; |
|
2884 bool in_comment = false; |
3427
|
2885 bool discard_space = true; |
3021
|
2886 int c; |
|
2887 |
|
2888 while ((c = getc (ffile)) != EOF) |
|
2889 { |
|
2890 if (update_pos) |
|
2891 current_input_column++; |
|
2892 |
|
2893 if (begin_comment) |
|
2894 { |
|
2895 if (c == '%' || c == '#') |
|
2896 continue; |
3427
|
2897 else if (discard_space && c == ' ') |
|
2898 { |
|
2899 discard_space = false; |
|
2900 continue; |
|
2901 } |
3021
|
2902 else |
|
2903 begin_comment = false; |
|
2904 } |
|
2905 |
|
2906 if (in_comment) |
|
2907 { |
|
2908 if (! have_help_text) |
|
2909 { |
|
2910 first_comments_seen = true; |
|
2911 help_txt += (char) c; |
|
2912 } |
|
2913 |
|
2914 if (c == '\n') |
|
2915 { |
|
2916 if (update_pos) |
|
2917 { |
|
2918 input_line_number++; |
|
2919 current_input_column = 0; |
|
2920 } |
3427
|
2921 |
3021
|
2922 in_comment = false; |
3427
|
2923 discard_space = true; |
3021
|
2924 |
|
2925 if (in_parts) |
|
2926 { |
|
2927 if ((c = getc (ffile)) != EOF) |
|
2928 { |
|
2929 if (update_pos) |
|
2930 current_input_column--; |
|
2931 ungetc (c, ffile); |
|
2932 if (c == '\n') |
|
2933 break; |
|
2934 } |
|
2935 else |
|
2936 break; |
|
2937 } |
|
2938 } |
|
2939 } |
|
2940 else |
|
2941 { |
|
2942 switch (c) |
|
2943 { |
|
2944 case ' ': |
|
2945 case '\t': |
|
2946 if (first_comments_seen) |
|
2947 have_help_text = true; |
|
2948 break; |
|
2949 |
|
2950 case '\n': |
|
2951 if (first_comments_seen) |
|
2952 have_help_text = true; |
|
2953 if (update_pos) |
|
2954 { |
|
2955 input_line_number++; |
|
2956 current_input_column = 0; |
|
2957 } |
|
2958 continue; |
|
2959 |
|
2960 case '%': |
|
2961 case '#': |
|
2962 begin_comment = true; |
|
2963 in_comment = true; |
|
2964 break; |
|
2965 |
|
2966 default: |
|
2967 if (update_pos) |
|
2968 current_input_column--; |
|
2969 ungetc (c, ffile); |
|
2970 goto done; |
|
2971 } |
|
2972 } |
|
2973 } |
|
2974 |
|
2975 done: |
|
2976 |
|
2977 if (! help_txt.empty ()) |
|
2978 { |
3665
|
2979 if (looks_like_octave_copyright (help_txt)) |
|
2980 { |
|
2981 if (save_copyright) |
|
2982 octave_comment_buffer::append (help_txt); |
|
2983 |
|
2984 help_txt.resize (0); |
|
2985 } |
3021
|
2986 |
|
2987 if (in_parts && help_txt.empty ()) |
3665
|
2988 help_txt = gobble_leading_white_space (ffile, in_parts, |
|
2989 update_pos, false); |
3021
|
2990 } |
|
2991 |
|
2992 return help_txt; |
|
2993 } |
|
2994 |
3536
|
2995 std::string |
3523
|
2996 get_help_from_file (const std::string& path) |
3021
|
2997 { |
3523
|
2998 std::string retval; |
3021
|
2999 |
|
3000 if (! path.empty ()) |
|
3001 { |
|
3002 FILE *fptr = fopen (path.c_str (), "r"); |
|
3003 |
|
3004 if (fptr) |
|
3005 { |
|
3006 unwind_protect::add (safe_fclose, (void *) fptr); |
|
3007 |
3665
|
3008 retval = gobble_leading_white_space (fptr, true, true, false); |
3021
|
3009 |
|
3010 unwind_protect::run (); |
|
3011 } |
|
3012 } |
|
3013 |
|
3014 return retval; |
|
3015 } |
|
3016 |
|
3017 static int |
|
3018 is_function_file (FILE *ffile) |
|
3019 { |
|
3020 int status = 0; |
|
3021 |
|
3022 long pos = ftell (ffile); |
|
3023 |
3665
|
3024 gobble_leading_white_space (ffile, false, false, false); |
3021
|
3025 |
|
3026 char buf [10]; |
|
3027 fgets (buf, 10, ffile); |
|
3028 int len = strlen (buf); |
|
3029 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
3030 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
3031 status = 1; |
|
3032 |
|
3033 fseek (ffile, pos, SEEK_SET); |
|
3034 |
|
3035 return status; |
|
3036 } |
|
3037 |
|
3038 static void |
|
3039 restore_command_history (void *) |
|
3040 { |
|
3041 command_history::ignore_entries (! Vsaving_history); |
|
3042 } |
|
3043 |
|
3044 static void |
|
3045 restore_input_stream (void *f) |
|
3046 { |
|
3047 command_editor::set_input_stream (static_cast<FILE *> (f)); |
|
3048 } |
|
3049 |
3180
|
3050 static void |
|
3051 clear_current_script_file_name (void *) |
|
3052 { |
|
3053 bind_builtin_variable ("current_script_file_name", octave_value ()); |
|
3054 } |
|
3055 |
3021
|
3056 static bool |
3523
|
3057 parse_fcn_file (const std::string& ff, bool exec_script, bool force_script = false) |
3021
|
3058 { |
|
3059 unwind_protect::begin_frame ("parse_fcn_file"); |
|
3060 |
|
3061 int script_file_executed = false; |
|
3062 |
|
3063 // Open function file and parse. |
|
3064 |
|
3065 bool old_reading_fcn_file_state = reading_fcn_file; |
|
3066 |
|
3067 FILE *in_stream = command_editor::get_input_stream (); |
|
3068 |
|
3069 unwind_protect::add (restore_input_stream, in_stream); |
|
3070 |
|
3071 unwind_protect_ptr (ff_instream); |
|
3072 |
|
3073 unwind_protect_int (input_line_number); |
|
3074 unwind_protect_int (current_input_column); |
|
3075 unwind_protect_bool (reading_fcn_file); |
|
3076 unwind_protect_bool (line_editing); |
|
3077 |
|
3078 input_line_number = 0; |
|
3079 current_input_column = 1; |
|
3080 reading_fcn_file = true; |
|
3081 line_editing = false; |
|
3082 |
|
3083 FILE *ffile = get_input_from_file (ff, 0); |
|
3084 |
|
3085 unwind_protect::add (safe_fclose, ffile); |
|
3086 |
|
3087 if (ffile) |
|
3088 { |
|
3089 // Check to see if this file defines a function or is just a |
|
3090 // list of commands. |
|
3091 |
3165
|
3092 if (! force_script && is_function_file (ffile)) |
3021
|
3093 { |
|
3094 // XXX FIXME XXX -- we shouldn't need both the |
|
3095 // command_history object and the |
|
3096 // Vsaving_history variable... |
|
3097 command_history::ignore_entries (); |
|
3098 |
|
3099 unwind_protect::add (restore_command_history, 0); |
|
3100 |
|
3101 unwind_protect_int (Vecho_executing_commands); |
|
3102 unwind_protect_bool (Vsaving_history); |
|
3103 unwind_protect_bool (reading_fcn_file); |
|
3104 unwind_protect_bool (input_from_command_line_file); |
|
3105 |
|
3106 Vecho_executing_commands = ECHO_OFF; |
|
3107 Vsaving_history = false; |
|
3108 reading_fcn_file = true; |
|
3109 input_from_command_line_file = false; |
|
3110 |
|
3111 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3112 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
3113 |
|
3114 unwind_protect::add (restore_input_buffer, (void *) old_buf); |
|
3115 unwind_protect::add (delete_input_buffer, (void *) new_buf); |
|
3116 |
|
3117 switch_to_buffer (new_buf); |
|
3118 |
|
3119 unwind_protect_ptr (curr_sym_tab); |
|
3120 |
|
3121 reset_parser (); |
|
3122 |
3665
|
3123 help_buf = gobble_leading_white_space (ffile, true, true, true); |
|
3124 |
|
3125 octave_comment_buffer::append (help_buf); |
3021
|
3126 |
|
3127 // XXX FIXME XXX -- this should not be necessary. |
3665
|
3128 gobble_leading_white_space (ffile, false, true, false); |
3021
|
3129 |
|
3130 int status = yyparse (); |
|
3131 |
|
3132 if (status != 0) |
|
3133 { |
|
3134 error ("parse error while reading function file %s", |
|
3135 ff.c_str ()); |
|
3136 global_sym_tab->clear (curr_fcn_file_name); |
|
3137 } |
|
3138 } |
|
3139 else if (exec_script) |
|
3140 { |
|
3141 // The value of `reading_fcn_file' will be restored to the |
|
3142 // proper value when we unwind from this frame. |
|
3143 reading_fcn_file = old_reading_fcn_file_state; |
|
3144 |
|
3145 // XXX FIXME XXX -- we shouldn't need both the |
|
3146 // command_history object and the |
|
3147 // Vsaving_history variable... |
|
3148 command_history::ignore_entries (); |
|
3149 |
|
3150 unwind_protect::add (restore_command_history, 0); |
|
3151 |
|
3152 unwind_protect_bool (Vsaving_history); |
|
3153 unwind_protect_bool (reading_script_file); |
|
3154 |
|
3155 Vsaving_history = false; |
|
3156 reading_script_file = true; |
|
3157 |
3180
|
3158 unwind_protect::add (clear_current_script_file_name, 0); |
|
3159 |
|
3160 bind_builtin_variable ("current_script_file_name", ff); |
|
3161 |
3021
|
3162 parse_and_execute (ffile); |
|
3163 |
|
3164 script_file_executed = true; |
|
3165 } |
|
3166 } |
|
3167 |
|
3168 unwind_protect::run_frame ("parse_fcn_file"); |
|
3169 |
|
3170 return script_file_executed; |
|
3171 } |
|
3172 |
|
3173 bool |
|
3174 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
|
3175 { |
|
3176 bool script_file_executed = false; |
|
3177 |
3523
|
3178 std::string nm = sym_rec->name (); |
3021
|
3179 |
3325
|
3180 if (octave_dynamic_loader::load (nm)) |
3021
|
3181 { |
|
3182 force_link_to_function (nm); |
|
3183 } |
|
3184 else |
|
3185 { |
3523
|
3186 std::string ff = fcn_file_in_path (nm); |
3021
|
3187 |
|
3188 // These are needed by yyparse. |
|
3189 |
|
3190 unwind_protect::begin_frame ("load_fcn_from_file"); |
|
3191 |
|
3192 unwind_protect_str (curr_fcn_file_name); |
|
3193 unwind_protect_str (curr_fcn_file_full_name); |
|
3194 |
|
3195 curr_fcn_file_name = nm; |
|
3196 curr_fcn_file_full_name = ff; |
|
3197 |
|
3198 if (ff.length () > 0) |
3165
|
3199 script_file_executed = parse_fcn_file (ff, exec_script); |
3021
|
3200 |
|
3201 if (! (error_state || script_file_executed)) |
|
3202 force_link_to_function (nm); |
|
3203 |
|
3204 unwind_protect::run_frame ("load_fcn_from_file"); |
|
3205 } |
|
3206 |
|
3207 return script_file_executed; |
|
3208 } |
|
3209 |
|
3210 DEFUN (source, args, , |
3371
|
3211 "-*- texinfo -*-\n\ |
|
3212 @deftypefn {Built-in Function} {} source (@var{file})\n\ |
|
3213 Parse and execute the contents of @var{file}. This is equivalent to\n\ |
|
3214 executing commands from a script file, but without requiring the file to\n\ |
|
3215 be named @file{@var{file}.m}.\n\ |
|
3216 @end deftypefn") |
3021
|
3217 { |
|
3218 octave_value_list retval; |
|
3219 |
|
3220 int nargin = args.length (); |
|
3221 |
|
3222 if (nargin == 1) |
|
3223 { |
3523
|
3224 std::string file_name = args(0).string_value (); |
3021
|
3225 |
|
3226 if (! error_state) |
|
3227 { |
3523
|
3228 std::string file_full_name = file_ops::tilde_expand (file_name); |
3388
|
3229 |
|
3230 unwind_protect::begin_frame ("Fsource"); |
|
3231 |
|
3232 unwind_protect_str (curr_fcn_file_name); |
|
3233 unwind_protect_str (curr_fcn_file_full_name); |
|
3234 |
|
3235 curr_fcn_file_name = file_name; |
|
3236 curr_fcn_file_full_name = file_full_name; |
|
3237 |
|
3238 parse_fcn_file (file_full_name, true, true); |
3021
|
3239 |
|
3240 if (error_state) |
3388
|
3241 error ("source: error sourcing file `%s'", |
|
3242 file_full_name.c_str ()); |
|
3243 |
|
3244 unwind_protect::run_frame ("Fsource"); |
3021
|
3245 } |
|
3246 else |
|
3247 error ("source: expecting file name as argument"); |
|
3248 } |
|
3249 else |
|
3250 print_usage ("source"); |
|
3251 |
|
3252 return retval; |
|
3253 } |
|
3254 |
|
3255 octave_value_list |
3523
|
3256 feval (const std::string& name, const octave_value_list& args, int nargout) |
3156
|
3257 { |
|
3258 octave_value_list retval; |
|
3259 |
|
3260 octave_function *fcn = is_valid_function (name, "feval", 1); |
|
3261 |
|
3262 if (fcn) |
3544
|
3263 retval = fcn->do_multi_index_op (nargout, args); |
3156
|
3264 |
|
3265 return retval; |
|
3266 } |
|
3267 |
|
3268 octave_value_list |
3021
|
3269 feval (const octave_value_list& args, int nargout) |
|
3270 { |
|
3271 octave_value_list retval; |
|
3272 |
3156
|
3273 if (args.length () > 0) |
3021
|
3274 { |
3523
|
3275 std::string name = args(0).string_value (); |
3156
|
3276 |
|
3277 if (! error_state) |
3021
|
3278 { |
3156
|
3279 int tmp_nargin = args.length () - 1; |
|
3280 |
|
3281 octave_value_list tmp_args (tmp_nargin, octave_value ()); |
|
3282 |
|
3283 for (int i = 0; i < tmp_nargin; i++) |
3178
|
3284 tmp_args(i) = args(i+1); |
|
3285 |
|
3286 string_vector arg_names = args.name_tags (); |
|
3287 |
|
3288 if (! arg_names.empty ()) |
3156
|
3289 { |
3178
|
3290 assert (arg_names.length () == tmp_nargin + 1); |
|
3291 |
|
3292 string_vector tmp_arg_names (tmp_nargin); |
|
3293 |
|
3294 for (int i = 0; i < tmp_nargin; i++) |
|
3295 tmp_arg_names(i) = arg_names(i+1); |
|
3296 |
|
3297 tmp_args.stash_name_tags (tmp_arg_names); |
3156
|
3298 } |
|
3299 |
|
3300 retval = feval (name, tmp_args, nargout); |
3021
|
3301 } |
|
3302 } |
|
3303 |
|
3304 return retval; |
|
3305 } |
|
3306 |
|
3307 DEFUN (feval, args, nargout, |
3371
|
3308 "-*- texinfo -*-\n\ |
|
3309 @deftypefn {Built-in Function} {} feval (@var{name}, @dots{})\n\ |
|
3310 Evaluate the function named @var{name}. Any arguments after the first\n\ |
|
3311 are passed on to the named function. For example,\n\ |
|
3312 \n\ |
|
3313 @example\n\ |
|
3314 feval (\"acos\", -1)\n\ |
|
3315 @result{} 3.1416\n\ |
|
3316 @end example\n\ |
3021
|
3317 \n\ |
3371
|
3318 @noindent\n\ |
|
3319 calls the function @code{acos} with the argument @samp{-1}.\n\ |
|
3320 \n\ |
|
3321 The function @code{feval} is necessary in order to be able to write\n\ |
|
3322 functions that call user-supplied functions, because Octave does not\n\ |
|
3323 have a way to declare a pointer to a function (like C) or to declare a\n\ |
|
3324 special kind of variable that can be used to hold the name of a function\n\ |
|
3325 (like @code{EXTERNAL} in Fortran). Instead, you must refer to functions\n\ |
|
3326 by name, and use @code{feval} to call them.\n\ |
|
3327 @end deftypefn") |
3021
|
3328 { |
|
3329 octave_value_list retval; |
|
3330 |
|
3331 int nargin = args.length (); |
|
3332 |
|
3333 if (nargin > 0) |
|
3334 retval = feval (args, nargout); |
|
3335 else |
|
3336 print_usage ("feval"); |
|
3337 |
|
3338 return retval; |
|
3339 } |
|
3340 |
3099
|
3341 octave_value_list |
3523
|
3342 eval_string (const std::string& s, bool silent, int& parse_status, int nargout) |
3021
|
3343 { |
|
3344 unwind_protect::begin_frame ("eval_string"); |
|
3345 |
|
3346 unwind_protect_bool (get_input_from_eval_string); |
|
3347 unwind_protect_bool (input_from_command_line_file); |
|
3348 unwind_protect_ptr (global_command); |
|
3349 unwind_protect_str (current_eval_string); |
|
3350 |
|
3351 get_input_from_eval_string = true; |
|
3352 input_from_command_line_file = false; |
|
3353 current_eval_string = s; |
|
3354 |
|
3355 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3356 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
3357 |
|
3358 unwind_protect::add (restore_input_buffer, old_buf); |
|
3359 unwind_protect::add (delete_input_buffer, new_buf); |
|
3360 |
|
3361 switch_to_buffer (new_buf); |
|
3362 |
|
3363 unwind_protect_ptr (curr_sym_tab); |
|
3364 |
|
3365 reset_parser (); |
|
3366 |
|
3367 parse_status = yyparse (); |
|
3368 |
|
3369 // Important to reset the idea of where input is coming from before |
|
3370 // trying to eval the command we just parsed -- it might contain the |
|
3371 // name of an function file that still needs to be parsed! |
|
3372 |
|
3373 tree_statement_list *command = global_command; |
|
3374 |
|
3375 unwind_protect::run_frame ("eval_string"); |
|
3376 |
|
3377 octave_value_list retval; |
|
3378 |
|
3379 if (parse_status == 0 && command) |
|
3380 { |
|
3381 retval = command->eval (silent, nargout); |
|
3382 delete command; |
|
3383 } |
|
3384 |
|
3385 return retval; |
|
3386 } |
|
3387 |
|
3388 octave_value |
3523
|
3389 eval_string (const std::string& s, bool silent, int& parse_status) |
3021
|
3390 { |
|
3391 octave_value retval; |
|
3392 |
|
3393 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
|
3394 |
|
3395 if (! tmp.empty ()) |
|
3396 retval = tmp(0); |
|
3397 |
|
3398 return retval; |
|
3399 } |
|
3400 |
|
3401 static octave_value_list |
|
3402 eval_string (const octave_value& arg, bool silent, int& parse_status, |
|
3403 int nargout) |
|
3404 { |
3523
|
3405 std::string s = arg.string_value (); |
3021
|
3406 |
|
3407 if (error_state) |
|
3408 { |
3523
|
3409 error ("eval: expecting std::string argument"); |
3021
|
3410 return -1.0; |
|
3411 } |
|
3412 |
|
3413 return eval_string (s, silent, parse_status, nargout); |
|
3414 } |
|
3415 |
|
3416 DEFUN (eval, args, nargout, |
3371
|
3417 "-*- texinfo -*-\n\ |
|
3418 @deftypefn {Built-in Function} {} eval (@var{try}, @var{catch})\n\ |
|
3419 Parse the string @var{try} and evaluate it as if it were an Octave\n\ |
|
3420 program, returning the last value computed. If that fails, evaluate\n\ |
|
3421 the string @var{catch}. The string @var{try} is evaluated in the\n\ |
|
3422 current context, so any results remain available after @code{eval}\n\ |
|
3423 returns. For example,\n\ |
|
3424 \n\ |
|
3425 @example\n\ |
|
3426 @group\n\ |
|
3427 eval (\"a = 13\")\n\ |
|
3428 @print{} a = 13\n\ |
|
3429 @result{} 13\n\ |
|
3430 @end group\n\ |
|
3431 @end example\n\ |
3021
|
3432 \n\ |
3371
|
3433 In this case, the value of the evaluated expression is printed and it is\n\ |
|
3434 also returned returned from @code{eval}. Just as with any other\n\ |
|
3435 expression, you can turn printing off by ending the expression in a\n\ |
|
3436 semicolon. For example,\n\ |
|
3437 \n\ |
|
3438 @example\n\ |
|
3439 eval (\"a = 13;\")\n\ |
|
3440 @result{} 13\n\ |
|
3441 @end example\n\ |
|
3442 \n\ |
|
3443 In this example, the variable @code{a} has been given the value 13, but\n\ |
|
3444 the value of the expression is not printed. You can also turn off\n\ |
|
3445 automatic printing for all expressions executed by @code{eval} using the\n\ |
|
3446 variable @code{default_eval_print_flag}.\n\ |
|
3447 @end deftypefn") |
3021
|
3448 { |
|
3449 octave_value_list retval; |
|
3450 |
|
3451 int nargin = args.length (); |
|
3452 |
|
3453 if (nargin > 0) |
|
3454 { |
|
3455 unwind_protect::begin_frame ("Feval"); |
|
3456 |
|
3457 if (nargin > 1) |
|
3458 { |
|
3459 unwind_protect_bool (buffer_error_messages); |
|
3460 buffer_error_messages = true; |
|
3461 } |
|
3462 |
|
3463 int parse_status = 0; |
|
3464 |
|
3465 retval = eval_string (args(0), ! Vdefault_eval_print_flag, |
|
3466 parse_status, nargout); |
|
3467 |
|
3468 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
3469 { |
|
3470 error_state = 0; |
|
3471 |
|
3472 // Set up for letting the user print any messages from |
|
3473 // errors that occurred in the first part of this eval(). |
|
3474 |
|
3475 buffer_error_messages = false; |
|
3476 bind_global_error_variable (); |
|
3477 unwind_protect::add (clear_global_error_variable, 0); |
|
3478 |
|
3479 eval_string (args(1), 0, parse_status, nargout); |
|
3480 |
|
3481 retval = octave_value_list (); |
|
3482 } |
|
3483 |
|
3484 unwind_protect::run_frame ("Feval"); |
|
3485 } |
|
3486 else |
|
3487 print_usage ("eval"); |
|
3488 |
|
3489 return retval; |
|
3490 } |
|
3491 |
|
3492 static int |
|
3493 default_eval_print_flag (void) |
|
3494 { |
|
3495 Vdefault_eval_print_flag = check_preference ("default_eval_print_flag"); |
|
3496 |
|
3497 return 0; |
|
3498 } |
|
3499 |
2166
|
3500 static int |
|
3501 warn_assign_as_truth_value (void) |
|
3502 { |
|
3503 Vwarn_assign_as_truth_value |
|
3504 = check_preference ("warn_assign_as_truth_value"); |
|
3505 |
|
3506 return 0; |
|
3507 } |
|
3508 |
|
3509 static int |
|
3510 warn_function_name_clash (void) |
|
3511 { |
|
3512 Vwarn_function_name_clash = check_preference ("warn_function_name_clash"); |
|
3513 |
|
3514 return 0; |
|
3515 } |
|
3516 |
|
3517 static int |
3162
|
3518 warn_future_time_stamp (void) |
|
3519 { |
|
3520 Vwarn_future_time_stamp = check_preference ("warn_future_time_stamp"); |
|
3521 |
|
3522 return 0; |
|
3523 } |
|
3524 |
|
3525 static int |
2166
|
3526 warn_missing_semicolon (void) |
|
3527 { |
|
3528 Vwarn_missing_semicolon = check_preference ("warn_missing_semicolon"); |
|
3529 |
|
3530 return 0; |
|
3531 } |
|
3532 |
2764
|
3533 static int |
|
3534 warn_variable_switch_label (void) |
|
3535 { |
|
3536 Vwarn_variable_switch_label |
|
3537 = check_preference ("warn_variable_switch_label"); |
|
3538 |
|
3539 return 0; |
|
3540 } |
|
3541 |
2166
|
3542 void |
|
3543 symbols_of_parse (void) |
|
3544 { |
3258
|
3545 DEFVAR (default_eval_print_flag, 1.0, default_eval_print_flag, |
3371
|
3546 "-*- texinfo -*-\n\ |
|
3547 @defvr {Built-in Variable} default_eval_print_flag\n\ |
|
3548 If the value of this variable is nonzero, Octave prints the results of\n\ |
|
3549 commands executed by @code{eval} that do not end with semicolons. If it\n\ |
|
3550 is zero, automatic printing is suppressed. The default value is 1.\n\ |
|
3551 @end defvr"); |
3021
|
3552 |
3258
|
3553 DEFVAR (warn_assign_as_truth_value, 1.0, warn_assign_as_truth_value, |
3366
|
3554 "-*- texinfo -*-\n\ |
|
3555 @defvr {Built-in Variable} warn_assign_as_truth_value\n\ |
|
3556 If the value of @code{warn_assign_as_truth_value} is nonzero, a\n\ |
|
3557 warning is issued for statements like\n\ |
|
3558 \n\ |
|
3559 @example\n\ |
|
3560 if (s = t)\n\ |
|
3561 ...\n\ |
|
3562 @end example\n\ |
|
3563 \n\ |
|
3564 @noindent\n\ |
|
3565 since such statements are not common, and it is likely that the intent\n\ |
|
3566 was to write\n\ |
|
3567 \n\ |
|
3568 @example\n\ |
|
3569 if (s == t)\n\ |
|
3570 ...\n\ |
|
3571 @end example\n\ |
|
3572 \n\ |
|
3573 @noindent\n\ |
|
3574 instead.\n\ |
|
3575 \n\ |
|
3576 There are times when it is useful to write code that contains\n\ |
|
3577 assignments within the condition of a @code{while} or @code{if}\n\ |
|
3578 statement. For example, statements like\n\ |
|
3579 \n\ |
|
3580 @example\n\ |
|
3581 while (c = getc())\n\ |
|
3582 ...\n\ |
|
3583 @end example\n\ |
|
3584 \n\ |
|
3585 @noindent\n\ |
|
3586 are common in C programming.\n\ |
|
3587 \n\ |
|
3588 It is possible to avoid all warnings about such statements by setting\n\ |
|
3589 @code{warn_assign_as_truth_value} to 0, but that may also\n\ |
|
3590 let real errors like\n\ |
|
3591 \n\ |
|
3592 @example\n\ |
|
3593 if (x = 1) # intended to test (x == 1)!\n\ |
|
3594 ...\n\ |
|
3595 @end example\n\ |
|
3596 \n\ |
|
3597 @noindent\n\ |
|
3598 slip by.\n\ |
|
3599 \n\ |
|
3600 In such cases, it is possible suppress errors for specific statements by\n\ |
|
3601 writing them with an extra set of parentheses. For example, writing the\n\ |
|
3602 previous example as\n\ |
|
3603 \n\ |
|
3604 @example\n\ |
|
3605 while ((c = getc()))\n\ |
|
3606 ...\n\ |
|
3607 @end example\n\ |
|
3608 \n\ |
|
3609 @noindent\n\ |
|
3610 will prevent the warning from being printed for this statement, while\n\ |
|
3611 allowing Octave to warn about other assignments used in conditional\n\ |
|
3612 contexts.\n\ |
|
3613 \n\ |
|
3614 The default value of @code{warn_assign_as_truth_value} is 1.\n\ |
|
3615 @end defvr"); |
2166
|
3616 |
3258
|
3617 DEFVAR (warn_function_name_clash, 1.0, warn_function_name_clash, |
3371
|
3618 "-*- texinfo -*-\n\ |
|
3619 @defvr {Built-in Variable} warn_function_name_clash\n\ |
|
3620 If the value of @code{warn_function_name_clash} is nonzero, a warning is\n\ |
|
3621 issued when Octave finds that the name of a function defined in a\n\ |
|
3622 function file differs from the name of the file. (If the names\n\ |
|
3623 disagree, the name declared inside the file is ignored.) If the value\n\ |
|
3624 is 0, the warning is omitted. The default value is 1.\n\ |
|
3625 @end defvr"); |
2166
|
3626 |
3258
|
3627 DEFVAR (warn_future_time_stamp, 1.0, warn_future_time_stamp, |
3446
|
3628 "-*- texinfo -*-\n\ |
|
3629 @defvr {Built-in Variable} warn_future_time_stamp\n\ |
|
3630 If the value of this variable is nonzero, Octave will print a warning\n\ |
|
3631 if it finds a function file with a time stamp that is in the future.\n\ |
|
3632 @end defvr"); |
3162
|
3633 |
3258
|
3634 DEFVAR (warn_missing_semicolon, 0.0, warn_missing_semicolon, |
3371
|
3635 "-*- texinfo -*-\n\ |
|
3636 @defvr {Built-in Variable} warn_missing_semicolon\n\ |
|
3637 If the value of this variable is nonzero, Octave will warn when\n\ |
|
3638 statements in function definitions don't end in semicolons. The default\n\ |
|
3639 value is 0.\n\ |
|
3640 @end defvr"); |
2764
|
3641 |
3258
|
3642 DEFVAR (warn_variable_switch_label, 0.0, warn_variable_switch_label, |
3366
|
3643 "-*- texinfo -*-\n\ |
|
3644 @defvr {Built-in Variable} warn_variable_switch_label\n\ |
|
3645 If the value of this variable is nonzero, Octave will print a warning if\n\ |
|
3646 a switch label is not a constant or constant expression\n\ |
|
3647 @end defvr"); |
3347
|
3648 |
2166
|
3649 } |
|
3650 |
1994
|
3651 /* |
|
3652 ;;; Local Variables: *** |
|
3653 ;;; mode: text *** |
|
3654 ;;; End: *** |
|
3655 */ |