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 { |
3110
|
1492 tree_expression *retval = e; |
|
1493 |
|
1494 unwind_protect::begin_frame ("fold"); |
|
1495 |
|
1496 unwind_protect_int (error_state); |
|
1497 |
|
1498 unwind_protect_bool (buffer_error_messages); |
|
1499 buffer_error_messages = true; |
|
1500 |
|
1501 unwind_protect::add (clear_global_error_variable, 0); |
2533
|
1502 |
|
1503 tree_expression *op1 = e->lhs (); |
|
1504 tree_expression *op2 = e->rhs (); |
|
1505 |
|
1506 if (op1->is_constant () && op2->is_constant ()) |
|
1507 { |
2970
|
1508 octave_value tmp = e->rvalue (); |
2533
|
1509 |
|
1510 if (! error_state) |
|
1511 { |
|
1512 tree_constant *tc_retval = new tree_constant (tmp); |
|
1513 |
|
1514 ostrstream buf; |
|
1515 |
|
1516 tree_print_code tpc (buf); |
|
1517 |
|
1518 e->accept (tpc); |
|
1519 |
|
1520 buf << ends; |
|
1521 |
|
1522 char *s = buf.str (); |
|
1523 |
|
1524 tc_retval->stash_original_text (s); |
|
1525 |
|
1526 delete [] s; |
|
1527 |
|
1528 delete e; |
|
1529 |
|
1530 retval = tc_retval; |
|
1531 } |
|
1532 } |
3110
|
1533 |
|
1534 unwind_protect::run_frame ("fold"); |
2533
|
1535 |
|
1536 return retval; |
|
1537 } |
|
1538 |
1623
|
1539 // Finish building a range. |
|
1540 |
|
1541 static tree_expression * |
|
1542 finish_colon_expression (tree_colon_expression *e) |
|
1543 { |
3110
|
1544 tree_expression *retval = e; |
|
1545 |
|
1546 unwind_protect::begin_frame ("finish_colon_expression"); |
|
1547 |
|
1548 unwind_protect_int (error_state); |
|
1549 |
|
1550 unwind_protect_bool (buffer_error_messages); |
|
1551 buffer_error_messages = true; |
|
1552 |
|
1553 unwind_protect::add (clear_global_error_variable, 0); |
1623
|
1554 |
2533
|
1555 tree_expression *base = e->base (); |
|
1556 tree_expression *limit = e->limit (); |
|
1557 tree_expression *incr = e->increment (); |
|
1558 |
2970
|
1559 if (base) |
1623
|
1560 { |
2970
|
1561 if (limit) |
2533
|
1562 { |
2970
|
1563 if (base->is_constant () && limit->is_constant () |
|
1564 && (! incr || (incr && incr->is_constant ()))) |
|
1565 { |
|
1566 octave_value tmp = e->rvalue (); |
|
1567 |
|
1568 if (! error_state) |
|
1569 { |
|
1570 tree_constant *tc_retval = new tree_constant (tmp); |
|
1571 |
|
1572 ostrstream buf; |
|
1573 |
|
1574 tree_print_code tpc (buf); |
|
1575 |
|
1576 e->accept (tpc); |
|
1577 |
|
1578 buf << ends; |
|
1579 |
|
1580 char *s = buf.str (); |
|
1581 |
|
1582 tc_retval->stash_original_text (s); |
|
1583 |
|
1584 delete [] s; |
|
1585 |
|
1586 delete e; |
|
1587 |
|
1588 retval = tc_retval; |
|
1589 } |
|
1590 } |
2533
|
1591 } |
|
1592 else |
2970
|
1593 { |
2990
|
1594 e->preserve_base (); |
|
1595 delete e; |
2970
|
1596 |
|
1597 // XXX FIXME XXX -- need to attempt constant folding here |
|
1598 // too (we need a generic way to do that). |
|
1599 retval = base; |
|
1600 } |
1623
|
1601 } |
|
1602 |
3110
|
1603 unwind_protect::run_frame ("finish_colon_expression"); |
|
1604 |
1623
|
1605 return retval; |
|
1606 } |
|
1607 |
1607
|
1608 // Make a constant. |
|
1609 |
2375
|
1610 static tree_constant * |
1607
|
1611 make_constant (int op, token *tok_val) |
|
1612 { |
|
1613 int l = tok_val->line (); |
|
1614 int c = tok_val->column (); |
|
1615 |
2375
|
1616 tree_constant *retval; |
1607
|
1617 |
|
1618 switch (op) |
|
1619 { |
|
1620 case NUM: |
2533
|
1621 { |
2883
|
1622 octave_value tmp (tok_val->number ()); |
|
1623 retval = new tree_constant (tmp, l, c); |
2533
|
1624 retval->stash_original_text (tok_val->text_rep ()); |
|
1625 } |
1607
|
1626 break; |
|
1627 |
|
1628 case IMAG_NUM: |
|
1629 { |
2883
|
1630 octave_value tmp (Complex (0.0, tok_val->number ())); |
|
1631 retval = new tree_constant (tmp, l, c); |
1607
|
1632 retval->stash_original_text (tok_val->text_rep ()); |
|
1633 } |
|
1634 break; |
|
1635 |
|
1636 case TEXT: |
2883
|
1637 { |
|
1638 octave_value tmp (tok_val->text ()); |
|
1639 retval = new tree_constant (tmp, l, c); |
|
1640 } |
1607
|
1641 break; |
|
1642 |
|
1643 default: |
|
1644 panic_impossible (); |
|
1645 break; |
|
1646 } |
|
1647 |
|
1648 return retval; |
|
1649 } |
|
1650 |
666
|
1651 // Build a binary expression. |
|
1652 |
578
|
1653 static tree_expression * |
|
1654 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1655 tree_expression *op2) |
|
1656 { |
2883
|
1657 octave_value::binary_op t = octave_value::unknown_binary_op; |
1623
|
1658 |
578
|
1659 switch (op) |
|
1660 { |
|
1661 case POW: |
2883
|
1662 t = octave_value::pow; |
578
|
1663 break; |
777
|
1664 |
578
|
1665 case EPOW: |
2883
|
1666 t = octave_value::el_pow; |
578
|
1667 break; |
777
|
1668 |
578
|
1669 case '+': |
2883
|
1670 t = octave_value::add; |
578
|
1671 break; |
777
|
1672 |
578
|
1673 case '-': |
2883
|
1674 t = octave_value::sub; |
578
|
1675 break; |
777
|
1676 |
578
|
1677 case '*': |
2883
|
1678 t = octave_value::mul; |
578
|
1679 break; |
777
|
1680 |
578
|
1681 case '/': |
2883
|
1682 t = octave_value::div; |
578
|
1683 break; |
777
|
1684 |
578
|
1685 case EMUL: |
2883
|
1686 t = octave_value::el_mul; |
578
|
1687 break; |
777
|
1688 |
578
|
1689 case EDIV: |
2883
|
1690 t = octave_value::el_div; |
578
|
1691 break; |
777
|
1692 |
578
|
1693 case LEFTDIV: |
2883
|
1694 t = octave_value::ldiv; |
578
|
1695 break; |
777
|
1696 |
578
|
1697 case ELEFTDIV: |
2883
|
1698 t = octave_value::el_ldiv; |
578
|
1699 break; |
777
|
1700 |
2899
|
1701 case LSHIFT: |
|
1702 t = octave_value::lshift; |
|
1703 break; |
|
1704 |
|
1705 case RSHIFT: |
|
1706 t = octave_value::rshift; |
|
1707 break; |
|
1708 |
578
|
1709 case EXPR_LT: |
2883
|
1710 t = octave_value::lt; |
578
|
1711 break; |
777
|
1712 |
578
|
1713 case EXPR_LE: |
2883
|
1714 t = octave_value::le; |
578
|
1715 break; |
777
|
1716 |
578
|
1717 case EXPR_EQ: |
2883
|
1718 t = octave_value::eq; |
578
|
1719 break; |
777
|
1720 |
578
|
1721 case EXPR_GE: |
2883
|
1722 t = octave_value::ge; |
578
|
1723 break; |
777
|
1724 |
578
|
1725 case EXPR_GT: |
2883
|
1726 t = octave_value::gt; |
578
|
1727 break; |
777
|
1728 |
578
|
1729 case EXPR_NE: |
2883
|
1730 t = octave_value::ne; |
578
|
1731 break; |
777
|
1732 |
578
|
1733 case EXPR_AND: |
2883
|
1734 t = octave_value::el_and; |
578
|
1735 break; |
777
|
1736 |
578
|
1737 case EXPR_OR: |
2883
|
1738 t = octave_value::el_or; |
578
|
1739 break; |
777
|
1740 |
578
|
1741 default: |
|
1742 panic_impossible (); |
|
1743 break; |
|
1744 } |
|
1745 |
|
1746 int l = tok_val->line (); |
|
1747 int c = tok_val->column (); |
|
1748 |
2533
|
1749 tree_binary_expression *e |
|
1750 = new tree_binary_expression (op1, op2, l, c, t); |
1623
|
1751 |
2533
|
1752 return fold (e); |
578
|
1753 } |
|
1754 |
2375
|
1755 // Build a boolean expression. |
666
|
1756 |
578
|
1757 static tree_expression * |
2375
|
1758 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
1759 tree_expression *op2) |
578
|
1760 { |
2375
|
1761 tree_boolean_expression::type t; |
|
1762 |
578
|
1763 switch (op) |
|
1764 { |
2375
|
1765 case EXPR_AND_AND: |
2805
|
1766 t = tree_boolean_expression::bool_and; |
578
|
1767 break; |
777
|
1768 |
2375
|
1769 case EXPR_OR_OR: |
2805
|
1770 t = tree_boolean_expression::bool_or; |
578
|
1771 break; |
777
|
1772 |
578
|
1773 default: |
|
1774 panic_impossible (); |
|
1775 break; |
|
1776 } |
|
1777 |
|
1778 int l = tok_val->line (); |
|
1779 int c = tok_val->column (); |
|
1780 |
2533
|
1781 tree_boolean_expression *e |
|
1782 = new tree_boolean_expression (op1, op2, l, c, t); |
2375
|
1783 |
2533
|
1784 return fold (e); |
578
|
1785 } |
|
1786 |
2375
|
1787 // Build a prefix expression. |
666
|
1788 |
578
|
1789 static tree_expression * |
2960
|
1790 make_prefix_op (int op, tree_expression *op1, token *tok_val) |
578
|
1791 { |
2375
|
1792 tree_prefix_expression::type t; |
|
1793 |
578
|
1794 switch (op) |
|
1795 { |
2960
|
1796 case EXPR_NOT: |
|
1797 t = tree_prefix_expression::unot; |
|
1798 break; |
|
1799 |
|
1800 case '-': |
|
1801 t = tree_prefix_expression::uminus; |
|
1802 break; |
|
1803 |
578
|
1804 case PLUS_PLUS: |
2375
|
1805 t = tree_prefix_expression::increment; |
578
|
1806 break; |
777
|
1807 |
578
|
1808 case MINUS_MINUS: |
2375
|
1809 t = tree_prefix_expression::decrement; |
578
|
1810 break; |
777
|
1811 |
578
|
1812 default: |
|
1813 panic_impossible (); |
|
1814 break; |
|
1815 } |
|
1816 |
|
1817 int l = tok_val->line (); |
|
1818 int c = tok_val->column (); |
|
1819 |
3110
|
1820 // XXX FIXME XXX -- what about constant folding here? |
|
1821 |
2960
|
1822 return new tree_prefix_expression (t, op1, l, c); |
578
|
1823 } |
|
1824 |
2375
|
1825 // Build a postfix expression. |
666
|
1826 |
578
|
1827 static tree_expression * |
2960
|
1828 make_postfix_op (int op, tree_expression *op1, token *tok_val) |
578
|
1829 { |
2375
|
1830 tree_postfix_expression::type t; |
1623
|
1831 |
578
|
1832 switch (op) |
|
1833 { |
2960
|
1834 case QUOTE: |
|
1835 t = tree_postfix_expression::hermitian; |
|
1836 break; |
|
1837 |
|
1838 case TRANSPOSE: |
|
1839 t = tree_postfix_expression::transpose; |
|
1840 break; |
|
1841 |
2375
|
1842 case PLUS_PLUS: |
|
1843 t = tree_postfix_expression::increment; |
578
|
1844 break; |
777
|
1845 |
2375
|
1846 case MINUS_MINUS: |
|
1847 t = tree_postfix_expression::decrement; |
578
|
1848 break; |
777
|
1849 |
578
|
1850 default: |
|
1851 panic_impossible (); |
|
1852 break; |
|
1853 } |
|
1854 |
|
1855 int l = tok_val->line (); |
|
1856 int c = tok_val->column (); |
|
1857 |
3110
|
1858 // XXX FIXME XXX -- what about constant folding here? |
|
1859 |
2960
|
1860 return new tree_postfix_expression (t, op1, l, c); |
1623
|
1861 } |
|
1862 |
|
1863 // Build an unwind-protect command. |
|
1864 |
|
1865 static tree_command * |
|
1866 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
|
1867 tree_statement_list *cleanup, token *end_tok) |
|
1868 { |
|
1869 tree_command *retval = 0; |
|
1870 |
2857
|
1871 if (end_token_ok (end_tok, token::unwind_protect_end)) |
1623
|
1872 { |
|
1873 int l = unwind_tok->line (); |
|
1874 int c = unwind_tok->column (); |
|
1875 |
|
1876 retval = new tree_unwind_protect_command (body, cleanup, l, c); |
|
1877 } |
|
1878 |
|
1879 return retval; |
|
1880 } |
|
1881 |
|
1882 // Build a try-catch command. |
|
1883 |
|
1884 static tree_command * |
|
1885 make_try_command (token *try_tok, tree_statement_list *body, |
|
1886 tree_statement_list *cleanup, token *end_tok) |
|
1887 { |
|
1888 tree_command *retval = 0; |
|
1889 |
2857
|
1890 if (end_token_ok (end_tok, token::try_catch_end)) |
1623
|
1891 { |
|
1892 int l = try_tok->line (); |
|
1893 int c = try_tok->column (); |
|
1894 |
|
1895 retval = new tree_try_catch_command (body, cleanup, l, c); |
|
1896 } |
|
1897 |
|
1898 return retval; |
|
1899 } |
|
1900 |
|
1901 // Build a while command. |
|
1902 |
|
1903 static tree_command * |
|
1904 make_while_command (token *while_tok, tree_expression *expr, |
|
1905 tree_statement_list *body, token *end_tok) |
|
1906 { |
|
1907 tree_command *retval = 0; |
|
1908 |
|
1909 maybe_warn_assign_as_truth_value (expr); |
|
1910 |
2857
|
1911 if (end_token_ok (end_tok, token::while_end)) |
1623
|
1912 { |
1826
|
1913 lexer_flags.looping--; |
1623
|
1914 |
|
1915 int l = while_tok->line (); |
|
1916 int c = while_tok->column (); |
|
1917 |
|
1918 retval = new tree_while_command (expr, body, l, c); |
|
1919 } |
|
1920 |
|
1921 return retval; |
|
1922 } |
|
1923 |
|
1924 // Build a for command. |
|
1925 |
|
1926 static tree_command * |
2970
|
1927 make_for_command (token *for_tok, tree_argument_list *lhs, |
1623
|
1928 tree_expression *expr, tree_statement_list *body, |
|
1929 token *end_tok) |
|
1930 { |
|
1931 tree_command *retval = 0; |
|
1932 |
2857
|
1933 if (end_token_ok (end_tok, token::for_end)) |
1623
|
1934 { |
1826
|
1935 lexer_flags.looping--; |
1623
|
1936 |
|
1937 int l = for_tok->line (); |
|
1938 int c = for_tok->column (); |
|
1939 |
2970
|
1940 if (lhs->length () == 1) |
|
1941 { |
|
1942 tree_expression *tmp = lhs->remove_front (); |
|
1943 |
|
1944 retval = new tree_simple_for_command (tmp, expr, body, l, c); |
|
1945 |
|
1946 delete lhs; |
|
1947 } |
|
1948 else |
|
1949 retval = new tree_complex_for_command (lhs, expr, body, l, c); |
1623
|
1950 } |
|
1951 |
|
1952 return retval; |
|
1953 } |
|
1954 |
|
1955 // Build a break command. |
|
1956 |
|
1957 static tree_command * |
|
1958 make_break_command (token *break_tok) |
|
1959 { |
|
1960 tree_command *retval = 0; |
|
1961 |
2620
|
1962 int l = break_tok->line (); |
|
1963 int c = break_tok->column (); |
|
1964 |
|
1965 if (lexer_flags.looping || lexer_flags.defining_func || reading_script_file) |
|
1966 retval = new tree_break_command (l, c); |
1623
|
1967 else |
2620
|
1968 retval = new tree_no_op_command ("break", l, c); |
1623
|
1969 |
|
1970 return retval; |
|
1971 } |
|
1972 |
|
1973 // Build a continue command. |
|
1974 |
|
1975 static tree_command * |
|
1976 make_continue_command (token *continue_tok) |
|
1977 { |
|
1978 tree_command *retval = 0; |
|
1979 |
2620
|
1980 int l = continue_tok->line (); |
|
1981 int c = continue_tok->column (); |
|
1982 |
|
1983 if (lexer_flags.looping) |
|
1984 retval = new tree_continue_command (l, c); |
1623
|
1985 else |
2620
|
1986 retval = new tree_no_op_command ("continue", l, c); |
1623
|
1987 |
|
1988 return retval; |
|
1989 } |
|
1990 |
|
1991 // Build a return command. |
|
1992 |
|
1993 static tree_command * |
|
1994 make_return_command (token *return_tok) |
|
1995 { |
|
1996 tree_command *retval = 0; |
|
1997 |
2620
|
1998 int l = return_tok->line (); |
|
1999 int c = return_tok->column (); |
|
2000 |
|
2001 if (lexer_flags.defining_func || reading_script_file) |
|
2002 retval = new tree_return_command (l, c); |
1623
|
2003 else |
2620
|
2004 retval = new tree_no_op_command ("return", l, c); |
1623
|
2005 |
|
2006 return retval; |
|
2007 } |
|
2008 |
|
2009 // Start an if command. |
|
2010 |
|
2011 static tree_if_command_list * |
|
2012 start_if_command (tree_expression *expr, tree_statement_list *list) |
|
2013 { |
|
2014 maybe_warn_assign_as_truth_value (expr); |
|
2015 |
|
2016 tree_if_clause *t = new tree_if_clause (expr, list); |
|
2017 |
|
2018 return new tree_if_command_list (t); |
|
2019 } |
|
2020 |
|
2021 // Finish an if command. |
|
2022 |
|
2023 static tree_if_command * |
|
2024 finish_if_command (token *if_tok, tree_if_command_list *list, |
|
2025 token *end_tok) |
|
2026 { |
|
2027 tree_if_command *retval = 0; |
|
2028 |
2857
|
2029 if (end_token_ok (end_tok, token::if_end)) |
1623
|
2030 { |
|
2031 int l = if_tok->line (); |
|
2032 int c = if_tok->column (); |
|
2033 |
|
2034 retval = new tree_if_command (list, l, c); |
|
2035 } |
|
2036 |
|
2037 return retval; |
|
2038 } |
|
2039 |
|
2040 // Build an elseif clause. |
|
2041 |
|
2042 static tree_if_clause * |
|
2043 make_elseif_clause (tree_expression *expr, tree_statement_list *list) |
|
2044 { |
|
2045 maybe_warn_assign_as_truth_value (expr); |
|
2046 |
|
2047 return new tree_if_clause (expr, list); |
|
2048 } |
|
2049 |
2764
|
2050 // Finish a switch command. |
|
2051 |
|
2052 static tree_switch_command * |
|
2053 finish_switch_command (token *switch_tok, tree_expression *expr, |
|
2054 tree_switch_case_list *list, token *end_tok) |
|
2055 { |
|
2056 tree_switch_command *retval = 0; |
|
2057 |
2857
|
2058 if (end_token_ok (end_tok, token::switch_end)) |
2764
|
2059 { |
|
2060 int l = switch_tok->line (); |
|
2061 int c = switch_tok->column (); |
|
2062 |
|
2063 retval = new tree_switch_command (expr, list, l, c); |
|
2064 } |
|
2065 |
|
2066 return retval; |
|
2067 } |
|
2068 |
|
2069 // Build a switch case. |
|
2070 |
|
2071 static tree_switch_case * |
|
2072 make_switch_case (tree_expression *expr, tree_statement_list *list) |
|
2073 { |
|
2074 maybe_warn_variable_switch_label (expr); |
|
2075 |
|
2076 return new tree_switch_case (expr, list); |
|
2077 } |
|
2078 |
1623
|
2079 // Build an assignment to a variable. |
|
2080 |
|
2081 static tree_expression * |
2970
|
2082 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
2083 tree_expression *rhs) |
1623
|
2084 { |
2970
|
2085 tree_expression *retval = 0; |
|
2086 |
2883
|
2087 octave_value::assign_op t = octave_value::unknown_assign_op; |
|
2088 |
|
2089 switch (op) |
|
2090 { |
|
2091 case '=': |
|
2092 t = octave_value::asn_eq; |
|
2093 break; |
|
2094 |
|
2095 case ADD_EQ: |
|
2096 t = octave_value::add_eq; |
|
2097 break; |
|
2098 |
|
2099 case SUB_EQ: |
|
2100 t = octave_value::sub_eq; |
|
2101 break; |
|
2102 |
|
2103 case MUL_EQ: |
|
2104 t = octave_value::mul_eq; |
|
2105 break; |
|
2106 |
|
2107 case DIV_EQ: |
|
2108 t = octave_value::div_eq; |
|
2109 break; |
|
2110 |
2899
|
2111 case LSHIFT_EQ: |
|
2112 t = octave_value::lshift_eq; |
|
2113 break; |
|
2114 |
|
2115 case RSHIFT_EQ: |
|
2116 t = octave_value::rshift_eq; |
|
2117 break; |
|
2118 |
2883
|
2119 case EMUL_EQ: |
|
2120 t = octave_value::el_mul_eq; |
|
2121 break; |
|
2122 |
|
2123 case EDIV_EQ: |
|
2124 t = octave_value::el_div_eq; |
|
2125 break; |
|
2126 |
|
2127 case AND_EQ: |
|
2128 t = octave_value::el_and_eq; |
|
2129 break; |
|
2130 |
|
2131 case OR_EQ: |
|
2132 t = octave_value::el_or_eq; |
|
2133 break; |
|
2134 |
|
2135 default: |
|
2136 panic_impossible (); |
|
2137 break; |
|
2138 } |
|
2139 |
1623
|
2140 int l = eq_tok->line (); |
|
2141 int c = eq_tok->column (); |
|
2142 |
2970
|
2143 if (lhs->length () == 1) |
666
|
2144 { |
2970
|
2145 tree_expression *tmp = lhs->remove_front (); |
|
2146 |
|
2147 retval = new tree_simple_assignment (tmp, rhs, false, l, c, t); |
|
2148 |
|
2149 delete lhs; |
666
|
2150 } |
|
2151 else |
2970
|
2152 return new tree_multi_assignment (lhs, rhs, 0, l, c); |
666
|
2153 |
|
2154 return retval; |
|
2155 } |
751
|
2156 |
1623
|
2157 // Begin defining a function. |
|
2158 |
2891
|
2159 static octave_user_function * |
|
2160 start_function (tree_parameter_list *param_list, tree_statement_list *body) |
1623
|
2161 { |
|
2162 body->mark_as_function_body (); |
|
2163 |
2891
|
2164 // We'll fill in the return list later. |
|
2165 |
|
2166 octave_user_function *fcn |
|
2167 = new octave_user_function (param_list, 0, body, curr_sym_tab); |
1623
|
2168 |
|
2169 return fcn; |
|
2170 } |
|
2171 |
|
2172 // Do most of the work for defining a function. |
|
2173 |
2891
|
2174 static octave_user_function * |
|
2175 frob_function (tree_identifier *id, octave_user_function *fcn) |
1623
|
2176 { |
1755
|
2177 string id_name = id->name (); |
1623
|
2178 |
|
2179 // If input is coming from a file, issue a warning if the name of |
|
2180 // the file does not match the name of the function stated in the |
|
2181 // file. Matlab doesn't provide a diagnostic (it ignores the stated |
|
2182 // name). |
|
2183 |
|
2184 fcn->stash_function_name (id_name); |
|
2185 |
|
2186 if (reading_fcn_file) |
|
2187 { |
1755
|
2188 if (curr_fcn_file_name != id_name) |
1623
|
2189 { |
2166
|
2190 if (Vwarn_function_name_clash) |
1623
|
2191 warning ("function name `%s' does not agree with function\ |
1755
|
2192 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2193 |
|
2194 global_sym_tab->rename (id_name, curr_fcn_file_name); |
|
2195 |
|
2196 if (error_state) |
|
2197 return 0; |
|
2198 |
|
2199 id_name = id->name (); |
|
2200 } |
|
2201 |
|
2202 fcn->stash_function_name (id_name); |
|
2203 fcn->stash_fcn_file_name (); |
|
2204 fcn->stash_fcn_file_time (time (0)); |
|
2205 fcn->mark_as_system_fcn_file (); |
|
2206 } |
|
2207 else if (! (input_from_tmp_history_file || input_from_startup_file) |
|
2208 && reading_script_file |
1755
|
2209 && curr_fcn_file_name == id_name) |
1623
|
2210 { |
|
2211 warning ("function `%s' defined within script file `%s'", |
1755
|
2212 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2213 } |
|
2214 |
|
2215 top_level_sym_tab->clear (id_name); |
|
2216 |
2856
|
2217 symbol_record *sr = global_sym_tab->lookup (id_name); |
2791
|
2218 |
|
2219 if (sr) |
|
2220 fcn->stash_symtab_ptr (sr); |
|
2221 else |
|
2222 panic_impossible (); |
|
2223 |
3016
|
2224 id->define (fcn, symbol_record::USER_FUNCTION); |
1755
|
2225 |
1623
|
2226 id->document (help_buf); |
|
2227 |
|
2228 return fcn; |
|
2229 } |
|
2230 |
|
2231 // Finish defining a function. |
|
2232 |
2891
|
2233 static octave_user_function * |
|
2234 finish_function (tree_identifier *id, octave_user_function *fcn) |
1623
|
2235 { |
2883
|
2236 tree_parameter_list *tpl = new tree_parameter_list (id); |
1623
|
2237 |
|
2238 tpl->mark_as_formal_parameters (); |
|
2239 |
|
2240 return fcn->define_ret_list (tpl); |
|
2241 } |
|
2242 |
|
2243 // Finish defining a function a different way. |
|
2244 |
2891
|
2245 static octave_user_function * |
|
2246 finish_function (tree_parameter_list *ret_list, octave_user_function *fcn) |
1623
|
2247 { |
|
2248 ret_list->mark_as_formal_parameters (); |
|
2249 |
|
2250 return fcn->define_ret_list (ret_list); |
|
2251 } |
|
2252 |
2883
|
2253 static void |
|
2254 recover_from_parsing_function (void) |
|
2255 { |
|
2256 curr_sym_tab = top_level_sym_tab; |
|
2257 |
|
2258 lexer_flags.defining_func = false; |
|
2259 lexer_flags.beginning_of_function = false; |
|
2260 lexer_flags.parsed_function_name = false; |
|
2261 lexer_flags.looking_at_return_list = false; |
|
2262 lexer_flags.looking_at_parameter_list = false; |
|
2263 } |
|
2264 |
2846
|
2265 // Make an index expression. |
|
2266 |
751
|
2267 static tree_index_expression * |
2970
|
2268 make_index_expression (tree_expression *expr, tree_argument_list *args) |
751
|
2269 { |
|
2270 tree_index_expression *retval = 0; |
|
2271 |
2970
|
2272 int l = expr->line (); |
|
2273 int c = expr->column (); |
|
2274 |
|
2275 expr->mark_postfix_indexed (); |
|
2276 |
|
2277 retval = new tree_index_expression (expr, args, l, c); |
|
2278 |
|
2279 return retval; |
|
2280 } |
|
2281 |
|
2282 // Make an indirect reference expression. |
|
2283 |
|
2284 static tree_indirect_ref * |
|
2285 make_indirect_ref (tree_expression *expr, const string& elt) |
|
2286 { |
|
2287 tree_indirect_ref *retval = 0; |
|
2288 |
|
2289 int l = expr->line (); |
|
2290 int c = expr->column (); |
|
2291 |
|
2292 retval = new tree_indirect_ref (expr, elt, l, c); |
|
2293 |
|
2294 lexer_flags.looking_at_indirect_ref = false; |
751
|
2295 |
|
2296 return retval; |
|
2297 } |
1511
|
2298 |
2846
|
2299 // Make a declaration command. |
|
2300 |
|
2301 static tree_decl_command * |
|
2302 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) |
|
2303 { |
|
2304 tree_decl_command *retval = 0; |
|
2305 |
|
2306 int l = tok_val->line (); |
|
2307 int c = tok_val->column (); |
|
2308 |
|
2309 switch (tok) |
|
2310 { |
|
2311 case GLOBAL: |
|
2312 retval = new tree_global_command (lst, l, c); |
|
2313 break; |
|
2314 |
|
2315 case STATIC: |
|
2316 if (lexer_flags.defining_func) |
|
2317 retval = new tree_static_command (lst, l, c); |
|
2318 else |
|
2319 { |
|
2320 if (reading_script_file) |
|
2321 warning ("ignoring static declaration near line %d of file `%s'", |
|
2322 l, curr_fcn_file_full_name.c_str ()); |
|
2323 else |
|
2324 warning ("ignoring static declaration near line %d", l); |
|
2325 } |
|
2326 break; |
|
2327 |
|
2328 default: |
|
2329 panic_impossible (); |
|
2330 break; |
|
2331 } |
|
2332 |
|
2333 return retval; |
|
2334 } |
|
2335 |
1623
|
2336 // Finish building a matrix list. |
|
2337 |
|
2338 static tree_expression * |
1829
|
2339 finish_matrix (tree_matrix *m) |
1623
|
2340 { |
3110
|
2341 tree_expression *retval = m; |
|
2342 |
|
2343 unwind_protect::begin_frame ("finish_matrix"); |
|
2344 |
|
2345 unwind_protect_int (error_state); |
|
2346 |
|
2347 unwind_protect_bool (buffer_error_messages); |
|
2348 buffer_error_messages = true; |
|
2349 |
|
2350 unwind_protect::add (clear_global_error_variable, 0); |
1623
|
2351 |
2533
|
2352 if (m->all_elements_are_constant ()) |
1829
|
2353 { |
2970
|
2354 octave_value tmp = m->rvalue (); |
1623
|
2355 |
2533
|
2356 if (! error_state) |
|
2357 { |
|
2358 tree_constant *tc_retval = new tree_constant (tmp); |
|
2359 |
|
2360 ostrstream buf; |
|
2361 |
|
2362 tree_print_code tpc (buf); |
|
2363 |
|
2364 m->accept (tpc); |
|
2365 |
|
2366 buf << ends; |
1623
|
2367 |
2533
|
2368 char *s = buf.str (); |
|
2369 |
|
2370 tc_retval->stash_original_text (s); |
|
2371 |
|
2372 delete [] s; |
|
2373 |
|
2374 delete m; |
|
2375 |
|
2376 retval = tc_retval; |
|
2377 } |
1623
|
2378 } |
3110
|
2379 |
|
2380 unwind_protect::run_frame ("finish_matrix"); |
1623
|
2381 |
|
2382 return retval; |
|
2383 } |
|
2384 |
1511
|
2385 static void |
|
2386 maybe_warn_missing_semi (tree_statement_list *t) |
|
2387 { |
2166
|
2388 if (lexer_flags.defining_func && Vwarn_missing_semicolon) |
1511
|
2389 { |
|
2390 tree_statement *tmp = t->rear(); |
1607
|
2391 |
1511
|
2392 if (tmp->is_expression ()) |
1607
|
2393 warning ("missing semicolon near line %d, column %d in file `%s'", |
1755
|
2394 tmp->line (), tmp->column (), |
|
2395 curr_fcn_file_full_name.c_str ()); |
1511
|
2396 } |
|
2397 } |
1994
|
2398 |
2525
|
2399 static void |
|
2400 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2401 bool warn_missing_semi) |
|
2402 { |
|
2403 switch (sep) |
|
2404 { |
|
2405 case ';': |
|
2406 { |
|
2407 tree_statement *tmp = list->rear (); |
|
2408 tmp->set_print_flag (0); |
|
2409 } |
|
2410 break; |
|
2411 |
|
2412 case 0: |
|
2413 case ',': |
|
2414 case '\n': |
|
2415 if (warn_missing_semi) |
|
2416 maybe_warn_missing_semi (list); |
|
2417 break; |
|
2418 |
|
2419 default: |
|
2420 warning ("unrecognized separator type!"); |
|
2421 break; |
|
2422 } |
|
2423 } |
|
2424 |
3021
|
2425 void |
|
2426 parse_and_execute (FILE *f) |
|
2427 { |
|
2428 unwind_protect::begin_frame ("parse_and_execute"); |
|
2429 |
|
2430 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2431 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
2432 |
|
2433 unwind_protect::add (restore_input_buffer, old_buf); |
|
2434 unwind_protect::add (delete_input_buffer, new_buf); |
|
2435 |
|
2436 switch_to_buffer (new_buf); |
|
2437 |
|
2438 unwind_protect_bool (line_editing); |
|
2439 unwind_protect_bool (input_from_command_line_file); |
|
2440 |
|
2441 line_editing = false; |
|
2442 input_from_command_line_file = false; |
|
2443 |
|
2444 unwind_protect_ptr (curr_sym_tab); |
|
2445 |
|
2446 int retval; |
|
2447 do |
|
2448 { |
|
2449 reset_parser (); |
|
2450 |
|
2451 retval = yyparse (); |
|
2452 |
|
2453 if (retval == 0 && global_command) |
|
2454 { |
|
2455 global_command->eval (); |
|
2456 |
|
2457 delete global_command; |
|
2458 |
|
2459 global_command = 0; |
|
2460 |
|
2461 bool quit = (tree_return_command::returning |
|
2462 || tree_break_command::breaking); |
|
2463 |
|
2464 if (tree_return_command::returning) |
|
2465 tree_return_command::returning = 0; |
|
2466 |
|
2467 if (tree_break_command::breaking) |
|
2468 tree_break_command::breaking--; |
|
2469 |
|
2470 if (error_state) |
|
2471 { |
|
2472 error ("near line %d of file `%s'", input_line_number, |
|
2473 curr_fcn_file_full_name.c_str ()); |
|
2474 |
|
2475 break; |
|
2476 } |
|
2477 |
|
2478 if (quit) |
|
2479 break; |
|
2480 } |
|
2481 } |
|
2482 while (retval == 0); |
|
2483 |
|
2484 unwind_protect::run_frame ("parse_and_execute"); |
|
2485 } |
|
2486 |
|
2487 static void |
|
2488 safe_fclose (void *f) |
|
2489 { |
|
2490 if (f) |
|
2491 fclose (static_cast<FILE *> (f)); |
|
2492 } |
|
2493 |
|
2494 void |
|
2495 parse_and_execute (const string& s, bool verbose, const char *warn_for) |
|
2496 { |
|
2497 unwind_protect::begin_frame ("parse_and_execute_2"); |
|
2498 |
|
2499 unwind_protect_bool (reading_script_file); |
|
2500 unwind_protect_str (curr_fcn_file_full_name); |
|
2501 |
|
2502 reading_script_file = true; |
|
2503 curr_fcn_file_full_name = s; |
|
2504 |
|
2505 FILE *f = get_input_from_file (s, 0); |
|
2506 |
|
2507 if (f) |
|
2508 { |
|
2509 unwind_protect::add (safe_fclose, f); |
|
2510 |
|
2511 unwind_protect_int (input_line_number); |
|
2512 unwind_protect_int (current_input_column); |
|
2513 |
|
2514 input_line_number = 0; |
|
2515 current_input_column = 1; |
|
2516 |
|
2517 if (verbose) |
|
2518 { |
|
2519 cout << "reading commands from " << s << " ... "; |
|
2520 reading_startup_message_printed = true; |
|
2521 cout.flush (); |
|
2522 } |
|
2523 |
|
2524 parse_and_execute (f); |
|
2525 |
|
2526 if (verbose) |
|
2527 cout << "done." << endl; |
|
2528 } |
|
2529 else if (warn_for) |
|
2530 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
|
2531 |
|
2532 unwind_protect::run_frame ("parse_and_execute_2"); |
|
2533 } |
|
2534 |
|
2535 static bool |
|
2536 looks_like_octave_copyright (const string& s) |
|
2537 { |
|
2538 bool retval = false; |
|
2539 |
|
2540 string t = s.substr (0, 15); |
|
2541 |
|
2542 if (t == " Copyright (C) ") |
|
2543 { |
|
2544 size_t pos = s.find ('\n'); |
|
2545 |
|
2546 if (pos != NPOS) |
|
2547 { |
|
2548 pos = s.find ('\n', pos + 1); |
|
2549 |
|
2550 if (pos != NPOS) |
|
2551 { |
|
2552 pos++; |
|
2553 |
|
2554 t = s.substr (pos, 29); |
|
2555 |
|
2556 if (t == " This file is part of Octave." |
|
2557 || t == " This program is free softwar") |
|
2558 retval = true; |
|
2559 } |
|
2560 } |
|
2561 } |
|
2562 |
|
2563 return retval; |
|
2564 } |
|
2565 |
|
2566 // Eat whitespace and comments from FFILE, returning the text of the |
|
2567 // comments read if it doesn't look like a copyright notice. If |
|
2568 // IN_PARTS, consider each block of comments separately; otherwise, |
|
2569 // grab them all at once. If UPDATE_POS is TRUE, line and column |
|
2570 // number information is updated. |
|
2571 |
|
2572 // XXX FIXME XXX -- grab_help_text() in lex.l duplicates some of this |
|
2573 // code! |
|
2574 |
|
2575 static string |
|
2576 gobble_leading_white_space (FILE *ffile, bool in_parts, bool update_pos) |
|
2577 { |
|
2578 string help_txt; |
|
2579 |
|
2580 bool first_comments_seen = false; |
|
2581 bool begin_comment = false; |
|
2582 bool have_help_text = false; |
|
2583 bool in_comment = false; |
|
2584 int c; |
|
2585 |
|
2586 while ((c = getc (ffile)) != EOF) |
|
2587 { |
|
2588 if (update_pos) |
|
2589 current_input_column++; |
|
2590 |
|
2591 if (begin_comment) |
|
2592 { |
|
2593 if (c == '%' || c == '#') |
|
2594 continue; |
|
2595 else |
|
2596 begin_comment = false; |
|
2597 } |
|
2598 |
|
2599 if (in_comment) |
|
2600 { |
|
2601 if (! have_help_text) |
|
2602 { |
|
2603 first_comments_seen = true; |
|
2604 help_txt += (char) c; |
|
2605 } |
|
2606 |
|
2607 if (c == '\n') |
|
2608 { |
|
2609 if (update_pos) |
|
2610 { |
|
2611 input_line_number++; |
|
2612 current_input_column = 0; |
|
2613 } |
|
2614 in_comment = false; |
|
2615 |
|
2616 if (in_parts) |
|
2617 { |
|
2618 if ((c = getc (ffile)) != EOF) |
|
2619 { |
|
2620 if (update_pos) |
|
2621 current_input_column--; |
|
2622 ungetc (c, ffile); |
|
2623 if (c == '\n') |
|
2624 break; |
|
2625 } |
|
2626 else |
|
2627 break; |
|
2628 } |
|
2629 } |
|
2630 } |
|
2631 else |
|
2632 { |
|
2633 switch (c) |
|
2634 { |
|
2635 case ' ': |
|
2636 case '\t': |
|
2637 if (first_comments_seen) |
|
2638 have_help_text = true; |
|
2639 break; |
|
2640 |
|
2641 case '\n': |
|
2642 if (first_comments_seen) |
|
2643 have_help_text = true; |
|
2644 if (update_pos) |
|
2645 { |
|
2646 input_line_number++; |
|
2647 current_input_column = 0; |
|
2648 } |
|
2649 continue; |
|
2650 |
|
2651 case '%': |
|
2652 case '#': |
|
2653 begin_comment = true; |
|
2654 in_comment = true; |
|
2655 break; |
|
2656 |
|
2657 default: |
|
2658 if (update_pos) |
|
2659 current_input_column--; |
|
2660 ungetc (c, ffile); |
|
2661 goto done; |
|
2662 } |
|
2663 } |
|
2664 } |
|
2665 |
|
2666 done: |
|
2667 |
|
2668 if (! help_txt.empty ()) |
|
2669 { |
|
2670 if (looks_like_octave_copyright (help_txt)) |
|
2671 help_txt.resize (0); |
|
2672 |
|
2673 if (in_parts && help_txt.empty ()) |
|
2674 help_txt = gobble_leading_white_space (ffile, in_parts, update_pos); |
|
2675 } |
|
2676 |
|
2677 return help_txt; |
|
2678 } |
|
2679 |
|
2680 string |
|
2681 get_help_from_file (const string& path) |
|
2682 { |
|
2683 string retval; |
|
2684 |
|
2685 if (! path.empty ()) |
|
2686 { |
|
2687 FILE *fptr = fopen (path.c_str (), "r"); |
|
2688 |
|
2689 if (fptr) |
|
2690 { |
|
2691 unwind_protect::add (safe_fclose, (void *) fptr); |
|
2692 |
|
2693 retval = gobble_leading_white_space (fptr, true, true); |
|
2694 |
|
2695 unwind_protect::run (); |
|
2696 } |
|
2697 } |
|
2698 |
|
2699 return retval; |
|
2700 } |
|
2701 |
|
2702 static int |
|
2703 is_function_file (FILE *ffile) |
|
2704 { |
|
2705 int status = 0; |
|
2706 |
|
2707 long pos = ftell (ffile); |
|
2708 |
|
2709 gobble_leading_white_space (ffile, false, false); |
|
2710 |
|
2711 char buf [10]; |
|
2712 fgets (buf, 10, ffile); |
|
2713 int len = strlen (buf); |
|
2714 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
2715 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
2716 status = 1; |
|
2717 |
|
2718 fseek (ffile, pos, SEEK_SET); |
|
2719 |
|
2720 return status; |
|
2721 } |
|
2722 |
|
2723 static void |
|
2724 restore_command_history (void *) |
|
2725 { |
|
2726 command_history::ignore_entries (! Vsaving_history); |
|
2727 } |
|
2728 |
|
2729 static void |
|
2730 restore_input_stream (void *f) |
|
2731 { |
|
2732 command_editor::set_input_stream (static_cast<FILE *> (f)); |
|
2733 } |
|
2734 |
|
2735 static bool |
|
2736 parse_fcn_file (bool exec_script, const string& ff) |
|
2737 { |
|
2738 unwind_protect::begin_frame ("parse_fcn_file"); |
|
2739 |
|
2740 int script_file_executed = false; |
|
2741 |
|
2742 // Open function file and parse. |
|
2743 |
|
2744 bool old_reading_fcn_file_state = reading_fcn_file; |
|
2745 |
|
2746 FILE *in_stream = command_editor::get_input_stream (); |
|
2747 |
|
2748 unwind_protect::add (restore_input_stream, in_stream); |
|
2749 |
|
2750 unwind_protect_ptr (ff_instream); |
|
2751 |
|
2752 unwind_protect_int (input_line_number); |
|
2753 unwind_protect_int (current_input_column); |
|
2754 unwind_protect_bool (reading_fcn_file); |
|
2755 unwind_protect_bool (line_editing); |
|
2756 |
|
2757 input_line_number = 0; |
|
2758 current_input_column = 1; |
|
2759 reading_fcn_file = true; |
|
2760 line_editing = false; |
|
2761 |
|
2762 FILE *ffile = get_input_from_file (ff, 0); |
|
2763 |
|
2764 unwind_protect::add (safe_fclose, ffile); |
|
2765 |
|
2766 if (ffile) |
|
2767 { |
|
2768 // Check to see if this file defines a function or is just a |
|
2769 // list of commands. |
|
2770 |
|
2771 if (is_function_file (ffile)) |
|
2772 { |
|
2773 // XXX FIXME XXX -- we shouldn't need both the |
|
2774 // command_history object and the |
|
2775 // Vsaving_history variable... |
|
2776 command_history::ignore_entries (); |
|
2777 |
|
2778 unwind_protect::add (restore_command_history, 0); |
|
2779 |
|
2780 unwind_protect_int (Vecho_executing_commands); |
|
2781 unwind_protect_bool (Vsaving_history); |
|
2782 unwind_protect_bool (reading_fcn_file); |
|
2783 unwind_protect_bool (input_from_command_line_file); |
|
2784 |
|
2785 Vecho_executing_commands = ECHO_OFF; |
|
2786 Vsaving_history = false; |
|
2787 reading_fcn_file = true; |
|
2788 input_from_command_line_file = false; |
|
2789 |
|
2790 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2791 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
2792 |
|
2793 unwind_protect::add (restore_input_buffer, (void *) old_buf); |
|
2794 unwind_protect::add (delete_input_buffer, (void *) new_buf); |
|
2795 |
|
2796 switch_to_buffer (new_buf); |
|
2797 |
|
2798 unwind_protect_ptr (curr_sym_tab); |
|
2799 |
|
2800 reset_parser (); |
|
2801 |
|
2802 help_buf = gobble_leading_white_space (ffile, true, true); |
|
2803 |
|
2804 // XXX FIXME XXX -- this should not be necessary. |
|
2805 gobble_leading_white_space (ffile, false, true); |
|
2806 |
|
2807 int status = yyparse (); |
|
2808 |
|
2809 if (status != 0) |
|
2810 { |
|
2811 error ("parse error while reading function file %s", |
|
2812 ff.c_str ()); |
|
2813 global_sym_tab->clear (curr_fcn_file_name); |
|
2814 } |
|
2815 } |
|
2816 else if (exec_script) |
|
2817 { |
|
2818 // The value of `reading_fcn_file' will be restored to the |
|
2819 // proper value when we unwind from this frame. |
|
2820 reading_fcn_file = old_reading_fcn_file_state; |
|
2821 |
|
2822 // XXX FIXME XXX -- we shouldn't need both the |
|
2823 // command_history object and the |
|
2824 // Vsaving_history variable... |
|
2825 command_history::ignore_entries (); |
|
2826 |
|
2827 unwind_protect::add (restore_command_history, 0); |
|
2828 |
|
2829 unwind_protect_bool (Vsaving_history); |
|
2830 unwind_protect_bool (reading_script_file); |
|
2831 |
|
2832 Vsaving_history = false; |
|
2833 reading_script_file = true; |
|
2834 |
|
2835 parse_and_execute (ffile); |
|
2836 |
|
2837 script_file_executed = true; |
|
2838 } |
|
2839 } |
|
2840 |
|
2841 unwind_protect::run_frame ("parse_fcn_file"); |
|
2842 |
|
2843 return script_file_executed; |
|
2844 } |
|
2845 |
|
2846 bool |
|
2847 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
|
2848 { |
|
2849 bool script_file_executed = false; |
|
2850 |
|
2851 string nm = sym_rec->name (); |
|
2852 |
|
2853 if (octave_dynamic_loader::load_fcn_from_dot_oct_file (nm)) |
|
2854 { |
|
2855 force_link_to_function (nm); |
|
2856 } |
|
2857 else |
|
2858 { |
|
2859 string ff = fcn_file_in_path (nm); |
|
2860 |
|
2861 // These are needed by yyparse. |
|
2862 |
|
2863 unwind_protect::begin_frame ("load_fcn_from_file"); |
|
2864 |
|
2865 unwind_protect_str (curr_fcn_file_name); |
|
2866 unwind_protect_str (curr_fcn_file_full_name); |
|
2867 |
|
2868 curr_fcn_file_name = nm; |
|
2869 curr_fcn_file_full_name = ff; |
|
2870 |
|
2871 if (ff.length () > 0) |
|
2872 script_file_executed = parse_fcn_file (exec_script, ff); |
|
2873 |
|
2874 if (! (error_state || script_file_executed)) |
|
2875 force_link_to_function (nm); |
|
2876 |
|
2877 unwind_protect::run_frame ("load_fcn_from_file"); |
|
2878 } |
|
2879 |
|
2880 return script_file_executed; |
|
2881 } |
|
2882 |
|
2883 DEFUN (source, args, , |
|
2884 "source (FILE)\n\ |
|
2885 \n\ |
|
2886 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
2887 script file but without requiring the file to be named `FILE.m'.") |
|
2888 { |
|
2889 octave_value_list retval; |
|
2890 |
|
2891 int nargin = args.length (); |
|
2892 |
|
2893 if (nargin == 1) |
|
2894 { |
|
2895 string file = args(0).string_value (); |
|
2896 |
|
2897 if (! error_state) |
|
2898 { |
|
2899 file = file_ops::tilde_expand (file); |
|
2900 |
3095
|
2901 parse_fcn_file (true, file); |
3021
|
2902 |
|
2903 if (error_state) |
|
2904 error ("source: error sourcing file `%s'", file.c_str ()); |
|
2905 } |
|
2906 else |
|
2907 error ("source: expecting file name as argument"); |
|
2908 } |
|
2909 else |
|
2910 print_usage ("source"); |
|
2911 |
|
2912 return retval; |
|
2913 } |
|
2914 |
|
2915 octave_value_list |
|
2916 feval (const octave_value_list& args, int nargout) |
|
2917 { |
|
2918 octave_value_list retval; |
|
2919 |
|
2920 octave_function *fcn = is_valid_function (args(0), "feval", 1); |
|
2921 |
|
2922 if (fcn) |
|
2923 { |
|
2924 string_vector arg_names = args.name_tags (); |
|
2925 |
|
2926 int tmp_nargin = args.length () - 1; |
|
2927 |
|
2928 octave_value_list tmp_args (tmp_nargin, octave_value ()); |
|
2929 |
|
2930 string_vector tmp_arg_names (tmp_nargin); |
|
2931 |
|
2932 for (int i = 0; i < tmp_nargin; i++) |
|
2933 { |
|
2934 tmp_args(i) = args(i+1); |
|
2935 tmp_arg_names(i) = arg_names(i+1); |
|
2936 } |
|
2937 |
|
2938 tmp_args.stash_name_tags (tmp_arg_names); |
|
2939 |
|
2940 retval = fcn->do_index_op (nargout, tmp_args); |
|
2941 } |
|
2942 |
|
2943 return retval; |
|
2944 } |
|
2945 |
|
2946 DEFUN (feval, args, nargout, |
|
2947 "feval (NAME, ARGS, ...)\n\ |
|
2948 \n\ |
|
2949 evaluate NAME as a function, passing ARGS as its arguments") |
|
2950 { |
|
2951 octave_value_list retval; |
|
2952 |
|
2953 int nargin = args.length (); |
|
2954 |
|
2955 if (nargin > 0) |
|
2956 retval = feval (args, nargout); |
|
2957 else |
|
2958 print_usage ("feval"); |
|
2959 |
|
2960 return retval; |
|
2961 } |
|
2962 |
3099
|
2963 octave_value_list |
3021
|
2964 eval_string (const string& s, bool silent, int& parse_status, int nargout) |
|
2965 { |
|
2966 unwind_protect::begin_frame ("eval_string"); |
|
2967 |
|
2968 unwind_protect_bool (get_input_from_eval_string); |
|
2969 unwind_protect_bool (input_from_command_line_file); |
|
2970 unwind_protect_ptr (global_command); |
|
2971 unwind_protect_str (current_eval_string); |
|
2972 |
|
2973 get_input_from_eval_string = true; |
|
2974 input_from_command_line_file = false; |
|
2975 current_eval_string = s; |
|
2976 |
|
2977 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2978 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
2979 |
|
2980 unwind_protect::add (restore_input_buffer, old_buf); |
|
2981 unwind_protect::add (delete_input_buffer, new_buf); |
|
2982 |
|
2983 switch_to_buffer (new_buf); |
|
2984 |
|
2985 unwind_protect_ptr (curr_sym_tab); |
|
2986 |
|
2987 reset_parser (); |
|
2988 |
|
2989 parse_status = yyparse (); |
|
2990 |
|
2991 // Important to reset the idea of where input is coming from before |
|
2992 // trying to eval the command we just parsed -- it might contain the |
|
2993 // name of an function file that still needs to be parsed! |
|
2994 |
|
2995 tree_statement_list *command = global_command; |
|
2996 |
|
2997 unwind_protect::run_frame ("eval_string"); |
|
2998 |
|
2999 octave_value_list retval; |
|
3000 |
|
3001 if (parse_status == 0 && command) |
|
3002 { |
|
3003 retval = command->eval (silent, nargout); |
|
3004 delete command; |
|
3005 } |
|
3006 |
|
3007 return retval; |
|
3008 } |
|
3009 |
|
3010 octave_value |
|
3011 eval_string (const string& s, bool silent, int& parse_status) |
|
3012 { |
|
3013 octave_value retval; |
|
3014 |
|
3015 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
|
3016 |
|
3017 if (! tmp.empty ()) |
|
3018 retval = tmp(0); |
|
3019 |
|
3020 return retval; |
|
3021 } |
|
3022 |
|
3023 static octave_value_list |
|
3024 eval_string (const octave_value& arg, bool silent, int& parse_status, |
|
3025 int nargout) |
|
3026 { |
|
3027 string s = arg.string_value (); |
|
3028 |
|
3029 if (error_state) |
|
3030 { |
|
3031 error ("eval: expecting string argument"); |
|
3032 return -1.0; |
|
3033 } |
|
3034 |
|
3035 return eval_string (s, silent, parse_status, nargout); |
|
3036 } |
|
3037 |
|
3038 DEFUN (eval, args, nargout, |
|
3039 "eval (TRY, CATCH)\n\ |
|
3040 \n\ |
|
3041 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
3042 string CATCH.") |
|
3043 { |
|
3044 octave_value_list retval; |
|
3045 |
|
3046 int nargin = args.length (); |
|
3047 |
|
3048 if (nargin > 0) |
|
3049 { |
|
3050 unwind_protect::begin_frame ("Feval"); |
|
3051 |
|
3052 if (nargin > 1) |
|
3053 { |
|
3054 unwind_protect_bool (buffer_error_messages); |
|
3055 buffer_error_messages = true; |
|
3056 } |
|
3057 |
|
3058 int parse_status = 0; |
|
3059 |
|
3060 retval = eval_string (args(0), ! Vdefault_eval_print_flag, |
|
3061 parse_status, nargout); |
|
3062 |
|
3063 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
3064 { |
|
3065 error_state = 0; |
|
3066 |
|
3067 // Set up for letting the user print any messages from |
|
3068 // errors that occurred in the first part of this eval(). |
|
3069 |
|
3070 buffer_error_messages = false; |
|
3071 bind_global_error_variable (); |
|
3072 unwind_protect::add (clear_global_error_variable, 0); |
|
3073 |
|
3074 eval_string (args(1), 0, parse_status, nargout); |
|
3075 |
|
3076 retval = octave_value_list (); |
|
3077 } |
|
3078 |
|
3079 unwind_protect::run_frame ("Feval"); |
|
3080 } |
|
3081 else |
|
3082 print_usage ("eval"); |
|
3083 |
|
3084 return retval; |
|
3085 } |
|
3086 |
|
3087 static int |
|
3088 default_eval_print_flag (void) |
|
3089 { |
|
3090 Vdefault_eval_print_flag = check_preference ("default_eval_print_flag"); |
|
3091 |
|
3092 return 0; |
|
3093 } |
|
3094 |
2166
|
3095 static int |
|
3096 warn_assign_as_truth_value (void) |
|
3097 { |
|
3098 Vwarn_assign_as_truth_value |
|
3099 = check_preference ("warn_assign_as_truth_value"); |
|
3100 |
|
3101 return 0; |
|
3102 } |
|
3103 |
|
3104 static int |
|
3105 warn_function_name_clash (void) |
|
3106 { |
|
3107 Vwarn_function_name_clash = check_preference ("warn_function_name_clash"); |
|
3108 |
|
3109 return 0; |
|
3110 } |
|
3111 |
|
3112 static int |
|
3113 warn_missing_semicolon (void) |
|
3114 { |
|
3115 Vwarn_missing_semicolon = check_preference ("warn_missing_semicolon"); |
|
3116 |
|
3117 return 0; |
|
3118 } |
|
3119 |
2764
|
3120 static int |
|
3121 warn_variable_switch_label (void) |
|
3122 { |
|
3123 Vwarn_variable_switch_label |
|
3124 = check_preference ("warn_variable_switch_label"); |
|
3125 |
|
3126 return 0; |
|
3127 } |
|
3128 |
2166
|
3129 void |
|
3130 symbols_of_parse (void) |
|
3131 { |
3021
|
3132 DEFVAR (default_eval_print_flag, 1.0, 0, default_eval_print_flag, |
|
3133 "If the value of this variable is nonzero, Octave will print the\n\ |
|
3134 results of commands executed by eval() that do not end with semicolons."); |
|
3135 |
2166
|
3136 DEFVAR (warn_assign_as_truth_value, 1.0, 0, warn_assign_as_truth_value, |
|
3137 "produce warning for assignments used as truth values"); |
|
3138 |
|
3139 DEFVAR (warn_function_name_clash, 1.0, 0, warn_function_name_clash, |
|
3140 "produce warning if function name conflicts with file name"); |
|
3141 |
|
3142 DEFVAR (warn_missing_semicolon, 0.0, 0, warn_missing_semicolon, |
2764
|
3143 "produce a warning if a statement in a function file is not\n\ |
2166
|
3144 terminated with a semicolon"); |
2764
|
3145 |
|
3146 DEFVAR (warn_variable_switch_label, 0.0, 0, warn_variable_switch_label, |
|
3147 "produce warning for variables used as switch labels"); |
2166
|
3148 } |
|
3149 |
1994
|
3150 /* |
|
3151 ;;; Local Variables: *** |
|
3152 ;;; mode: text *** |
|
3153 ;;; End: *** |
|
3154 */ |