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 |
3156
|
34 #include <cstdio> |
|
35 |
2427
|
36 #ifdef YYBYACC |
|
37 #include <cstdlib> |
|
38 #endif |
|
39 |
581
|
40 #include <strstream.h> |
|
41 |
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" |
1
|
47 |
2166
|
48 #include "defun.h" |
3021
|
49 #include "dynamic-ld.h" |
1351
|
50 #include "error.h" |
|
51 #include "input.h" |
|
52 #include "lex.h" |
1743
|
53 #include "oct-hist.h" |
2970
|
54 #include "ov-usr-fcn.h" |
1670
|
55 #include "toplev.h" |
1351
|
56 #include "pager.h" |
|
57 #include "parse.h" |
2987
|
58 #include "pt-all.h" |
1351
|
59 #include "symtab.h" |
|
60 #include "token.h" |
3021
|
61 #include "unwind-prot.h" |
1
|
62 #include "utils.h" |
1351
|
63 #include "variables.h" |
1
|
64 |
3021
|
65 // TRUE means we print |
|
66 static bool Vdefault_eval_print_flag = true; |
|
67 |
2166
|
68 // If TRUE, generate a warning for the assignment in things like |
|
69 // |
|
70 // octave> if (a = 2 < n) |
|
71 // |
|
72 // but not |
|
73 // |
|
74 // octave> if ((a = 2) < n) |
|
75 // |
|
76 static bool Vwarn_assign_as_truth_value; |
|
77 |
2764
|
78 // If TRUE, generate a warning for variable swich labels. |
|
79 static bool Vwarn_variable_switch_label; |
|
80 |
2166
|
81 // If TRUE, generate warning if declared function name disagrees with |
|
82 // the name of the file in which it is defined. |
|
83 static bool Vwarn_function_name_clash; |
|
84 |
3162
|
85 // TRUE means warn about function files that have time stamps in the future. |
|
86 bool Vwarn_future_time_stamp; |
|
87 |
2166
|
88 // If TRUE, generate warning if a statement in a function is not |
|
89 // terminated with a semicolon. Useful for checking functions that |
|
90 // should only produce output using explicit printing statements. |
|
91 static bool Vwarn_missing_semicolon; |
|
92 |
1
|
93 // Temporary symbol table pointer used to cope with bogus function syntax. |
522
|
94 symbol_table *tmp_local_sym_tab = 0; |
1
|
95 |
|
96 // The current input line number. |
|
97 int input_line_number = 0; |
|
98 |
|
99 // The column of the current token. |
143
|
100 int current_input_column = 1; |
1
|
101 |
338
|
102 // Buffer for help text snagged from function files. |
1755
|
103 string help_buf; |
1
|
104 |
3021
|
105 // TRUE means we are using readline. |
|
106 // (--no-line-editing) |
|
107 bool line_editing = true; |
|
108 |
|
109 // TRUE means we printed messages about reading startup files. |
|
110 bool reading_startup_message_printed = false; |
|
111 |
|
112 // TRUE means input is coming from startup file. |
|
113 bool input_from_startup_file = false; |
|
114 |
|
115 // TRUE means that input is coming from a file that was named on |
|
116 // the command line. |
|
117 bool input_from_command_line_file = true; |
|
118 |
496
|
119 // Forward declarations for some functions defined at the bottom of |
|
120 // the file. |
|
121 |
|
122 // Generic error messages. |
2970
|
123 static void |
|
124 yyerror (const char *s); |
1
|
125 |
578
|
126 // Error mesages for mismatched end tokens. |
2970
|
127 static void |
|
128 end_error (const char *type, token::end_tok_type ettype, int l, int c); |
1
|
129 |
578
|
130 // Check to see that end tokens are properly matched. |
2970
|
131 static bool |
|
132 end_token_ok (token *tok, token::end_tok_type expected); |
496
|
133 |
|
134 // Maybe print a warning if an assignment expression is used as the |
|
135 // test in a logical expression. |
2970
|
136 static void |
|
137 maybe_warn_assign_as_truth_value (tree_expression *expr); |
1
|
138 |
2764
|
139 // Maybe print a warning about switch labels that aren't constants. |
2970
|
140 static void |
|
141 maybe_warn_variable_switch_label (tree_expression *expr); |
2764
|
142 |
1623
|
143 // Create a plot command. |
2970
|
144 static tree_plot_command * |
|
145 make_plot_command (token *tok, plot_limits *range, subplot_list *list); |
1623
|
146 |
|
147 // Finish building a range. |
2970
|
148 static tree_expression * |
|
149 finish_colon_expression (tree_colon_expression *e); |
1623
|
150 |
1607
|
151 // Build a constant. |
2970
|
152 static tree_constant * |
|
153 make_constant (int op, token *tok_val); |
1607
|
154 |
578
|
155 // Build a binary expression. |
2970
|
156 static tree_expression * |
|
157 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
158 tree_expression *op2); |
578
|
159 |
2375
|
160 // Build a boolean expression. |
2970
|
161 static tree_expression * |
|
162 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
163 tree_expression *op2); |
2375
|
164 |
578
|
165 // Build a prefix expression. |
2970
|
166 static tree_expression * |
|
167 make_prefix_op (int op, tree_expression *op1, token *tok_val); |
578
|
168 |
|
169 // Build a postfix expression. |
2970
|
170 static tree_expression * |
|
171 make_postfix_op (int op, tree_expression *op1, token *tok_val); |
578
|
172 |
1623
|
173 // Build an unwind-protect command. |
2970
|
174 static tree_command * |
|
175 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
|
176 tree_statement_list *cleanup, token *end_tok); |
1623
|
177 |
|
178 // Build a try-catch command. |
2970
|
179 static tree_command * |
|
180 make_try_command (token *try_tok, tree_statement_list *body, |
|
181 tree_statement_list *cleanup, token *end_tok); |
1623
|
182 |
|
183 // Build a while command. |
2970
|
184 static tree_command * |
|
185 make_while_command (token *while_tok, tree_expression *expr, |
|
186 tree_statement_list *body, token *end_tok); |
1623
|
187 |
|
188 // Build a for command. |
2970
|
189 static tree_command * |
|
190 make_for_command (token *for_tok, tree_argument_list *lhs, |
|
191 tree_expression *expr, tree_statement_list *body, |
|
192 token *end_tok); |
1623
|
193 |
|
194 // Build a break command. |
2970
|
195 static tree_command * |
|
196 make_break_command (token *break_tok); |
1623
|
197 |
|
198 // Build a continue command. |
2970
|
199 static tree_command * |
|
200 make_continue_command (token *continue_tok); |
1623
|
201 |
|
202 // Build a return command. |
2970
|
203 static tree_command * |
|
204 make_return_command (token *return_tok); |
1623
|
205 |
|
206 // Start an if command. |
2970
|
207 static tree_if_command_list * |
|
208 start_if_command (tree_expression *expr, tree_statement_list *list); |
1623
|
209 |
|
210 // Finish an if command. |
2970
|
211 static tree_if_command * |
|
212 finish_if_command (token *if_tok, tree_if_command_list *list, token *end_tok); |
1623
|
213 |
|
214 // Build an elseif clause. |
2970
|
215 static tree_if_clause * |
|
216 make_elseif_clause (tree_expression *expr, tree_statement_list *list); |
1623
|
217 |
2764
|
218 // Finish a switch command. |
2970
|
219 static tree_switch_command * |
|
220 finish_switch_command (token *switch_tok, tree_expression *expr, |
|
221 tree_switch_case_list *list, token *end_tok); |
2764
|
222 |
|
223 // Build a switch case. |
2970
|
224 static tree_switch_case * |
|
225 make_switch_case (tree_expression *expr, tree_statement_list *list); |
2764
|
226 |
1623
|
227 // Build an assignment to a variable. |
2970
|
228 static tree_expression * |
|
229 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
230 tree_expression *rhs); |
1623
|
231 |
|
232 // Begin defining a function. |
2970
|
233 static octave_user_function * |
|
234 start_function (tree_parameter_list *param_list, tree_statement_list *body); |
1623
|
235 |
|
236 // Do most of the work for defining a function. |
2970
|
237 static octave_user_function * |
|
238 frob_function (tree_identifier *id, octave_user_function *fcn); |
1623
|
239 |
|
240 // Finish defining a function. |
2970
|
241 static octave_user_function * |
|
242 finish_function (tree_identifier *id, octave_user_function *fcn); |
1623
|
243 |
|
244 // Finish defining a function a different way. |
2970
|
245 static octave_user_function * |
|
246 finish_function (tree_parameter_list *ret_list, octave_user_function *fcn); |
751
|
247 |
2883
|
248 // Reset state after parsing function. |
2970
|
249 static void |
|
250 recover_from_parsing_function (void); |
2883
|
251 |
751
|
252 // Make an index expression. |
2970
|
253 static tree_index_expression * |
|
254 make_index_expression (tree_expression *expr, tree_argument_list *args); |
|
255 |
|
256 // Make an indirect reference expression. |
|
257 static tree_indirect_ref * |
|
258 make_indirect_ref (tree_expression *expr, const string&); |
666
|
259 |
2846
|
260 // Make a declaration command. |
2970
|
261 static tree_decl_command * |
|
262 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst); |
2846
|
263 |
1623
|
264 // Finish building a matrix list. |
2970
|
265 static tree_expression * |
|
266 finish_matrix (tree_matrix *m); |
1623
|
267 |
1511
|
268 // Maybe print a warning. Duh. |
2970
|
269 static void |
|
270 maybe_warn_missing_semi (tree_statement_list *); |
1511
|
271 |
2525
|
272 // Set the print flag for a statement based on the separator type. |
2970
|
273 static void |
|
274 set_stmt_print_flag (tree_statement_list *, char, bool); |
2525
|
275 |
1
|
276 #define ABORT_PARSE \ |
|
277 do \ |
|
278 { \ |
522
|
279 global_command = 0; \ |
1
|
280 yyerrok; \ |
2865
|
281 if (interactive || forced_interactive) \ |
1
|
282 YYACCEPT; \ |
|
283 else \ |
|
284 YYABORT; \ |
|
285 } \ |
|
286 while (0) |
|
287 |
|
288 %} |
|
289 |
666
|
290 // Bison declarations. |
|
291 |
1
|
292 %union |
|
293 { |
2891
|
294 // The type of the basic tokens returned by the lexer. |
143
|
295 token *tok_val; |
|
296 |
2891
|
297 // Types for the nonterminals we generate. |
2525
|
298 char sep_type; |
1
|
299 tree *tree_type; |
1829
|
300 tree_matrix *tree_matrix_type; |
496
|
301 tree_expression *tree_expression_type; |
2375
|
302 tree_constant *tree_constant_type; |
1
|
303 tree_identifier *tree_identifier_type; |
|
304 tree_index_expression *tree_index_expression_type; |
|
305 tree_colon_expression *tree_colon_expression_type; |
|
306 tree_argument_list *tree_argument_list_type; |
|
307 tree_parameter_list *tree_parameter_list_type; |
|
308 tree_command *tree_command_type; |
|
309 tree_if_command *tree_if_command_type; |
578
|
310 tree_if_clause *tree_if_clause_type; |
|
311 tree_if_command_list *tree_if_command_list_type; |
2764
|
312 tree_switch_command *tree_switch_command_type; |
|
313 tree_switch_case *tree_switch_case_type; |
|
314 tree_switch_case_list *tree_switch_case_list_type; |
2846
|
315 tree_decl_elt *tree_decl_elt_type; |
|
316 tree_decl_init_list *tree_decl_init_list_type; |
|
317 tree_decl_command *tree_decl_command_type; |
578
|
318 tree_statement *tree_statement_type; |
|
319 tree_statement_list *tree_statement_list_type; |
1
|
320 tree_plot_command *tree_plot_command_type; |
578
|
321 subplot *subplot_type; |
|
322 subplot_list *subplot_list_type; |
|
323 plot_limits *plot_limits_type; |
|
324 plot_range *plot_range_type; |
|
325 subplot_using *subplot_using_type; |
|
326 subplot_style *subplot_style_type; |
2891
|
327 octave_user_function *octave_user_function_type; |
1
|
328 } |
|
329 |
143
|
330 // Tokens with line and column information. |
|
331 %token <tok_val> '=' ':' '-' '+' '*' '/' |
2883
|
332 %token <tok_val> ADD_EQ SUB_EQ MUL_EQ DIV_EQ EMUL_EQ EDIV_EQ AND_EQ OR_EQ |
2899
|
333 %token <tok_val> LSHIFT_EQ RSHIFT_EQ LSHIFT RSHIFT |
428
|
334 %token <tok_val> EXPR_AND_AND EXPR_OR_OR |
143
|
335 %token <tok_val> EXPR_AND EXPR_OR EXPR_NOT |
|
336 %token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
1276
|
337 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS |
|
338 %token <tok_val> QUOTE TRANSPOSE |
143
|
339 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW |
|
340 %token <tok_val> NUM IMAG_NUM |
2970
|
341 %token <tok_val> STRUCT_ELT |
2883
|
342 %token <tok_val> NAME |
143
|
343 %token <tok_val> END |
|
344 %token <tok_val> PLOT |
|
345 %token <tok_val> TEXT STYLE |
1491
|
346 %token <tok_val> FOR WHILE |
|
347 %token <tok_val> IF ELSEIF ELSE |
2764
|
348 %token <tok_val> SWITCH CASE OTHERWISE |
1491
|
349 %token <tok_val> BREAK CONTINUE FUNC_RET |
924
|
350 %token <tok_val> UNWIND CLEANUP |
1489
|
351 %token <tok_val> TRY CATCH |
2846
|
352 %token <tok_val> GLOBAL STATIC |
1
|
353 |
143
|
354 // Other tokens. |
2970
|
355 %token END_OF_INPUT LEXICAL_ERROR |
|
356 %token FCN ELLIPSIS ALL_VA_ARGS |
883
|
357 %token USING TITLE WITH COLON OPEN_BRACE CLOSE_BRACE CLEAR |
1
|
358 |
143
|
359 // Nonterminals we construct. |
2525
|
360 %type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep |
578
|
361 %type <tree_type> input |
2960
|
362 %type <tree_constant_type> constant magic_colon |
1829
|
363 %type <tree_matrix_type> rows rows1 |
2970
|
364 %type <tree_expression_type> title matrix |
|
365 %type <tree_expression_type> primary_expr postfix_expr prefix_expr binary_expr |
|
366 %type <tree_expression_type> simple_expr colon_expr assign_expr expression |
1
|
367 %type <tree_identifier_type> identifier |
2891
|
368 %type <octave_user_function_type> function1 function2 function3 |
2970
|
369 %type <tree_index_expression_type> word_list_cmd |
|
370 %type <tree_colon_expression_type> colon_expr1 |
|
371 %type <tree_argument_list_type> arg_list word_list assign_lhs matrix_row |
723
|
372 %type <tree_parameter_list_type> param_list param_list1 |
|
373 %type <tree_parameter_list_type> return_list return_list1 |
2970
|
374 %type <tree_command_type> command select_command loop_command |
|
375 %type <tree_command_type> jump_command except_command function |
578
|
376 %type <tree_if_command_type> if_command |
|
377 %type <tree_if_clause_type> elseif_clause else_clause |
|
378 %type <tree_if_command_list_type> if_cmd_list1 if_cmd_list |
2764
|
379 %type <tree_switch_command_type> switch_command |
|
380 %type <tree_switch_case_type> switch_case default_case |
|
381 %type <tree_switch_case_list_type> case_list1 case_list |
2846
|
382 %type <tree_decl_elt_type> decl2 |
|
383 %type <tree_decl_init_list_type> decl1 |
|
384 %type <tree_decl_command_type> declaration |
578
|
385 %type <tree_statement_type> statement |
627
|
386 %type <tree_statement_list_type> simple_list simple_list1 list list1 |
2891
|
387 %type <tree_statement_list_type> opt_list input1 function4 |
1
|
388 %type <tree_plot_command_type> plot_command |
578
|
389 %type <subplot_type> plot_command2 plot_options |
|
390 %type <subplot_list_type> plot_command1 |
|
391 %type <plot_limits_type> ranges |
|
392 %type <plot_range_type> ranges1 |
|
393 %type <subplot_using_type> using using1 |
|
394 %type <subplot_style_type> style |
1
|
395 |
143
|
396 // Precedence and associativity. |
1
|
397 %left ';' ',' '\n' |
2899
|
398 %right '=' ADD_EQ SUB_EQ MUL_EQ DIV_EQ EMUL_EQ EDIV_EQ OR_EQ AND_EQ LSHIFT_EQ RSHIFT_EQ |
428
|
399 %left EXPR_AND_AND EXPR_OR_OR |
1
|
400 %left EXPR_AND EXPR_OR |
|
401 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
2899
|
402 %left LSHIFT RSHIFT |
1
|
403 %left ':' |
1276
|
404 %left '-' '+' EPLUS EMINUS |
1
|
405 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
406 %left QUOTE TRANSPOSE |
|
407 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
408 %right POW EPOW |
2970
|
409 %left '(' '.' |
1
|
410 |
143
|
411 // Where to start. |
1
|
412 %start input |
|
413 |
|
414 %% |
|
415 |
2970
|
416 // ============================== |
|
417 // Statements and statement lists |
|
418 // ============================== |
|
419 |
627
|
420 input : input1 |
1
|
421 { |
627
|
422 global_command = $1; |
1
|
423 promptflag = 1; |
|
424 YYACCEPT; |
|
425 } |
|
426 | END_OF_INPUT |
|
427 { |
522
|
428 global_command = 0; |
1
|
429 promptflag = 1; |
|
430 YYABORT; |
|
431 } |
627
|
432 | simple_list parse_error |
1091
|
433 { ABORT_PARSE; } |
627
|
434 | parse_error |
1091
|
435 { ABORT_PARSE; } |
627
|
436 ; |
|
437 |
|
438 input1 : '\n' |
|
439 { $$ = 0; } |
327
|
440 | simple_list |
627
|
441 { $$ = $1; } |
1
|
442 | simple_list '\n' |
627
|
443 { $$ = $1; } |
1
|
444 | simple_list END_OF_INPUT |
627
|
445 { $$ = $1; } |
|
446 ; |
|
447 |
2525
|
448 simple_list : simple_list1 opt_sep_no_nl |
1
|
449 { |
2525
|
450 set_stmt_print_flag ($1, $2, false); |
1511
|
451 $$ = $1; |
1
|
452 } |
|
453 ; |
|
454 |
578
|
455 simple_list1 : statement |
|
456 { $$ = new tree_statement_list ($1); } |
2525
|
457 | simple_list1 sep_no_nl statement |
1
|
458 { |
2525
|
459 set_stmt_print_flag ($1, $2, false); |
578
|
460 $1->append ($3); |
1511
|
461 $$ = $1; |
1
|
462 } |
|
463 ; |
|
464 |
|
465 opt_list : // empty |
578
|
466 { $$ = new tree_statement_list (); } |
1
|
467 | list |
|
468 { $$ = $1; } |
496
|
469 ; |
1
|
470 |
2525
|
471 list : list1 opt_sep |
1511
|
472 { |
2525
|
473 set_stmt_print_flag ($1, $2, true); |
1829
|
474 $$ = $1; |
1
|
475 } |
|
476 ; |
|
477 |
578
|
478 list1 : statement |
440
|
479 { |
2857
|
480 lexer_flags.beginning_of_function = false; |
578
|
481 $$ = new tree_statement_list ($1); |
440
|
482 } |
2525
|
483 | list1 sep statement |
1511
|
484 { |
2525
|
485 set_stmt_print_flag ($1, $2, true); |
578
|
486 $1->append ($3); |
1511
|
487 $$ = $1; |
1
|
488 } |
|
489 ; |
|
490 |
2970
|
491 statement : expression |
578
|
492 { $$ = new tree_statement ($1); } |
2970
|
493 | command |
578
|
494 { $$ = new tree_statement ($1); } |
883
|
495 | PLOT CLEAR |
|
496 { |
|
497 symbol_record *sr = lookup_by_name ("clearplot", 0); |
|
498 tree_identifier *id = new tree_identifier (sr); |
|
499 $$ = new tree_statement (id); |
|
500 } |
1
|
501 ; |
|
502 |
2970
|
503 // =========== |
|
504 // Expressions |
|
505 // =========== |
|
506 |
|
507 identifier : NAME |
|
508 { |
|
509 $$ = new tree_identifier |
|
510 ($1->sym_rec (), $1->line (), $1->column ()); |
|
511 } |
|
512 ; |
|
513 |
|
514 constant : NUM |
|
515 { $$ = make_constant (NUM, $1); } |
|
516 | IMAG_NUM |
|
517 { $$ = make_constant (IMAG_NUM, $1); } |
|
518 | TEXT |
|
519 { $$ = make_constant (TEXT, $1); } |
|
520 ; |
|
521 |
|
522 matrix : '[' ']' |
|
523 { $$ = new tree_constant (octave_value (Matrix ())); } |
|
524 | '[' ';' ']' |
|
525 { $$ = new tree_constant (octave_value (Matrix ())); } |
|
526 | '[' rows ']' |
|
527 { $$ = finish_matrix ($2); } |
|
528 ; |
|
529 |
|
530 rows : rows1 |
|
531 { $$ = $1; } |
|
532 | rows1 ';' // Ignore trailing semicolon. |
|
533 { $$ = $1; } |
|
534 ; |
|
535 |
|
536 rows1 : matrix_row |
|
537 { $$ = new tree_matrix ($1); } |
|
538 | rows1 ';' matrix_row |
|
539 { |
|
540 $1->append ($3); |
|
541 $$ = $1; |
|
542 } |
|
543 ; |
|
544 |
|
545 matrix_row : arg_list |
|
546 { $$ = $1; } |
|
547 | arg_list ',' // Ignore trailing comma. |
|
548 { $$ = $1; } |
|
549 ; |
|
550 |
|
551 primary_expr : identifier |
|
552 { $$ = $1; } |
|
553 | constant |
|
554 { $$ = $1; } |
|
555 | matrix |
|
556 { $$ = $1; } |
|
557 | '(' expression ')' |
|
558 { $$ = $2->mark_in_parens (); } |
|
559 ; |
|
560 |
|
561 magic_colon : ':' |
|
562 { |
|
563 octave_value tmp (octave_value::magic_colon_t); |
|
564 $$ = new tree_constant (tmp); |
|
565 } |
|
566 ; |
|
567 |
|
568 arg_list : expression |
|
569 { $$ = new tree_argument_list ($1); } |
|
570 | magic_colon |
|
571 { $$ = new tree_argument_list ($1); } |
|
572 | ALL_VA_ARGS |
|
573 { |
|
574 octave_value tmp (octave_value::all_va_args_t); |
|
575 tree_constant *all_va_args = new tree_constant (tmp); |
|
576 $$ = new tree_argument_list (all_va_args); |
|
577 } |
|
578 | arg_list ',' magic_colon |
|
579 { |
|
580 $1->append ($3); |
|
581 $$ = $1; |
|
582 } |
|
583 | arg_list ',' expression |
|
584 { |
|
585 $1->append ($3); |
|
586 $$ = $1; |
|
587 } |
|
588 | arg_list ',' ALL_VA_ARGS |
|
589 { |
|
590 octave_value tmp (octave_value::all_va_args_t); |
|
591 tree_constant *all_va_args = new tree_constant (tmp); |
|
592 $1->append (all_va_args); |
|
593 $$ = $1; |
|
594 } |
|
595 ; |
|
596 |
|
597 parsing_indir : // empty |
|
598 { lexer_flags.looking_at_indirect_ref = true; } |
|
599 ; |
|
600 |
|
601 postfix_expr : primary_expr |
|
602 { $$ = $1; } |
|
603 | postfix_expr '(' ')' |
|
604 { $$ = make_index_expression ($1, 0); } |
|
605 | postfix_expr '(' arg_list ')' |
|
606 { $$ = make_index_expression ($1, $3); } |
|
607 | postfix_expr PLUS_PLUS |
|
608 { $$ = make_postfix_op (PLUS_PLUS, $1, $2); } |
|
609 | postfix_expr MINUS_MINUS |
|
610 { $$ = make_postfix_op (MINUS_MINUS, $1, $2); } |
|
611 | postfix_expr QUOTE |
|
612 { $$ = make_postfix_op (QUOTE, $1, $2); } |
|
613 | postfix_expr TRANSPOSE |
|
614 { $$ = make_postfix_op (TRANSPOSE, $1, $2); } |
|
615 | postfix_expr '.' parsing_indir STRUCT_ELT |
|
616 { $$ = make_indirect_ref ($1, $4->text ()); } |
|
617 ; |
|
618 |
|
619 prefix_expr : postfix_expr |
|
620 { $$ = $1; } |
|
621 | PLUS_PLUS prefix_expr %prec UNARY |
|
622 { $$ = make_prefix_op (PLUS_PLUS, $2, $1); } |
|
623 | MINUS_MINUS prefix_expr %prec UNARY |
|
624 { $$ = make_prefix_op (MINUS_MINUS, $2, $1); } |
|
625 | EXPR_NOT prefix_expr %prec UNARY |
|
626 { $$ = make_prefix_op (EXPR_NOT, $2, $1); } |
|
627 | '+' prefix_expr %prec UNARY |
|
628 { $$ = $2; } |
|
629 | '-' prefix_expr %prec UNARY |
|
630 { $$ = make_prefix_op ('-', $2, $1); } |
|
631 ; |
|
632 |
|
633 binary_expr : prefix_expr |
|
634 { $$ = $1; } |
|
635 | binary_expr POW binary_expr |
|
636 { $$ = make_binary_op (POW, $1, $2, $3); } |
|
637 | binary_expr EPOW binary_expr |
|
638 { $$ = make_binary_op (EPOW, $1, $2, $3); } |
|
639 | binary_expr '+' binary_expr |
|
640 { $$ = make_binary_op ('+', $1, $2, $3); } |
|
641 | binary_expr '-' binary_expr |
|
642 { $$ = make_binary_op ('-', $1, $2, $3); } |
|
643 | binary_expr '*' binary_expr |
|
644 { $$ = make_binary_op ('*', $1, $2, $3); } |
|
645 | binary_expr '/' binary_expr |
|
646 { $$ = make_binary_op ('/', $1, $2, $3); } |
|
647 | binary_expr EPLUS binary_expr |
|
648 { $$ = make_binary_op ('+', $1, $2, $3); } |
|
649 | binary_expr EMINUS binary_expr |
|
650 { $$ = make_binary_op ('-', $1, $2, $3); } |
|
651 | binary_expr EMUL binary_expr |
|
652 { $$ = make_binary_op (EMUL, $1, $2, $3); } |
|
653 | binary_expr EDIV binary_expr |
|
654 { $$ = make_binary_op (EDIV, $1, $2, $3); } |
|
655 | binary_expr LEFTDIV binary_expr |
|
656 { $$ = make_binary_op (LEFTDIV, $1, $2, $3); } |
|
657 | binary_expr ELEFTDIV binary_expr |
|
658 { $$ = make_binary_op (ELEFTDIV, $1, $2, $3); } |
|
659 ; |
|
660 |
|
661 colon_expr : colon_expr1 |
|
662 { $$ = finish_colon_expression ($1); } |
|
663 ; |
|
664 |
|
665 colon_expr1 : binary_expr |
|
666 { $$ = new tree_colon_expression ($1); } |
|
667 | colon_expr1 ':' binary_expr |
|
668 { |
|
669 if (! ($$ = $1->append ($3))) |
|
670 ABORT_PARSE; |
|
671 } |
|
672 ; |
|
673 |
|
674 simple_expr : colon_expr |
|
675 { $$ = $1; } |
|
676 | simple_expr LSHIFT simple_expr |
|
677 { $$ = make_binary_op (LSHIFT, $1, $2, $3); } |
|
678 | simple_expr RSHIFT simple_expr |
|
679 { $$ = make_binary_op (RSHIFT, $1, $2, $3); } |
|
680 | simple_expr EXPR_LT simple_expr |
|
681 { $$ = make_binary_op (EXPR_LT, $1, $2, $3); } |
|
682 | simple_expr EXPR_LE simple_expr |
|
683 { $$ = make_binary_op (EXPR_LE, $1, $2, $3); } |
|
684 | simple_expr EXPR_EQ simple_expr |
|
685 { $$ = make_binary_op (EXPR_EQ, $1, $2, $3); } |
|
686 | simple_expr EXPR_GE simple_expr |
|
687 { $$ = make_binary_op (EXPR_GE, $1, $2, $3); } |
|
688 | simple_expr EXPR_GT simple_expr |
|
689 { $$ = make_binary_op (EXPR_GT, $1, $2, $3); } |
|
690 | simple_expr EXPR_NE simple_expr |
|
691 { $$ = make_binary_op (EXPR_NE, $1, $2, $3); } |
|
692 | simple_expr EXPR_AND simple_expr |
|
693 { $$ = make_binary_op (EXPR_AND, $1, $2, $3); } |
|
694 | simple_expr EXPR_OR simple_expr |
|
695 { $$ = make_binary_op (EXPR_OR, $1, $2, $3); } |
|
696 | simple_expr EXPR_AND_AND simple_expr |
|
697 { $$ = make_boolean_op (EXPR_AND_AND, $1, $2, $3); } |
|
698 | simple_expr EXPR_OR_OR simple_expr |
|
699 { $$ = make_boolean_op (EXPR_OR_OR, $1, $2, $3); } |
|
700 ; |
|
701 |
|
702 // Arrange for the lexer to return CLOSE_BRACE for `]' by looking ahead |
|
703 // one token for an assignment op. |
|
704 |
|
705 assign_lhs : simple_expr |
|
706 { $$ = new tree_argument_list ($1); } |
|
707 | '[' arg_list CLOSE_BRACE |
|
708 { $$ = $2; } |
|
709 ; |
|
710 |
|
711 assign_expr : assign_lhs '=' expression |
|
712 { $$ = make_assign_op ('=', $1, $2, $3); } |
|
713 | assign_lhs ADD_EQ expression |
|
714 { $$ = make_assign_op (ADD_EQ, $1, $2, $3); } |
|
715 | assign_lhs SUB_EQ expression |
|
716 { $$ = make_assign_op (SUB_EQ, $1, $2, $3); } |
|
717 | assign_lhs MUL_EQ expression |
|
718 { $$ = make_assign_op (MUL_EQ, $1, $2, $3); } |
|
719 | assign_lhs DIV_EQ expression |
|
720 { $$ = make_assign_op (DIV_EQ, $1, $2, $3); } |
|
721 | assign_lhs LSHIFT_EQ expression |
|
722 { $$ = make_assign_op (LSHIFT_EQ, $1, $2, $3); } |
|
723 | assign_lhs RSHIFT_EQ expression |
|
724 { $$ = make_assign_op (RSHIFT_EQ, $1, $2, $3); } |
|
725 | assign_lhs EMUL_EQ expression |
|
726 { $$ = make_assign_op (EMUL_EQ, $1, $2, $3); } |
|
727 | assign_lhs EDIV_EQ expression |
|
728 { $$ = make_assign_op (EDIV_EQ, $1, $2, $3); } |
|
729 | assign_lhs AND_EQ expression |
|
730 { $$ = make_assign_op (AND_EQ, $1, $2, $3); } |
|
731 | assign_lhs OR_EQ expression |
|
732 { $$ = make_assign_op (OR_EQ, $1, $2, $3); } |
|
733 ; |
|
734 |
|
735 word_list_cmd : identifier word_list |
|
736 { $$ = make_index_expression ($1, $2); } |
|
737 ; |
|
738 |
|
739 word_list : TEXT |
|
740 { |
|
741 tree_constant *tmp = make_constant (TEXT, $1); |
|
742 $$ = new tree_argument_list (tmp); |
|
743 } |
|
744 | word_list TEXT |
|
745 { |
|
746 tree_constant *tmp = make_constant (TEXT, $2); |
|
747 $1->append (tmp); |
|
748 $$ = $1; |
|
749 } |
|
750 ; |
|
751 |
|
752 expression : simple_expr |
|
753 { $$ = $1; } |
|
754 | word_list_cmd |
|
755 { $$ = $1; } |
|
756 | assign_expr |
|
757 { $$ = $1; } |
|
758 ; |
|
759 |
|
760 // ================================================ |
|
761 // Commands, declarations, and function definitions |
|
762 // ================================================ |
|
763 |
|
764 command : declaration |
|
765 { $$ = $1; } |
|
766 | select_command |
|
767 { $$ = $1; } |
|
768 | loop_command |
|
769 { $$ = $1; } |
|
770 | jump_command |
|
771 { $$ = $1; } |
|
772 | except_command |
|
773 { $$ = $1; } |
|
774 | function |
|
775 { $$ = $1; } |
|
776 | plot_command |
|
777 { $$ = $1; } |
|
778 ; |
|
779 |
|
780 // ===================== |
|
781 // Declaration statemnts |
|
782 // ===================== |
|
783 |
|
784 declaration : GLOBAL decl1 |
|
785 { $$ = make_decl_command (GLOBAL, $1, $2); } |
|
786 | STATIC decl1 |
|
787 { $$ = make_decl_command (STATIC, $1, $2); } |
|
788 ; |
|
789 |
|
790 decl1 : decl2 |
|
791 { $$ = new tree_decl_init_list ($1); } |
|
792 | decl1 decl2 |
|
793 { |
|
794 $1->append ($2); |
|
795 $$ = $1; |
|
796 } |
|
797 ; |
|
798 |
|
799 decl2 : identifier |
|
800 { $$ = new tree_decl_elt ($1); } |
|
801 | identifier '=' expression |
|
802 { $$ = new tree_decl_elt ($1, $3); } |
|
803 ; |
|
804 |
|
805 // ==================== |
|
806 // Selection statements |
|
807 // ==================== |
|
808 |
|
809 select_command : if_command |
|
810 { $$ = $1; } |
|
811 | switch_command |
|
812 { $$ = $1; } |
|
813 ; |
|
814 |
|
815 // ============ |
|
816 // If statement |
|
817 // ============ |
|
818 |
|
819 if_command : IF if_cmd_list END |
|
820 { |
|
821 if (! ($$ = finish_if_command ($1, $2, $3))) |
|
822 ABORT_PARSE; |
|
823 } |
|
824 ; |
|
825 |
|
826 if_cmd_list : if_cmd_list1 |
|
827 { $$ = $1; } |
|
828 | if_cmd_list1 else_clause |
|
829 { |
|
830 $1->append ($2); |
|
831 $$ = $1; |
|
832 } |
|
833 ; |
|
834 |
|
835 if_cmd_list1 : expression opt_sep opt_list |
|
836 { $$ = start_if_command ($1, $3); } |
|
837 | if_cmd_list1 elseif_clause |
|
838 { |
|
839 $1->append ($2); |
|
840 $$ = $1; |
|
841 } |
|
842 ; |
|
843 |
|
844 elseif_clause : ELSEIF opt_sep expression opt_sep opt_list |
|
845 { $$ = make_elseif_clause ($3, $5); } |
|
846 ; |
|
847 |
|
848 else_clause : ELSE opt_sep opt_list |
|
849 { $$ = new tree_if_clause ($3); } |
|
850 ; |
|
851 |
|
852 // ================ |
|
853 // Switch statement |
|
854 // ================ |
|
855 |
|
856 switch_command : SWITCH expression opt_sep case_list END |
|
857 { |
|
858 if (! ($$ = finish_switch_command ($1, $2, $4, $5))) |
|
859 ABORT_PARSE; |
|
860 } |
|
861 ; |
|
862 |
|
863 case_list : case_list1 |
|
864 { $$ = $1; } |
|
865 | case_list1 default_case |
|
866 { |
|
867 $1->append ($2); |
|
868 $$ = $1; |
|
869 } |
|
870 ; |
|
871 |
|
872 case_list1 : switch_case |
|
873 { $$ = new tree_switch_case_list ($1); } |
|
874 | case_list1 switch_case |
|
875 { |
|
876 $1->append ($2); |
|
877 $$ = $1; |
|
878 } |
|
879 ; |
|
880 |
|
881 switch_case : CASE opt_sep expression opt_sep list |
|
882 { $$ = make_switch_case ($3, $5); } |
|
883 ; |
|
884 |
|
885 default_case : OTHERWISE opt_sep opt_list |
|
886 { $$ = new tree_switch_case ($3); } |
|
887 ; |
|
888 |
|
889 // ======= |
|
890 // Looping |
|
891 // ======= |
|
892 |
|
893 loop_command : WHILE expression opt_sep opt_list END |
|
894 { |
|
895 if (! ($$ = make_while_command ($1, $2, $4, $5))) |
|
896 ABORT_PARSE; |
|
897 } |
|
898 | FOR assign_lhs '=' expression opt_sep opt_list END |
|
899 { |
|
900 if (! ($$ = make_for_command ($1, $2, $4, $6, $7))) |
|
901 ABORT_PARSE; |
|
902 } |
|
903 ; |
|
904 |
|
905 // ======= |
|
906 // Jumping |
|
907 // ======= |
|
908 |
|
909 jump_command : BREAK |
|
910 { |
|
911 if (! ($$ = make_break_command ($1))) |
|
912 ABORT_PARSE; |
|
913 } |
|
914 | CONTINUE |
|
915 { |
|
916 if (! ($$ = make_continue_command ($1))) |
|
917 ABORT_PARSE; |
|
918 } |
|
919 | FUNC_RET |
|
920 { |
|
921 if (! ($$ = make_return_command ($1))) |
|
922 ABORT_PARSE; |
|
923 } |
|
924 ; |
|
925 |
|
926 // ========== |
|
927 // Exceptions |
|
928 // ========== |
|
929 |
|
930 except_command : UNWIND opt_sep opt_list CLEANUP opt_sep opt_list END |
|
931 { |
|
932 if (! ($$ = make_unwind_command ($1, $3, $6, $7))) |
|
933 ABORT_PARSE; |
|
934 } |
|
935 | TRY opt_sep opt_list CATCH opt_sep opt_list END |
|
936 { |
|
937 if (! ($$ = make_try_command ($1, $3, $6, $7))) |
|
938 ABORT_PARSE; |
|
939 } |
|
940 ; |
|
941 |
|
942 // =========================================== |
|
943 // Some `subroutines' for function definitions |
|
944 // =========================================== |
|
945 |
|
946 global_symtab : // empty |
|
947 { curr_sym_tab = global_sym_tab; } |
|
948 ; |
|
949 |
|
950 local_symtab : // empty |
|
951 { curr_sym_tab = tmp_local_sym_tab; } |
|
952 ; |
|
953 |
|
954 in_return_list : // empty |
|
955 { lexer_flags.looking_at_return_list = true; } |
|
956 ; |
|
957 |
|
958 parsed_fcn_name : // empty |
|
959 { lexer_flags.parsed_function_name = true; } |
|
960 ; |
|
961 |
|
962 // =========================== |
|
963 // List of function parameters |
|
964 // =========================== |
|
965 |
|
966 param_list_beg : '(' |
|
967 { lexer_flags.looking_at_parameter_list = true; } |
|
968 ; |
|
969 |
|
970 param_list_end : ')' |
|
971 { lexer_flags.looking_at_parameter_list = false; } |
|
972 ; |
|
973 |
|
974 param_list : param_list_beg param_list_end |
|
975 { |
|
976 lexer_flags.quote_is_transpose = false; |
|
977 $$ = 0; |
|
978 } |
|
979 | param_list_beg ELLIPSIS param_list_end |
|
980 { |
|
981 lexer_flags.quote_is_transpose = false; |
|
982 tree_parameter_list *tmp = new tree_parameter_list (); |
|
983 tmp->mark_varargs_only (); |
|
984 $$ = tmp; |
|
985 } |
|
986 | param_list1 param_list_end |
|
987 { |
|
988 lexer_flags.quote_is_transpose = false; |
|
989 $1->mark_as_formal_parameters (); |
|
990 $$ = $1; |
|
991 } |
|
992 | param_list1 ',' ELLIPSIS param_list_end |
|
993 { |
|
994 lexer_flags.quote_is_transpose = false; |
|
995 $1->mark_as_formal_parameters (); |
|
996 $1->mark_varargs (); |
|
997 $$ = $1; |
|
998 } |
|
999 ; |
|
1000 |
|
1001 param_list1 : param_list_beg identifier |
|
1002 { $$ = new tree_parameter_list ($2); } |
|
1003 | param_list1 ',' identifier |
|
1004 { |
|
1005 $1->append ($3); |
|
1006 $$ = $1; |
|
1007 } |
|
1008 | param_list_beg error |
|
1009 { |
|
1010 yyerror ("invalid parameter list"); |
|
1011 $$ = 0; |
|
1012 ABORT_PARSE; |
|
1013 } |
|
1014 | param_list1 ',' error |
|
1015 { |
|
1016 yyerror ("invalid parameter list"); |
|
1017 $$ = 0; |
|
1018 ABORT_PARSE; |
|
1019 } |
|
1020 ; |
|
1021 |
|
1022 // =================================== |
|
1023 // List of function return value names |
|
1024 // =================================== |
|
1025 |
|
1026 return_list_beg : '[' local_symtab in_return_list |
|
1027 ; |
|
1028 |
|
1029 return_list : return_list_beg return_list_end |
|
1030 { |
|
1031 lexer_flags.looking_at_return_list = false; |
|
1032 $$ = new tree_parameter_list (); |
|
1033 } |
|
1034 | return_list_beg ELLIPSIS return_list_end |
|
1035 { |
|
1036 lexer_flags.looking_at_return_list = false; |
|
1037 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1038 tmp->mark_varargs_only (); |
|
1039 $$ = tmp; |
|
1040 } |
|
1041 | return_list_beg return_list1 return_list_end |
|
1042 { |
|
1043 lexer_flags.looking_at_return_list = false; |
|
1044 $$ = $2; |
|
1045 } |
|
1046 | return_list_beg return_list1 ',' ELLIPSIS return_list_end |
|
1047 { |
|
1048 lexer_flags.looking_at_return_list = false; |
|
1049 $2->mark_varargs (); |
|
1050 $$ = $2; |
|
1051 } |
|
1052 ; |
|
1053 |
|
1054 return_list1 : identifier |
|
1055 { $$ = new tree_parameter_list ($1); } |
|
1056 | return_list1 ',' identifier |
|
1057 { |
|
1058 $1->append ($3); |
|
1059 $$ = $1; |
|
1060 } |
|
1061 ; |
|
1062 |
|
1063 return_list_end : global_symtab ']' |
|
1064 ; |
|
1065 |
|
1066 // =================== |
|
1067 // Function definition |
|
1068 // =================== |
|
1069 |
|
1070 function_beg : FCN global_symtab |
|
1071 ; |
|
1072 |
|
1073 function : function_beg function2 |
|
1074 { |
|
1075 recover_from_parsing_function (); |
|
1076 $$ = 0; |
|
1077 } |
|
1078 | function_beg identifier function1 |
|
1079 { |
|
1080 finish_function ($2, $3); |
|
1081 recover_from_parsing_function (); |
|
1082 $$ = 0; |
|
1083 } |
|
1084 | function_beg return_list function1 |
|
1085 { |
|
1086 finish_function ($2, $3); |
|
1087 recover_from_parsing_function (); |
|
1088 $$ = 0; |
|
1089 } |
|
1090 ; |
|
1091 |
|
1092 function1 : global_symtab '=' function2 |
|
1093 { $$ = $3; } |
|
1094 ; |
|
1095 |
|
1096 function2 : identifier local_symtab parsed_fcn_name function3 |
|
1097 { |
|
1098 if (! ($$ = frob_function ($1, $4))) |
|
1099 ABORT_PARSE; |
|
1100 } |
|
1101 ; |
|
1102 |
|
1103 function3 : param_list function4 |
|
1104 { $$ = start_function ($1, $2); } |
|
1105 | function4 |
|
1106 { $$ = start_function (0, $1); } |
|
1107 ; |
|
1108 |
|
1109 function4 : opt_sep opt_list function_end |
|
1110 { $$ = $2; } |
|
1111 ; |
|
1112 |
|
1113 function_end : END |
|
1114 { |
|
1115 if (end_token_ok ($1, token::function_end)) |
|
1116 { |
|
1117 if (reading_fcn_file) |
|
1118 check_for_garbage_after_fcn_def (); |
|
1119 } |
|
1120 else |
|
1121 ABORT_PARSE; |
|
1122 } |
|
1123 | END_OF_INPUT |
|
1124 { |
|
1125 if (! (reading_fcn_file || reading_script_file)) |
|
1126 YYABORT; |
|
1127 } |
|
1128 ; |
|
1129 |
|
1130 // ======== |
|
1131 // Plotting |
|
1132 // ======== |
|
1133 |
1
|
1134 plot_command : PLOT plot_command1 |
|
1135 { |
1623
|
1136 if (! ($$ = make_plot_command ($1, 0, $2))) |
|
1137 ABORT_PARSE; |
1
|
1138 } |
|
1139 | PLOT ranges plot_command1 |
|
1140 { |
1623
|
1141 if (! ($$ = make_plot_command ($1, $2, $3))) |
|
1142 ABORT_PARSE; |
1
|
1143 } |
|
1144 ; |
|
1145 |
|
1146 ranges : ranges1 |
578
|
1147 { $$ = new plot_limits ($1); } |
1
|
1148 | ranges1 ranges1 |
578
|
1149 { $$ = new plot_limits ($1, $2); } |
1
|
1150 | ranges1 ranges1 ranges1 |
578
|
1151 { $$ = new plot_limits ($1, $2, $3); } |
1
|
1152 ; |
|
1153 |
|
1154 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
578
|
1155 { $$ = new plot_range ($2, $4); } |
1
|
1156 | OPEN_BRACE COLON expression CLOSE_BRACE |
578
|
1157 { $$ = new plot_range (0, $3); } |
1
|
1158 | OPEN_BRACE expression COLON CLOSE_BRACE |
578
|
1159 { $$ = new plot_range ($2, 0); } |
1
|
1160 | OPEN_BRACE COLON CLOSE_BRACE |
578
|
1161 { $$ = new plot_range (); } |
1
|
1162 | OPEN_BRACE CLOSE_BRACE |
578
|
1163 { $$ = new plot_range (); } |
1
|
1164 ; |
|
1165 |
478
|
1166 plot_command1 : // empty |
522
|
1167 { $$ = 0; } |
478
|
1168 | plot_command2 |
578
|
1169 { $$ = new subplot_list ($1); } |
1
|
1170 | plot_command1 ',' plot_command2 |
1829
|
1171 { |
|
1172 $1->append ($3); |
|
1173 $$ = $1; |
|
1174 } |
1
|
1175 ; |
|
1176 |
|
1177 plot_command2 : expression |
578
|
1178 { $$ = new subplot ($1); } |
1
|
1179 | expression plot_options |
1623
|
1180 { $$ = $2->set_data ($1); } |
1
|
1181 ; |
|
1182 |
|
1183 plot_options : using |
578
|
1184 { $$ = new subplot ($1, 0, 0); } |
1
|
1185 | title |
578
|
1186 { $$ = new subplot (0, $1, 0); } |
1
|
1187 | style |
578
|
1188 { $$ = new subplot (0, 0, $1); } |
1
|
1189 | using title |
578
|
1190 { $$ = new subplot ($1, $2, 0); } |
|
1191 | title using |
|
1192 { $$ = new subplot ($2, $1, 0); } |
|
1193 | using style |
|
1194 { $$ = new subplot ($1, 0, $2); } |
|
1195 | style using |
|
1196 { $$ = new subplot ($2, 0, $1); } |
|
1197 | title style |
|
1198 { $$ = new subplot (0, $1, $2); } |
|
1199 | style title |
|
1200 { $$ = new subplot (0, $2, $1); } |
|
1201 | using title style |
|
1202 { $$ = new subplot ($1, $2, $3); } |
|
1203 | using style title |
|
1204 { $$ = new subplot ($1, $3, $2); } |
|
1205 | title using style |
|
1206 { $$ = new subplot ($2, $1, $3); } |
|
1207 | title style using |
|
1208 { $$ = new subplot ($3, $1, $2); } |
|
1209 | style using title |
|
1210 { $$ = new subplot ($2, $3, $1); } |
|
1211 | style title using |
|
1212 { $$ = new subplot ($3, $2, $1); } |
1
|
1213 ; |
|
1214 |
|
1215 using : using1 |
|
1216 { |
2857
|
1217 lexer_flags.in_plot_using = false; |
1
|
1218 $$ = $1; |
|
1219 } |
|
1220 | using1 expression |
|
1221 { |
2857
|
1222 lexer_flags.in_plot_using = false; |
1
|
1223 $$ = $1->set_format ($2); |
|
1224 } |
|
1225 ; |
|
1226 |
|
1227 using1 : USING expression |
|
1228 { |
578
|
1229 subplot_using *tmp = new subplot_using (); |
1
|
1230 $$ = tmp->add_qualifier ($2); |
|
1231 } |
|
1232 | using1 COLON expression |
|
1233 { $$ = $1->add_qualifier ($3); } |
|
1234 ; |
|
1235 |
|
1236 title : TITLE expression |
|
1237 { $$ = $2; } |
|
1238 ; |
|
1239 |
|
1240 style : WITH STYLE |
1755
|
1241 { $$ = new subplot_style ($2->text ()); } |
1
|
1242 | WITH STYLE expression |
1755
|
1243 { $$ = new subplot_style ($2->text (), $3); } |
2542
|
1244 | WITH STYLE expression expression |
|
1245 { $$ = new subplot_style ($2->text (), $3, $4); } |
1
|
1246 ; |
|
1247 |
2970
|
1248 // ============= |
|
1249 // Miscellaneous |
|
1250 // ============= |
|
1251 |
|
1252 parse_error : LEXICAL_ERROR |
|
1253 { yyerror ("parse error"); } |
|
1254 | error |
1
|
1255 ; |
|
1256 |
2525
|
1257 sep_no_nl : ',' |
|
1258 { $$ = ','; } |
|
1259 | ';' |
|
1260 { $$ = ';'; } |
|
1261 | sep_no_nl ',' |
|
1262 { $$ = $1; } |
|
1263 | sep_no_nl ';' |
|
1264 { $$ = $1; } |
|
1265 ; |
|
1266 |
|
1267 opt_sep_no_nl : // empty |
|
1268 { $$ = 0; } |
|
1269 | sep_no_nl |
|
1270 { $$ = $1; } |
|
1271 ; |
|
1272 |
|
1273 sep : ',' |
|
1274 { $$ = ','; } |
|
1275 | ';' |
|
1276 { $$ = ';'; } |
|
1277 | '\n' |
|
1278 { $$ = '\n'; } |
|
1279 | sep ',' |
|
1280 { $$ = $1; } |
|
1281 | sep ';' |
|
1282 { $$ = $1; } |
|
1283 | sep '\n' |
|
1284 { $$ = $1; } |
|
1285 ; |
|
1286 |
|
1287 opt_sep : // empty |
|
1288 { $$ = 0; } |
|
1289 | sep |
|
1290 { $$ = $1; } |
|
1291 ; |
|
1292 |
1
|
1293 %% |
|
1294 |
666
|
1295 // Generic error messages. |
|
1296 |
1
|
1297 static void |
2805
|
1298 yyerror (const char *s) |
1
|
1299 { |
143
|
1300 int err_col = current_input_column - 1; |
1
|
1301 |
581
|
1302 ostrstream output_buf; |
1
|
1303 |
1005
|
1304 if (reading_fcn_file || reading_script_file) |
|
1305 output_buf << "parse error near line " << input_line_number |
1607
|
1306 << " of file " << curr_fcn_file_full_name; |
1005
|
1307 else |
|
1308 output_buf << "parse error:"; |
581
|
1309 |
1005
|
1310 if (s && strcmp (s, "parse error") != 0) |
|
1311 output_buf << "\n\n " << s; |
|
1312 |
|
1313 output_buf << "\n\n"; |
1
|
1314 |
1755
|
1315 if (! current_input_line.empty ()) |
1
|
1316 { |
1755
|
1317 size_t len = current_input_line.length (); |
1060
|
1318 |
1755
|
1319 if (current_input_line[len-1] == '\n') |
|
1320 current_input_line.resize (len-1); |
1005
|
1321 |
335
|
1322 // Print the line, maybe with a pointer near the error token. |
1005
|
1323 |
1755
|
1324 output_buf << ">>> " << current_input_line << "\n"; |
1060
|
1325 |
|
1326 if (err_col == 0) |
|
1327 err_col = len; |
|
1328 |
|
1329 for (int i = 0; i < err_col + 3; i++) |
|
1330 output_buf << " "; |
|
1331 |
|
1332 output_buf << "^"; |
1
|
1333 } |
1005
|
1334 |
|
1335 output_buf << "\n" << ends; |
581
|
1336 |
1005
|
1337 char *msg = output_buf.str (); |
|
1338 |
1090
|
1339 parse_error ("%s", msg); |
1005
|
1340 |
|
1341 delete [] msg; |
1
|
1342 } |
|
1343 |
666
|
1344 // Error mesages for mismatched end tokens. |
|
1345 |
496
|
1346 static void |
2805
|
1347 end_error (const char *type, token::end_tok_type ettype, int l, int c) |
496
|
1348 { |
2805
|
1349 static const char *fmt |
|
1350 = "`%s' command matched by `%s' near line %d column %d"; |
496
|
1351 |
|
1352 switch (ettype) |
|
1353 { |
|
1354 case token::simple_end: |
|
1355 error (fmt, type, "end", l, c); |
|
1356 break; |
777
|
1357 |
496
|
1358 case token::for_end: |
|
1359 error (fmt, type, "endfor", l, c); |
|
1360 break; |
777
|
1361 |
496
|
1362 case token::function_end: |
|
1363 error (fmt, type, "endfunction", l, c); |
|
1364 break; |
777
|
1365 |
496
|
1366 case token::if_end: |
|
1367 error (fmt, type, "endif", l, c); |
|
1368 break; |
777
|
1369 |
496
|
1370 case token::while_end: |
|
1371 error (fmt, type, "endwhile", l, c); |
|
1372 break; |
777
|
1373 |
1371
|
1374 case token::unwind_protect_end: |
|
1375 error (fmt, type, "end_unwind_protect", l, c); |
|
1376 break; |
|
1377 |
496
|
1378 default: |
|
1379 panic_impossible (); |
|
1380 break; |
|
1381 } |
|
1382 } |
|
1383 |
666
|
1384 // Check to see that end tokens are properly matched. |
|
1385 |
2857
|
1386 static bool |
|
1387 end_token_ok (token *tok, token::end_tok_type expected) |
143
|
1388 { |
2857
|
1389 bool retval = true; |
|
1390 |
143
|
1391 token::end_tok_type ettype = tok->ettype (); |
2857
|
1392 |
143
|
1393 if (ettype != expected && ettype != token::simple_end) |
|
1394 { |
2857
|
1395 retval = false; |
|
1396 |
143
|
1397 yyerror ("parse error"); |
|
1398 |
|
1399 int l = tok->line (); |
|
1400 int c = tok->column (); |
|
1401 |
|
1402 switch (expected) |
|
1403 { |
|
1404 case token::for_end: |
|
1405 end_error ("for", ettype, l, c); |
|
1406 break; |
777
|
1407 |
143
|
1408 case token::function_end: |
|
1409 end_error ("function", ettype, l, c); |
|
1410 break; |
777
|
1411 |
143
|
1412 case token::if_end: |
|
1413 end_error ("if", ettype, l, c); |
|
1414 break; |
777
|
1415 |
1489
|
1416 case token::try_catch_end: |
|
1417 end_error ("try", ettype, l, c); |
|
1418 break; |
|
1419 |
2764
|
1420 case token::switch_end: |
|
1421 end_error ("switch", ettype, l, c); |
|
1422 break; |
|
1423 |
1489
|
1424 case token::unwind_protect_end: |
|
1425 end_error ("unwind_protect", ettype, l, c); |
|
1426 break; |
|
1427 |
143
|
1428 case token::while_end: |
|
1429 end_error ("while", ettype, l, c); |
|
1430 break; |
777
|
1431 |
143
|
1432 default: |
|
1433 panic_impossible (); |
|
1434 break; |
|
1435 } |
|
1436 } |
2857
|
1437 |
|
1438 return retval; |
143
|
1439 } |
|
1440 |
666
|
1441 // Maybe print a warning if an assignment expression is used as the |
|
1442 // test in a logical expression. |
|
1443 |
496
|
1444 static void |
|
1445 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1446 { |
2166
|
1447 if (Vwarn_assign_as_truth_value |
1
|
1448 && expr->is_assignment_expression () |
2961
|
1449 && expr->paren_count () < 2) |
1
|
1450 { |
|
1451 warning ("suggest parenthesis around assignment used as truth value"); |
|
1452 } |
|
1453 } |
578
|
1454 |
2764
|
1455 // Maybe print a warning about switch labels that aren't constants. |
|
1456 |
|
1457 static void |
|
1458 maybe_warn_variable_switch_label (tree_expression *expr) |
|
1459 { |
|
1460 if (Vwarn_variable_switch_label && ! expr->is_constant ()) |
|
1461 { |
|
1462 warning ("variable switch label"); |
|
1463 } |
|
1464 } |
|
1465 |
1623
|
1466 // Create a plot command. |
|
1467 |
|
1468 static tree_plot_command * |
|
1469 make_plot_command (token *tok, plot_limits *range, subplot_list *list) |
|
1470 { |
|
1471 if (range) |
|
1472 { |
|
1473 if (tok->pttype () == token::replot) |
|
1474 { |
|
1475 yyerror ("cannot specify new ranges with replot"); |
|
1476 return 0; |
|
1477 } |
|
1478 } |
|
1479 else if (! list && tok->pttype () != token::replot) |
|
1480 { |
|
1481 yyerror ("must have something to plot"); |
|
1482 return 0; |
|
1483 } |
|
1484 |
2857
|
1485 lexer_flags.plotting = false; |
|
1486 lexer_flags.past_plot_range = false; |
|
1487 lexer_flags.in_plot_range = false; |
|
1488 lexer_flags.in_plot_using = false; |
|
1489 lexer_flags.in_plot_style = false; |
1623
|
1490 |
|
1491 return new tree_plot_command (list, range, tok->pttype ()); |
|
1492 } |
|
1493 |
2533
|
1494 static tree_expression * |
|
1495 fold (tree_binary_expression *e) |
|
1496 { |
3110
|
1497 tree_expression *retval = e; |
|
1498 |
|
1499 unwind_protect::begin_frame ("fold"); |
|
1500 |
|
1501 unwind_protect_int (error_state); |
|
1502 |
|
1503 unwind_protect_bool (buffer_error_messages); |
|
1504 buffer_error_messages = true; |
|
1505 |
|
1506 unwind_protect::add (clear_global_error_variable, 0); |
2533
|
1507 |
|
1508 tree_expression *op1 = e->lhs (); |
|
1509 tree_expression *op2 = e->rhs (); |
|
1510 |
|
1511 if (op1->is_constant () && op2->is_constant ()) |
|
1512 { |
2970
|
1513 octave_value tmp = e->rvalue (); |
2533
|
1514 |
|
1515 if (! error_state) |
|
1516 { |
|
1517 tree_constant *tc_retval = new tree_constant (tmp); |
|
1518 |
|
1519 ostrstream buf; |
|
1520 |
|
1521 tree_print_code tpc (buf); |
|
1522 |
|
1523 e->accept (tpc); |
|
1524 |
|
1525 buf << ends; |
|
1526 |
|
1527 char *s = buf.str (); |
|
1528 |
|
1529 tc_retval->stash_original_text (s); |
|
1530 |
|
1531 delete [] s; |
|
1532 |
|
1533 delete e; |
|
1534 |
|
1535 retval = tc_retval; |
|
1536 } |
|
1537 } |
3110
|
1538 |
|
1539 unwind_protect::run_frame ("fold"); |
2533
|
1540 |
|
1541 return retval; |
|
1542 } |
|
1543 |
1623
|
1544 // Finish building a range. |
|
1545 |
|
1546 static tree_expression * |
|
1547 finish_colon_expression (tree_colon_expression *e) |
|
1548 { |
3110
|
1549 tree_expression *retval = e; |
|
1550 |
|
1551 unwind_protect::begin_frame ("finish_colon_expression"); |
|
1552 |
|
1553 unwind_protect_int (error_state); |
|
1554 |
|
1555 unwind_protect_bool (buffer_error_messages); |
|
1556 buffer_error_messages = true; |
|
1557 |
|
1558 unwind_protect::add (clear_global_error_variable, 0); |
1623
|
1559 |
2533
|
1560 tree_expression *base = e->base (); |
|
1561 tree_expression *limit = e->limit (); |
|
1562 tree_expression *incr = e->increment (); |
|
1563 |
2970
|
1564 if (base) |
1623
|
1565 { |
2970
|
1566 if (limit) |
2533
|
1567 { |
2970
|
1568 if (base->is_constant () && limit->is_constant () |
|
1569 && (! incr || (incr && incr->is_constant ()))) |
|
1570 { |
|
1571 octave_value tmp = e->rvalue (); |
|
1572 |
|
1573 if (! error_state) |
|
1574 { |
|
1575 tree_constant *tc_retval = new tree_constant (tmp); |
|
1576 |
|
1577 ostrstream buf; |
|
1578 |
|
1579 tree_print_code tpc (buf); |
|
1580 |
|
1581 e->accept (tpc); |
|
1582 |
|
1583 buf << ends; |
|
1584 |
|
1585 char *s = buf.str (); |
|
1586 |
|
1587 tc_retval->stash_original_text (s); |
|
1588 |
|
1589 delete [] s; |
|
1590 |
|
1591 delete e; |
|
1592 |
|
1593 retval = tc_retval; |
|
1594 } |
|
1595 } |
2533
|
1596 } |
|
1597 else |
2970
|
1598 { |
2990
|
1599 e->preserve_base (); |
|
1600 delete e; |
2970
|
1601 |
|
1602 // XXX FIXME XXX -- need to attempt constant folding here |
|
1603 // too (we need a generic way to do that). |
|
1604 retval = base; |
|
1605 } |
1623
|
1606 } |
|
1607 |
3110
|
1608 unwind_protect::run_frame ("finish_colon_expression"); |
|
1609 |
1623
|
1610 return retval; |
|
1611 } |
|
1612 |
1607
|
1613 // Make a constant. |
|
1614 |
2375
|
1615 static tree_constant * |
1607
|
1616 make_constant (int op, token *tok_val) |
|
1617 { |
|
1618 int l = tok_val->line (); |
|
1619 int c = tok_val->column (); |
|
1620 |
2375
|
1621 tree_constant *retval; |
1607
|
1622 |
|
1623 switch (op) |
|
1624 { |
|
1625 case NUM: |
2533
|
1626 { |
2883
|
1627 octave_value tmp (tok_val->number ()); |
|
1628 retval = new tree_constant (tmp, l, c); |
2533
|
1629 retval->stash_original_text (tok_val->text_rep ()); |
|
1630 } |
1607
|
1631 break; |
|
1632 |
|
1633 case IMAG_NUM: |
|
1634 { |
2883
|
1635 octave_value tmp (Complex (0.0, tok_val->number ())); |
|
1636 retval = new tree_constant (tmp, l, c); |
1607
|
1637 retval->stash_original_text (tok_val->text_rep ()); |
|
1638 } |
|
1639 break; |
|
1640 |
|
1641 case TEXT: |
2883
|
1642 { |
|
1643 octave_value tmp (tok_val->text ()); |
|
1644 retval = new tree_constant (tmp, l, c); |
|
1645 } |
1607
|
1646 break; |
|
1647 |
|
1648 default: |
|
1649 panic_impossible (); |
|
1650 break; |
|
1651 } |
|
1652 |
|
1653 return retval; |
|
1654 } |
|
1655 |
666
|
1656 // Build a binary expression. |
|
1657 |
578
|
1658 static tree_expression * |
|
1659 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1660 tree_expression *op2) |
|
1661 { |
2883
|
1662 octave_value::binary_op t = octave_value::unknown_binary_op; |
1623
|
1663 |
578
|
1664 switch (op) |
|
1665 { |
|
1666 case POW: |
2883
|
1667 t = octave_value::pow; |
578
|
1668 break; |
777
|
1669 |
578
|
1670 case EPOW: |
2883
|
1671 t = octave_value::el_pow; |
578
|
1672 break; |
777
|
1673 |
578
|
1674 case '+': |
2883
|
1675 t = octave_value::add; |
578
|
1676 break; |
777
|
1677 |
578
|
1678 case '-': |
2883
|
1679 t = octave_value::sub; |
578
|
1680 break; |
777
|
1681 |
578
|
1682 case '*': |
2883
|
1683 t = octave_value::mul; |
578
|
1684 break; |
777
|
1685 |
578
|
1686 case '/': |
2883
|
1687 t = octave_value::div; |
578
|
1688 break; |
777
|
1689 |
578
|
1690 case EMUL: |
2883
|
1691 t = octave_value::el_mul; |
578
|
1692 break; |
777
|
1693 |
578
|
1694 case EDIV: |
2883
|
1695 t = octave_value::el_div; |
578
|
1696 break; |
777
|
1697 |
578
|
1698 case LEFTDIV: |
2883
|
1699 t = octave_value::ldiv; |
578
|
1700 break; |
777
|
1701 |
578
|
1702 case ELEFTDIV: |
2883
|
1703 t = octave_value::el_ldiv; |
578
|
1704 break; |
777
|
1705 |
2899
|
1706 case LSHIFT: |
|
1707 t = octave_value::lshift; |
|
1708 break; |
|
1709 |
|
1710 case RSHIFT: |
|
1711 t = octave_value::rshift; |
|
1712 break; |
|
1713 |
578
|
1714 case EXPR_LT: |
2883
|
1715 t = octave_value::lt; |
578
|
1716 break; |
777
|
1717 |
578
|
1718 case EXPR_LE: |
2883
|
1719 t = octave_value::le; |
578
|
1720 break; |
777
|
1721 |
578
|
1722 case EXPR_EQ: |
2883
|
1723 t = octave_value::eq; |
578
|
1724 break; |
777
|
1725 |
578
|
1726 case EXPR_GE: |
2883
|
1727 t = octave_value::ge; |
578
|
1728 break; |
777
|
1729 |
578
|
1730 case EXPR_GT: |
2883
|
1731 t = octave_value::gt; |
578
|
1732 break; |
777
|
1733 |
578
|
1734 case EXPR_NE: |
2883
|
1735 t = octave_value::ne; |
578
|
1736 break; |
777
|
1737 |
578
|
1738 case EXPR_AND: |
2883
|
1739 t = octave_value::el_and; |
578
|
1740 break; |
777
|
1741 |
578
|
1742 case EXPR_OR: |
2883
|
1743 t = octave_value::el_or; |
578
|
1744 break; |
777
|
1745 |
578
|
1746 default: |
|
1747 panic_impossible (); |
|
1748 break; |
|
1749 } |
|
1750 |
|
1751 int l = tok_val->line (); |
|
1752 int c = tok_val->column (); |
|
1753 |
2533
|
1754 tree_binary_expression *e |
|
1755 = new tree_binary_expression (op1, op2, l, c, t); |
1623
|
1756 |
2533
|
1757 return fold (e); |
578
|
1758 } |
|
1759 |
2375
|
1760 // Build a boolean expression. |
666
|
1761 |
578
|
1762 static tree_expression * |
2375
|
1763 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
1764 tree_expression *op2) |
578
|
1765 { |
2375
|
1766 tree_boolean_expression::type t; |
|
1767 |
578
|
1768 switch (op) |
|
1769 { |
2375
|
1770 case EXPR_AND_AND: |
2805
|
1771 t = tree_boolean_expression::bool_and; |
578
|
1772 break; |
777
|
1773 |
2375
|
1774 case EXPR_OR_OR: |
2805
|
1775 t = tree_boolean_expression::bool_or; |
578
|
1776 break; |
777
|
1777 |
578
|
1778 default: |
|
1779 panic_impossible (); |
|
1780 break; |
|
1781 } |
|
1782 |
|
1783 int l = tok_val->line (); |
|
1784 int c = tok_val->column (); |
|
1785 |
2533
|
1786 tree_boolean_expression *e |
|
1787 = new tree_boolean_expression (op1, op2, l, c, t); |
2375
|
1788 |
2533
|
1789 return fold (e); |
578
|
1790 } |
|
1791 |
2375
|
1792 // Build a prefix expression. |
666
|
1793 |
578
|
1794 static tree_expression * |
2960
|
1795 make_prefix_op (int op, tree_expression *op1, token *tok_val) |
578
|
1796 { |
2375
|
1797 tree_prefix_expression::type t; |
|
1798 |
578
|
1799 switch (op) |
|
1800 { |
2960
|
1801 case EXPR_NOT: |
|
1802 t = tree_prefix_expression::unot; |
|
1803 break; |
|
1804 |
|
1805 case '-': |
|
1806 t = tree_prefix_expression::uminus; |
|
1807 break; |
|
1808 |
578
|
1809 case PLUS_PLUS: |
2375
|
1810 t = tree_prefix_expression::increment; |
578
|
1811 break; |
777
|
1812 |
578
|
1813 case MINUS_MINUS: |
2375
|
1814 t = tree_prefix_expression::decrement; |
578
|
1815 break; |
777
|
1816 |
578
|
1817 default: |
|
1818 panic_impossible (); |
|
1819 break; |
|
1820 } |
|
1821 |
|
1822 int l = tok_val->line (); |
|
1823 int c = tok_val->column (); |
|
1824 |
3110
|
1825 // XXX FIXME XXX -- what about constant folding here? |
|
1826 |
2960
|
1827 return new tree_prefix_expression (t, op1, l, c); |
578
|
1828 } |
|
1829 |
2375
|
1830 // Build a postfix expression. |
666
|
1831 |
578
|
1832 static tree_expression * |
2960
|
1833 make_postfix_op (int op, tree_expression *op1, token *tok_val) |
578
|
1834 { |
2375
|
1835 tree_postfix_expression::type t; |
1623
|
1836 |
578
|
1837 switch (op) |
|
1838 { |
2960
|
1839 case QUOTE: |
|
1840 t = tree_postfix_expression::hermitian; |
|
1841 break; |
|
1842 |
|
1843 case TRANSPOSE: |
|
1844 t = tree_postfix_expression::transpose; |
|
1845 break; |
|
1846 |
2375
|
1847 case PLUS_PLUS: |
|
1848 t = tree_postfix_expression::increment; |
578
|
1849 break; |
777
|
1850 |
2375
|
1851 case MINUS_MINUS: |
|
1852 t = tree_postfix_expression::decrement; |
578
|
1853 break; |
777
|
1854 |
578
|
1855 default: |
|
1856 panic_impossible (); |
|
1857 break; |
|
1858 } |
|
1859 |
|
1860 int l = tok_val->line (); |
|
1861 int c = tok_val->column (); |
|
1862 |
3110
|
1863 // XXX FIXME XXX -- what about constant folding here? |
|
1864 |
2960
|
1865 return new tree_postfix_expression (t, op1, l, c); |
1623
|
1866 } |
|
1867 |
|
1868 // Build an unwind-protect command. |
|
1869 |
|
1870 static tree_command * |
|
1871 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
|
1872 tree_statement_list *cleanup, token *end_tok) |
|
1873 { |
|
1874 tree_command *retval = 0; |
|
1875 |
2857
|
1876 if (end_token_ok (end_tok, token::unwind_protect_end)) |
1623
|
1877 { |
|
1878 int l = unwind_tok->line (); |
|
1879 int c = unwind_tok->column (); |
|
1880 |
|
1881 retval = new tree_unwind_protect_command (body, cleanup, l, c); |
|
1882 } |
|
1883 |
|
1884 return retval; |
|
1885 } |
|
1886 |
|
1887 // Build a try-catch command. |
|
1888 |
|
1889 static tree_command * |
|
1890 make_try_command (token *try_tok, tree_statement_list *body, |
|
1891 tree_statement_list *cleanup, token *end_tok) |
|
1892 { |
|
1893 tree_command *retval = 0; |
|
1894 |
2857
|
1895 if (end_token_ok (end_tok, token::try_catch_end)) |
1623
|
1896 { |
|
1897 int l = try_tok->line (); |
|
1898 int c = try_tok->column (); |
|
1899 |
|
1900 retval = new tree_try_catch_command (body, cleanup, l, c); |
|
1901 } |
|
1902 |
|
1903 return retval; |
|
1904 } |
|
1905 |
|
1906 // Build a while command. |
|
1907 |
|
1908 static tree_command * |
|
1909 make_while_command (token *while_tok, tree_expression *expr, |
|
1910 tree_statement_list *body, token *end_tok) |
|
1911 { |
|
1912 tree_command *retval = 0; |
|
1913 |
|
1914 maybe_warn_assign_as_truth_value (expr); |
|
1915 |
2857
|
1916 if (end_token_ok (end_tok, token::while_end)) |
1623
|
1917 { |
1826
|
1918 lexer_flags.looping--; |
1623
|
1919 |
|
1920 int l = while_tok->line (); |
|
1921 int c = while_tok->column (); |
|
1922 |
|
1923 retval = new tree_while_command (expr, body, l, c); |
|
1924 } |
|
1925 |
|
1926 return retval; |
|
1927 } |
|
1928 |
|
1929 // Build a for command. |
|
1930 |
|
1931 static tree_command * |
2970
|
1932 make_for_command (token *for_tok, tree_argument_list *lhs, |
1623
|
1933 tree_expression *expr, tree_statement_list *body, |
|
1934 token *end_tok) |
|
1935 { |
|
1936 tree_command *retval = 0; |
|
1937 |
2857
|
1938 if (end_token_ok (end_tok, token::for_end)) |
1623
|
1939 { |
1826
|
1940 lexer_flags.looping--; |
1623
|
1941 |
|
1942 int l = for_tok->line (); |
|
1943 int c = for_tok->column (); |
|
1944 |
2970
|
1945 if (lhs->length () == 1) |
|
1946 { |
|
1947 tree_expression *tmp = lhs->remove_front (); |
|
1948 |
|
1949 retval = new tree_simple_for_command (tmp, expr, body, l, c); |
|
1950 |
|
1951 delete lhs; |
|
1952 } |
|
1953 else |
|
1954 retval = new tree_complex_for_command (lhs, expr, body, l, c); |
1623
|
1955 } |
|
1956 |
|
1957 return retval; |
|
1958 } |
|
1959 |
|
1960 // Build a break command. |
|
1961 |
|
1962 static tree_command * |
|
1963 make_break_command (token *break_tok) |
|
1964 { |
|
1965 tree_command *retval = 0; |
|
1966 |
2620
|
1967 int l = break_tok->line (); |
|
1968 int c = break_tok->column (); |
|
1969 |
|
1970 if (lexer_flags.looping || lexer_flags.defining_func || reading_script_file) |
|
1971 retval = new tree_break_command (l, c); |
1623
|
1972 else |
2620
|
1973 retval = new tree_no_op_command ("break", l, c); |
1623
|
1974 |
|
1975 return retval; |
|
1976 } |
|
1977 |
|
1978 // Build a continue command. |
|
1979 |
|
1980 static tree_command * |
|
1981 make_continue_command (token *continue_tok) |
|
1982 { |
|
1983 tree_command *retval = 0; |
|
1984 |
2620
|
1985 int l = continue_tok->line (); |
|
1986 int c = continue_tok->column (); |
|
1987 |
|
1988 if (lexer_flags.looping) |
|
1989 retval = new tree_continue_command (l, c); |
1623
|
1990 else |
2620
|
1991 retval = new tree_no_op_command ("continue", l, c); |
1623
|
1992 |
|
1993 return retval; |
|
1994 } |
|
1995 |
|
1996 // Build a return command. |
|
1997 |
|
1998 static tree_command * |
|
1999 make_return_command (token *return_tok) |
|
2000 { |
|
2001 tree_command *retval = 0; |
|
2002 |
2620
|
2003 int l = return_tok->line (); |
|
2004 int c = return_tok->column (); |
|
2005 |
|
2006 if (lexer_flags.defining_func || reading_script_file) |
|
2007 retval = new tree_return_command (l, c); |
1623
|
2008 else |
2620
|
2009 retval = new tree_no_op_command ("return", l, c); |
1623
|
2010 |
|
2011 return retval; |
|
2012 } |
|
2013 |
|
2014 // Start an if command. |
|
2015 |
|
2016 static tree_if_command_list * |
|
2017 start_if_command (tree_expression *expr, tree_statement_list *list) |
|
2018 { |
|
2019 maybe_warn_assign_as_truth_value (expr); |
|
2020 |
|
2021 tree_if_clause *t = new tree_if_clause (expr, list); |
|
2022 |
|
2023 return new tree_if_command_list (t); |
|
2024 } |
|
2025 |
|
2026 // Finish an if command. |
|
2027 |
|
2028 static tree_if_command * |
|
2029 finish_if_command (token *if_tok, tree_if_command_list *list, |
|
2030 token *end_tok) |
|
2031 { |
|
2032 tree_if_command *retval = 0; |
|
2033 |
2857
|
2034 if (end_token_ok (end_tok, token::if_end)) |
1623
|
2035 { |
|
2036 int l = if_tok->line (); |
|
2037 int c = if_tok->column (); |
|
2038 |
|
2039 retval = new tree_if_command (list, l, c); |
|
2040 } |
|
2041 |
|
2042 return retval; |
|
2043 } |
|
2044 |
|
2045 // Build an elseif clause. |
|
2046 |
|
2047 static tree_if_clause * |
|
2048 make_elseif_clause (tree_expression *expr, tree_statement_list *list) |
|
2049 { |
|
2050 maybe_warn_assign_as_truth_value (expr); |
|
2051 |
|
2052 return new tree_if_clause (expr, list); |
|
2053 } |
|
2054 |
2764
|
2055 // Finish a switch command. |
|
2056 |
|
2057 static tree_switch_command * |
|
2058 finish_switch_command (token *switch_tok, tree_expression *expr, |
|
2059 tree_switch_case_list *list, token *end_tok) |
|
2060 { |
|
2061 tree_switch_command *retval = 0; |
|
2062 |
2857
|
2063 if (end_token_ok (end_tok, token::switch_end)) |
2764
|
2064 { |
|
2065 int l = switch_tok->line (); |
|
2066 int c = switch_tok->column (); |
|
2067 |
|
2068 retval = new tree_switch_command (expr, list, l, c); |
|
2069 } |
|
2070 |
|
2071 return retval; |
|
2072 } |
|
2073 |
|
2074 // Build a switch case. |
|
2075 |
|
2076 static tree_switch_case * |
|
2077 make_switch_case (tree_expression *expr, tree_statement_list *list) |
|
2078 { |
|
2079 maybe_warn_variable_switch_label (expr); |
|
2080 |
|
2081 return new tree_switch_case (expr, list); |
|
2082 } |
|
2083 |
1623
|
2084 // Build an assignment to a variable. |
|
2085 |
|
2086 static tree_expression * |
2970
|
2087 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
2088 tree_expression *rhs) |
1623
|
2089 { |
2970
|
2090 tree_expression *retval = 0; |
|
2091 |
2883
|
2092 octave_value::assign_op t = octave_value::unknown_assign_op; |
|
2093 |
|
2094 switch (op) |
|
2095 { |
|
2096 case '=': |
|
2097 t = octave_value::asn_eq; |
|
2098 break; |
|
2099 |
|
2100 case ADD_EQ: |
|
2101 t = octave_value::add_eq; |
|
2102 break; |
|
2103 |
|
2104 case SUB_EQ: |
|
2105 t = octave_value::sub_eq; |
|
2106 break; |
|
2107 |
|
2108 case MUL_EQ: |
|
2109 t = octave_value::mul_eq; |
|
2110 break; |
|
2111 |
|
2112 case DIV_EQ: |
|
2113 t = octave_value::div_eq; |
|
2114 break; |
|
2115 |
2899
|
2116 case LSHIFT_EQ: |
|
2117 t = octave_value::lshift_eq; |
|
2118 break; |
|
2119 |
|
2120 case RSHIFT_EQ: |
|
2121 t = octave_value::rshift_eq; |
|
2122 break; |
|
2123 |
2883
|
2124 case EMUL_EQ: |
|
2125 t = octave_value::el_mul_eq; |
|
2126 break; |
|
2127 |
|
2128 case EDIV_EQ: |
|
2129 t = octave_value::el_div_eq; |
|
2130 break; |
|
2131 |
|
2132 case AND_EQ: |
|
2133 t = octave_value::el_and_eq; |
|
2134 break; |
|
2135 |
|
2136 case OR_EQ: |
|
2137 t = octave_value::el_or_eq; |
|
2138 break; |
|
2139 |
|
2140 default: |
|
2141 panic_impossible (); |
|
2142 break; |
|
2143 } |
|
2144 |
1623
|
2145 int l = eq_tok->line (); |
|
2146 int c = eq_tok->column (); |
|
2147 |
2970
|
2148 if (lhs->length () == 1) |
666
|
2149 { |
2970
|
2150 tree_expression *tmp = lhs->remove_front (); |
|
2151 |
|
2152 retval = new tree_simple_assignment (tmp, rhs, false, l, c, t); |
|
2153 |
|
2154 delete lhs; |
666
|
2155 } |
|
2156 else |
2970
|
2157 return new tree_multi_assignment (lhs, rhs, 0, l, c); |
666
|
2158 |
|
2159 return retval; |
|
2160 } |
751
|
2161 |
1623
|
2162 // Begin defining a function. |
|
2163 |
2891
|
2164 static octave_user_function * |
|
2165 start_function (tree_parameter_list *param_list, tree_statement_list *body) |
1623
|
2166 { |
|
2167 body->mark_as_function_body (); |
|
2168 |
2891
|
2169 // We'll fill in the return list later. |
|
2170 |
|
2171 octave_user_function *fcn |
|
2172 = new octave_user_function (param_list, 0, body, curr_sym_tab); |
1623
|
2173 |
|
2174 return fcn; |
|
2175 } |
|
2176 |
|
2177 // Do most of the work for defining a function. |
|
2178 |
2891
|
2179 static octave_user_function * |
|
2180 frob_function (tree_identifier *id, octave_user_function *fcn) |
1623
|
2181 { |
1755
|
2182 string id_name = id->name (); |
1623
|
2183 |
|
2184 // If input is coming from a file, issue a warning if the name of |
|
2185 // the file does not match the name of the function stated in the |
|
2186 // file. Matlab doesn't provide a diagnostic (it ignores the stated |
|
2187 // name). |
|
2188 |
|
2189 fcn->stash_function_name (id_name); |
|
2190 |
|
2191 if (reading_fcn_file) |
|
2192 { |
1755
|
2193 if (curr_fcn_file_name != id_name) |
1623
|
2194 { |
2166
|
2195 if (Vwarn_function_name_clash) |
1623
|
2196 warning ("function name `%s' does not agree with function\ |
1755
|
2197 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2198 |
|
2199 global_sym_tab->rename (id_name, curr_fcn_file_name); |
|
2200 |
|
2201 if (error_state) |
|
2202 return 0; |
|
2203 |
|
2204 id_name = id->name (); |
|
2205 } |
|
2206 |
3162
|
2207 time_t now = time (0); |
|
2208 |
1623
|
2209 fcn->stash_function_name (id_name); |
|
2210 fcn->stash_fcn_file_name (); |
3162
|
2211 fcn->stash_fcn_file_time (now); |
1623
|
2212 fcn->mark_as_system_fcn_file (); |
3162
|
2213 |
|
2214 if (Vwarn_future_time_stamp) |
|
2215 { |
|
2216 string nm = fcn->fcn_file_name (); |
|
2217 |
|
2218 file_stat fs (nm); |
|
2219 |
|
2220 if (fs && fs.is_newer (now)) |
|
2221 warning ("time stamp for `%s' is in the future", nm.c_str ()); |
|
2222 } |
1623
|
2223 } |
|
2224 else if (! (input_from_tmp_history_file || input_from_startup_file) |
|
2225 && reading_script_file |
1755
|
2226 && curr_fcn_file_name == id_name) |
1623
|
2227 { |
|
2228 warning ("function `%s' defined within script file `%s'", |
1755
|
2229 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2230 } |
|
2231 |
|
2232 top_level_sym_tab->clear (id_name); |
|
2233 |
2856
|
2234 symbol_record *sr = global_sym_tab->lookup (id_name); |
2791
|
2235 |
|
2236 if (sr) |
|
2237 fcn->stash_symtab_ptr (sr); |
|
2238 else |
|
2239 panic_impossible (); |
|
2240 |
3016
|
2241 id->define (fcn, symbol_record::USER_FUNCTION); |
1755
|
2242 |
1623
|
2243 id->document (help_buf); |
|
2244 |
|
2245 return fcn; |
|
2246 } |
|
2247 |
|
2248 // Finish defining a function. |
|
2249 |
2891
|
2250 static octave_user_function * |
|
2251 finish_function (tree_identifier *id, octave_user_function *fcn) |
1623
|
2252 { |
2883
|
2253 tree_parameter_list *tpl = new tree_parameter_list (id); |
1623
|
2254 |
|
2255 tpl->mark_as_formal_parameters (); |
|
2256 |
|
2257 return fcn->define_ret_list (tpl); |
|
2258 } |
|
2259 |
|
2260 // Finish defining a function a different way. |
|
2261 |
2891
|
2262 static octave_user_function * |
|
2263 finish_function (tree_parameter_list *ret_list, octave_user_function *fcn) |
1623
|
2264 { |
|
2265 ret_list->mark_as_formal_parameters (); |
|
2266 |
|
2267 return fcn->define_ret_list (ret_list); |
|
2268 } |
|
2269 |
2883
|
2270 static void |
|
2271 recover_from_parsing_function (void) |
|
2272 { |
|
2273 curr_sym_tab = top_level_sym_tab; |
|
2274 |
|
2275 lexer_flags.defining_func = false; |
|
2276 lexer_flags.beginning_of_function = false; |
|
2277 lexer_flags.parsed_function_name = false; |
|
2278 lexer_flags.looking_at_return_list = false; |
|
2279 lexer_flags.looking_at_parameter_list = false; |
|
2280 } |
|
2281 |
2846
|
2282 // Make an index expression. |
|
2283 |
751
|
2284 static tree_index_expression * |
2970
|
2285 make_index_expression (tree_expression *expr, tree_argument_list *args) |
751
|
2286 { |
|
2287 tree_index_expression *retval = 0; |
|
2288 |
2970
|
2289 int l = expr->line (); |
|
2290 int c = expr->column (); |
|
2291 |
|
2292 expr->mark_postfix_indexed (); |
|
2293 |
|
2294 retval = new tree_index_expression (expr, args, l, c); |
|
2295 |
|
2296 return retval; |
|
2297 } |
|
2298 |
|
2299 // Make an indirect reference expression. |
|
2300 |
|
2301 static tree_indirect_ref * |
|
2302 make_indirect_ref (tree_expression *expr, const string& elt) |
|
2303 { |
|
2304 tree_indirect_ref *retval = 0; |
|
2305 |
|
2306 int l = expr->line (); |
|
2307 int c = expr->column (); |
|
2308 |
|
2309 retval = new tree_indirect_ref (expr, elt, l, c); |
|
2310 |
|
2311 lexer_flags.looking_at_indirect_ref = false; |
751
|
2312 |
|
2313 return retval; |
|
2314 } |
1511
|
2315 |
2846
|
2316 // Make a declaration command. |
|
2317 |
|
2318 static tree_decl_command * |
|
2319 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) |
|
2320 { |
|
2321 tree_decl_command *retval = 0; |
|
2322 |
|
2323 int l = tok_val->line (); |
|
2324 int c = tok_val->column (); |
|
2325 |
|
2326 switch (tok) |
|
2327 { |
|
2328 case GLOBAL: |
|
2329 retval = new tree_global_command (lst, l, c); |
|
2330 break; |
|
2331 |
|
2332 case STATIC: |
|
2333 if (lexer_flags.defining_func) |
|
2334 retval = new tree_static_command (lst, l, c); |
|
2335 else |
|
2336 { |
|
2337 if (reading_script_file) |
|
2338 warning ("ignoring static declaration near line %d of file `%s'", |
|
2339 l, curr_fcn_file_full_name.c_str ()); |
|
2340 else |
|
2341 warning ("ignoring static declaration near line %d", l); |
|
2342 } |
|
2343 break; |
|
2344 |
|
2345 default: |
|
2346 panic_impossible (); |
|
2347 break; |
|
2348 } |
|
2349 |
|
2350 return retval; |
|
2351 } |
|
2352 |
1623
|
2353 // Finish building a matrix list. |
|
2354 |
|
2355 static tree_expression * |
1829
|
2356 finish_matrix (tree_matrix *m) |
1623
|
2357 { |
3110
|
2358 tree_expression *retval = m; |
|
2359 |
|
2360 unwind_protect::begin_frame ("finish_matrix"); |
|
2361 |
|
2362 unwind_protect_int (error_state); |
|
2363 |
|
2364 unwind_protect_bool (buffer_error_messages); |
|
2365 buffer_error_messages = true; |
|
2366 |
|
2367 unwind_protect::add (clear_global_error_variable, 0); |
1623
|
2368 |
2533
|
2369 if (m->all_elements_are_constant ()) |
1829
|
2370 { |
2970
|
2371 octave_value tmp = m->rvalue (); |
1623
|
2372 |
2533
|
2373 if (! error_state) |
|
2374 { |
|
2375 tree_constant *tc_retval = new tree_constant (tmp); |
|
2376 |
|
2377 ostrstream buf; |
|
2378 |
|
2379 tree_print_code tpc (buf); |
|
2380 |
|
2381 m->accept (tpc); |
|
2382 |
|
2383 buf << ends; |
1623
|
2384 |
2533
|
2385 char *s = buf.str (); |
|
2386 |
|
2387 tc_retval->stash_original_text (s); |
|
2388 |
|
2389 delete [] s; |
|
2390 |
|
2391 delete m; |
|
2392 |
|
2393 retval = tc_retval; |
|
2394 } |
1623
|
2395 } |
3110
|
2396 |
|
2397 unwind_protect::run_frame ("finish_matrix"); |
1623
|
2398 |
|
2399 return retval; |
|
2400 } |
|
2401 |
1511
|
2402 static void |
|
2403 maybe_warn_missing_semi (tree_statement_list *t) |
|
2404 { |
2166
|
2405 if (lexer_flags.defining_func && Vwarn_missing_semicolon) |
1511
|
2406 { |
|
2407 tree_statement *tmp = t->rear(); |
1607
|
2408 |
1511
|
2409 if (tmp->is_expression ()) |
1607
|
2410 warning ("missing semicolon near line %d, column %d in file `%s'", |
1755
|
2411 tmp->line (), tmp->column (), |
|
2412 curr_fcn_file_full_name.c_str ()); |
1511
|
2413 } |
|
2414 } |
1994
|
2415 |
2525
|
2416 static void |
|
2417 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2418 bool warn_missing_semi) |
|
2419 { |
|
2420 switch (sep) |
|
2421 { |
|
2422 case ';': |
|
2423 { |
|
2424 tree_statement *tmp = list->rear (); |
|
2425 tmp->set_print_flag (0); |
|
2426 } |
|
2427 break; |
|
2428 |
|
2429 case 0: |
|
2430 case ',': |
|
2431 case '\n': |
|
2432 if (warn_missing_semi) |
|
2433 maybe_warn_missing_semi (list); |
|
2434 break; |
|
2435 |
|
2436 default: |
|
2437 warning ("unrecognized separator type!"); |
|
2438 break; |
|
2439 } |
|
2440 } |
|
2441 |
3021
|
2442 void |
|
2443 parse_and_execute (FILE *f) |
|
2444 { |
|
2445 unwind_protect::begin_frame ("parse_and_execute"); |
|
2446 |
|
2447 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2448 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
2449 |
|
2450 unwind_protect::add (restore_input_buffer, old_buf); |
|
2451 unwind_protect::add (delete_input_buffer, new_buf); |
|
2452 |
|
2453 switch_to_buffer (new_buf); |
|
2454 |
|
2455 unwind_protect_bool (line_editing); |
|
2456 unwind_protect_bool (input_from_command_line_file); |
|
2457 |
|
2458 line_editing = false; |
|
2459 input_from_command_line_file = false; |
|
2460 |
|
2461 unwind_protect_ptr (curr_sym_tab); |
|
2462 |
|
2463 int retval; |
|
2464 do |
|
2465 { |
|
2466 reset_parser (); |
|
2467 |
|
2468 retval = yyparse (); |
|
2469 |
|
2470 if (retval == 0 && global_command) |
|
2471 { |
|
2472 global_command->eval (); |
|
2473 |
|
2474 delete global_command; |
|
2475 |
|
2476 global_command = 0; |
|
2477 |
|
2478 bool quit = (tree_return_command::returning |
|
2479 || tree_break_command::breaking); |
|
2480 |
|
2481 if (tree_return_command::returning) |
|
2482 tree_return_command::returning = 0; |
|
2483 |
|
2484 if (tree_break_command::breaking) |
|
2485 tree_break_command::breaking--; |
|
2486 |
|
2487 if (error_state) |
|
2488 { |
|
2489 error ("near line %d of file `%s'", input_line_number, |
|
2490 curr_fcn_file_full_name.c_str ()); |
|
2491 |
|
2492 break; |
|
2493 } |
|
2494 |
|
2495 if (quit) |
|
2496 break; |
|
2497 } |
|
2498 } |
|
2499 while (retval == 0); |
|
2500 |
|
2501 unwind_protect::run_frame ("parse_and_execute"); |
|
2502 } |
|
2503 |
|
2504 static void |
|
2505 safe_fclose (void *f) |
|
2506 { |
|
2507 if (f) |
|
2508 fclose (static_cast<FILE *> (f)); |
|
2509 } |
|
2510 |
|
2511 void |
|
2512 parse_and_execute (const string& s, bool verbose, const char *warn_for) |
|
2513 { |
|
2514 unwind_protect::begin_frame ("parse_and_execute_2"); |
|
2515 |
|
2516 unwind_protect_bool (reading_script_file); |
|
2517 unwind_protect_str (curr_fcn_file_full_name); |
|
2518 |
|
2519 reading_script_file = true; |
|
2520 curr_fcn_file_full_name = s; |
|
2521 |
|
2522 FILE *f = get_input_from_file (s, 0); |
|
2523 |
|
2524 if (f) |
|
2525 { |
|
2526 unwind_protect::add (safe_fclose, f); |
|
2527 |
|
2528 unwind_protect_int (input_line_number); |
|
2529 unwind_protect_int (current_input_column); |
|
2530 |
|
2531 input_line_number = 0; |
|
2532 current_input_column = 1; |
|
2533 |
|
2534 if (verbose) |
|
2535 { |
|
2536 cout << "reading commands from " << s << " ... "; |
|
2537 reading_startup_message_printed = true; |
|
2538 cout.flush (); |
|
2539 } |
|
2540 |
|
2541 parse_and_execute (f); |
|
2542 |
|
2543 if (verbose) |
|
2544 cout << "done." << endl; |
|
2545 } |
|
2546 else if (warn_for) |
|
2547 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
|
2548 |
|
2549 unwind_protect::run_frame ("parse_and_execute_2"); |
|
2550 } |
|
2551 |
|
2552 static bool |
|
2553 looks_like_octave_copyright (const string& s) |
|
2554 { |
|
2555 bool retval = false; |
|
2556 |
|
2557 string t = s.substr (0, 15); |
|
2558 |
|
2559 if (t == " Copyright (C) ") |
|
2560 { |
|
2561 size_t pos = s.find ('\n'); |
|
2562 |
|
2563 if (pos != NPOS) |
|
2564 { |
|
2565 pos = s.find ('\n', pos + 1); |
|
2566 |
|
2567 if (pos != NPOS) |
|
2568 { |
|
2569 pos++; |
|
2570 |
|
2571 t = s.substr (pos, 29); |
|
2572 |
|
2573 if (t == " This file is part of Octave." |
|
2574 || t == " This program is free softwar") |
|
2575 retval = true; |
|
2576 } |
|
2577 } |
|
2578 } |
|
2579 |
|
2580 return retval; |
|
2581 } |
|
2582 |
|
2583 // Eat whitespace and comments from FFILE, returning the text of the |
|
2584 // comments read if it doesn't look like a copyright notice. If |
|
2585 // IN_PARTS, consider each block of comments separately; otherwise, |
|
2586 // grab them all at once. If UPDATE_POS is TRUE, line and column |
|
2587 // number information is updated. |
|
2588 |
|
2589 // XXX FIXME XXX -- grab_help_text() in lex.l duplicates some of this |
|
2590 // code! |
|
2591 |
|
2592 static string |
|
2593 gobble_leading_white_space (FILE *ffile, bool in_parts, bool update_pos) |
|
2594 { |
|
2595 string help_txt; |
|
2596 |
|
2597 bool first_comments_seen = false; |
|
2598 bool begin_comment = false; |
|
2599 bool have_help_text = false; |
|
2600 bool in_comment = false; |
|
2601 int c; |
|
2602 |
|
2603 while ((c = getc (ffile)) != EOF) |
|
2604 { |
|
2605 if (update_pos) |
|
2606 current_input_column++; |
|
2607 |
|
2608 if (begin_comment) |
|
2609 { |
|
2610 if (c == '%' || c == '#') |
|
2611 continue; |
|
2612 else |
|
2613 begin_comment = false; |
|
2614 } |
|
2615 |
|
2616 if (in_comment) |
|
2617 { |
|
2618 if (! have_help_text) |
|
2619 { |
|
2620 first_comments_seen = true; |
|
2621 help_txt += (char) c; |
|
2622 } |
|
2623 |
|
2624 if (c == '\n') |
|
2625 { |
|
2626 if (update_pos) |
|
2627 { |
|
2628 input_line_number++; |
|
2629 current_input_column = 0; |
|
2630 } |
|
2631 in_comment = false; |
|
2632 |
|
2633 if (in_parts) |
|
2634 { |
|
2635 if ((c = getc (ffile)) != EOF) |
|
2636 { |
|
2637 if (update_pos) |
|
2638 current_input_column--; |
|
2639 ungetc (c, ffile); |
|
2640 if (c == '\n') |
|
2641 break; |
|
2642 } |
|
2643 else |
|
2644 break; |
|
2645 } |
|
2646 } |
|
2647 } |
|
2648 else |
|
2649 { |
|
2650 switch (c) |
|
2651 { |
|
2652 case ' ': |
|
2653 case '\t': |
|
2654 if (first_comments_seen) |
|
2655 have_help_text = true; |
|
2656 break; |
|
2657 |
|
2658 case '\n': |
|
2659 if (first_comments_seen) |
|
2660 have_help_text = true; |
|
2661 if (update_pos) |
|
2662 { |
|
2663 input_line_number++; |
|
2664 current_input_column = 0; |
|
2665 } |
|
2666 continue; |
|
2667 |
|
2668 case '%': |
|
2669 case '#': |
|
2670 begin_comment = true; |
|
2671 in_comment = true; |
|
2672 break; |
|
2673 |
|
2674 default: |
|
2675 if (update_pos) |
|
2676 current_input_column--; |
|
2677 ungetc (c, ffile); |
|
2678 goto done; |
|
2679 } |
|
2680 } |
|
2681 } |
|
2682 |
|
2683 done: |
|
2684 |
|
2685 if (! help_txt.empty ()) |
|
2686 { |
|
2687 if (looks_like_octave_copyright (help_txt)) |
|
2688 help_txt.resize (0); |
|
2689 |
|
2690 if (in_parts && help_txt.empty ()) |
|
2691 help_txt = gobble_leading_white_space (ffile, in_parts, update_pos); |
|
2692 } |
|
2693 |
|
2694 return help_txt; |
|
2695 } |
|
2696 |
|
2697 string |
|
2698 get_help_from_file (const string& path) |
|
2699 { |
|
2700 string retval; |
|
2701 |
|
2702 if (! path.empty ()) |
|
2703 { |
|
2704 FILE *fptr = fopen (path.c_str (), "r"); |
|
2705 |
|
2706 if (fptr) |
|
2707 { |
|
2708 unwind_protect::add (safe_fclose, (void *) fptr); |
|
2709 |
|
2710 retval = gobble_leading_white_space (fptr, true, true); |
|
2711 |
|
2712 unwind_protect::run (); |
|
2713 } |
|
2714 } |
|
2715 |
|
2716 return retval; |
|
2717 } |
|
2718 |
|
2719 static int |
|
2720 is_function_file (FILE *ffile) |
|
2721 { |
|
2722 int status = 0; |
|
2723 |
|
2724 long pos = ftell (ffile); |
|
2725 |
|
2726 gobble_leading_white_space (ffile, false, false); |
|
2727 |
|
2728 char buf [10]; |
|
2729 fgets (buf, 10, ffile); |
|
2730 int len = strlen (buf); |
|
2731 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
2732 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
2733 status = 1; |
|
2734 |
|
2735 fseek (ffile, pos, SEEK_SET); |
|
2736 |
|
2737 return status; |
|
2738 } |
|
2739 |
|
2740 static void |
|
2741 restore_command_history (void *) |
|
2742 { |
|
2743 command_history::ignore_entries (! Vsaving_history); |
|
2744 } |
|
2745 |
|
2746 static void |
|
2747 restore_input_stream (void *f) |
|
2748 { |
|
2749 command_editor::set_input_stream (static_cast<FILE *> (f)); |
|
2750 } |
|
2751 |
|
2752 static bool |
|
2753 parse_fcn_file (bool exec_script, const string& ff) |
|
2754 { |
|
2755 unwind_protect::begin_frame ("parse_fcn_file"); |
|
2756 |
|
2757 int script_file_executed = false; |
|
2758 |
|
2759 // Open function file and parse. |
|
2760 |
|
2761 bool old_reading_fcn_file_state = reading_fcn_file; |
|
2762 |
|
2763 FILE *in_stream = command_editor::get_input_stream (); |
|
2764 |
|
2765 unwind_protect::add (restore_input_stream, in_stream); |
|
2766 |
|
2767 unwind_protect_ptr (ff_instream); |
|
2768 |
|
2769 unwind_protect_int (input_line_number); |
|
2770 unwind_protect_int (current_input_column); |
|
2771 unwind_protect_bool (reading_fcn_file); |
|
2772 unwind_protect_bool (line_editing); |
|
2773 |
|
2774 input_line_number = 0; |
|
2775 current_input_column = 1; |
|
2776 reading_fcn_file = true; |
|
2777 line_editing = false; |
|
2778 |
|
2779 FILE *ffile = get_input_from_file (ff, 0); |
|
2780 |
|
2781 unwind_protect::add (safe_fclose, ffile); |
|
2782 |
|
2783 if (ffile) |
|
2784 { |
|
2785 // Check to see if this file defines a function or is just a |
|
2786 // list of commands. |
|
2787 |
|
2788 if (is_function_file (ffile)) |
|
2789 { |
|
2790 // XXX FIXME XXX -- we shouldn't need both the |
|
2791 // command_history object and the |
|
2792 // Vsaving_history variable... |
|
2793 command_history::ignore_entries (); |
|
2794 |
|
2795 unwind_protect::add (restore_command_history, 0); |
|
2796 |
|
2797 unwind_protect_int (Vecho_executing_commands); |
|
2798 unwind_protect_bool (Vsaving_history); |
|
2799 unwind_protect_bool (reading_fcn_file); |
|
2800 unwind_protect_bool (input_from_command_line_file); |
|
2801 |
|
2802 Vecho_executing_commands = ECHO_OFF; |
|
2803 Vsaving_history = false; |
|
2804 reading_fcn_file = true; |
|
2805 input_from_command_line_file = false; |
|
2806 |
|
2807 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2808 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
2809 |
|
2810 unwind_protect::add (restore_input_buffer, (void *) old_buf); |
|
2811 unwind_protect::add (delete_input_buffer, (void *) new_buf); |
|
2812 |
|
2813 switch_to_buffer (new_buf); |
|
2814 |
|
2815 unwind_protect_ptr (curr_sym_tab); |
|
2816 |
|
2817 reset_parser (); |
|
2818 |
|
2819 help_buf = gobble_leading_white_space (ffile, true, true); |
|
2820 |
|
2821 // XXX FIXME XXX -- this should not be necessary. |
|
2822 gobble_leading_white_space (ffile, false, true); |
|
2823 |
|
2824 int status = yyparse (); |
|
2825 |
|
2826 if (status != 0) |
|
2827 { |
|
2828 error ("parse error while reading function file %s", |
|
2829 ff.c_str ()); |
|
2830 global_sym_tab->clear (curr_fcn_file_name); |
|
2831 } |
|
2832 } |
|
2833 else if (exec_script) |
|
2834 { |
|
2835 // The value of `reading_fcn_file' will be restored to the |
|
2836 // proper value when we unwind from this frame. |
|
2837 reading_fcn_file = old_reading_fcn_file_state; |
|
2838 |
|
2839 // XXX FIXME XXX -- we shouldn't need both the |
|
2840 // command_history object and the |
|
2841 // Vsaving_history variable... |
|
2842 command_history::ignore_entries (); |
|
2843 |
|
2844 unwind_protect::add (restore_command_history, 0); |
|
2845 |
|
2846 unwind_protect_bool (Vsaving_history); |
|
2847 unwind_protect_bool (reading_script_file); |
|
2848 |
|
2849 Vsaving_history = false; |
|
2850 reading_script_file = true; |
|
2851 |
|
2852 parse_and_execute (ffile); |
|
2853 |
|
2854 script_file_executed = true; |
|
2855 } |
|
2856 } |
|
2857 |
|
2858 unwind_protect::run_frame ("parse_fcn_file"); |
|
2859 |
|
2860 return script_file_executed; |
|
2861 } |
|
2862 |
|
2863 bool |
|
2864 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
|
2865 { |
|
2866 bool script_file_executed = false; |
|
2867 |
|
2868 string nm = sym_rec->name (); |
|
2869 |
|
2870 if (octave_dynamic_loader::load_fcn_from_dot_oct_file (nm)) |
|
2871 { |
|
2872 force_link_to_function (nm); |
|
2873 } |
|
2874 else |
|
2875 { |
|
2876 string ff = fcn_file_in_path (nm); |
|
2877 |
|
2878 // These are needed by yyparse. |
|
2879 |
|
2880 unwind_protect::begin_frame ("load_fcn_from_file"); |
|
2881 |
|
2882 unwind_protect_str (curr_fcn_file_name); |
|
2883 unwind_protect_str (curr_fcn_file_full_name); |
|
2884 |
|
2885 curr_fcn_file_name = nm; |
|
2886 curr_fcn_file_full_name = ff; |
|
2887 |
|
2888 if (ff.length () > 0) |
|
2889 script_file_executed = parse_fcn_file (exec_script, ff); |
|
2890 |
|
2891 if (! (error_state || script_file_executed)) |
|
2892 force_link_to_function (nm); |
|
2893 |
|
2894 unwind_protect::run_frame ("load_fcn_from_file"); |
|
2895 } |
|
2896 |
|
2897 return script_file_executed; |
|
2898 } |
|
2899 |
|
2900 DEFUN (source, args, , |
|
2901 "source (FILE)\n\ |
|
2902 \n\ |
|
2903 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
2904 script file but without requiring the file to be named `FILE.m'.") |
|
2905 { |
|
2906 octave_value_list retval; |
|
2907 |
|
2908 int nargin = args.length (); |
|
2909 |
|
2910 if (nargin == 1) |
|
2911 { |
|
2912 string file = args(0).string_value (); |
|
2913 |
|
2914 if (! error_state) |
|
2915 { |
|
2916 file = file_ops::tilde_expand (file); |
|
2917 |
3095
|
2918 parse_fcn_file (true, file); |
3021
|
2919 |
|
2920 if (error_state) |
|
2921 error ("source: error sourcing file `%s'", file.c_str ()); |
|
2922 } |
|
2923 else |
|
2924 error ("source: expecting file name as argument"); |
|
2925 } |
|
2926 else |
|
2927 print_usage ("source"); |
|
2928 |
|
2929 return retval; |
|
2930 } |
|
2931 |
|
2932 octave_value_list |
3156
|
2933 feval (const string& name, const octave_value_list& args, int nargout) |
|
2934 { |
|
2935 octave_value_list retval; |
|
2936 |
|
2937 octave_function *fcn = is_valid_function (name, "feval", 1); |
|
2938 |
|
2939 if (fcn) |
|
2940 retval = fcn->do_index_op (nargout, args); |
|
2941 |
|
2942 return retval; |
|
2943 } |
|
2944 |
|
2945 octave_value_list |
3021
|
2946 feval (const octave_value_list& args, int nargout) |
|
2947 { |
|
2948 octave_value_list retval; |
|
2949 |
3156
|
2950 if (args.length () > 0) |
3021
|
2951 { |
3156
|
2952 string name = args(0).string_value (); |
|
2953 |
|
2954 if (! error_state) |
3021
|
2955 { |
3156
|
2956 string_vector arg_names = args.name_tags (); |
|
2957 |
|
2958 int tmp_nargin = args.length () - 1; |
|
2959 |
|
2960 octave_value_list tmp_args (tmp_nargin, octave_value ()); |
|
2961 |
|
2962 string_vector tmp_arg_names (tmp_nargin); |
|
2963 |
|
2964 for (int i = 0; i < tmp_nargin; i++) |
|
2965 { |
|
2966 tmp_args(i) = args(i+1); |
|
2967 tmp_arg_names(i) = arg_names(i+1); |
|
2968 } |
|
2969 |
|
2970 tmp_args.stash_name_tags (tmp_arg_names); |
|
2971 |
|
2972 retval = feval (name, tmp_args, nargout); |
3021
|
2973 } |
|
2974 } |
|
2975 |
|
2976 return retval; |
|
2977 } |
|
2978 |
|
2979 DEFUN (feval, args, nargout, |
|
2980 "feval (NAME, ARGS, ...)\n\ |
|
2981 \n\ |
|
2982 evaluate NAME as a function, passing ARGS as its arguments") |
|
2983 { |
|
2984 octave_value_list retval; |
|
2985 |
|
2986 int nargin = args.length (); |
|
2987 |
|
2988 if (nargin > 0) |
|
2989 retval = feval (args, nargout); |
|
2990 else |
|
2991 print_usage ("feval"); |
|
2992 |
|
2993 return retval; |
|
2994 } |
|
2995 |
3099
|
2996 octave_value_list |
3021
|
2997 eval_string (const string& s, bool silent, int& parse_status, int nargout) |
|
2998 { |
|
2999 unwind_protect::begin_frame ("eval_string"); |
|
3000 |
|
3001 unwind_protect_bool (get_input_from_eval_string); |
|
3002 unwind_protect_bool (input_from_command_line_file); |
|
3003 unwind_protect_ptr (global_command); |
|
3004 unwind_protect_str (current_eval_string); |
|
3005 |
|
3006 get_input_from_eval_string = true; |
|
3007 input_from_command_line_file = false; |
|
3008 current_eval_string = s; |
|
3009 |
|
3010 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3011 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
3012 |
|
3013 unwind_protect::add (restore_input_buffer, old_buf); |
|
3014 unwind_protect::add (delete_input_buffer, new_buf); |
|
3015 |
|
3016 switch_to_buffer (new_buf); |
|
3017 |
|
3018 unwind_protect_ptr (curr_sym_tab); |
|
3019 |
|
3020 reset_parser (); |
|
3021 |
|
3022 parse_status = yyparse (); |
|
3023 |
|
3024 // Important to reset the idea of where input is coming from before |
|
3025 // trying to eval the command we just parsed -- it might contain the |
|
3026 // name of an function file that still needs to be parsed! |
|
3027 |
|
3028 tree_statement_list *command = global_command; |
|
3029 |
|
3030 unwind_protect::run_frame ("eval_string"); |
|
3031 |
|
3032 octave_value_list retval; |
|
3033 |
|
3034 if (parse_status == 0 && command) |
|
3035 { |
|
3036 retval = command->eval (silent, nargout); |
|
3037 delete command; |
|
3038 } |
|
3039 |
|
3040 return retval; |
|
3041 } |
|
3042 |
|
3043 octave_value |
|
3044 eval_string (const string& s, bool silent, int& parse_status) |
|
3045 { |
|
3046 octave_value retval; |
|
3047 |
|
3048 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
|
3049 |
|
3050 if (! tmp.empty ()) |
|
3051 retval = tmp(0); |
|
3052 |
|
3053 return retval; |
|
3054 } |
|
3055 |
|
3056 static octave_value_list |
|
3057 eval_string (const octave_value& arg, bool silent, int& parse_status, |
|
3058 int nargout) |
|
3059 { |
|
3060 string s = arg.string_value (); |
|
3061 |
|
3062 if (error_state) |
|
3063 { |
|
3064 error ("eval: expecting string argument"); |
|
3065 return -1.0; |
|
3066 } |
|
3067 |
|
3068 return eval_string (s, silent, parse_status, nargout); |
|
3069 } |
|
3070 |
|
3071 DEFUN (eval, args, nargout, |
|
3072 "eval (TRY, CATCH)\n\ |
|
3073 \n\ |
|
3074 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
3075 string CATCH.") |
|
3076 { |
|
3077 octave_value_list retval; |
|
3078 |
|
3079 int nargin = args.length (); |
|
3080 |
|
3081 if (nargin > 0) |
|
3082 { |
|
3083 unwind_protect::begin_frame ("Feval"); |
|
3084 |
|
3085 if (nargin > 1) |
|
3086 { |
|
3087 unwind_protect_bool (buffer_error_messages); |
|
3088 buffer_error_messages = true; |
|
3089 } |
|
3090 |
|
3091 int parse_status = 0; |
|
3092 |
|
3093 retval = eval_string (args(0), ! Vdefault_eval_print_flag, |
|
3094 parse_status, nargout); |
|
3095 |
|
3096 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
3097 { |
|
3098 error_state = 0; |
|
3099 |
|
3100 // Set up for letting the user print any messages from |
|
3101 // errors that occurred in the first part of this eval(). |
|
3102 |
|
3103 buffer_error_messages = false; |
|
3104 bind_global_error_variable (); |
|
3105 unwind_protect::add (clear_global_error_variable, 0); |
|
3106 |
|
3107 eval_string (args(1), 0, parse_status, nargout); |
|
3108 |
|
3109 retval = octave_value_list (); |
|
3110 } |
|
3111 |
|
3112 unwind_protect::run_frame ("Feval"); |
|
3113 } |
|
3114 else |
|
3115 print_usage ("eval"); |
|
3116 |
|
3117 return retval; |
|
3118 } |
|
3119 |
|
3120 static int |
|
3121 default_eval_print_flag (void) |
|
3122 { |
|
3123 Vdefault_eval_print_flag = check_preference ("default_eval_print_flag"); |
|
3124 |
|
3125 return 0; |
|
3126 } |
|
3127 |
2166
|
3128 static int |
|
3129 warn_assign_as_truth_value (void) |
|
3130 { |
|
3131 Vwarn_assign_as_truth_value |
|
3132 = check_preference ("warn_assign_as_truth_value"); |
|
3133 |
|
3134 return 0; |
|
3135 } |
|
3136 |
|
3137 static int |
|
3138 warn_function_name_clash (void) |
|
3139 { |
|
3140 Vwarn_function_name_clash = check_preference ("warn_function_name_clash"); |
|
3141 |
|
3142 return 0; |
|
3143 } |
|
3144 |
|
3145 static int |
3162
|
3146 warn_future_time_stamp (void) |
|
3147 { |
|
3148 Vwarn_future_time_stamp = check_preference ("warn_future_time_stamp"); |
|
3149 |
|
3150 return 0; |
|
3151 } |
|
3152 |
|
3153 static int |
2166
|
3154 warn_missing_semicolon (void) |
|
3155 { |
|
3156 Vwarn_missing_semicolon = check_preference ("warn_missing_semicolon"); |
|
3157 |
|
3158 return 0; |
|
3159 } |
|
3160 |
2764
|
3161 static int |
|
3162 warn_variable_switch_label (void) |
|
3163 { |
|
3164 Vwarn_variable_switch_label |
|
3165 = check_preference ("warn_variable_switch_label"); |
|
3166 |
|
3167 return 0; |
|
3168 } |
|
3169 |
2166
|
3170 void |
|
3171 symbols_of_parse (void) |
|
3172 { |
3021
|
3173 DEFVAR (default_eval_print_flag, 1.0, 0, default_eval_print_flag, |
|
3174 "If the value of this variable is nonzero, Octave will print the\n\ |
|
3175 results of commands executed by eval() that do not end with semicolons."); |
|
3176 |
2166
|
3177 DEFVAR (warn_assign_as_truth_value, 1.0, 0, warn_assign_as_truth_value, |
|
3178 "produce warning for assignments used as truth values"); |
|
3179 |
|
3180 DEFVAR (warn_function_name_clash, 1.0, 0, warn_function_name_clash, |
|
3181 "produce warning if function name conflicts with file name"); |
|
3182 |
3162
|
3183 DEFVAR (warn_future_time_stamp, 1.0, 0, warn_future_time_stamp, |
|
3184 "warn if a function file has a time stamp that is in the future"); |
|
3185 |
2166
|
3186 DEFVAR (warn_missing_semicolon, 0.0, 0, warn_missing_semicolon, |
2764
|
3187 "produce a warning if a statement in a function file is not\n\ |
2166
|
3188 terminated with a semicolon"); |
2764
|
3189 |
|
3190 DEFVAR (warn_variable_switch_label, 0.0, 0, warn_variable_switch_label, |
|
3191 "produce warning for variables used as switch labels"); |
2166
|
3192 } |
|
3193 |
1994
|
3194 /* |
|
3195 ;;; Local Variables: *** |
|
3196 ;;; mode: text *** |
|
3197 ;;; End: *** |
|
3198 */ |