3 Copyright (C) 2009 John W. Eaton
5 This file is part of Octave.
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 3 of the License, or (at your
10 option) any later version.
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
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
38 #include "ov-fcn-handle.h"
39 #include "ov-usr-fcn.h"
40 #include "variables.h"
44 #include "unwind-prot.h"
46 static tree_evaluator std_evaluator;
48 tree_evaluator *current_evaluator = &std_evaluator;
50 int tree_evaluator::dbstep_flag = 0;
52 size_t tree_evaluator::current_frame = 0;
54 bool tree_evaluator::debug_mode = false;
56 bool tree_evaluator::in_fcn_or_script_body = false;
58 bool tree_evaluator::in_loop_command = false;
60 int tree_evaluator::db_line = -1;
61 int tree_evaluator::db_column = -1;
63 // If TRUE, turn off printing of results in functions (as if a
64 // semicolon has been appended to each statement).
65 static bool Vsilent_functions = false;
70 tree_evaluator::visit_anon_fcn_handle (tree_anon_fcn_handle&)
76 tree_evaluator::visit_argument_list (tree_argument_list&)
82 tree_evaluator::visit_binary_expression (tree_binary_expression&)
88 tree_evaluator::visit_break_command (tree_break_command& cmd)
93 do_breakpoint (cmd.is_breakpoint (), cmd.line (), cmd.column ());
95 tree_break_command::breaking = 1;
100 tree_evaluator::visit_colon_expression (tree_colon_expression&)
106 tree_evaluator::visit_continue_command (tree_continue_command&)
109 tree_continue_command::continuing = 1;
113 do_global_init (tree_decl_elt& elt)
115 tree_identifier *id = elt.ident ();
123 octave_lvalue ult = id->lvalue ();
125 if (ult.is_undefined ())
127 tree_expression *expr = elt.expression ();
129 octave_value init_val;
132 init_val = expr->rvalue1 ();
134 init_val = Matrix ();
136 ult.assign (octave_value::op_asn_eq, init_val);
143 do_static_init (tree_decl_elt& elt)
145 tree_identifier *id = elt.ident ();
149 id->mark_as_static ();
151 octave_lvalue ult = id->lvalue ();
153 if (ult.is_undefined ())
155 tree_expression *expr = elt.expression ();
157 octave_value init_val;
160 init_val = expr->rvalue1 ();
162 init_val = Matrix ();
164 ult.assign (octave_value::op_asn_eq, init_val);
170 tree_evaluator::do_decl_init_list (decl_elt_init_fcn fcn,
171 tree_decl_init_list *init_list)
175 for (tree_decl_init_list::iterator p = init_list->begin ();
176 p != init_list->end (); p++)
178 tree_decl_elt *elt = *p;
189 tree_evaluator::visit_global_command (tree_global_command& cmd)
192 do_breakpoint (cmd.is_breakpoint (), cmd.line (), cmd.column ());
194 do_decl_init_list (do_global_init, cmd.initializer_list ());
198 tree_evaluator::visit_static_command (tree_static_command& cmd)
201 do_breakpoint (cmd.is_breakpoint (), cmd.line (), cmd.column ());
203 do_decl_init_list (do_static_init, cmd.initializer_list ());
207 tree_evaluator::visit_decl_elt (tree_decl_elt&)
214 tree_decl_elt::eval (void)
220 octave_lvalue ult = id->lvalue ();
222 octave_value init_val = expr->rvalue1 ();
226 ult.assign (octave_value::op_asn_eq, init_val);
237 tree_evaluator::visit_decl_init_list (tree_decl_init_list&)
242 // Decide if it's time to quit a for or while loop.
248 // Maybe handle `continue N' someday...
250 if (tree_continue_command::continuing)
251 tree_continue_command::continuing--;
253 bool quit = (error_state
254 || tree_return_command::returning
255 || tree_break_command::breaking
256 || tree_continue_command::continuing);
258 if (tree_break_command::breaking)
259 tree_break_command::breaking--;
264 #define DO_SIMPLE_FOR_LOOP_ONCE(VAL) \
267 ult.assign (octave_value::op_asn_eq, VAL); \
269 if (! error_state && loop_body) \
270 loop_body->accept (*this); \
272 quit = quit_loop_now (); \
277 tree_evaluator::visit_simple_for_command (tree_simple_for_command& cmd)
283 do_breakpoint (cmd.is_breakpoint (), cmd.line (), cmd.column ());
285 unwind_protect::begin_frame ("tree_evaluator::visit_simple_for_command");
287 unwind_protect_bool (in_loop_command);
289 in_loop_command = true;
291 tree_expression *expr = cmd.control_expr ();
293 octave_value rhs = expr->rvalue1 ();
295 if (error_state || rhs.is_undefined ())
299 tree_expression *lhs = cmd.left_hand_side ();
301 octave_lvalue ult = lhs->lvalue ();
306 tree_statement_list *loop_body = cmd.body ();
310 Range rng = rhs.range_value ();
312 octave_idx_type steps = rng.nelem ();
313 double b = rng.base ();
314 double increment = rng.inc ();
317 for (octave_idx_type i = 0; i < steps; i++)
319 // Use multiplication here rather than declaring a
320 // temporary variable outside the loop and using
322 // tmp_val += increment
324 // to avoid problems with limited precision. Also, this
325 // is consistent with the way Range::matrix_value is
328 octave_value val (b + i * increment);
330 DO_SIMPLE_FOR_LOOP_ONCE (val);
336 else if (rhs.is_scalar_type ())
340 DO_SIMPLE_FOR_LOOP_ONCE (rhs);
342 else if (rhs.is_matrix_type () || rhs.is_cell () || rhs.is_string ()
345 // A matrix or cell is reshaped to 2 dimensions and iterated by
350 dim_vector dv = rhs.dims ().redim (2);
352 octave_idx_type nrows = dv(0), steps = dv(1);
356 octave_value arg = rhs;
357 if (rhs.ndims () > 2)
358 arg = arg.reshape (dv);
360 // for row vectors, use single index to speed things up.
361 octave_value_list idx;
362 octave_idx_type iidx;
371 idx(0) = octave_value::magic_colon_t;
375 for (octave_idx_type i = 1; i <= steps; i++)
377 // do_index_op expects one-based indices.
379 octave_value val = arg.do_index_op (idx);
380 DO_SIMPLE_FOR_LOOP_ONCE (val);
389 ::error ("invalid type in for loop expression near line %d, column %d",
390 cmd.line (), cmd.column ());
395 unwind_protect::run_frame ("tree_evaluator::visit_simple_for_command");
399 tree_evaluator::visit_complex_for_command (tree_complex_for_command& cmd)
405 do_breakpoint (cmd.is_breakpoint (), cmd.line (), cmd.column ());
407 unwind_protect::begin_frame ("tree_evaluator::visit_complex_for_command");
409 unwind_protect_bool (in_loop_command);
411 in_loop_command = true;
413 tree_expression *expr = cmd.control_expr ();
415 octave_value rhs = expr->rvalue1 ();
417 if (error_state || rhs.is_undefined ())
422 // Cycle through structure elements. First element of id_list
423 // is set to value and the second is set to the name of the
424 // structure element.
426 tree_argument_list *lhs = cmd.left_hand_side ();
428 tree_argument_list::iterator p = lhs->begin ();
430 tree_expression *elt = *p++;
432 octave_lvalue val_ref = elt->lvalue ();
436 octave_lvalue key_ref = elt->lvalue ();
438 const Octave_map tmp_val (rhs.map_value ());
440 tree_statement_list *loop_body = cmd.body ();
442 for (Octave_map::const_iterator q = tmp_val.begin (); q != tmp_val.end (); q++)
444 octave_value key = tmp_val.key (q);
446 const Cell val_lst = tmp_val.contents (q);
448 octave_idx_type n = tmp_val.numel ();
450 octave_value val = (n == 1) ? val_lst(0) : octave_value (val_lst);
452 val_ref.assign (octave_value::op_asn_eq, val);
453 key_ref.assign (octave_value::op_asn_eq, key);
455 if (! error_state && loop_body)
456 loop_body->accept (*this);
458 if (quit_loop_now ())
463 error ("in statement `for [X, Y] = VAL', VAL must be a structure");
466 unwind_protect::run_frame ("tree_evaluator::visit_complex_for_command");
470 tree_evaluator::visit_octave_user_script (octave_user_script&)
476 tree_evaluator::visit_octave_user_function (octave_user_function&)
482 tree_evaluator::visit_octave_user_function_header (octave_user_function&)
488 tree_evaluator::visit_octave_user_function_trailer (octave_user_function&)
494 tree_evaluator::visit_function_def (tree_function_def& cmd)
496 octave_value fcn = cmd.function ();
498 octave_function *f = fcn.function_value ();
502 std::string nm = f->name ();
504 symbol_table::install_cmdline_function (nm, fcn);
506 // Make sure that any variable with the same name as the new
507 // function is cleared.
509 symbol_table::varref (nm) = octave_value ();
514 tree_evaluator::visit_identifier (tree_identifier&)
520 tree_evaluator::visit_if_clause (tree_if_clause&)
526 tree_evaluator::visit_if_command (tree_if_command& cmd)
528 tree_if_command_list *lst = cmd.cmd_list ();
535 tree_evaluator::visit_if_command_list (tree_if_command_list& lst)
537 for (tree_if_command_list::iterator p = lst.begin (); p != lst.end (); p++)
539 tree_if_clause *tic = *p;
541 tree_expression *expr = tic->condition ();
543 if (debug_mode && ! tic->is_else_clause ())
544 do_breakpoint (tic->is_breakpoint (), tic->line (), tic->column ());
546 if (tic->is_else_clause () || expr->is_logically_true ("if"))
550 tree_statement_list *stmt_lst = tic->commands ();
553 stmt_lst->accept (*this);
562 tree_evaluator::visit_index_expression (tree_index_expression&)
568 tree_evaluator::visit_matrix (tree_matrix&)
574 tree_evaluator::visit_cell (tree_cell&)
580 tree_evaluator::visit_multi_assignment (tree_multi_assignment&)
586 tree_evaluator::visit_no_op_command (tree_no_op_command& cmd)
588 if (debug_mode && cmd.is_end_of_fcn_or_script ())
589 do_breakpoint (cmd.is_breakpoint (), cmd.line (), cmd.column (), true);
593 tree_evaluator::visit_constant (tree_constant&)
599 tree_evaluator::visit_fcn_handle (tree_fcn_handle&)
605 tree_evaluator::visit_parameter_list (tree_parameter_list&)
611 tree_evaluator::visit_postfix_expression (tree_postfix_expression&)
617 tree_evaluator::visit_prefix_expression (tree_prefix_expression&)
623 tree_evaluator::visit_return_command (tree_return_command& cmd)
628 do_breakpoint (cmd.is_breakpoint (), cmd.line (), cmd.column ());
630 tree_return_command::returning = 1;
635 tree_evaluator::visit_return_list (tree_return_list&)
641 tree_evaluator::visit_simple_assignment (tree_simple_assignment&)
647 tree_evaluator::visit_statement (tree_statement& stmt)
649 tree_command *cmd = stmt.command ();
650 tree_expression *expr = stmt.expression ();
654 if (in_fcn_or_script_body)
656 octave_call_stack::set_statement (&stmt);
658 if (Vecho_executing_commands & ECHO_FUNCTIONS)
669 do_breakpoint (expr->is_breakpoint (), expr->line (),
672 if (in_fcn_or_script_body && Vsilent_functions)
673 expr->set_print_flag (false);
675 // FIXME -- maybe all of this should be packaged in
676 // one virtual function that returns a flag saying whether
677 // or not the expression will take care of binding ans and
678 // printing the result.
680 // FIXME -- it seems that we should just have to
681 // call expr->rvalue1 () and that should take care of
682 // everything, binding ans as necessary?
684 bool do_bind_ans = false;
686 if (expr->is_identifier ())
688 tree_identifier *id = dynamic_cast<tree_identifier *> (expr);
690 do_bind_ans = (! id->is_variable ());
693 do_bind_ans = (! expr->is_assignment_expression ());
695 octave_value tmp_result = expr->rvalue1 (0);
697 if (do_bind_ans && ! (error_state || tmp_result.is_undefined ()))
698 bind_ans (tmp_result, expr->print_result ());
700 // if (tmp_result.is_defined ())
701 // result_values(0) = tmp_result;
704 catch (octave_execution_exception)
706 gripe_library_execution_error ();
712 tree_evaluator::visit_statement_list (tree_statement_list& lst)
714 static octave_value_list empty_list;
719 tree_statement_list::iterator p = lst.begin ();
725 tree_statement *elt = *p++;
736 if (tree_break_command::breaking
737 || tree_continue_command::continuing)
740 if (tree_return_command::returning)
747 // Clear preivous values before next statement is
748 // evaluated so that we aren't holding an extra
749 // reference to a value that may be used next. For
750 // example, in code like this:
752 // X = rand (N); ## refcount for X should be 1
753 // ## after this statement
755 // X(idx) = val; ## no extra copy of X should be
756 // ## needed, but we will be faked
757 // ## out if retval is not cleared
758 // ## between statements here
760 // result_values = empty_list;
764 error ("invalid statement found in statement list!");
770 tree_evaluator::visit_switch_case (tree_switch_case&)
776 tree_evaluator::visit_switch_case_list (tree_switch_case_list&)
782 tree_evaluator::visit_switch_command (tree_switch_command& cmd)
784 tree_expression *expr = cmd.switch_value ();
788 octave_value val = expr->rvalue1 ();
790 tree_switch_case_list *lst = cmd.case_list ();
792 if (! error_state && lst)
794 for (tree_switch_case_list::iterator p = lst->begin ();
795 p != lst->end (); p++)
797 tree_switch_case *t = *p;
799 if (debug_mode && ! t->is_default_case ())
800 do_breakpoint (t->is_breakpoint (), t->line (), t->column ());
802 if (t->is_default_case () || t->label_matches (val))
807 tree_statement_list *stmt_lst = t->commands ();
810 stmt_lst->accept (*this);
818 ::error ("missing value in switch command near line %d, column %d",
819 cmd.line (), cmd.column ());
823 do_catch_code (void *ptr)
825 // Is it safe to call OCTAVE_QUIT here? We are already running
826 // something on the unwind_protect stack, but the element for this
827 // action would have already been popped from the top of the stack,
828 // so we should not be attempting to run it again.
832 // If we are interrupting immediately, or if an interrupt is in
833 // progress (octave_interrupt_state < 0), then we don't want to run
834 // the catch code (it should only run on errors, not interrupts).
836 // If octave_interrupt_state is positive, an interrupt is pending.
837 // The only way that could happen would be for the interrupt to
838 // come in after the OCTAVE_QUIT above and before the if statement
839 // below -- it's possible, but unlikely. In any case, we should
840 // probably let the catch code throw the exception because we don't
841 // want to skip that and potentially run some other code. For
842 // example, an error may have originally brought us here for some
843 // cleanup operation and we shouldn't skip that.
845 if (octave_interrupt_immediately || octave_interrupt_state < 0)
848 tree_statement_list *list = static_cast<tree_statement_list *> (ptr);
850 // Set up for letting the user print any messages from errors that
851 // occurred in the body of the try_catch statement.
853 buffer_error_messages--;
856 list->accept (*current_evaluator);
860 tree_evaluator::visit_try_catch_command (tree_try_catch_command& cmd)
862 unwind_protect::begin_frame ("tree_evaluator::visit_try_catch_command");
864 unwind_protect_int (buffer_error_messages);
865 unwind_protect_bool (Vdebug_on_error);
866 unwind_protect_bool (Vdebug_on_warning);
868 buffer_error_messages++;
869 Vdebug_on_error = false;
870 Vdebug_on_warning = false;
872 tree_statement_list *catch_code = cmd.cleanup ();
874 unwind_protect::add (do_catch_code, catch_code);
876 tree_statement_list *try_code = cmd.body ();
879 try_code->accept (*this);
881 if (catch_code && error_state)
884 unwind_protect::run_frame ("tree_evaluator::visit_try_catch_command");
890 // Unwind stack elements must be cleared or run in the reverse
891 // order in which they were added to the stack.
893 // For clearing the do_catch_code cleanup function.
894 unwind_protect::discard ();
896 // For restoring Vdebug_on_warning, Vdebug_on_error, and
897 // buffer_error_messages.
898 unwind_protect::run ();
899 unwind_protect::run ();
900 unwind_protect::run ();
902 // Also clear the frame marker.
903 unwind_protect::discard ();
907 void restore_interrupt_state (void *ptr)
909 octave_interrupt_state = *(reinterpret_cast<sig_atomic_t *> (ptr));
913 do_unwind_protect_cleanup_code (void *ptr)
915 tree_statement_list *list = static_cast<tree_statement_list *> (ptr);
917 sig_atomic_t saved_octave_interrupt_state = octave_interrupt_state;
918 unwind_protect::add (restore_interrupt_state, &saved_octave_interrupt_state);
919 octave_interrupt_state = 0;
921 // We want to run the cleanup code without error_state being set,
922 // but we need to restore its value, so that any errors encountered
923 // in the first part of the unwind_protect are not completely
926 unwind_protect_int (error_state);
929 // Similarly, if we have seen a return or break statement, allow all
930 // the cleanup code to run before returning or handling the break.
931 // We don't have to worry about continue statements because they can
932 // only occur in loops.
934 unwind_protect_int (tree_return_command::returning);
935 tree_return_command::returning = 0;
937 unwind_protect_int (tree_break_command::breaking);
938 tree_break_command::breaking = 0;
941 list->accept (*current_evaluator);
943 // The unwind_protects are popped off the stack in the reverse of
944 // the order they are pushed on.
946 // FIXME -- these statements say that if we see a break or
947 // return statement in the cleanup block, that we want to use the
948 // new value of the breaking or returning flag instead of restoring
949 // the previous value. Is that the right thing to do? I think so.
950 // Consider the case of
954 // stderr << "1: this should always be executed\n";
956 // stderr << "1: this should never be executed\n";
957 // unwind_protect_cleanup
958 // stderr << "2: this should always be executed\n";
960 // stderr << "2: this should never be executed\n";
961 // end_unwind_protect
964 // If we reset the value of the breaking flag, both the returning
965 // flag and the breaking flag will be set, and we shouldn't have
966 // both. So, use the most recent one. If there is no return or
967 // break in the cleanup block, the values should be reset to
968 // whatever they were when the cleanup block was entered.
970 if (tree_break_command::breaking || tree_return_command::returning)
972 unwind_protect::discard ();
973 unwind_protect::discard ();
977 unwind_protect::run ();
978 unwind_protect::run ();
981 // We don't want to ignore errors that occur in the cleanup code, so
982 // if an error is encountered there, leave error_state alone.
983 // Otherwise, set it back to what it was before.
986 unwind_protect::discard ();
988 unwind_protect::run ();
990 unwind_protect::run ();
994 tree_evaluator::visit_unwind_protect_command (tree_unwind_protect_command& cmd)
996 tree_statement_list *cleanup_code = cmd.cleanup ();
998 unwind_protect::add (do_unwind_protect_cleanup_code, cleanup_code);
1000 tree_statement_list *unwind_protect_code = cmd.body ();
1002 if (unwind_protect_code)
1003 unwind_protect_code->accept (*this);
1005 unwind_protect::run ();
1009 tree_evaluator::visit_while_command (tree_while_command& cmd)
1014 unwind_protect::begin_frame ("tree_evaluator::visit_while_command");
1016 unwind_protect_bool (in_loop_command);
1018 in_loop_command = true;
1020 tree_expression *expr = cmd.condition ();
1023 panic_impossible ();
1025 int l = expr->line ();
1026 int c = expr->column ();
1031 do_breakpoint (cmd.is_breakpoint (), l, c);
1033 if (expr->is_logically_true ("while"))
1035 tree_statement_list *loop_body = cmd.body ();
1039 loop_body->accept (*this);
1045 if (quit_loop_now ())
1053 unwind_protect::run_frame ("tree_evaluator::visit_while_command");
1057 tree_evaluator::visit_do_until_command (tree_do_until_command& cmd)
1062 unwind_protect::begin_frame ("tree_evaluator::visit_do_until_command");
1064 unwind_protect_bool (in_loop_command);
1066 in_loop_command = true;
1068 tree_expression *expr = cmd.condition ();
1071 panic_impossible ();
1073 int l = expr->line ();
1074 int c = expr->column ();
1078 tree_statement_list *loop_body = cmd.body ();
1082 loop_body->accept (*this);
1088 if (quit_loop_now ())
1092 do_breakpoint (cmd.is_breakpoint (), l, c);
1094 if (expr->is_logically_true ("do-until"))
1099 unwind_protect::run_frame ("tree_evaluator::visit_do_until_command");
1103 tree_evaluator::do_breakpoint (tree_statement& stmt) const
1105 do_breakpoint (stmt.is_breakpoint (), stmt.line (), stmt.column (),
1106 stmt.is_end_of_fcn_or_script ());
1110 tree_evaluator::do_breakpoint (bool is_breakpoint, int l, int c,
1111 bool is_end_of_fcn_or_script) const
1113 bool break_on_this_statement = false;
1115 // Don't decrement break flag unless we are in the same frame as we
1116 // were when we saw the "dbstep N" command.
1118 if (dbstep_flag > 1)
1120 if (octave_call_stack::current_frame () == current_frame)
1122 // Don't allow dbstep N to step past end of current frame.
1124 if (is_end_of_fcn_or_script)
1131 if (octave_debug_on_interrupt_state)
1133 break_on_this_statement = true;
1135 octave_debug_on_interrupt_state = false;
1137 current_frame = octave_call_stack::current_frame ();
1139 else if (is_breakpoint)
1141 break_on_this_statement = true;
1145 current_frame = octave_call_stack::current_frame ();
1147 else if (dbstep_flag == 1)
1149 if (octave_call_stack::current_frame () == current_frame)
1151 // We get here if we are doing a "dbstep" or a "dbstep N"
1152 // and the count has reached 1 and we are in the current
1155 break_on_this_statement = true;
1160 else if (dbstep_flag == -1)
1162 // We get here if we are doing a "dbstep in".
1164 break_on_this_statement = true;
1168 current_frame = octave_call_stack::current_frame ();
1170 else if (dbstep_flag == -2)
1172 // We get here if we are doing a "dbstep out".
1174 if (is_end_of_fcn_or_script)
1178 if (break_on_this_statement)
1180 octave_function *xfcn = octave_call_stack::current ();
1183 std::cerr << xfcn->name () << ": ";
1185 std::cerr << "line " << l << ", " << "column " << c << std::endl;
1190 // FIXME -- probably we just want to print one line, not the
1191 // entire statement, which might span many lines...
1193 // tree_print_code tpc (octave_stdout);
1194 // stmt.accept (tpc);
1200 DEFUN (silent_functions, args, nargout,
1202 @deftypefn {Built-in Function} {@var{val} =} silent_functions ()\n\
1203 @deftypefnx {Built-in Function} {@var{old_val} =} silent_functions (@var{new_val})\n\
1204 Query or set the internal variable that controls whether internal\n\
1205 output from a function is suppressed. If this option is disabled,\n\
1206 Octave will display the results produced by evaluating expressions\n\
1207 within a function body that are not terminated with a semicolon.\n\
1210 return SET_INTERNAL_VARIABLE (silent_functions);
1214 ;;; Local Variables: ***