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