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