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