octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

saveing/loading symbol table of annymous functions


From: John W. Eaton
Subject: saveing/loading symbol table of annymous functions
Date: Fri, 6 Apr 2007 12:11:06 -0400

On  6-Apr-2007, David Bateman wrote:

| Looking at how to read matlab dot-mat files with anonymous function
| handles in them, it seems that these files contain the local symbol
| table of the anonymous function. Octave also stores these in
| tree_anon_fcn_handle, as the octave saving/loading of anonymous function
| handles are in ov-fcn-handle.cc, we don't have access to the local
| symbol. This is true both for loading and saving the symbol table.
| 
| Can the symbol table be moved from the tree_anon_fcn_handle class to the
| octave_fcn_handle class? What else does this imply? If it is moved it
| would then by possible to save the symbol table of the function handle
| and reload it, as well as implementing the loading of function handles
| in matlab files..

I'm not sure why we were duplicating the data for a function directly
in the tree_anon_fcn_handle class, so I've checked in the following
change.  Now the tree_anon_fcn_handle object just contains an
octave_user_function object that contains the parameter list, function
body, return list, and symbol table.

The octave_fcn_handle class has an octave_value object that contains
the function.  You can extract a pointer to the user function object
and then get the symbol table from there using the new
octave_user_function::sym_tab method.  Does that give you everything
you need to write the load/save functions for anonymous function
handles?

Thanks,

jwe


src/ChangeLog:

2007-04-06  John W. Eaton  <address@hidden>

        * pt-fcn-handle.cc (tree_anon_fcn_handle::param_list, 
        tree_anon_fcn_handle::cmd_list, tree_anon_fcn_handle::ret_list,
        tree_anon_fcn_handle::sym_tab): Delete.  Remove all uses.
        (tree_anon_fcn_handle::fcn): New data member.
        (tree_anon_fcn_handle::tree_anon_fcn_handle): Initialize it.
        (tree_anon_fcn_handle::rvalue, tree_anon_fcn_handle::dup):
        Extract parameter list, return list, function body, and symbol
        table from fcn.
        (tree_anon_fcn_handle::parameter_list, tree_anon_fcn_handle::body):
        Forward request to fcn.

        * ov-usr-fcn.h (octave_user_function::local_sym_tab): Rename from
        sym_tab.  Change all uses.
        (octave_user_function::sym_tab): New function.


Index: src/ov-usr-fcn.cc
===================================================================
RCS file: /cvs/octave/src/ov-usr-fcn.cc,v
retrieving revision 1.73
diff -u -u -r1.73 ov-usr-fcn.cc
--- src/ov-usr-fcn.cc   17 Feb 2007 02:51:02 -0000      1.73
+++ src/ov-usr-fcn.cc   6 Apr 2007 16:03:04 -0000
@@ -73,7 +73,7 @@
    tree_statement_list *cl, symbol_table *st)
   : octave_function (std::string (), std::string ()),
     param_list (pl), ret_list (rl), cmd_list (cl),
-    sym_tab (st), lead_comm (), trail_comm (), file_name (),
+    local_sym_tab (st), lead_comm (), trail_comm (), file_name (),
     parent_name (), t_parsed (static_cast<time_t> (0)),
     t_checked (static_cast<time_t> (0)),
     system_fcn_file (false), call_depth (0), num_named_args (0),
@@ -89,7 +89,7 @@
 {
   delete param_list;
   delete ret_list;
-  delete sym_tab;
+  delete local_sym_tab;
   delete cmd_list;
   delete lead_comm;
   delete trail_comm;
@@ -277,15 +277,15 @@
 
   if (call_depth > 1)
     {
-      sym_tab->push_context ();
-      unwind_protect::add (pop_symbol_table_context, sym_tab);
+      local_sym_tab->push_context ();
+      unwind_protect::add (pop_symbol_table_context, local_sym_tab);
     }
 
   install_automatic_vars ();
 
   // Force symbols to be undefined again when this function exits.
 
-  unwind_protect::add (clear_symbol_table, sym_tab);
+  unwind_protect::add (clear_symbol_table, local_sym_tab);
 
   // Save old and set current symbol table context, for
   // eval_undefined_error().
@@ -294,7 +294,7 @@
   curr_caller_sym_tab = curr_sym_tab;
 
   unwind_protect_ptr (curr_sym_tab);
-  curr_sym_tab = sym_tab;
+  curr_sym_tab = local_sym_tab;
 
   unwind_protect_ptr (curr_caller_statement);
   curr_caller_statement = curr_statement;
@@ -381,7 +381,7 @@
 
        Cell varargout;
 
-       symbol_record *sr = sym_tab->lookup ("varargout");
+       symbol_record *sr = local_sym_tab->lookup ("varargout");
 
        if (sr && sr->is_variable ())
          {
@@ -436,8 +436,8 @@
 void
 octave_user_function::print_symtab_info (std::ostream& os) const
 {
-  if (sym_tab)
-    sym_tab->print_info (os);
+  if (local_sym_tab)
+    local_sym_tab->print_info (os);
   else
     warning ("%s: no symbol table info!", my_name.c_str ());
 }
@@ -461,14 +461,14 @@
 void
 octave_user_function::install_automatic_vars (void)
 {
-  if (sym_tab)
+  if (local_sym_tab)
     {
-      argn_sr = sym_tab->lookup ("argn", true);
-      nargin_sr = sym_tab->lookup ("__nargin__", true);
-      nargout_sr = sym_tab->lookup ("__nargout__", true);
+      argn_sr = local_sym_tab->lookup ("argn", true);
+      nargin_sr = local_sym_tab->lookup ("__nargin__", true);
+      nargout_sr = local_sym_tab->lookup ("__nargout__", true);
 
       if (takes_varargs ())
-       varargin_sr = sym_tab->lookup ("varargin", true);
+       varargin_sr = local_sym_tab->lookup ("varargin", true);
     }
 }
 
Index: src/ov-usr-fcn.h
===================================================================
RCS file: /cvs/octave/src/ov-usr-fcn.h,v
retrieving revision 1.37
diff -u -u -r1.37 ov-usr-fcn.h
--- src/ov-usr-fcn.h    17 Feb 2007 02:51:02 -0000      1.37
+++ src/ov-usr-fcn.h    6 Apr 2007 16:03:04 -0000
@@ -195,6 +195,8 @@
 
   tree_statement_list *body (void) { return cmd_list; }
 
+  symbol_table *sym_tab (void) { return local_sym_tab; }
+
   octave_comment_list *leading_comment (void) { return lead_comm; }
 
   octave_comment_list *trailing_comment (void) { return trail_comm; }
@@ -216,7 +218,7 @@
   tree_statement_list *cmd_list;
 
   // The local symbol table for this function.
-  symbol_table *sym_tab;
+  symbol_table *local_sym_tab;
 
   // The comments preceding the FUNCTION token.
   octave_comment_list *lead_comm;
Index: src/pt-fcn-handle.cc
===================================================================
RCS file: /cvs/octave/src/pt-fcn-handle.cc,v
retrieving revision 1.6
diff -u -u -r1.6 pt-fcn-handle.cc
--- src/pt-fcn-handle.cc        9 Nov 2006 18:26:56 -0000       1.6
+++ src/pt-fcn-handle.cc        6 Apr 2007 16:03:04 -0000
@@ -72,7 +72,7 @@
 }
 
 tree_expression *
-tree_fcn_handle::dup (symbol_table *sym_tab)
+tree_fcn_handle::dup (symbol_table *)
 {
   tree_fcn_handle *new_fh = new tree_fcn_handle (nm, line (), column ());
 
@@ -87,19 +87,16 @@
   tw.visit_fcn_handle (*this);
 }
 
-tree_anon_fcn_handle::~tree_anon_fcn_handle (void)
-{
-  delete param_list;
-  delete cmd_list;
-  delete ret_list;
-  delete sym_tab;
-}
-
 octave_value
 tree_anon_fcn_handle::rvalue (void)
 {
   MAYBE_DO_BREAKPOINT;
 
+  tree_parameter_list *param_list = fcn.parameter_list ();
+  tree_parameter_list *ret_list = fcn.return_list ();
+  tree_statement_list *cmd_list = fcn.body ();
+  symbol_table *sym_tab = fcn.sym_tab ();
+
   symbol_table *new_sym_tab = sym_tab ? sym_tab->dup () : 0;
 
   if (new_sym_tab)
@@ -120,9 +117,9 @@
 
   uf->mark_as_inline_function ();
 
-  octave_value fcn (uf);
+  octave_value ov_fcn (uf);
 
-  octave_value fh (new octave_fcn_handle (fcn, "@<anonymous>"));
+  octave_value fh (new octave_fcn_handle (ov_fcn, "@<anonymous>"));
 
   return fh;
 }
@@ -143,6 +140,11 @@
 tree_expression *
 tree_anon_fcn_handle::dup (symbol_table *st)
 {
+  tree_parameter_list *param_list = fcn.parameter_list ();
+  tree_parameter_list *ret_list = fcn.return_list ();
+  tree_statement_list *cmd_list = fcn.body ();
+  symbol_table *sym_tab = fcn.sym_tab ();
+
   symbol_table *new_sym_tab = sym_tab ? sym_tab->dup () : 0;
 
   if (new_sym_tab)
Index: src/pt-fcn-handle.h
===================================================================
RCS file: /cvs/octave/src/pt-fcn-handle.h,v
retrieving revision 1.5
diff -u -u -r1.5 pt-fcn-handle.h
--- src/pt-fcn-handle.h 16 Jun 2006 05:09:42 -0000      1.5
+++ src/pt-fcn-handle.h 6 Apr 2007 16:03:04 -0000
@@ -37,6 +37,7 @@
 class tree_walker;
 
 #include "ov.h"
+#include "ov-usr-fcn.h"
 
 class
 tree_fcn_handle : public tree_expression
@@ -89,16 +90,14 @@
 public:
 
   tree_anon_fcn_handle (int l = -1, int c = -1)
-    : tree_expression (l, c), param_list (0), cmd_list (0),
-      ret_list (0), sym_tab (0) { }
+    : tree_expression (l, c), fcn () { }
 
-  tree_anon_fcn_handle (tree_parameter_list *p, tree_parameter_list *r,
+  tree_anon_fcn_handle (tree_parameter_list *pl, tree_parameter_list *rl,
                        tree_statement_list *cl, symbol_table *st,
                        int l = -1, int c = -1)
-    : tree_expression (l, c), param_list (p), cmd_list (cl),
-      ret_list (r), sym_tab (st) { }
+    : tree_expression (l, c), fcn (pl, rl, cl, st) { }
 
-  ~tree_anon_fcn_handle (void);
+  ~tree_anon_fcn_handle (void) { }
 
   bool has_magic_end (void) const { return false; }
 
@@ -108,9 +107,9 @@
 
   octave_value_list rvalue (int nargout);
 
-  tree_parameter_list *parameter_list (void) { return param_list; }
+  tree_parameter_list *parameter_list (void) { return fcn.parameter_list (); }
 
-  tree_statement_list *body (void) { return cmd_list; }
+  tree_statement_list *body (void) { return fcn.body (); }
 
   tree_expression *dup (symbol_table *sym_tab);
 
@@ -118,17 +117,8 @@
 
 private:
 
-  // The parameter list.
-  tree_parameter_list *param_list;
-
-  // The statement that makes up the body of the function.
-  tree_statement_list *cmd_list;
-
-  // The list of return values.
-  tree_parameter_list *ret_list;
-
-  // The symbol table.
-  symbol_table *sym_tab;
+  // The function.
+  octave_user_function fcn;
 
   // No copying!
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]