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