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