changeset 2620:5c773d4745d6

[project @ 1997-01-23 16:26:25 by jwe]
author jwe
date Thu, 23 Jan 1997 16:26:26 +0000
parents 7e641ec64694
children 337a09dd1c06
files src/ChangeLog src/error.cc src/parse.y src/pt-cmd.cc src/pt-cmd.h src/pt-pr-code.cc src/pt-pr-code.h src/pt-walk.h src/toplev.cc
diffstat 9 files changed, 140 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,21 @@
+Thu Jan 23 10:00:00 1997  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* pt-pr-code.h, pt-pr-code.cc (tree_print_code::visit_no_op_command):
+	New function. 
+	* pt-cmd.h, pt-cmd.cc (tree_no_op_command): New class.
+	* parse.y (make_break_command, make_continue_command,
+	make_return_command): Where they don't really make sense, turn
+	these commands into no-ops.  Accept return and break if reading a
+	script file.
+	* toplev.cc (parse_and_execute): Handle return and break in script
+	files.  Quit executing commands if an error occurs when reading a
+	script file.  Set global_command to 0 after deleting it.
+	(main_loop): If not interactive or forced_interactive, handle
+	break and return, and quit executing commands if an error occurs.
+	Set global_command to 0 after deleting it.
+	* error.cc (Ferror): Doc fix.
+	* pt-walk.h (tree_walker): Add declaration for visit_no_op_command.
+
 Wed Jan 22 20:54:12 1997  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* input.cc (gnu_readline): If not using readline, flush
--- a/src/error.cc
+++ b/src/error.cc
@@ -252,8 +252,8 @@
 This should eventually take us up to the top level, possibly\n\
 printing traceback messages as we go.\n\
 \n\
-If MESSAGE ends in a newline character, traceback messages are not\n\
-printed.\n\
+If the resulting error message ends in a newline character, traceback
+messages are not printed.\n\
 \n\
 See also: printf") 
 {
--- a/src/parse.y
+++ b/src/parse.y
@@ -1869,15 +1869,13 @@
 {
   tree_command *retval = 0;
 
-  if (! (lexer_flags.looping || lexer_flags.defining_func))
-    yyerror ("break: only meaningful within a loop or function body");
+  int l = break_tok->line ();
+  int c = break_tok->column ();
+
+  if (lexer_flags.looping || lexer_flags.defining_func || reading_script_file)
+    retval = new tree_break_command (l, c);
   else
-    {
-      int l = break_tok->line ();
-      int c = break_tok->column ();
-
-      retval = new tree_break_command (l, c);
-    }
+    retval = new tree_no_op_command ("break", l, c);
 
   return retval;
 }
@@ -1889,15 +1887,13 @@
 {
   tree_command *retval = 0;
 
-  if (! lexer_flags.looping)
-    yyerror ("continue: only meaningful within a `for' or `while' loop");
+  int l = continue_tok->line ();
+  int c = continue_tok->column ();
+
+  if (lexer_flags.looping)
+    retval = new tree_continue_command (l, c);
   else
-    {
-      int l = continue_tok->line ();
-      int c = continue_tok->column ();
-
-      retval = new tree_continue_command (l, c);
-    }
+    retval = new tree_no_op_command ("continue", l, c);
 
   return retval;
 }
@@ -1909,15 +1905,13 @@
 {
   tree_command *retval = 0;
 
-  if (! lexer_flags.defining_func)
-    yyerror ("return: only meaningful within a function");
+  int l = return_tok->line ();
+  int c = return_tok->column ();
+
+  if (lexer_flags.defining_func || reading_script_file)
+    retval = new tree_return_command (l, c);
   else
-    {
-      int l = return_tok->line ();
-      int c = return_tok->column ();
-
-      retval = new tree_return_command (l, c);
-    }
+    retval = new tree_no_op_command ("return", l, c);
 
   return retval;
 }
--- a/src/pt-cmd.cc
+++ b/src/pt-cmd.cc
@@ -686,6 +686,14 @@
   tw.visit_unwind_protect_command (*this);
 }
 
+// No-op.
+
+void
+tree_no_op_command::accept (tree_walker& tw)
+{
+  tw.visit_no_op_command (*this);
+}
+
 // Break.
 
 void
--- a/src/pt-cmd.h
+++ b/src/pt-cmd.h
@@ -48,12 +48,15 @@
 class tree_if_command;
 class tree_try_catch_command;
 class tree_unwind_protect_command;
+class tree_no_op_command;
 class tree_break_command;
 class tree_continue_command;
 class tree_return_command;
 
 class tree_walker;
 
+#include <string>
+
 #include "pt-base.h"
 
 // A base class for commands.
@@ -290,6 +293,29 @@
   tree_statement_list *catch_code;
 };
 
+// No-op.
+
+class
+tree_no_op_command : public tree_command
+{
+public:
+
+  tree_no_op_command (const string& cmd = "no_op", int l = -1, int c = -1)
+    : tree_command (l, c), orig_cmd (cmd) { }
+
+  ~tree_no_op_command (void) { }
+
+  void eval (void) { }
+
+  void accept (tree_walker& tw);
+
+  string original_command (void) { return orig_cmd; }
+
+private:
+
+  string orig_cmd;
+};
+
 // Break.
 
 class
--- a/src/pt-pr-code.cc
+++ b/src/pt-pr-code.cc
@@ -553,6 +553,14 @@
 }
 
 void
+tree_print_code::visit_no_op_command (tree_no_op_command& cmd)
+{
+  indent ();
+
+  os << cmd.original_command ();
+}
+
+void
 tree_print_code::visit_oct_obj (tree_oct_obj&)
 {
   ::error ("visit_oct_obj: internal error");
--- a/src/pt-pr-code.h
+++ b/src/pt-pr-code.h
@@ -88,6 +88,8 @@
 
   void visit_multi_assignment_expression (tree_multi_assignment_expression&);
 
+  void visit_no_op_command (tree_no_op_command&);
+
   void visit_oct_obj (tree_oct_obj&);
 
   void visit_constant (tree_constant&);
--- a/src/pt-walk.h
+++ b/src/pt-walk.h
@@ -99,6 +99,9 @@
   visit_multi_assignment_expression (tree_multi_assignment_expression&) = 0;
 
   virtual void
+  visit_no_op_command (tree_no_op_command&) = 0;
+
+  virtual void
   visit_oct_obj (tree_oct_obj&) = 0;
 
   virtual void
--- a/src/toplev.cc
+++ b/src/toplev.cc
@@ -91,6 +91,12 @@
 // User's home directory.
 string Vhome_directory;
 
+// Nonzero means we're breaking out of a loop or function body.
+extern int breaking;
+
+// Nonzero means we're returning from a function.
+extern int returning;
+
 // Nonzero means we are using readline.
 // (--no-line-editing)
 #if defined (USE_READLINE)
@@ -160,7 +166,29 @@
       if (retval == 0 && global_command)
 	{
 	  global_command->eval (print);
+
 	  delete global_command;
+
+	  global_command = 0;
+
+	  bool quit = (returning || breaking);
+
+	  if (returning)
+	    returning = 0;
+
+	  if (breaking)
+	    breaking--;
+
+	  if (error_state)
+	    {
+	      error ("near line %d of file `%s'", input_line_number,
+		     curr_fcn_file_full_name.c_str ());
+
+	      break;
+	    }
+
+	  if (quit)
+	    break;
 	}
     }
   while (retval == 0);
@@ -254,10 +282,34 @@
 
 	  delete global_command;
 
-	  if (octave_completion_matches_called)
-	    octave_completion_matches_called = false;	    
+	  global_command = 0;
+
+	  if (! (interactive || forced_interactive))
+	    {
+	      bool quit = (returning || breaking);
+
+	      if (returning)
+		returning = 0;
+
+	      if (breaking)
+		breaking--;
+
+	      if (quit)
+		break;
+	    }
+
+	  if (error_state)
+	    {
+	      if (! (interactive || forced_interactive))
+		break;
+	    }
 	  else
-	    current_command_number++;
+	    {
+	      if (octave_completion_matches_called)
+		octave_completion_matches_called = false;	    
+	      else
+		current_command_number++;
+	    }
 	}
     }
   while (retval == 0);