octave-maintainers
[Top][All Lists]
Advanced

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

namespaces, etc.


From: John W. Eaton
Subject: namespaces, etc.
Date: Fri, 01 Feb 2008 03:17:20 -0500

On  1-Feb-2008, John W. Eaton wrote:

| OTOH, I think it would be fairly easy to provide functions like
| 
|   create_symbol (NAME)  ## place NAME in current symbol table scope
| 
|   symbol_value (NAME)   ## value of symbol in current scope (could be
|                         ## variable, function, private function, class
|                         ## method, etc.)
| 
|   variable_value (NAME)   ## like symbol_value, but only look at the
|                           ## variable slot corresponding to the symbol
|                           ## NAME in the current scope
| 
|   set_variable (NAME, VALUE)  ## set the variable slot corresponding
|                               ## to the symbol NAME in the current
|                               ## scope to VALUE
| 
| and so on (there might be other useful get/set functions in addition
| to these).

To expand on this just a bit, here are set_variable and variable_value
(for CVS Octave only because the symbol table code was completely
rewritten to support classes, so it is not at all compatible with
3.0.x or earlier versions of Octave):

DEFUN (set_variable, args, , "set_variable (NAME, VALUE)")
{
  octave_value retval;

  if (args.length () == 2)
    {
      std::string name = args(0).string_value ();

      if (! error_state)
        symbol_table::varref (name) = args(1);
      else
        error ("set_variable: expecting variable name as first argument");
    }
  else
    print_usage ();

  return retval;
}

DEFUN (variable_value, args, , "VALUE = variable_value (NAME)")
{
  octave_value retval;

  if (args.length () == 1)
    {
      std::string name = args(0).string_value ();

      if (! error_state)
        {
          retval = symbol_table::varval (name);

          if (retval.is_undefined ())
            error ("variable_value: `%s' is not a variable in the current 
scope",
                   name.c_str ());
        }
      else
        error ("variable_value: expecting variable name as first argument");
    }
  else
    print_usage ();

  return retval;
}


With these functions, I see:

  octave:1> varname = "foo"
  varname = foo
  octave:2> foo
  error: `foo' undefined near line 2 column 1
  octave:2> set_variable (varname, 42)
  octave:3> variable_value (varname)
  ans =  42
  octave:4> foo
  foo =  42
  octave:5> clear foo
  octave:6> variable_value (varname)
  error: variable_value: `foo' is not a variable in the current scope
  octave:6> foo
  error: `foo' undefined near line 6 column 1

jwe


reply via email to

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