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 ()) |
|
1785 { |
5861
|
1786 symbol_record *sr = fcn_sym_tab->lookup ("__retval__", true); |
4935
|
1787 |
|
1788 tree_expression *e = stmt->expression (); |
|
1789 |
|
1790 tree_identifier *id = new tree_identifier (sr); |
|
1791 |
|
1792 tree_simple_assignment *asn = new tree_simple_assignment (id, e); |
|
1793 |
|
1794 stmt->set_expression (asn); |
|
1795 |
|
1796 stmt->set_print_flag (false); |
|
1797 |
5775
|
1798 // FIXME -- would like to delete old_stmt here or |
4935
|
1799 // replace expression inside it with the new expression we just |
|
1800 // created so we don't have to create a new statement at all. |
|
1801 |
|
1802 id = new tree_identifier (sr); |
6215
|
1803 tree_decl_elt *elt = new tree_decl_elt (id); |
|
1804 |
|
1805 ret_list = new tree_parameter_list (elt); |
4935
|
1806 } |
|
1807 |
|
1808 tree_statement_list *body = new tree_statement_list (stmt); |
|
1809 |
|
1810 body->mark_as_function_body (); |
|
1811 |
5861
|
1812 tree_anon_fcn_handle *retval |
|
1813 = new tree_anon_fcn_handle (param_list, ret_list, body, fcn_sym_tab, l, c); |
4935
|
1814 |
|
1815 return retval; |
|
1816 } |
|
1817 |
5161
|
1818 static void |
|
1819 maybe_warn_associativity_change (tree_expression *op) |
|
1820 { |
5781
|
1821 if (op->paren_count () == 0 && op->is_binary_expression ()) |
5161
|
1822 { |
|
1823 tree_binary_expression *e |
|
1824 = dynamic_cast<tree_binary_expression *> (op); |
|
1825 |
|
1826 octave_value::binary_op op_type = e->op_type (); |
|
1827 |
|
1828 if (op_type == octave_value::op_pow |
|
1829 || op_type == octave_value::op_el_pow) |
|
1830 { |
|
1831 std::string op_str = octave_value::binary_op_as_string (op_type); |
|
1832 |
5781
|
1833 warning_with_id |
|
1834 ("Octave:associativity-change", |
|
1835 "meaning may have changed due to change in associativity for %s operator", op_str.c_str ()); |
5161
|
1836 } |
|
1837 } |
|
1838 } |
|
1839 |
666
|
1840 // Build a binary expression. |
|
1841 |
578
|
1842 static tree_expression * |
|
1843 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1844 tree_expression *op2) |
|
1845 { |
2883
|
1846 octave_value::binary_op t = octave_value::unknown_binary_op; |
1623
|
1847 |
578
|
1848 switch (op) |
|
1849 { |
|
1850 case POW: |
3525
|
1851 t = octave_value::op_pow; |
5161
|
1852 maybe_warn_associativity_change (op1); |
578
|
1853 break; |
777
|
1854 |
578
|
1855 case EPOW: |
3525
|
1856 t = octave_value::op_el_pow; |
5161
|
1857 maybe_warn_associativity_change (op1); |
578
|
1858 break; |
777
|
1859 |
578
|
1860 case '+': |
3525
|
1861 t = octave_value::op_add; |
578
|
1862 break; |
777
|
1863 |
578
|
1864 case '-': |
3525
|
1865 t = octave_value::op_sub; |
578
|
1866 break; |
777
|
1867 |
578
|
1868 case '*': |
3525
|
1869 t = octave_value::op_mul; |
578
|
1870 break; |
777
|
1871 |
578
|
1872 case '/': |
3525
|
1873 t = octave_value::op_div; |
578
|
1874 break; |
777
|
1875 |
578
|
1876 case EMUL: |
3525
|
1877 t = octave_value::op_el_mul; |
578
|
1878 break; |
777
|
1879 |
578
|
1880 case EDIV: |
3525
|
1881 t = octave_value::op_el_div; |
578
|
1882 break; |
777
|
1883 |
578
|
1884 case LEFTDIV: |
3525
|
1885 t = octave_value::op_ldiv; |
578
|
1886 break; |
777
|
1887 |
578
|
1888 case ELEFTDIV: |
3525
|
1889 t = octave_value::op_el_ldiv; |
578
|
1890 break; |
777
|
1891 |
2899
|
1892 case LSHIFT: |
3525
|
1893 t = octave_value::op_lshift; |
2899
|
1894 break; |
|
1895 |
|
1896 case RSHIFT: |
3525
|
1897 t = octave_value::op_rshift; |
2899
|
1898 break; |
|
1899 |
578
|
1900 case EXPR_LT: |
3525
|
1901 t = octave_value::op_lt; |
578
|
1902 break; |
777
|
1903 |
578
|
1904 case EXPR_LE: |
3525
|
1905 t = octave_value::op_le; |
578
|
1906 break; |
777
|
1907 |
578
|
1908 case EXPR_EQ: |
3525
|
1909 t = octave_value::op_eq; |
578
|
1910 break; |
777
|
1911 |
578
|
1912 case EXPR_GE: |
3525
|
1913 t = octave_value::op_ge; |
578
|
1914 break; |
777
|
1915 |
578
|
1916 case EXPR_GT: |
3525
|
1917 t = octave_value::op_gt; |
578
|
1918 break; |
777
|
1919 |
578
|
1920 case EXPR_NE: |
3525
|
1921 t = octave_value::op_ne; |
578
|
1922 break; |
777
|
1923 |
578
|
1924 case EXPR_AND: |
3525
|
1925 t = octave_value::op_el_and; |
578
|
1926 break; |
777
|
1927 |
578
|
1928 case EXPR_OR: |
3525
|
1929 t = octave_value::op_el_or; |
5781
|
1930 if (op2->paren_count () == 0 && op2->is_binary_expression ()) |
4023
|
1931 { |
|
1932 tree_binary_expression *e |
|
1933 = dynamic_cast<tree_binary_expression *> (op2); |
|
1934 |
|
1935 if (e->op_type () == octave_value::op_el_and) |
5781
|
1936 warning_with_id |
|
1937 ("Octave:precedence-change", |
|
1938 "meaning may have changed due to change in precedence for & and | operators"); |
4023
|
1939 } |
578
|
1940 break; |
777
|
1941 |
578
|
1942 default: |
|
1943 panic_impossible (); |
|
1944 break; |
|
1945 } |
|
1946 |
|
1947 int l = tok_val->line (); |
|
1948 int c = tok_val->column (); |
|
1949 |
2533
|
1950 tree_binary_expression *e |
|
1951 = new tree_binary_expression (op1, op2, l, c, t); |
1623
|
1952 |
2533
|
1953 return fold (e); |
578
|
1954 } |
|
1955 |
2375
|
1956 // Build a boolean expression. |
666
|
1957 |
578
|
1958 static tree_expression * |
2375
|
1959 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
1960 tree_expression *op2) |
578
|
1961 { |
2375
|
1962 tree_boolean_expression::type t; |
|
1963 |
578
|
1964 switch (op) |
|
1965 { |
2375
|
1966 case EXPR_AND_AND: |
2805
|
1967 t = tree_boolean_expression::bool_and; |
578
|
1968 break; |
777
|
1969 |
2375
|
1970 case EXPR_OR_OR: |
2805
|
1971 t = tree_boolean_expression::bool_or; |
5781
|
1972 if (op2->paren_count () == 0 && op2->is_boolean_expression ()) |
4023
|
1973 { |
|
1974 tree_boolean_expression *e |
|
1975 = dynamic_cast<tree_boolean_expression *> (op2); |
|
1976 |
|
1977 if (e->op_type () == tree_boolean_expression::bool_and) |
5781
|
1978 warning_with_id |
|
1979 ("Octave:precedence-change", |
|
1980 "meaning may have changed due to change in precedence for && and || operators"); |
4023
|
1981 } |
578
|
1982 break; |
777
|
1983 |
578
|
1984 default: |
|
1985 panic_impossible (); |
|
1986 break; |
|
1987 } |
|
1988 |
|
1989 int l = tok_val->line (); |
|
1990 int c = tok_val->column (); |
|
1991 |
2533
|
1992 tree_boolean_expression *e |
|
1993 = new tree_boolean_expression (op1, op2, l, c, t); |
2375
|
1994 |
2533
|
1995 return fold (e); |
578
|
1996 } |
|
1997 |
2375
|
1998 // Build a prefix expression. |
666
|
1999 |
578
|
2000 static tree_expression * |
2960
|
2001 make_prefix_op (int op, tree_expression *op1, token *tok_val) |
578
|
2002 { |
3203
|
2003 octave_value::unary_op t = octave_value::unknown_unary_op; |
2375
|
2004 |
578
|
2005 switch (op) |
|
2006 { |
2960
|
2007 case EXPR_NOT: |
3525
|
2008 t = octave_value::op_not; |
2960
|
2009 break; |
|
2010 |
4965
|
2011 case '+': |
|
2012 t = octave_value::op_uplus; |
|
2013 break; |
|
2014 |
2960
|
2015 case '-': |
3525
|
2016 t = octave_value::op_uminus; |
2960
|
2017 break; |
|
2018 |
578
|
2019 case PLUS_PLUS: |
3525
|
2020 t = octave_value::op_incr; |
578
|
2021 break; |
777
|
2022 |
578
|
2023 case MINUS_MINUS: |
3525
|
2024 t = octave_value::op_decr; |
578
|
2025 break; |
777
|
2026 |
578
|
2027 default: |
|
2028 panic_impossible (); |
|
2029 break; |
|
2030 } |
|
2031 |
|
2032 int l = tok_val->line (); |
|
2033 int c = tok_val->column (); |
|
2034 |
3292
|
2035 tree_prefix_expression *e |
|
2036 = new tree_prefix_expression (op1, l, c, t); |
|
2037 |
|
2038 return fold (e); |
578
|
2039 } |
|
2040 |
2375
|
2041 // Build a postfix expression. |
666
|
2042 |
578
|
2043 static tree_expression * |
2960
|
2044 make_postfix_op (int op, tree_expression *op1, token *tok_val) |
578
|
2045 { |
3203
|
2046 octave_value::unary_op t = octave_value::unknown_unary_op; |
1623
|
2047 |
578
|
2048 switch (op) |
|
2049 { |
2960
|
2050 case QUOTE: |
3525
|
2051 t = octave_value::op_hermitian; |
2960
|
2052 break; |
|
2053 |
|
2054 case TRANSPOSE: |
3525
|
2055 t = octave_value::op_transpose; |
2960
|
2056 break; |
|
2057 |
2375
|
2058 case PLUS_PLUS: |
3525
|
2059 t = octave_value::op_incr; |
578
|
2060 break; |
777
|
2061 |
2375
|
2062 case MINUS_MINUS: |
3525
|
2063 t = octave_value::op_decr; |
578
|
2064 break; |
777
|
2065 |
578
|
2066 default: |
|
2067 panic_impossible (); |
|
2068 break; |
|
2069 } |
|
2070 |
|
2071 int l = tok_val->line (); |
|
2072 int c = tok_val->column (); |
|
2073 |
3292
|
2074 tree_postfix_expression *e |
|
2075 = new tree_postfix_expression (op1, l, c, t); |
|
2076 |
|
2077 return fold (e); |
1623
|
2078 } |
|
2079 |
|
2080 // Build an unwind-protect command. |
|
2081 |
|
2082 static tree_command * |
|
2083 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
3665
|
2084 tree_statement_list *cleanup, token *end_tok, |
|
2085 octave_comment_list *lc, octave_comment_list *mc) |
1623
|
2086 { |
|
2087 tree_command *retval = 0; |
|
2088 |
2857
|
2089 if (end_token_ok (end_tok, token::unwind_protect_end)) |
1623
|
2090 { |
3665
|
2091 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2092 |
1623
|
2093 int l = unwind_tok->line (); |
|
2094 int c = unwind_tok->column (); |
|
2095 |
3665
|
2096 retval = new tree_unwind_protect_command (body, cleanup, |
|
2097 lc, mc, tc, l, c); |
1623
|
2098 } |
|
2099 |
|
2100 return retval; |
|
2101 } |
|
2102 |
|
2103 // Build a try-catch command. |
|
2104 |
|
2105 static tree_command * |
|
2106 make_try_command (token *try_tok, tree_statement_list *body, |
3665
|
2107 tree_statement_list *cleanup, token *end_tok, |
|
2108 octave_comment_list *lc, octave_comment_list *mc) |
1623
|
2109 { |
|
2110 tree_command *retval = 0; |
|
2111 |
2857
|
2112 if (end_token_ok (end_tok, token::try_catch_end)) |
1623
|
2113 { |
3665
|
2114 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2115 |
1623
|
2116 int l = try_tok->line (); |
|
2117 int c = try_tok->column (); |
|
2118 |
3665
|
2119 retval = new tree_try_catch_command (body, cleanup, |
|
2120 lc, mc, tc, l, c); |
1623
|
2121 } |
|
2122 |
|
2123 return retval; |
|
2124 } |
|
2125 |
|
2126 // Build a while command. |
|
2127 |
|
2128 static tree_command * |
|
2129 make_while_command (token *while_tok, tree_expression *expr, |
3665
|
2130 tree_statement_list *body, token *end_tok, |
|
2131 octave_comment_list *lc) |
1623
|
2132 { |
|
2133 tree_command *retval = 0; |
|
2134 |
|
2135 maybe_warn_assign_as_truth_value (expr); |
|
2136 |
2857
|
2137 if (end_token_ok (end_tok, token::while_end)) |
1623
|
2138 { |
3665
|
2139 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2140 |
1826
|
2141 lexer_flags.looping--; |
1623
|
2142 |
|
2143 int l = while_tok->line (); |
|
2144 int c = while_tok->column (); |
|
2145 |
3665
|
2146 retval = new tree_while_command (expr, body, lc, tc, l, c); |
1623
|
2147 } |
|
2148 |
|
2149 return retval; |
|
2150 } |
|
2151 |
3484
|
2152 // Build a do-until command. |
|
2153 |
|
2154 static tree_command * |
|
2155 make_do_until_command (token *do_tok, tree_statement_list *body, |
3665
|
2156 tree_expression *expr, octave_comment_list *lc) |
3484
|
2157 { |
|
2158 tree_command *retval = 0; |
|
2159 |
|
2160 maybe_warn_assign_as_truth_value (expr); |
|
2161 |
3665
|
2162 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2163 |
3484
|
2164 lexer_flags.looping--; |
|
2165 |
|
2166 int l = do_tok->line (); |
|
2167 int c = do_tok->column (); |
|
2168 |
3665
|
2169 retval = new tree_do_until_command (expr, body, lc, tc, l, c); |
3484
|
2170 |
|
2171 return retval; |
|
2172 } |
|
2173 |
1623
|
2174 // Build a for command. |
|
2175 |
|
2176 static tree_command * |
2970
|
2177 make_for_command (token *for_tok, tree_argument_list *lhs, |
1623
|
2178 tree_expression *expr, tree_statement_list *body, |
3665
|
2179 token *end_tok, octave_comment_list *lc) |
1623
|
2180 { |
|
2181 tree_command *retval = 0; |
|
2182 |
2857
|
2183 if (end_token_ok (end_tok, token::for_end)) |
1623
|
2184 { |
3665
|
2185 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2186 |
1826
|
2187 lexer_flags.looping--; |
1623
|
2188 |
|
2189 int l = for_tok->line (); |
|
2190 int c = for_tok->column (); |
|
2191 |
2970
|
2192 if (lhs->length () == 1) |
|
2193 { |
|
2194 tree_expression *tmp = lhs->remove_front (); |
|
2195 |
3665
|
2196 retval = new tree_simple_for_command (tmp, expr, body, |
|
2197 lc, tc, l, c); |
2970
|
2198 |
|
2199 delete lhs; |
|
2200 } |
|
2201 else |
3665
|
2202 retval = new tree_complex_for_command (lhs, expr, body, |
|
2203 lc, tc, l, c); |
1623
|
2204 } |
|
2205 |
|
2206 return retval; |
|
2207 } |
|
2208 |
4207
|
2209 // Build a break command. |
|
2210 |
|
2211 static tree_command * |
|
2212 make_break_command (token *break_tok) |
1623
|
2213 { |
4207
|
2214 tree_command *retval = 0; |
1623
|
2215 |
2620
|
2216 int l = break_tok->line (); |
|
2217 int c = break_tok->column (); |
|
2218 |
3489
|
2219 if (lexer_flags.looping || lexer_flags.defining_func |
3877
|
2220 || reading_script_file || evaluating_function_body |
|
2221 || evaluating_looping_command) |
4207
|
2222 retval = new tree_break_command (l, c); |
1623
|
2223 else |
4207
|
2224 retval = new tree_no_op_command ("break", l, c); |
1623
|
2225 |
|
2226 return retval; |
|
2227 } |
|
2228 |
4207
|
2229 // Build a continue command. |
|
2230 |
|
2231 static tree_command * |
|
2232 make_continue_command (token *continue_tok) |
1623
|
2233 { |
4207
|
2234 tree_command *retval = 0; |
1623
|
2235 |
2620
|
2236 int l = continue_tok->line (); |
|
2237 int c = continue_tok->column (); |
|
2238 |
3877
|
2239 if (lexer_flags.looping || evaluating_looping_command) |
4207
|
2240 retval = new tree_continue_command (l, c); |
1623
|
2241 else |
4207
|
2242 retval = new tree_no_op_command ("continue", l, c); |
1623
|
2243 |
|
2244 return retval; |
|
2245 } |
|
2246 |
4207
|
2247 // Build a return command. |
|
2248 |
|
2249 static tree_command * |
|
2250 make_return_command (token *return_tok) |
1623
|
2251 { |
4207
|
2252 tree_command *retval = 0; |
1623
|
2253 |
2620
|
2254 int l = return_tok->line (); |
|
2255 int c = return_tok->column (); |
|
2256 |
3489
|
2257 if (lexer_flags.defining_func || reading_script_file |
|
2258 || evaluating_function_body) |
4207
|
2259 retval = new tree_return_command (l, c); |
1623
|
2260 else |
4207
|
2261 retval = new tree_no_op_command ("return", l, c); |
1623
|
2262 |
|
2263 return retval; |
|
2264 } |
|
2265 |
|
2266 // Start an if command. |
|
2267 |
|
2268 static tree_if_command_list * |
|
2269 start_if_command (tree_expression *expr, tree_statement_list *list) |
|
2270 { |
|
2271 maybe_warn_assign_as_truth_value (expr); |
|
2272 |
|
2273 tree_if_clause *t = new tree_if_clause (expr, list); |
|
2274 |
|
2275 return new tree_if_command_list (t); |
|
2276 } |
|
2277 |
|
2278 // Finish an if command. |
|
2279 |
|
2280 static tree_if_command * |
|
2281 finish_if_command (token *if_tok, tree_if_command_list *list, |
3665
|
2282 token *end_tok, octave_comment_list *lc) |
1623
|
2283 { |
|
2284 tree_if_command *retval = 0; |
|
2285 |
2857
|
2286 if (end_token_ok (end_tok, token::if_end)) |
1623
|
2287 { |
3665
|
2288 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2289 |
1623
|
2290 int l = if_tok->line (); |
|
2291 int c = if_tok->column (); |
|
2292 |
3665
|
2293 retval = new tree_if_command (list, lc, tc, l, c); |
1623
|
2294 } |
|
2295 |
|
2296 return retval; |
|
2297 } |
|
2298 |
|
2299 // Build an elseif clause. |
|
2300 |
|
2301 static tree_if_clause * |
3665
|
2302 make_elseif_clause (tree_expression *expr, tree_statement_list *list, |
|
2303 octave_comment_list *lc) |
1623
|
2304 { |
|
2305 maybe_warn_assign_as_truth_value (expr); |
|
2306 |
3665
|
2307 return new tree_if_clause (expr, list, lc); |
1623
|
2308 } |
|
2309 |
2764
|
2310 // Finish a switch command. |
|
2311 |
|
2312 static tree_switch_command * |
|
2313 finish_switch_command (token *switch_tok, tree_expression *expr, |
3665
|
2314 tree_switch_case_list *list, token *end_tok, |
|
2315 octave_comment_list *lc) |
2764
|
2316 { |
|
2317 tree_switch_command *retval = 0; |
|
2318 |
2857
|
2319 if (end_token_ok (end_tok, token::switch_end)) |
2764
|
2320 { |
3665
|
2321 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2322 |
2764
|
2323 int l = switch_tok->line (); |
|
2324 int c = switch_tok->column (); |
|
2325 |
3665
|
2326 retval = new tree_switch_command (expr, list, lc, tc, l, c); |
2764
|
2327 } |
|
2328 |
|
2329 return retval; |
|
2330 } |
|
2331 |
|
2332 // Build a switch case. |
|
2333 |
|
2334 static tree_switch_case * |
3665
|
2335 make_switch_case (tree_expression *expr, tree_statement_list *list, |
|
2336 octave_comment_list *lc) |
2764
|
2337 { |
|
2338 maybe_warn_variable_switch_label (expr); |
|
2339 |
3665
|
2340 return new tree_switch_case (expr, list, lc); |
2764
|
2341 } |
|
2342 |
1623
|
2343 // Build an assignment to a variable. |
|
2344 |
|
2345 static tree_expression * |
2970
|
2346 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
2347 tree_expression *rhs) |
1623
|
2348 { |
2970
|
2349 tree_expression *retval = 0; |
|
2350 |
2883
|
2351 octave_value::assign_op t = octave_value::unknown_assign_op; |
|
2352 |
|
2353 switch (op) |
|
2354 { |
|
2355 case '=': |
3525
|
2356 t = octave_value::op_asn_eq; |
2883
|
2357 break; |
|
2358 |
|
2359 case ADD_EQ: |
3525
|
2360 t = octave_value::op_add_eq; |
2883
|
2361 break; |
|
2362 |
|
2363 case SUB_EQ: |
3525
|
2364 t = octave_value::op_sub_eq; |
2883
|
2365 break; |
|
2366 |
|
2367 case MUL_EQ: |
3525
|
2368 t = octave_value::op_mul_eq; |
2883
|
2369 break; |
|
2370 |
|
2371 case DIV_EQ: |
3525
|
2372 t = octave_value::op_div_eq; |
2883
|
2373 break; |
|
2374 |
3204
|
2375 case LEFTDIV_EQ: |
3525
|
2376 t = octave_value::op_ldiv_eq; |
3204
|
2377 break; |
|
2378 |
4018
|
2379 case POW_EQ: |
|
2380 t = octave_value::op_pow_eq; |
|
2381 break; |
|
2382 |
2899
|
2383 case LSHIFT_EQ: |
3525
|
2384 t = octave_value::op_lshift_eq; |
2899
|
2385 break; |
|
2386 |
|
2387 case RSHIFT_EQ: |
3525
|
2388 t = octave_value::op_rshift_eq; |
2899
|
2389 break; |
|
2390 |
2883
|
2391 case EMUL_EQ: |
3525
|
2392 t = octave_value::op_el_mul_eq; |
2883
|
2393 break; |
|
2394 |
|
2395 case EDIV_EQ: |
3525
|
2396 t = octave_value::op_el_div_eq; |
2883
|
2397 break; |
|
2398 |
3204
|
2399 case ELEFTDIV_EQ: |
3525
|
2400 t = octave_value::op_el_ldiv_eq; |
3204
|
2401 break; |
|
2402 |
4018
|
2403 case EPOW_EQ: |
|
2404 t = octave_value::op_el_pow_eq; |
|
2405 break; |
|
2406 |
2883
|
2407 case AND_EQ: |
3525
|
2408 t = octave_value::op_el_and_eq; |
2883
|
2409 break; |
|
2410 |
|
2411 case OR_EQ: |
3525
|
2412 t = octave_value::op_el_or_eq; |
2883
|
2413 break; |
|
2414 |
|
2415 default: |
|
2416 panic_impossible (); |
|
2417 break; |
|
2418 } |
|
2419 |
1623
|
2420 int l = eq_tok->line (); |
|
2421 int c = eq_tok->column (); |
|
2422 |
5841
|
2423 if (lhs->is_simple_assign_lhs ()) |
666
|
2424 { |
2970
|
2425 tree_expression *tmp = lhs->remove_front (); |
|
2426 |
|
2427 retval = new tree_simple_assignment (tmp, rhs, false, l, c, t); |
|
2428 |
|
2429 delete lhs; |
666
|
2430 } |
|
2431 else |
3208
|
2432 return new tree_multi_assignment (lhs, rhs, false, l, c, t); |
666
|
2433 |
|
2434 return retval; |
|
2435 } |
751
|
2436 |
1623
|
2437 // Begin defining a function. |
|
2438 |
2891
|
2439 static octave_user_function * |
|
2440 start_function (tree_parameter_list *param_list, tree_statement_list *body) |
1623
|
2441 { |
|
2442 body->mark_as_function_body (); |
|
2443 |
2891
|
2444 // We'll fill in the return list later. |
|
2445 |
|
2446 octave_user_function *fcn |
|
2447 = new octave_user_function (param_list, 0, body, curr_sym_tab); |
1623
|
2448 |
3665
|
2449 if (fcn) |
|
2450 { |
|
2451 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2452 |
|
2453 fcn->stash_trailing_comment (tc); |
|
2454 } |
|
2455 |
1623
|
2456 return fcn; |
|
2457 } |
|
2458 |
|
2459 // Do most of the work for defining a function. |
|
2460 |
2891
|
2461 static octave_user_function * |
4872
|
2462 frob_function (const std::string& fname, octave_user_function *fcn) |
1623
|
2463 { |
4872
|
2464 std::string id_name = fname; |
1623
|
2465 |
|
2466 // If input is coming from a file, issue a warning if the name of |
|
2467 // the file does not match the name of the function stated in the |
|
2468 // file. Matlab doesn't provide a diagnostic (it ignores the stated |
|
2469 // name). |
|
2470 |
5484
|
2471 if (reading_fcn_file || autoloading) |
1623
|
2472 { |
5484
|
2473 if (! (lexer_flags.parsing_nested_function || autoloading) |
4238
|
2474 && curr_fcn_file_name != id_name) |
1623
|
2475 { |
5781
|
2476 warning_with_id |
|
2477 ("Octave:function-name-clash", |
|
2478 "function name `%s' does not agree with function file name `%s'", |
|
2479 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2480 |
4872
|
2481 id_name = curr_fcn_file_name; |
1623
|
2482 } |
|
2483 |
3712
|
2484 octave_time now; |
3162
|
2485 |
4343
|
2486 fcn->stash_fcn_file_name (curr_fcn_file_full_name); |
3162
|
2487 fcn->stash_fcn_file_time (now); |
1623
|
2488 fcn->mark_as_system_fcn_file (); |
3162
|
2489 |
6323
|
2490 if (fcn_file_from_relative_lookup) |
|
2491 fcn->mark_relative (); |
|
2492 |
|
2493 if (lexer_flags.parsing_nested_function) |
|
2494 fcn->stash_parent_fcn_name (parent_function_name); |
|
2495 |
5781
|
2496 std::string nm = fcn->fcn_file_name (); |
|
2497 |
|
2498 file_stat fs (nm); |
|
2499 |
|
2500 if (fs && fs.is_newer (now)) |
|
2501 warning_with_id ("Octave:future-time-stamp", |
|
2502 "time stamp for `%s' is in the future", nm.c_str ()); |
1623
|
2503 } |
|
2504 else if (! (input_from_tmp_history_file || input_from_startup_file) |
|
2505 && reading_script_file |
1755
|
2506 && curr_fcn_file_name == id_name) |
1623
|
2507 { |
|
2508 warning ("function `%s' defined within script file `%s'", |
1755
|
2509 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2510 } |
|
2511 |
4872
|
2512 fcn->stash_function_name (id_name); |
|
2513 |
5006
|
2514 // Enter the new function in fbi_sym_tab. If there is already a |
|
2515 // variable of the same name in the current symbol table, we won't |
|
2516 // find the new function when we try to call it, so we need to clear |
|
2517 // the old symbol from the current symbol table. Note that this |
|
2518 // means that for things like |
|
2519 // |
|
2520 // function f () eval ("function g () 1, end"); end |
|
2521 // g = 13; |
|
2522 // f (); |
|
2523 // g |
|
2524 // |
|
2525 // G will still refer to the variable G (with value 13) rather |
|
2526 // than the function G, until the variable G is cleared. |
|
2527 |
|
2528 curr_sym_tab->clear (id_name); |
1623
|
2529 |
5609
|
2530 if (! lexer_flags.parsing_nested_function |
|
2531 && symtab_context.top () == top_level_sym_tab) |
|
2532 { |
|
2533 symbol_record *sr = top_level_sym_tab->lookup (id_name); |
|
2534 |
|
2535 // Only clear the existing name if it is already defined as a |
|
2536 // function. If there is already a variable defined with the |
|
2537 // same name as a the current function, it will continue to |
|
2538 // shadow this name until the variable is cleared. This means |
|
2539 // that for something like the following at the command line, |
|
2540 // |
|
2541 // f = 13; |
|
2542 // function f () 7, end |
|
2543 // f |
|
2544 // |
|
2545 // F will still refer to the variable F (with value 13) rather |
|
2546 // than the function F, until the variable F is cleared. |
|
2547 |
|
2548 if (sr && sr->is_function ()) |
|
2549 top_level_sym_tab->clear (id_name); |
|
2550 } |
|
2551 |
4872
|
2552 symbol_record *sr = fbi_sym_tab->lookup (id_name, true); |
2791
|
2553 |
|
2554 if (sr) |
4238
|
2555 { |
|
2556 fcn->stash_symtab_ptr (sr); |
|
2557 |
|
2558 if (lexer_flags.parsing_nested_function) |
5397
|
2559 { |
|
2560 fcn->mark_as_nested_function (); |
|
2561 sr->hide (); |
|
2562 } |
4238
|
2563 } |
2791
|
2564 else |
|
2565 panic_impossible (); |
|
2566 |
4872
|
2567 sr->define (fcn, symbol_record::USER_FUNCTION); |
1755
|
2568 |
4426
|
2569 if (! help_buf.empty ()) |
|
2570 { |
4872
|
2571 sr->document (help_buf.top ()); |
4426
|
2572 help_buf.pop (); |
|
2573 } |
3844
|
2574 |
5397
|
2575 // Also insert the full name in the symbol table. This way, we can |
|
2576 // properly cope with changes to LOADPATH. |
|
2577 |
|
2578 if (reading_fcn_file) |
|
2579 { |
|
2580 symbol_record *full_sr |
|
2581 = fbi_sym_tab->lookup (curr_fcn_file_full_name, true); |
|
2582 |
|
2583 full_sr->alias (sr, true); |
|
2584 full_sr->hide (); |
|
2585 } |
|
2586 |
4240
|
2587 if (lexer_flags.parsing_nested_function < 0) |
|
2588 lexer_flags.parsing_nested_function = 0; |
4426
|
2589 |
1623
|
2590 return fcn; |
|
2591 } |
|
2592 |
|
2593 // Finish defining a function. |
|
2594 |
2891
|
2595 static octave_user_function * |
3665
|
2596 finish_function (tree_identifier *id, octave_user_function *fcn, |
|
2597 octave_comment_list *lc) |
1623
|
2598 { |
6215
|
2599 tree_decl_elt *tmp = new tree_decl_elt (id); |
|
2600 |
|
2601 tree_parameter_list *tpl = new tree_parameter_list (tmp); |
1623
|
2602 |
|
2603 tpl->mark_as_formal_parameters (); |
|
2604 |
3665
|
2605 fcn->stash_leading_comment (lc); |
|
2606 |
1623
|
2607 return fcn->define_ret_list (tpl); |
|
2608 } |
|
2609 |
|
2610 // Finish defining a function a different way. |
|
2611 |
2891
|
2612 static octave_user_function * |
3665
|
2613 finish_function (tree_parameter_list *ret_list, |
|
2614 octave_user_function *fcn, octave_comment_list *lc) |
1623
|
2615 { |
|
2616 ret_list->mark_as_formal_parameters (); |
|
2617 |
3665
|
2618 fcn->stash_leading_comment (lc); |
|
2619 |
1623
|
2620 return fcn->define_ret_list (ret_list); |
|
2621 } |
|
2622 |
2883
|
2623 static void |
|
2624 recover_from_parsing_function (void) |
|
2625 { |
4238
|
2626 if (symtab_context.empty ()) |
3903
|
2627 panic_impossible (); |
|
2628 |
4238
|
2629 curr_sym_tab = symtab_context.top (); |
|
2630 symtab_context.pop (); |
2883
|
2631 |
|
2632 lexer_flags.defining_func = false; |
|
2633 lexer_flags.beginning_of_function = false; |
|
2634 lexer_flags.parsed_function_name = false; |
|
2635 lexer_flags.looking_at_return_list = false; |
|
2636 lexer_flags.looking_at_parameter_list = false; |
|
2637 } |
|
2638 |
2846
|
2639 // Make an index expression. |
|
2640 |
751
|
2641 static tree_index_expression * |
3929
|
2642 make_index_expression (tree_expression *expr, tree_argument_list *args, |
3933
|
2643 char type) |
751
|
2644 { |
|
2645 tree_index_expression *retval = 0; |
|
2646 |
2970
|
2647 int l = expr->line (); |
|
2648 int c = expr->column (); |
|
2649 |
|
2650 expr->mark_postfix_indexed (); |
|
2651 |
3933
|
2652 if (expr->is_index_expression ()) |
|
2653 { |
|
2654 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2655 |
|
2656 tmp->append (args, type); |
|
2657 |
|
2658 retval = tmp; |
|
2659 } |
|
2660 else |
|
2661 retval = new tree_index_expression (expr, args, l, c, type); |
2970
|
2662 |
|
2663 return retval; |
|
2664 } |
|
2665 |
|
2666 // Make an indirect reference expression. |
|
2667 |
3930
|
2668 static tree_index_expression * |
3523
|
2669 make_indirect_ref (tree_expression *expr, const std::string& elt) |
2970
|
2670 { |
3930
|
2671 tree_index_expression *retval = 0; |
2970
|
2672 |
|
2673 int l = expr->line (); |
|
2674 int c = expr->column (); |
|
2675 |
3933
|
2676 if (expr->is_index_expression ()) |
|
2677 { |
|
2678 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2679 |
|
2680 tmp->append (elt); |
|
2681 |
|
2682 retval = tmp; |
|
2683 } |
|
2684 else |
|
2685 retval = new tree_index_expression (expr, elt, l, c); |
2970
|
2686 |
|
2687 lexer_flags.looking_at_indirect_ref = false; |
751
|
2688 |
|
2689 return retval; |
|
2690 } |
1511
|
2691 |
4131
|
2692 // Make an indirect reference expression with dynamic field name. |
|
2693 |
|
2694 static tree_index_expression * |
|
2695 make_indirect_ref (tree_expression *expr, tree_expression *elt) |
|
2696 { |
|
2697 tree_index_expression *retval = 0; |
|
2698 |
|
2699 int l = expr->line (); |
|
2700 int c = expr->column (); |
|
2701 |
|
2702 if (expr->is_index_expression ()) |
|
2703 { |
|
2704 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2705 |
|
2706 tmp->append (elt); |
|
2707 |
|
2708 retval = tmp; |
|
2709 } |
|
2710 else |
|
2711 retval = new tree_index_expression (expr, elt, l, c); |
|
2712 |
|
2713 lexer_flags.looking_at_indirect_ref = false; |
|
2714 |
|
2715 return retval; |
|
2716 } |
|
2717 |
2846
|
2718 // Make a declaration command. |
|
2719 |
|
2720 static tree_decl_command * |
|
2721 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) |
|
2722 { |
|
2723 tree_decl_command *retval = 0; |
|
2724 |
|
2725 int l = tok_val->line (); |
|
2726 int c = tok_val->column (); |
|
2727 |
|
2728 switch (tok) |
|
2729 { |
|
2730 case GLOBAL: |
|
2731 retval = new tree_global_command (lst, l, c); |
|
2732 break; |
|
2733 |
|
2734 case STATIC: |
|
2735 if (lexer_flags.defining_func) |
|
2736 retval = new tree_static_command (lst, l, c); |
|
2737 else |
|
2738 { |
|
2739 if (reading_script_file) |
4844
|
2740 warning ("ignoring persistent declaration near line %d of file `%s'", |
2846
|
2741 l, curr_fcn_file_full_name.c_str ()); |
|
2742 else |
4844
|
2743 warning ("ignoring persistent declaration near line %d", l); |
2846
|
2744 } |
|
2745 break; |
|
2746 |
|
2747 default: |
|
2748 panic_impossible (); |
|
2749 break; |
|
2750 } |
|
2751 |
|
2752 return retval; |
|
2753 } |
|
2754 |
1623
|
2755 // Finish building a matrix list. |
|
2756 |
|
2757 static tree_expression * |
1829
|
2758 finish_matrix (tree_matrix *m) |
1623
|
2759 { |
3110
|
2760 tree_expression *retval = m; |
|
2761 |
|
2762 unwind_protect::begin_frame ("finish_matrix"); |
|
2763 |
|
2764 unwind_protect_int (error_state); |
4452
|
2765 unwind_protect_int (warning_state); |
3110
|
2766 |
3815
|
2767 unwind_protect_bool (discard_error_messages); |
4452
|
2768 unwind_protect_bool (discard_warning_messages); |
|
2769 |
3815
|
2770 discard_error_messages = true; |
4452
|
2771 discard_warning_messages = true; |
1623
|
2772 |
2533
|
2773 if (m->all_elements_are_constant ()) |
1829
|
2774 { |
2970
|
2775 octave_value tmp = m->rvalue (); |
1623
|
2776 |
3489
|
2777 if (! (error_state || warning_state)) |
2533
|
2778 { |
|
2779 tree_constant *tc_retval = new tree_constant (tmp); |
|
2780 |
5765
|
2781 std::ostringstream buf; |
2533
|
2782 |
|
2783 tree_print_code tpc (buf); |
|
2784 |
|
2785 m->accept (tpc); |
|
2786 |
5765
|
2787 tc_retval->stash_original_text (buf.str ()); |
2533
|
2788 |
|
2789 delete m; |
|
2790 |
|
2791 retval = tc_retval; |
|
2792 } |
1623
|
2793 } |
3110
|
2794 |
|
2795 unwind_protect::run_frame ("finish_matrix"); |
1623
|
2796 |
|
2797 return retval; |
|
2798 } |
|
2799 |
3351
|
2800 // Finish building a cell list. |
|
2801 |
|
2802 static tree_expression * |
|
2803 finish_cell (tree_cell *c) |
|
2804 { |
5875
|
2805 return finish_matrix (c); |
3351
|
2806 } |
|
2807 |
1511
|
2808 static void |
|
2809 maybe_warn_missing_semi (tree_statement_list *t) |
|
2810 { |
5781
|
2811 if (lexer_flags.defining_func) |
1511
|
2812 { |
4219
|
2813 tree_statement *tmp = t->back(); |
1607
|
2814 |
1511
|
2815 if (tmp->is_expression ()) |
5781
|
2816 warning_with_id |
|
2817 ("Octave:missing-semicolon", |
|
2818 "missing semicolon near line %d, column %d in file `%s'", |
|
2819 tmp->line (), tmp->column (), curr_fcn_file_full_name.c_str ()); |
1511
|
2820 } |
|
2821 } |
1994
|
2822 |
2525
|
2823 static void |
|
2824 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2825 bool warn_missing_semi) |
|
2826 { |
|
2827 switch (sep) |
|
2828 { |
|
2829 case ';': |
|
2830 { |
4219
|
2831 tree_statement *tmp = list->back (); |
2525
|
2832 tmp->set_print_flag (0); |
|
2833 } |
|
2834 break; |
|
2835 |
|
2836 case 0: |
|
2837 case ',': |
|
2838 case '\n': |
|
2839 if (warn_missing_semi) |
|
2840 maybe_warn_missing_semi (list); |
|
2841 break; |
|
2842 |
|
2843 default: |
|
2844 warning ("unrecognized separator type!"); |
|
2845 break; |
|
2846 } |
|
2847 } |
|
2848 |
3021
|
2849 void |
|
2850 parse_and_execute (FILE *f) |
|
2851 { |
|
2852 unwind_protect::begin_frame ("parse_and_execute"); |
3880
|
2853 |
5309
|
2854 unwind_protect_ptr (global_command); |
|
2855 |
3021
|
2856 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2857 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
2858 |
|
2859 unwind_protect::add (restore_input_buffer, old_buf); |
|
2860 unwind_protect::add (delete_input_buffer, new_buf); |
|
2861 |
|
2862 switch_to_buffer (new_buf); |
|
2863 |
|
2864 unwind_protect_bool (line_editing); |
3880
|
2865 unwind_protect_bool (get_input_from_eval_string); |
3883
|
2866 unwind_protect_bool (parser_end_of_input); |
3021
|
2867 |
|
2868 line_editing = false; |
3880
|
2869 get_input_from_eval_string = false; |
3883
|
2870 parser_end_of_input = false; |
3021
|
2871 |
|
2872 unwind_protect_ptr (curr_sym_tab); |
|
2873 |
|
2874 int retval; |
|
2875 do |
|
2876 { |
|
2877 reset_parser (); |
|
2878 |
|
2879 retval = yyparse (); |
|
2880 |
3883
|
2881 if (retval == 0) |
|
2882 { |
|
2883 if (global_command) |
3021
|
2884 { |
3883
|
2885 global_command->eval (); |
|
2886 |
|
2887 delete global_command; |
|
2888 |
|
2889 global_command = 0; |
|
2890 |
4171
|
2891 OCTAVE_QUIT; |
|
2892 |
4207
|
2893 bool quit = (tree_return_command::returning |
|
2894 || tree_break_command::breaking); |
|
2895 |
|
2896 if (tree_return_command::returning) |
|
2897 tree_return_command::returning = 0; |
|
2898 |
|
2899 if (tree_break_command::breaking) |
|
2900 tree_break_command::breaking--; |
3883
|
2901 |
|
2902 if (error_state) |
|
2903 { |
|
2904 error ("near line %d of file `%s'", input_line_number, |
|
2905 curr_fcn_file_full_name.c_str ()); |
|
2906 |
|
2907 break; |
|
2908 } |
|
2909 |
|
2910 if (quit) |
|
2911 break; |
3021
|
2912 } |
3883
|
2913 else if (parser_end_of_input) |
3021
|
2914 break; |
3883
|
2915 } |
3021
|
2916 } |
|
2917 while (retval == 0); |
|
2918 |
|
2919 unwind_protect::run_frame ("parse_and_execute"); |
|
2920 } |
|
2921 |
|
2922 static void |
|
2923 safe_fclose (void *f) |
|
2924 { |
5775
|
2925 // FIXME -- comments at the end of an input file are |
3765
|
2926 // discarded (otherwise, they would be appended to the next |
|
2927 // statement, possibly from the command line or another file, which |
|
2928 // can be quite confusing). |
|
2929 |
5308
|
2930 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2931 |
|
2932 delete tc; |
3765
|
2933 |
3021
|
2934 if (f) |
|
2935 fclose (static_cast<FILE *> (f)); |
|
2936 } |
|
2937 |
|
2938 void |
3523
|
2939 parse_and_execute (const std::string& s, bool verbose, const char *warn_for) |
3021
|
2940 { |
|
2941 unwind_protect::begin_frame ("parse_and_execute_2"); |
|
2942 |
|
2943 unwind_protect_bool (reading_script_file); |
|
2944 unwind_protect_str (curr_fcn_file_full_name); |
|
2945 |
|
2946 reading_script_file = true; |
|
2947 curr_fcn_file_full_name = s; |
|
2948 |
|
2949 FILE *f = get_input_from_file (s, 0); |
|
2950 |
|
2951 if (f) |
|
2952 { |
|
2953 unwind_protect::add (safe_fclose, f); |
|
2954 |
5744
|
2955 octave_user_script *script = new octave_user_script (s, s, ""); |
|
2956 octave_call_stack::push (script); |
|
2957 unwind_protect::add (octave_call_stack::unwind_pop_script, 0); |
|
2958 |
3021
|
2959 unwind_protect_int (input_line_number); |
|
2960 unwind_protect_int (current_input_column); |
|
2961 |
|
2962 input_line_number = 0; |
|
2963 current_input_column = 1; |
|
2964 |
|
2965 if (verbose) |
|
2966 { |
3531
|
2967 std::cout << "reading commands from " << s << " ... "; |
3021
|
2968 reading_startup_message_printed = true; |
3531
|
2969 std::cout.flush (); |
3021
|
2970 } |
|
2971 |
|
2972 parse_and_execute (f); |
|
2973 |
|
2974 if (verbose) |
3538
|
2975 std::cout << "done." << std::endl; |
3021
|
2976 } |
|
2977 else if (warn_for) |
|
2978 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
|
2979 |
|
2980 unwind_protect::run_frame ("parse_and_execute_2"); |
|
2981 } |
|
2982 |
|
2983 static bool |
5175
|
2984 looks_like_copyright (const std::string& s) |
3021
|
2985 { |
5175
|
2986 // Perhaps someday we will want to do more here, so leave this as a |
|
2987 // separate function. |
|
2988 |
5180
|
2989 return (s.substr (0, 9) == "Copyright"); |
3021
|
2990 } |
|
2991 |
4540
|
2992 static int |
|
2993 text_getc (FILE *f) |
|
2994 { |
|
2995 int c = getc (f); |
|
2996 |
|
2997 // Convert CRLF into just LF. |
|
2998 |
|
2999 if (c == '\r') |
|
3000 { |
|
3001 c = getc (f); |
|
3002 |
|
3003 if (c != '\n') |
|
3004 { |
|
3005 ungetc (c, f); |
|
3006 c = '\r'; |
|
3007 } |
|
3008 } |
|
3009 |
|
3010 return c; |
|
3011 } |
|
3012 |
3021
|
3013 // Eat whitespace and comments from FFILE, returning the text of the |
|
3014 // comments read if it doesn't look like a copyright notice. If |
|
3015 // IN_PARTS, consider each block of comments separately; otherwise, |
|
3016 // grab them all at once. If UPDATE_POS is TRUE, line and column |
3665
|
3017 // number information is updated. If SAVE_COPYRIGHT is TRUE, then |
|
3018 // comments that are recognized as a copyright notice are saved in the |
5932
|
3019 // comment buffer. If SKIP_CODE is TRUE, then ignore code, otherwise |
|
3020 // stop at the first non-whitespace character that is not part of a |
|
3021 // comment. |
|
3022 |
|
3023 // FIXME -- skipping code will fail for something like this: |
|
3024 // |
|
3025 // function foo (x) |
|
3026 // fprintf ('%d\n', x); |
|
3027 // |
|
3028 // % This is the help for foo. |
|
3029 // |
|
3030 // because we recognize the '%' in the fprintf format as a comment |
|
3031 // character. Fixing this will probably require actually parsing the |
|
3032 // file properly. |
3021
|
3033 |
5775
|
3034 // FIXME -- grab_help_text() in lex.l duplicates some of this |
3021
|
3035 // code! |
|
3036 |
3536
|
3037 static std::string |
3665
|
3038 gobble_leading_white_space (FILE *ffile, bool in_parts, |
5932
|
3039 bool update_pos, bool save_copyright, |
|
3040 bool skip_code) |
3021
|
3041 { |
3523
|
3042 std::string help_txt; |
3021
|
3043 |
3802
|
3044 // TRUE means we have already seen the first block of comments. |
3021
|
3045 bool first_comments_seen = false; |
3802
|
3046 |
|
3047 // TRUE means we are at the beginning of a comment block. |
3021
|
3048 bool begin_comment = false; |
3802
|
3049 |
|
3050 // TRUE means we have already cached the help text. |
3021
|
3051 bool have_help_text = false; |
3802
|
3052 |
|
3053 // TRUE means we are currently reading a comment block. |
3021
|
3054 bool in_comment = false; |
3802
|
3055 |
|
3056 // TRUE means we should discard the first space from the input |
|
3057 // (used to strip leading spaces from the help text). |
3427
|
3058 bool discard_space = true; |
3802
|
3059 |
3021
|
3060 int c; |
|
3061 |
4540
|
3062 while ((c = text_getc (ffile)) != EOF) |
3021
|
3063 { |
|
3064 if (update_pos) |
|
3065 current_input_column++; |
|
3066 |
|
3067 if (begin_comment) |
|
3068 { |
|
3069 if (c == '%' || c == '#') |
|
3070 continue; |
3427
|
3071 else if (discard_space && c == ' ') |
|
3072 { |
|
3073 discard_space = false; |
|
3074 continue; |
|
3075 } |
3021
|
3076 else |
|
3077 begin_comment = false; |
|
3078 } |
|
3079 |
|
3080 if (in_comment) |
|
3081 { |
|
3082 if (! have_help_text) |
|
3083 { |
|
3084 first_comments_seen = true; |
5760
|
3085 help_txt += static_cast<char> (c); |
3021
|
3086 } |
|
3087 |
|
3088 if (c == '\n') |
|
3089 { |
|
3090 if (update_pos) |
|
3091 { |
|
3092 input_line_number++; |
|
3093 current_input_column = 0; |
|
3094 } |
3427
|
3095 |
3021
|
3096 in_comment = false; |
3427
|
3097 discard_space = true; |
3021
|
3098 |
|
3099 if (in_parts) |
|
3100 { |
4540
|
3101 if ((c = text_getc (ffile)) != EOF) |
3021
|
3102 { |
|
3103 if (update_pos) |
|
3104 current_input_column--; |
|
3105 ungetc (c, ffile); |
|
3106 if (c == '\n') |
|
3107 break; |
|
3108 } |
|
3109 else |
|
3110 break; |
|
3111 } |
|
3112 } |
|
3113 } |
|
3114 else |
|
3115 { |
|
3116 switch (c) |
|
3117 { |
|
3118 case ' ': |
|
3119 case '\t': |
|
3120 if (first_comments_seen) |
|
3121 have_help_text = true; |
|
3122 break; |
|
3123 |
3802
|
3124 case '%': |
|
3125 case '#': |
|
3126 begin_comment = true; |
|
3127 in_comment = true; |
|
3128 break; |
|
3129 |
3021
|
3130 case '\n': |
|
3131 if (first_comments_seen) |
|
3132 have_help_text = true; |
|
3133 if (update_pos) |
|
3134 { |
|
3135 input_line_number++; |
|
3136 current_input_column = 0; |
|
3137 } |
|
3138 continue; |
|
3139 |
|
3140 default: |
5932
|
3141 if (skip_code) |
|
3142 continue; |
|
3143 else |
|
3144 { |
|
3145 if (update_pos) |
|
3146 current_input_column--; |
|
3147 ungetc (c, ffile); |
|
3148 goto done; |
|
3149 } |
3021
|
3150 } |
|
3151 } |
|
3152 } |
|
3153 |
|
3154 done: |
|
3155 |
|
3156 if (! help_txt.empty ()) |
|
3157 { |
5175
|
3158 if (looks_like_copyright (help_txt)) |
3665
|
3159 { |
|
3160 if (save_copyright) |
|
3161 octave_comment_buffer::append (help_txt); |
|
3162 |
|
3163 help_txt.resize (0); |
|
3164 } |
3021
|
3165 |
|
3166 if (in_parts && help_txt.empty ()) |
5932
|
3167 help_txt = gobble_leading_white_space (ffile, in_parts, update_pos, |
|
3168 false, skip_code); |
3021
|
3169 } |
|
3170 |
|
3171 return help_txt; |
|
3172 } |
|
3173 |
3536
|
3174 std::string |
5484
|
3175 get_help_from_file (const std::string& nm, bool& symbol_found, |
5931
|
3176 std::string& file) |
3021
|
3177 { |
3523
|
3178 std::string retval; |
3021
|
3179 |
5931
|
3180 file = fcn_file_in_path (nm); |
5484
|
3181 |
|
3182 if (! file.empty ()) |
3021
|
3183 { |
5484
|
3184 symbol_found = true; |
|
3185 |
|
3186 FILE *fptr = fopen (file.c_str (), "r"); |
3021
|
3187 |
|
3188 if (fptr) |
|
3189 { |
5760
|
3190 unwind_protect::add (safe_fclose, fptr); |
3021
|
3191 |
5932
|
3192 retval = gobble_leading_white_space (fptr, true, true, false, true); |
3021
|
3193 |
|
3194 unwind_protect::run (); |
|
3195 } |
|
3196 } |
|
3197 |
|
3198 return retval; |
|
3199 } |
|
3200 |
5931
|
3201 std::string |
|
3202 get_help_from_file (const std::string& nm, bool& symbol_found) |
|
3203 { |
|
3204 std::string file; |
|
3205 return get_help_from_file (nm, symbol_found, file); |
|
3206 } |
|
3207 |
3021
|
3208 static int |
|
3209 is_function_file (FILE *ffile) |
|
3210 { |
|
3211 int status = 0; |
|
3212 |
|
3213 long pos = ftell (ffile); |
|
3214 |
5932
|
3215 gobble_leading_white_space (ffile, false, false, false, false); |
3021
|
3216 |
|
3217 char buf [10]; |
|
3218 fgets (buf, 10, ffile); |
|
3219 int len = strlen (buf); |
|
3220 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
3221 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
3222 status = 1; |
|
3223 |
|
3224 fseek (ffile, pos, SEEK_SET); |
|
3225 |
|
3226 return status; |
|
3227 } |
|
3228 |
|
3229 static void |
|
3230 restore_command_history (void *) |
|
3231 { |
|
3232 command_history::ignore_entries (! Vsaving_history); |
|
3233 } |
|
3234 |
|
3235 static void |
|
3236 restore_input_stream (void *f) |
|
3237 { |
|
3238 command_editor::set_input_stream (static_cast<FILE *> (f)); |
|
3239 } |
|
3240 |
|
3241 static bool |
6323
|
3242 parse_fcn_file (const std::string& ff, bool exec_script, |
|
3243 bool force_script = false) |
3021
|
3244 { |
|
3245 unwind_protect::begin_frame ("parse_fcn_file"); |
|
3246 |
|
3247 int script_file_executed = false; |
|
3248 |
|
3249 // Open function file and parse. |
|
3250 |
|
3251 bool old_reading_fcn_file_state = reading_fcn_file; |
|
3252 |
|
3253 FILE *in_stream = command_editor::get_input_stream (); |
|
3254 |
|
3255 unwind_protect::add (restore_input_stream, in_stream); |
|
3256 |
|
3257 unwind_protect_ptr (ff_instream); |
|
3258 |
|
3259 unwind_protect_int (input_line_number); |
|
3260 unwind_protect_int (current_input_column); |
4238
|
3261 unwind_protect_int (end_tokens_expected); |
3021
|
3262 unwind_protect_bool (reading_fcn_file); |
|
3263 unwind_protect_bool (line_editing); |
4238
|
3264 unwind_protect_str (parent_function_name); |
3021
|
3265 |
|
3266 input_line_number = 0; |
|
3267 current_input_column = 1; |
4238
|
3268 end_tokens_expected = 0; |
3021
|
3269 reading_fcn_file = true; |
|
3270 line_editing = false; |
4238
|
3271 parent_function_name = ""; |
3021
|
3272 |
|
3273 FILE *ffile = get_input_from_file (ff, 0); |
|
3274 |
|
3275 unwind_protect::add (safe_fclose, ffile); |
|
3276 |
|
3277 if (ffile) |
|
3278 { |
|
3279 // Check to see if this file defines a function or is just a |
|
3280 // list of commands. |
|
3281 |
3165
|
3282 if (! force_script && is_function_file (ffile)) |
3021
|
3283 { |
5775
|
3284 // FIXME -- we shouldn't need both the |
3021
|
3285 // command_history object and the |
|
3286 // Vsaving_history variable... |
|
3287 command_history::ignore_entries (); |
|
3288 |
|
3289 unwind_protect::add (restore_command_history, 0); |
|
3290 |
|
3291 unwind_protect_int (Vecho_executing_commands); |
|
3292 unwind_protect_bool (Vsaving_history); |
|
3293 unwind_protect_bool (reading_fcn_file); |
3877
|
3294 unwind_protect_bool (get_input_from_eval_string); |
3883
|
3295 unwind_protect_bool (parser_end_of_input); |
3021
|
3296 |
|
3297 Vecho_executing_commands = ECHO_OFF; |
|
3298 Vsaving_history = false; |
|
3299 reading_fcn_file = true; |
3877
|
3300 get_input_from_eval_string = false; |
3883
|
3301 parser_end_of_input = false; |
3021
|
3302 |
|
3303 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3304 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
3305 |
5760
|
3306 unwind_protect::add (restore_input_buffer, old_buf); |
|
3307 unwind_protect::add (delete_input_buffer, new_buf); |
3021
|
3308 |
|
3309 switch_to_buffer (new_buf); |
|
3310 |
|
3311 unwind_protect_ptr (curr_sym_tab); |
|
3312 |
|
3313 reset_parser (); |
|
3314 |
4426
|
3315 std::string txt |
5932
|
3316 = gobble_leading_white_space (ffile, true, true, true, false); |
4426
|
3317 |
|
3318 help_buf.push (txt); |
|
3319 |
|
3320 octave_comment_buffer::append (txt); |
3021
|
3321 |
5775
|
3322 // FIXME -- this should not be necessary. |
5932
|
3323 gobble_leading_white_space (ffile, false, true, false, false); |
3021
|
3324 |
|
3325 int status = yyparse (); |
|
3326 |
|
3327 if (status != 0) |
|
3328 { |
|
3329 error ("parse error while reading function file %s", |
|
3330 ff.c_str ()); |
4009
|
3331 fbi_sym_tab->clear (curr_fcn_file_name); |
3021
|
3332 } |
|
3333 } |
|
3334 else if (exec_script) |
|
3335 { |
|
3336 // The value of `reading_fcn_file' will be restored to the |
|
3337 // proper value when we unwind from this frame. |
|
3338 reading_fcn_file = old_reading_fcn_file_state; |
|
3339 |
5775
|
3340 // FIXME -- we shouldn't need both the |
3021
|
3341 // command_history object and the |
|
3342 // Vsaving_history variable... |
|
3343 command_history::ignore_entries (); |
|
3344 |
|
3345 unwind_protect::add (restore_command_history, 0); |
|
3346 |
|
3347 unwind_protect_bool (Vsaving_history); |
|
3348 unwind_protect_bool (reading_script_file); |
|
3349 |
|
3350 Vsaving_history = false; |
|
3351 reading_script_file = true; |
|
3352 |
5744
|
3353 octave_user_script *script = new octave_user_script (ff, ff, ""); |
|
3354 octave_call_stack::push (script); |
|
3355 unwind_protect::add (octave_call_stack::unwind_pop_script, 0); |
|
3356 |
3021
|
3357 parse_and_execute (ffile); |
|
3358 |
|
3359 script_file_executed = true; |
|
3360 } |
|
3361 } |
3880
|
3362 else |
|
3363 error ("no such file, `%s'", ff.c_str ()); |
3021
|
3364 |
|
3365 unwind_protect::run_frame ("parse_fcn_file"); |
|
3366 |
|
3367 return script_file_executed; |
|
3368 } |
|
3369 |
5484
|
3370 std::string |
|
3371 lookup_autoload (const std::string& nm) |
|
3372 { |
5626
|
3373 std::string retval; |
|
3374 |
|
3375 typedef std::map<std::string, std::string>::const_iterator am_iter; |
|
3376 |
|
3377 am_iter p = autoload_map.find (nm); |
|
3378 |
|
3379 if (p != autoload_map.end ()) |
6323
|
3380 retval = load_path::find_file (p->second); |
5626
|
3381 |
|
3382 return retval; |
5484
|
3383 } |
|
3384 |
5592
|
3385 string_vector |
|
3386 autoloaded_functions (void) |
|
3387 { |
|
3388 string_vector names (autoload_map.size()); |
|
3389 |
|
3390 octave_idx_type i = 0; |
5626
|
3391 typedef std::map<std::string, std::string>::const_iterator am_iter; |
|
3392 for (am_iter p = autoload_map.begin (); p != autoload_map.end (); p++) |
5592
|
3393 names[i++] = p->first; |
|
3394 |
|
3395 return names; |
|
3396 } |
|
3397 |
|
3398 string_vector |
|
3399 reverse_lookup_autoload (const std::string& nm) |
|
3400 { |
|
3401 string_vector names; |
|
3402 |
5626
|
3403 typedef std::map<std::string, std::string>::const_iterator am_iter; |
|
3404 for (am_iter p = autoload_map.begin (); p != autoload_map.end (); p++) |
5592
|
3405 if (nm == p->second) |
|
3406 names.append (p->first); |
|
3407 |
|
3408 return names; |
|
3409 } |
|
3410 |
3021
|
3411 bool |
6238
|
3412 load_fcn_from_file (const std::string& nm_arg, bool exec_script) |
3021
|
3413 { |
5484
|
3414 unwind_protect::begin_frame ("load_fcn_from_file"); |
|
3415 |
3021
|
3416 bool script_file_executed = false; |
|
3417 |
6238
|
3418 std::string nm = nm_arg; |
|
3419 |
|
3420 size_t nm_len = nm.length (); |
5472
|
3421 |
|
3422 std::string file; |
|
3423 |
6323
|
3424 unwind_protect_bool (fcn_file_from_relative_lookup); |
|
3425 |
|
3426 fcn_file_from_relative_lookup = false; |
|
3427 |
5472
|
3428 if (octave_env::absolute_pathname (nm) |
|
3429 && ((nm_len > 4 && nm.substr (nm_len-4) == ".oct") |
5864
|
3430 || (nm_len > 4 && nm.substr (nm_len-4) == ".mex") |
6235
|
3431 || (nm_len > 2 && nm.substr (nm_len-2) == ".m"))) |
5472
|
3432 { |
|
3433 file = nm; |
6238
|
3434 |
|
3435 nm = octave_env::base_pathname (file); |
|
3436 nm = nm.substr (0, nm.find_last_of ('.')); |
5472
|
3437 } |
|
3438 else |
|
3439 { |
5484
|
3440 file = lookup_autoload (nm); |
|
3441 |
|
3442 if (! file.empty ()) |
|
3443 { |
|
3444 unwind_protect_bool (autoloading); |
|
3445 |
|
3446 autoloading = true; |
|
3447 exec_script = true; |
|
3448 } |
|
3449 else |
6323
|
3450 file = load_path::find_fcn (nm); |
|
3451 |
|
3452 fcn_file_from_relative_lookup = ! octave_env::absolute_pathname (file); |
|
3453 |
|
3454 file = octave_env::make_absolute (file, octave_env::getcwd ()); |
5472
|
3455 } |
4243
|
3456 |
|
3457 int len = file.length (); |
|
3458 |
4244
|
3459 if (len > 4 && file.substr (len-4, len-1) == ".oct") |
3021
|
3460 { |
6323
|
3461 if (octave_dynamic_loader::load_oct (nm, file, fcn_file_from_relative_lookup)) |
5864
|
3462 force_link_to_function (nm); |
|
3463 } |
|
3464 else if (len > 4 && file.substr (len-4, len-1) == ".mex") |
|
3465 { |
6323
|
3466 if (octave_dynamic_loader::load_mex (nm, file, fcn_file_from_relative_lookup)) |
4243
|
3467 force_link_to_function (nm); |
3021
|
3468 } |
4244
|
3469 else if (len > 2) |
3021
|
3470 { |
|
3471 // These are needed by yyparse. |
|
3472 |
|
3473 unwind_protect_str (curr_fcn_file_name); |
|
3474 unwind_protect_str (curr_fcn_file_full_name); |
|
3475 |
|
3476 curr_fcn_file_name = nm; |
4243
|
3477 curr_fcn_file_full_name = file; |
|
3478 |
5484
|
3479 script_file_executed = parse_fcn_file (file, exec_script, autoloading); |
|
3480 |
|
3481 if (! error_state) |
|
3482 { |
|
3483 if (autoloading) |
|
3484 { |
|
3485 script_file_executed = false; |
|
3486 force_link_to_function (nm); |
|
3487 } |
|
3488 else if (! script_file_executed) |
|
3489 force_link_to_function (nm); |
|
3490 } |
3021
|
3491 } |
|
3492 |
5484
|
3493 unwind_protect::run_frame ("load_fcn_from_file"); |
|
3494 |
3021
|
3495 return script_file_executed; |
|
3496 } |
|
3497 |
5312
|
3498 bool |
|
3499 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
|
3500 { |
|
3501 return load_fcn_from_file (sym_rec->name (), exec_script); |
|
3502 } |
|
3503 |
5484
|
3504 DEFCMD (autoload, args, , |
|
3505 "-*- texinfo -*-\n\ |
|
3506 @deftypefn {Built-in Function} {} autoload (@var{function}, @var{file})\n\ |
|
3507 Define @var{function} to autoload from @var{file}.\n\ |
5626
|
3508 \n\ |
6380
|
3509 The second argument, @var{file}, should be an absolute file name and\n\ |
|
3510 should not depend on the Octave load path.\n\ |
|
3511 \n\ |
|
3512 Normally, calls to @code{autoload} appear in PKG_ADD script files that\n\ |
|
3513 are evaluated when a directory is added to the Octave's load path. To\n\ |
|
3514 avoid having to hardcode directory names in @var{file}, it is customary\n\ |
|
3515 to use\n\ |
|
3516 \n\ |
|
3517 @example\n\ |
|
3518 autoload (\"foo\",\n\ |
|
3519 fullfile (fileparts (mfilename (\"fullpath\")),\n\ |
|
3520 \"bar.oct\"));\n\ |
|
3521 @end example\n\ |
|
3522 \n\ |
|
3523 Uses like\n\ |
|
3524 \n\ |
|
3525 @example\n\ |
|
3526 autoload (\"foo\", file_in_loadpth (\"bar.oct\"))\n\ |
|
3527 @end example\n\ |
|
3528 \n\ |
|
3529 @noindent\n\ |
|
3530 are strongly discouraged.\n\ |
|
3531 \n\ |
|
3532 With no arguments, return a structure containing the curre autoload map.\n\ |
|
3533 @seealso{PKG_ADD}\n\ |
5484
|
3534 @end deftypefn") |
|
3535 { |
5626
|
3536 octave_value retval; |
5484
|
3537 |
|
3538 int nargin = args.length (); |
|
3539 |
5626
|
3540 if (nargin == 0) |
|
3541 { |
|
3542 Cell func_names (dim_vector (autoload_map.size ()), 1); |
|
3543 Cell file_names (dim_vector (autoload_map.size ()), 1); |
|
3544 |
|
3545 octave_idx_type i = 0; |
|
3546 typedef std::map<std::string, std::string>::const_iterator am_iter; |
|
3547 for (am_iter p = autoload_map.begin (); p != autoload_map.end (); p++) |
|
3548 { |
|
3549 func_names(i) = p->first; |
|
3550 file_names(i) = p->second; |
|
3551 |
|
3552 i++; |
|
3553 } |
|
3554 |
|
3555 Octave_map m; |
|
3556 |
|
3557 m.assign ("function", func_names); |
|
3558 m.assign ("file", file_names); |
|
3559 |
|
3560 retval = m; |
|
3561 } |
|
3562 else if (nargin == 2) |
5484
|
3563 { |
|
3564 string_vector argv = args.make_argv ("autoload"); |
|
3565 |
|
3566 if (! error_state) |
6380
|
3567 { |
|
3568 std::string nm = argv[2]; |
|
3569 |
|
3570 if (! octave_env::absolute_pathname (nm)) |
|
3571 warning ("autoload: `%s' is not an absolute file name", |
|
3572 nm.c_str ()); |
|
3573 |
|
3574 autoload_map[argv[1]] = nm; |
|
3575 } |
5484
|
3576 } |
|
3577 else |
5823
|
3578 print_usage (); |
5484
|
3579 |
|
3580 return retval; |
|
3581 } |
|
3582 |
4486
|
3583 void |
5975
|
3584 source_file (const std::string& file_name, const std::string& context) |
4486
|
3585 { |
|
3586 std::string file_full_name = file_ops::tilde_expand (file_name); |
|
3587 |
|
3588 unwind_protect::begin_frame ("source_file"); |
|
3589 |
|
3590 unwind_protect_str (curr_fcn_file_name); |
|
3591 unwind_protect_str (curr_fcn_file_full_name); |
|
3592 |
|
3593 curr_fcn_file_name = file_name; |
|
3594 curr_fcn_file_full_name = file_full_name; |
|
3595 |
5975
|
3596 if (! context.empty ()) |
|
3597 { |
|
3598 unwind_protect_ptr (curr_sym_tab); |
|
3599 |
|
3600 if (context == "caller") |
|
3601 curr_sym_tab = curr_caller_sym_tab; |
|
3602 else if (context == "base") |
|
3603 curr_sym_tab = top_level_sym_tab; |
|
3604 else |
|
3605 error ("source: context must be \"caller\" or \"base\""); |
|
3606 } |
|
3607 |
|
3608 if (! error_state) |
|
3609 { |
|
3610 parse_fcn_file (file_full_name, true, true); |
|
3611 |
|
3612 if (error_state) |
|
3613 error ("source: error sourcing file `%s'", |
|
3614 file_full_name.c_str ()); |
|
3615 } |
4486
|
3616 |
|
3617 unwind_protect::run_frame ("source_file"); |
|
3618 } |
|
3619 |
5739
|
3620 DEFUN (mfilename, args, , |
|
3621 "-*- texinfo -*-\n\ |
|
3622 @deftypefn {Built-in Function} {} mfilename ()\n\ |
|
3623 @deftypefnx {Built-in Function} {} mfilename (@code{\"fullpath\"})\n\ |
|
3624 @deftypefnx {Built-in Function} {} mfilename (@code{\"fullpathext\"})\n\ |
|
3625 Return the name of the currently executing file. At the top-level,\n\ |
|
3626 return the empty string. Given the argument @code{\"fullpath\"},\n\ |
|
3627 include the directory part of the file name, but not the extension.\n\ |
|
3628 Given the argument @code{\"fullpathext\"}, include the directory part\n\ |
|
3629 of the file name and the extension.\n\ |
5774
|
3630 @end deftypefn") |
5739
|
3631 { |
|
3632 octave_value retval; |
|
3633 |
|
3634 int nargin = args.length (); |
|
3635 |
|
3636 if (nargin > 1) |
|
3637 { |
5823
|
3638 print_usage (); |
5739
|
3639 return retval; |
|
3640 } |
|
3641 |
|
3642 std::string arg; |
|
3643 |
|
3644 if (nargin == 1) |
|
3645 { |
|
3646 arg = args(0).string_value (); |
|
3647 |
|
3648 if (error_state) |
|
3649 { |
|
3650 error ("mfilename: expecting argument to be a character string"); |
|
3651 return retval; |
|
3652 } |
|
3653 } |
|
3654 |
|
3655 std::string fname; |
|
3656 |
5744
|
3657 octave_function *fcn = octave_call_stack::caller_user_script_or_function (); |
5743
|
3658 |
|
3659 if (fcn) |
|
3660 { |
|
3661 fname = fcn->fcn_file_name (); |
|
3662 |
|
3663 if (fname.empty ()) |
|
3664 fname = fcn->name (); |
|
3665 } |
5739
|
3666 |
|
3667 if (arg == "fullpathext") |
|
3668 retval = fname; |
|
3669 else |
|
3670 { |
5747
|
3671 size_t dpos = fname.rfind (file_ops::dir_sep_char); |
|
3672 size_t epos = fname.rfind ('.'); |
|
3673 |
|
3674 if (epos <= dpos) |
|
3675 epos = NPOS; |
|
3676 |
|
3677 fname = (epos != NPOS) ? fname.substr (0, epos) : fname; |
5739
|
3678 |
|
3679 if (arg == "fullpath") |
|
3680 retval = fname; |
|
3681 else |
5747
|
3682 retval = (dpos != NPOS) ? fname.substr (dpos+1) : fname; |
5739
|
3683 } |
|
3684 |
|
3685 return retval; |
|
3686 } |
|
3687 |
|
3688 |
3021
|
3689 DEFUN (source, args, , |
3371
|
3690 "-*- texinfo -*-\n\ |
|
3691 @deftypefn {Built-in Function} {} source (@var{file})\n\ |
|
3692 Parse and execute the contents of @var{file}. This is equivalent to\n\ |
|
3693 executing commands from a script file, but without requiring the file to\n\ |
|
3694 be named @file{@var{file}.m}.\n\ |
|
3695 @end deftypefn") |
3021
|
3696 { |
|
3697 octave_value_list retval; |
|
3698 |
|
3699 int nargin = args.length (); |
|
3700 |
5975
|
3701 if (nargin == 1 || nargin == 2) |
3021
|
3702 { |
3523
|
3703 std::string file_name = args(0).string_value (); |
3021
|
3704 |
|
3705 if (! error_state) |
5975
|
3706 { |
|
3707 std::string context; |
|
3708 |
|
3709 if (nargin == 2) |
|
3710 context = args(1).string_value (); |
|
3711 |
|
3712 if (! error_state) |
|
3713 source_file (file_name, context); |
|
3714 else |
|
3715 error ("source: expecting context to be character string"); |
|
3716 } |
3021
|
3717 else |
|
3718 error ("source: expecting file name as argument"); |
|
3719 } |
|
3720 else |
5823
|
3721 print_usage (); |
3021
|
3722 |
|
3723 return retval; |
|
3724 } |
|
3725 |
3726
|
3726 // Evaluate an Octave function (built-in or interpreted) and return |
3844
|
3727 // the list of result values. NAME is the name of the function to |
|
3728 // call. ARGS are the arguments to the function. NARGOUT is the |
|
3729 // number of output arguments expected. |
3726
|
3730 |
3021
|
3731 octave_value_list |
3523
|
3732 feval (const std::string& name, const octave_value_list& args, int nargout) |
3156
|
3733 { |
|
3734 octave_value_list retval; |
|
3735 |
|
3736 octave_function *fcn = is_valid_function (name, "feval", 1); |
|
3737 |
|
3738 if (fcn) |
3544
|
3739 retval = fcn->do_multi_index_op (nargout, args); |
3156
|
3740 |
|
3741 return retval; |
|
3742 } |
|
3743 |
4342
|
3744 octave_value_list |
|
3745 feval (octave_function *fcn, const octave_value_list& args, int nargout) |
|
3746 { |
|
3747 octave_value_list retval; |
|
3748 |
|
3749 if (fcn) |
|
3750 retval = fcn->do_multi_index_op (nargout, args); |
|
3751 |
|
3752 return retval; |
|
3753 } |
|
3754 |
|
3755 static octave_value_list |
|
3756 get_feval_args (const octave_value_list& args) |
|
3757 { |
|
3758 int tmp_nargin = args.length () - 1; |
|
3759 |
|
3760 octave_value_list retval (tmp_nargin, octave_value ()); |
|
3761 |
|
3762 for (int i = 0; i < tmp_nargin; i++) |
|
3763 retval(i) = args(i+1); |
|
3764 |
|
3765 string_vector arg_names = args.name_tags (); |
|
3766 |
|
3767 if (! arg_names.empty ()) |
|
3768 { |
|
3769 // tmp_nargin and arg_names.length () - 1 may differ if |
|
3770 // we are passed all_va_args. |
|
3771 |
|
3772 int n = arg_names.length () - 1; |
|
3773 |
|
3774 int len = n > tmp_nargin ? tmp_nargin : n; |
|
3775 |
|
3776 string_vector tmp_arg_names (len); |
|
3777 |
|
3778 for (int i = 0; i < len; i++) |
|
3779 tmp_arg_names(i) = arg_names(i+1); |
|
3780 |
|
3781 retval.stash_name_tags (tmp_arg_names); |
|
3782 } |
|
3783 |
|
3784 return retval; |
|
3785 } |
|
3786 |
|
3787 |
3726
|
3788 // Evaluate an Octave function (built-in or interpreted) and return |
|
3789 // the list of result values. The first element of ARGS should be a |
|
3790 // string containing the name of the function to call, then the rest |
|
3791 // are the actual arguments to the function. NARGOUT is the number of |
|
3792 // output arguments expected. |
|
3793 |
3156
|
3794 octave_value_list |
3021
|
3795 feval (const octave_value_list& args, int nargout) |
|
3796 { |
|
3797 octave_value_list retval; |
|
3798 |
4342
|
3799 int nargin = args.length (); |
|
3800 |
|
3801 if (nargin > 0) |
3021
|
3802 { |
4342
|
3803 octave_value f_arg = args(0); |
|
3804 |
|
3805 if (f_arg.is_string ()) |
|
3806 { |
|
3807 std::string name = f_arg.string_value (); |
|
3808 |
|
3809 if (! error_state) |
3156
|
3810 { |
4342
|
3811 octave_value_list tmp_args = get_feval_args (args); |
|
3812 |
|
3813 retval = feval (name, tmp_args, nargout); |
3156
|
3814 } |
4342
|
3815 } |
|
3816 else |
|
3817 { |
|
3818 octave_function *fcn = f_arg.function_value (); |
|
3819 |
|
3820 if (fcn) |
|
3821 { |
|
3822 octave_value_list tmp_args = get_feval_args (args); |
|
3823 |
|
3824 retval = feval (fcn, tmp_args, nargout); |
|
3825 } |
3021
|
3826 } |
|
3827 } |
|
3828 |
|
3829 return retval; |
|
3830 } |
|
3831 |
|
3832 DEFUN (feval, args, nargout, |
3371
|
3833 "-*- texinfo -*-\n\ |
|
3834 @deftypefn {Built-in Function} {} feval (@var{name}, @dots{})\n\ |
|
3835 Evaluate the function named @var{name}. Any arguments after the first\n\ |
|
3836 are passed on to the named function. For example,\n\ |
|
3837 \n\ |
|
3838 @example\n\ |
|
3839 feval (\"acos\", -1)\n\ |
|
3840 @result{} 3.1416\n\ |
|
3841 @end example\n\ |
3021
|
3842 \n\ |
3371
|
3843 @noindent\n\ |
|
3844 calls the function @code{acos} with the argument @samp{-1}.\n\ |
|
3845 \n\ |
|
3846 The function @code{feval} is necessary in order to be able to write\n\ |
|
3847 functions that call user-supplied functions, because Octave does not\n\ |
|
3848 have a way to declare a pointer to a function (like C) or to declare a\n\ |
|
3849 special kind of variable that can be used to hold the name of a function\n\ |
|
3850 (like @code{EXTERNAL} in Fortran). Instead, you must refer to functions\n\ |
|
3851 by name, and use @code{feval} to call them.\n\ |
|
3852 @end deftypefn") |
3021
|
3853 { |
|
3854 octave_value_list retval; |
|
3855 |
|
3856 int nargin = args.length (); |
|
3857 |
|
3858 if (nargin > 0) |
|
3859 retval = feval (args, nargout); |
|
3860 else |
5823
|
3861 print_usage (); |
3021
|
3862 |
|
3863 return retval; |
|
3864 } |
|
3865 |
3099
|
3866 octave_value_list |
3523
|
3867 eval_string (const std::string& s, bool silent, int& parse_status, int nargout) |
3021
|
3868 { |
3877
|
3869 octave_value_list retval; |
|
3870 |
3021
|
3871 unwind_protect::begin_frame ("eval_string"); |
|
3872 |
|
3873 unwind_protect_bool (get_input_from_eval_string); |
3877
|
3874 unwind_protect_bool (input_from_eval_string_pending); |
3883
|
3875 unwind_protect_bool (parser_end_of_input); |
5189
|
3876 unwind_protect_bool (line_editing); |
3021
|
3877 unwind_protect_str (current_eval_string); |
|
3878 |
|
3879 get_input_from_eval_string = true; |
3877
|
3880 input_from_eval_string_pending = true; |
3883
|
3881 parser_end_of_input = false; |
5189
|
3882 line_editing = false; |
|
3883 |
3021
|
3884 current_eval_string = s; |
|
3885 |
3877
|
3886 unwind_protect_ptr (global_command); |
|
3887 |
3021
|
3888 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3889 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
3890 |
|
3891 unwind_protect::add (restore_input_buffer, old_buf); |
|
3892 unwind_protect::add (delete_input_buffer, new_buf); |
|
3893 |
|
3894 switch_to_buffer (new_buf); |
|
3895 |
|
3896 unwind_protect_ptr (curr_sym_tab); |
|
3897 |
3877
|
3898 do |
|
3899 { |
4318
|
3900 reset_parser (); |
|
3901 |
3877
|
3902 parse_status = yyparse (); |
|
3903 |
|
3904 tree_statement_list *command = global_command; |
|
3905 |
3883
|
3906 if (parse_status == 0) |
3877
|
3907 { |
3883
|
3908 if (command) |
|
3909 { |
|
3910 retval = command->eval (silent, nargout); |
|
3911 |
|
3912 delete command; |
|
3913 |
|
3914 command = 0; |
|
3915 |
|
3916 if (error_state |
4207
|
3917 || tree_return_command::returning |
|
3918 || tree_break_command::breaking |
|
3919 || tree_continue_command::continuing) |
3883
|
3920 break; |
|
3921 } |
|
3922 else if (parser_end_of_input) |
3877
|
3923 break; |
3883
|
3924 } |
3877
|
3925 } |
|
3926 while (parse_status == 0); |
3021
|
3927 |
|
3928 unwind_protect::run_frame ("eval_string"); |
|
3929 |
|
3930 return retval; |
|
3931 } |
|
3932 |
|
3933 octave_value |
3523
|
3934 eval_string (const std::string& s, bool silent, int& parse_status) |
3021
|
3935 { |
|
3936 octave_value retval; |
|
3937 |
|
3938 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
|
3939 |
|
3940 if (! tmp.empty ()) |
|
3941 retval = tmp(0); |
|
3942 |
|
3943 return retval; |
|
3944 } |
|
3945 |
|
3946 static octave_value_list |
|
3947 eval_string (const octave_value& arg, bool silent, int& parse_status, |
|
3948 int nargout) |
|
3949 { |
3523
|
3950 std::string s = arg.string_value (); |
3021
|
3951 |
|
3952 if (error_state) |
|
3953 { |
3523
|
3954 error ("eval: expecting std::string argument"); |
4233
|
3955 return octave_value (-1); |
3021
|
3956 } |
|
3957 |
|
3958 return eval_string (s, silent, parse_status, nargout); |
|
3959 } |
|
3960 |
|
3961 DEFUN (eval, args, nargout, |
3371
|
3962 "-*- texinfo -*-\n\ |
|
3963 @deftypefn {Built-in Function} {} eval (@var{try}, @var{catch})\n\ |
|
3964 Parse the string @var{try} and evaluate it as if it were an Octave\n\ |
4463
|
3965 program. If that fails, evaluate the string @var{catch}.\n\ |
|
3966 The string @var{try} is evaluated in the current context,\n\ |
|
3967 so any results remain available after @code{eval} returns.\n\ |
3371
|
3968 @end deftypefn") |
3021
|
3969 { |
|
3970 octave_value_list retval; |
|
3971 |
|
3972 int nargin = args.length (); |
|
3973 |
|
3974 if (nargin > 0) |
|
3975 { |
|
3976 unwind_protect::begin_frame ("Feval"); |
|
3977 |
|
3978 if (nargin > 1) |
|
3979 { |
4699
|
3980 unwind_protect_int (buffer_error_messages); |
|
3981 buffer_error_messages++; |
3021
|
3982 } |
|
3983 |
|
3984 int parse_status = 0; |
|
3985 |
4463
|
3986 octave_value_list tmp = eval_string (args(0), nargout > 0, |
|
3987 parse_status, nargout); |
|
3988 |
|
3989 if (nargout > 0) |
|
3990 retval = tmp; |
3021
|
3991 |
|
3992 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
3993 { |
|
3994 error_state = 0; |
|
3995 |
|
3996 // Set up for letting the user print any messages from |
|
3997 // errors that occurred in the first part of this eval(). |
|
3998 |
4699
|
3999 buffer_error_messages--; |
3021
|
4000 |
|
4001 eval_string (args(1), 0, parse_status, nargout); |
|
4002 |
|
4003 retval = octave_value_list (); |
|
4004 } |
|
4005 |
|
4006 unwind_protect::run_frame ("Feval"); |
|
4007 } |
|
4008 else |
5823
|
4009 print_usage (); |
3021
|
4010 |
|
4011 return retval; |
|
4012 } |
|
4013 |
4661
|
4014 DEFUN (assignin, args, , |
4297
|
4015 "-*- texinfo -*-\n\ |
|
4016 @deftypefn {Built-in Function} {} assignin (@var{context}, @var{varname}, @var{value})\n\ |
|
4017 Assign @var{value} to @var{varname} in context @var{context}, which\n\ |
|
4018 may be either @code{\"base\"} or @code{\"caller\"}.\n\ |
|
4019 @end deftypefn") |
|
4020 { |
|
4021 octave_value_list retval; |
|
4022 |
|
4023 int nargin = args.length (); |
|
4024 |
|
4025 if (nargin == 3) |
|
4026 { |
|
4027 std::string context = args(0).string_value (); |
|
4028 |
|
4029 if (! error_state) |
|
4030 { |
|
4031 unwind_protect::begin_frame ("Fassignin"); |
|
4032 |
|
4033 unwind_protect_ptr (curr_sym_tab); |
|
4034 |
|
4035 if (context == "caller") |
|
4036 curr_sym_tab = curr_caller_sym_tab; |
|
4037 else if (context == "base") |
|
4038 curr_sym_tab = top_level_sym_tab; |
|
4039 else |
|
4040 error ("assignin: context must be \"caller\" or \"base\""); |
|
4041 |
|
4042 if (! error_state) |
|
4043 { |
|
4044 std::string nm = args(1).string_value (); |
|
4045 |
|
4046 if (! error_state) |
|
4047 { |
|
4048 if (valid_identifier (nm)) |
|
4049 { |
|
4050 symbol_record *sr = curr_sym_tab->lookup (nm, true); |
|
4051 |
|
4052 if (sr) |
|
4053 { |
|
4054 tree_identifier *id = new tree_identifier (sr); |
|
4055 tree_constant *rhs = new tree_constant (args(2)); |
|
4056 |
|
4057 tree_simple_assignment tsa (id, rhs); |
|
4058 |
|
4059 tsa.rvalue (); |
|
4060 } |
|
4061 } |
|
4062 else |
|
4063 error ("assignin: invalid variable name"); |
|
4064 } |
|
4065 else |
|
4066 error ("assignin: expecting variable name as second argument"); |
|
4067 } |
|
4068 |
|
4069 unwind_protect::run_frame ("Fassignin"); |
|
4070 } |
|
4071 else |
|
4072 error ("assignin: expecting string as first argument"); |
|
4073 } |
|
4074 else |
5823
|
4075 print_usage (); |
4297
|
4076 |
|
4077 return retval; |
|
4078 } |
|
4079 |
4245
|
4080 DEFUN (evalin, args, nargout, |
|
4081 "-*- texinfo -*-\n\ |
|
4082 @deftypefn {Built-in Function} {} evalin (@var{context}, @var{try}, @var{catch})\n\ |
|
4083 Like @code{eval}, except that the expressions are evaluated in the\n\ |
|
4084 context @var{context}, which may be either @code{\"caller\"} or\n\ |
4246
|
4085 @code{\"base\"}.\n\ |
4245
|
4086 @end deftypefn") |
|
4087 { |
|
4088 octave_value_list retval; |
|
4089 |
|
4090 int nargin = args.length (); |
|
4091 |
|
4092 if (nargin > 1) |
|
4093 { |
|
4094 std::string context = args(0).string_value (); |
|
4095 |
|
4096 if (! error_state) |
|
4097 { |
|
4098 unwind_protect::begin_frame ("Fevalin"); |
|
4099 |
|
4100 unwind_protect_ptr (curr_sym_tab); |
|
4101 |
|
4102 if (context == "caller") |
|
4103 curr_sym_tab = curr_caller_sym_tab; |
|
4104 else if (context == "base") |
|
4105 curr_sym_tab = top_level_sym_tab; |
|
4106 else |
|
4107 error ("evalin: context must be \"caller\" or \"base\""); |
|
4108 |
4297
|
4109 if (! error_state) |
4245
|
4110 { |
4297
|
4111 if (nargin > 2) |
|
4112 { |
4699
|
4113 unwind_protect_int (buffer_error_messages); |
|
4114 buffer_error_messages++; |
4297
|
4115 } |
|
4116 |
|
4117 int parse_status = 0; |
|
4118 |
4463
|
4119 octave_value_list tmp = eval_string (args(1), nargout > 0, |
|
4120 parse_status, nargout); |
|
4121 |
|
4122 if (nargout > 0) |
|
4123 retval = tmp; |
4297
|
4124 |
|
4125 if (nargin > 2 && (parse_status != 0 || error_state)) |
|
4126 { |
|
4127 error_state = 0; |
|
4128 |
|
4129 // Set up for letting the user print any messages from |
|
4130 // errors that occurred in the first part of this eval(). |
|
4131 |
4699
|
4132 buffer_error_messages--; |
4297
|
4133 |
|
4134 eval_string (args(2), 0, parse_status, nargout); |
|
4135 |
|
4136 retval = octave_value_list (); |
|
4137 } |
4245
|
4138 } |
|
4139 |
|
4140 unwind_protect::run_frame ("Fevalin"); |
|
4141 } |
|
4142 else |
|
4143 error ("evalin: expecting string as first argument"); |
|
4144 } |
|
4145 else |
5823
|
4146 print_usage (); |
4245
|
4147 |
|
4148 return retval; |
|
4149 } |
|
4150 |
1994
|
4151 /* |
|
4152 ;;; Local Variables: *** |
|
4153 ;;; mode: text *** |
|
4154 ;;; End: *** |
|
4155 */ |