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