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