changeset 4342:813effe14ee1

[project @ 2003-02-20 08:35:55 by jwe]
author jwe
date Thu, 20 Feb 2003 08:35:55 +0000
parents de9b588bf002
children db5e0814277a
files src/ChangeLog src/Makefile.in src/lex.l src/ov.cc src/ov.h src/parse.h src/parse.y src/pt-all.h src/pt-bp.cc src/pt-bp.h src/pt-check.cc src/pt-check.h src/pt-pr-code.cc src/pt-pr-code.h src/pt-walk.h src/variables.cc src/variables.h
diffstat 17 files changed, 223 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/ChangeLog	Thu Feb 20 08:35:55 2003 +0000
@@ -1,5 +1,29 @@
+2003-02-20  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* ov.cc (octave_value (const octave_fcn_handle&)): New constructor.
+	* parse.y (get_feval_args): New function.
+	(feval (octave_function *, const octave_value_list&, int)): Likewise.
+	(feval (const octave_value_list&, int)): Allow the first arg to be
+	a function handle as well as a string.
+	* variables.cc (load_function): New function.
+	* pt-walk.h, pt-pr-code.h, pt-pr-code.cc, pt-check.h, pt-check.cc,
+	pt-bp.h, pt-bp.cc: Handle new tree_fcn_handle class.
+	* pt-all.h: Include pt-fcn-handle.h.
+	* pt-fcn-handle.h, pt-fcn-handle.cc, ov-fcn-handle.h,
+	ov-fcn-handle.cc: New files.
+
 2003-02-19  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
+	* parse.y (FCN_HANDLE): New token type.
+	(fcn_handle): New non-terminal.
+	(primary_expr): Also accept fcn_handle.
+	(tree_fcn_handle_type): New %type.
+	(union): New field, tree_fcn_handle_type.
+	(make_fcn_handle): New function.
+
+	* lex.l (@{IDENT}): Recognize function handle syntax.
+	(@): Don't recognize "@" as a single token.
+
 	* load-save.cc (struct hdf5_callback_data): Provide constructor.
 	(hdf5_callback_data::name, hdf5_callback_data::doc): Now
 	std::string instead of char*.  Change all uses.
--- a/src/Makefile.in	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/Makefile.in	Thu Feb 20 08:35:55 2003 +0000
@@ -71,11 +71,11 @@
 	ov-colon.h ov-base.h ov-base-mat.h ov-base-scalar.h \
 	ov-str-mat.h ov-bool-mat.h ov-bool.h ov-file.h ov-cell.h ov.h \
 	ov-fcn.h ov-builtin.h ov-dld-fcn.h ov-mapper.h ov-usr-fcn.h \
-	ov-base-nd-array.h ov-re-nd-array.h ov-typeinfo.h
+	ov-base-nd-array.h ov-re-nd-array.h ov-fcn-handle.h ov-typeinfo.h
 
 PT_INCLUDES := pt.h pt-all.h pt-arg-list.h pt-assign.h pt-binop.h \
         pt-bp.h	pt-cell.h pt-check.h pt-cmd.h pt-colon.h pt-const.h \
-	pt-decl.h pt-except.h pt-exp.h pt-id.h pt-idx.h \
+	pt-decl.h pt-except.h pt-exp.h pt-fcn-handle.h pt-id.h pt-idx.h \
 	pt-jump.h pt-loop.h pt-mat.h pt-misc.h pt-plot.h \
 	pt-pr-code.h pt-select.h pt-stmt.h pt-unop.h pt-walk.h \
 
@@ -111,13 +111,13 @@
 	ov-colon.cc ov-bool-mat.cc ov-bool.cc ov-file.cc ov-cell.cc \
 	ov.cc ov-fcn.cc ov-builtin.cc ov-dld-fcn.cc ov-mapper.cc \
 	ov-usr-fcn.cc ov-base-nd-array.cc ov-re-nd-array.cc \
-	ov-typeinfo.cc
+	ov-fcn-handle.cc ov-typeinfo.cc
 
-PT_SRC := pt.cc pt-arg-list.cc pt-assign.cc pt-bp.cc pt-binop.cc pt-cell.cc \
-	pt-check.cc pt-cmd.cc pt-colon.cc pt-const.cc pt-decl.cc \
-	pt-except.cc pt-exp.cc pt-id.cc pt-idx.cc pt-jump.cc \
-	pt-loop.cc pt-mat.cc pt-misc.cc pt-plot.cc pt-pr-code.cc \
-	pt-select.cc pt-stmt.cc pt-unop.cc
+PT_SRC := pt.cc pt-arg-list.cc pt-assign.cc pt-bp.cc pt-binop.cc \
+	pt-cell.cc pt-check.cc pt-cmd.cc pt-colon.cc pt-const.cc \
+	pt-decl.cc pt-except.cc pt-exp.cc pt-fcn-handle.cc pt-id.cc \
+	pt-idx.cc pt-jump.cc pt-loop.cc pt-mat.cc pt-misc.cc \
+	pt-plot.cc pt-pr-code.cc pt-select.cc pt-stmt.cc pt-unop.cc
 
 DIST_SRC := Cell.cc c-file-ptr-stream.cc comment-list.cc \
 	cutils.c data.cc debug.cc defaults.cc defun.cc dirfns.cc \
--- a/src/lex.l	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/lex.l	Thu Feb 20 08:35:55 2003 +0000
@@ -555,6 +555,14 @@
   }
 
 %{
+// Function handles.
+%}
+
+@{IDENT} {
+    TOK_PUSH_AND_RETURN (&yytext[1], FCN_HANDLE);
+  }
+
+%{
 // A new line character.  New line characters inside matrix constants
 // are handled by the <MATRIX_START> start state code above.  If closest
 // nesting is inside parentheses, don't return a row separator.
@@ -746,14 +754,6 @@
     TOK_RETURN ('.');
   }
 
-"@" {
-    current_input_column++;
-    lexer_flags.cant_be_identifier = false;
-    lexer_flags.quote_is_transpose = false;
-    lexer_flags.convert_spaces_to_comma = false;
-    return '@';
-  }
-
 "+="	{ XBIN_OP_RETURN (ADD_EQ, false); }
 "-="	{ XBIN_OP_RETURN (SUB_EQ, false); }
 "*="	{ XBIN_OP_RETURN (MUL_EQ, false); }
--- a/src/ov.cc	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/ov.cc	Thu Feb 20 08:35:55 2003 +0000
@@ -54,6 +54,7 @@
 #include "ov-builtin.h"
 #include "ov-mapper.h"
 #include "ov-usr-fcn.h"
+#include "ov-fcn-handle.h"
 #include "ov-typeinfo.h"
 
 #include "defun.h"
@@ -549,6 +550,12 @@
   rep->count = 1;
 }
 
+octave_value::octave_value (const octave_fcn_handle& fh)
+  : rep (new octave_fcn_handle (fh))
+{
+  rep->count = 1;
+}
+
 octave_value::octave_value (const octave_value_list& l, bool is_cs_list)
   : rep (0)
 {
--- a/src/ov.h	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/ov.h	Thu Feb 20 08:35:55 2003 +0000
@@ -44,6 +44,7 @@
 class Octave_map;
 class octave_stream;
 class octave_function;
+class octave_fcn_handle;
 class octave_value_list;
 class octave_lvalue;
 
@@ -188,6 +189,7 @@
   octave_value (const Octave_map& m);
   octave_value (const octave_stream& s, int n);
   octave_value (octave_function *f);
+  octave_value (const octave_fcn_handle& fh);
   octave_value (const octave_value_list& m, bool is_cs_list = false);
   octave_value (octave_value::magic_colon);
   octave_value (octave_value::all_va_args);
--- a/src/parse.h	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/parse.h	Thu Feb 20 08:35:55 2003 +0000
@@ -34,6 +34,7 @@
 class tree;
 class tree_matrix;
 class tree_identifier;
+class octaev_function;
 class symbol_record;
 class symbol_table;
 
@@ -103,6 +104,11 @@
        int nargout = 0);
 
 extern octave_value_list
+feval (octave_function *fcn,
+       const octave_value_list& args = octave_value_list (),
+       int nargout = 0);
+
+extern octave_value_list
 feval (const octave_value_list& args, int nargout = 0);
 
 extern octave_value_list
--- a/src/parse.y	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/parse.y	Thu Feb 20 08:35:55 2003 +0000
@@ -182,6 +182,10 @@
 static tree_constant *
 make_constant (int op, token *tok_val);
 
+// Build a function handle.
+static tree_fcn_handle *
+make_fcn_handle (token *tok_val);
+
 // Build a binary expression.
 static tree_expression *
 make_binary_op (int op, tree_expression *op1, token *tok_val,
@@ -362,6 +366,7 @@
   tree_cell *tree_cell_type;
   tree_expression *tree_expression_type;
   tree_constant *tree_constant_type;
+  tree_fcn_handle *tree_fcn_handle_type;
   tree_identifier *tree_identifier_type;
   tree_index_expression *tree_index_expression_type;
   tree_colon_expression *tree_colon_expression_type;
@@ -414,6 +419,7 @@
 %token <tok_val> UNWIND CLEANUP
 %token <tok_val> TRY CATCH
 %token <tok_val> GLOBAL STATIC
+%token <tok_val> FCN_HANDLE
 
 // Other tokens.
 %token END_OF_INPUT LEXICAL_ERROR
@@ -425,6 +431,7 @@
 %type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep
 %type <tree_type> input
 %type <tree_constant_type> constant magic_colon
+%type <tree_fcn_handle_type> fcn_handle
 %type <tree_matrix_type> matrix_rows matrix_rows1
 %type <tree_cell_type> cell_rows cell_rows1
 %type <tree_expression_type> title matrix cell
@@ -663,10 +670,16 @@
 		  { $$ = $1; }
 		;
 
+fcn_handle	: FCN_HANDLE
+		  { $$ = make_fcn_handle ($1); }
+		;
+
 primary_expr	: identifier
 		  { $$ = $1; }
 		| constant
 		  { $$ = $1; }
+		| fcn_handle
+		  { $$ = $1; }
 		| matrix
 		  { $$ = $1; }
 		| cell
@@ -1912,6 +1925,19 @@
   return retval;
 }
 
+// Make a function handle.
+
+static tree_fcn_handle *
+make_fcn_handle (token *tok_val)
+{
+  int l = tok_val->line ();
+  int c = tok_val->column ();
+
+  tree_fcn_handle *retval = new tree_fcn_handle (tok_val->text (), l, c);
+
+  return retval;
+}
+
 // Build a binary expression.
 
 static tree_expression *
@@ -3478,6 +3504,50 @@
   return retval;
 }
 
+octave_value_list
+feval (octave_function *fcn, const octave_value_list& args, int nargout)
+{
+  octave_value_list retval;
+
+  if (fcn)
+    retval = fcn->do_multi_index_op (nargout, args);
+
+  return retval;
+}
+
+static octave_value_list
+get_feval_args (const octave_value_list& args)
+{
+  int tmp_nargin = args.length () - 1;
+
+  octave_value_list retval (tmp_nargin, octave_value ());
+
+  for (int i = 0; i < tmp_nargin; i++)
+    retval(i) = args(i+1);
+
+  string_vector arg_names = args.name_tags ();
+
+  if (! arg_names.empty ())
+    {
+      // tmp_nargin and arg_names.length () - 1 may differ if
+      // we are passed all_va_args.
+
+      int n = arg_names.length () - 1;
+
+      int len = n > tmp_nargin ? tmp_nargin : n;
+
+      string_vector tmp_arg_names (len);
+
+      for (int i = 0; i < len; i++)
+	tmp_arg_names(i) = arg_names(i+1);
+
+      retval.stash_name_tags (tmp_arg_names);
+    }
+
+  return retval;
+}
+
+
 // Evaluate an Octave function (built-in or interpreted) and return
 // the list of result values.  The first element of ARGS should be a
 // string containing the name of the function to call, then the rest
@@ -3489,39 +3559,33 @@
 {
   octave_value_list retval;
 
-  if (args.length () > 0)
+  int nargin = args.length ();
+
+  if (nargin > 0)
     {
-      std::string name = args(0).string_value ();
-
-      if (! error_state)
-	{
-	  int tmp_nargin = args.length () - 1;
-
-	  octave_value_list tmp_args (tmp_nargin, octave_value ());
-
-	  for (int i = 0; i < tmp_nargin; i++)
-	    tmp_args(i) = args(i+1);
-
-	  string_vector arg_names = args.name_tags ();
-
-	  if (! arg_names.empty ())
+      octave_value f_arg = args(0);
+
+      if (f_arg.is_string ())
+        {
+	  std::string name = f_arg.string_value ();
+
+	  if (! error_state)
 	    {
-	      // tmp_nargin and arg_names.length () - 1 may differ if
-	      // we are passed all_va_args.
-
-	      int n = arg_names.length () - 1;
-
-	      int len = n > tmp_nargin ? tmp_nargin : n;
-
-	      string_vector tmp_arg_names (len);
-
-	      for (int i = 0; i < len; i++)
-		tmp_arg_names(i) = arg_names(i+1);
-
-	      tmp_args.stash_name_tags (tmp_arg_names);
+	      octave_value_list tmp_args = get_feval_args (args);
+
+	      retval = feval (name, tmp_args, nargout);
 	    }
-
-	  retval = feval (name, tmp_args, nargout);
+	}
+      else
+	{
+	  octave_function *fcn = f_arg.function_value ();
+
+	  if (fcn)
+	    {
+	      octave_value_list tmp_args = get_feval_args (args);
+
+	      retval = feval (fcn, tmp_args, nargout);
+	    }
 	}
     }
 
--- a/src/pt-all.h	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/pt-all.h	Thu Feb 20 08:35:55 2003 +0000
@@ -35,6 +35,7 @@
 #include "pt-decl.h"
 #include "pt-except.h"
 #include "pt-exp.h"
+#include "pt-fcn-handle.h"
 #include "pt-id.h"
 #include "pt-idx.h"
 #include "pt-jump.h"
--- a/src/pt-bp.cc	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/pt-bp.cc	Thu Feb 20 08:35:55 2003 +0000
@@ -442,6 +442,16 @@
 }
 
 void 
+tree_breakpoint::visit_fcn_handle (tree_fcn_handle& fh)
+{
+  if (found)
+    return;
+
+  if (fh.line () >= line)
+    take_action (fh);
+}
+
+void 
 tree_breakpoint::visit_parameter_list (tree_parameter_list& lst)
 {
   if (found)
--- a/src/pt-bp.h	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/pt-bp.h	Thu Feb 20 08:35:55 2003 +0000
@@ -99,6 +99,8 @@
 
   void visit_constant (tree_constant&);
 
+  void visit_fcn_handle (tree_fcn_handle&);
+
   void visit_parameter_list (tree_parameter_list&);
 
   void visit_plot_command (tree_plot_command&);
--- a/src/pt-check.cc	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/pt-check.cc	Thu Feb 20 08:35:55 2003 +0000
@@ -314,6 +314,11 @@
 }
 
 void
+tree_checker::visit_fcn_handle (tree_fcn_handle& /* fh */)
+{
+}
+
+void
 tree_checker::visit_parameter_list (tree_parameter_list& lst)
 {
   tree_parameter_list::iterator p = lst.begin ();
--- a/src/pt-check.h	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/pt-check.h	Thu Feb 20 08:35:55 2003 +0000
@@ -83,6 +83,8 @@
 
   void visit_constant (tree_constant&);
 
+  void visit_fcn_handle (tree_fcn_handle&);
+
   void visit_parameter_list (tree_parameter_list&);
 
   void visit_plot_command (tree_plot_command&);
--- a/src/pt-pr-code.cc	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/pt-pr-code.cc	Thu Feb 20 08:35:55 2003 +0000
@@ -642,6 +642,18 @@
 }
 
 void
+tree_print_code::visit_fcn_handle (tree_fcn_handle& fh)
+{
+  indent ();
+
+  print_parens (fh, "(");
+
+  fh.print_raw (os, true, print_original_text);
+
+  print_parens (fh, ")");
+}
+
+void
 tree_print_code::visit_parameter_list (tree_parameter_list& lst)
 {
   tree_parameter_list::iterator p = lst.begin ();
--- a/src/pt-pr-code.h	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/pt-pr-code.h	Thu Feb 20 08:35:55 2003 +0000
@@ -95,6 +95,8 @@
 
   void visit_constant (tree_constant&);
 
+  void visit_fcn_handle (tree_fcn_handle&);
+
   void visit_parameter_list (tree_parameter_list&);
 
   void visit_plot_command (tree_plot_command&);
--- a/src/pt-walk.h	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/pt-walk.h	Thu Feb 20 08:35:55 2003 +0000
@@ -47,6 +47,7 @@
 class tree_multi_assignment;
 class tree_no_op_command;
 class tree_constant;
+class tree_fcn_handle;
 class tree_parameter_list;
 class tree_plot_command;
 class plot_limits;
@@ -146,6 +147,9 @@
   visit_constant (tree_constant&) = 0;
 
   virtual void
+  visit_fcn_handle (tree_fcn_handle&) = 0;
+
+  virtual void
   visit_parameter_list (tree_parameter_list&) = 0;
 
   virtual void
--- a/src/variables.cc	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/variables.cc	Thu Feb 20 08:35:55 2003 +0000
@@ -790,6 +790,39 @@
   return sym_rec;
 }
 
+octave_function *
+lookup_function (const std::string& nm)
+{
+  octave_function *retval = 0;
+
+  symbol_record *sr = 0;
+
+  if (curr_parent_function)
+    {
+      std::string parent = curr_parent_function->function_name ();
+
+      sr = fbi_sym_tab->lookup (parent + ":" + nm);
+    }
+
+  if (! sr || ! sr->is_function ())
+    {
+      sr = fbi_sym_tab->lookup (nm, true);
+
+      if (sr && ! sr->is_function ())
+	load_fcn_from_file (sr, false);
+    }
+
+  if (sr)
+    {
+      octave_value v = sr->def ();
+
+      if (v.is_function ())
+	retval = v.function_value ();
+    }
+
+  return retval;
+}
+
 octave_value
 get_global_value (const std::string& nm)
 {
--- a/src/variables.h	Thu Feb 20 04:49:55 2003 +0000
+++ b/src/variables.h	Thu Feb 20 08:35:55 2003 +0000
@@ -80,6 +80,9 @@
 extern symbol_record *
 lookup_by_name (const std::string& nm, bool exec_script = true);
 
+extern octave_function *
+lookup_function (const std::string& nm);
+
 extern octave_value get_global_value (const std::string& nm);
 
 extern void set_global_value (const std::string& nm, const octave_value& val);