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