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