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