Variables

Basics

Variables are stored in a variable table. This table can be accessed during parsing or evaluation. During parsing, the parser looks up whether variables exists. And during evaluation the variables are looked up in the table in order to obtain their values.

Two options are available in working with variables:

  1. Do not allow undeclared variables (default): In this case it is necessary to add a variable before parsing an expression with that variable in it. This is accomplished with the AddVariable(String, Object) method.
  2. Allow undeclared variables: If you allow undeclared variables, they are automatically added to the symbol table while parsing an expression. See the section below on how to accomplish this.

After an expression has been parsed, variable values can be updated using AddVariable. This allows repeated evaluation of an expression with different variable values.

The value of a variable can be queried using GetVariableValue(String) method. A further method GetVariable(String) returns an object representing the variable.

Note that the VariableTable class stores objects representing variables rather than just their values.

top

Allowing undeclared variables

To enable parsing of undeclared variables, set the AllowUndeclared property of the JepInstance to true. The default setting is false (undeclared variables are not allowed).

If you do not know what variable names may occur in the expression before parsing it, you can use jep.AllowUndeclared = true. With this option enabled, it is not necessary to add variables to the parser before parsing an expression. If a new variable is found while parsing, it is automatically added to the symbol table with null as value unless specified otherwise (see SetDefaultValue() method). See Obtaining a list of variables to read about how to access these variables.

top

Obtaining a list of variables

While parsing an expression with undeclared variables allowed, a list of all the variables and constants that have been added to the parser, can be obtained using the VarTab property. method. With the returned variable table you can then check its contents to see which variables have been added while parsing.

top

Assignment

Assignment allows the values of variables to be set by using the = operator in equations so it is possible to assign values with expressions like

x = 3,

then use the assigned values in an expression such as

y = x^2,

and y will have the value 9. To switch on assignment facilities, the boolean AllowAssignment property of the main JepInstance object should be called. Setting jep.AllowUndeclared = true is also required for the JepInstance to parse the assignment equations.

Important: It is necessary to call the Evaluate() method after parsing for the variable value to be assigned.

// standard initialisation
JepInstance j = new JepInstance();
j.AllowUndeclared = true;

// switch assignment facilities on
j.AllowAssignment = true;

// parse assignment equations
try { j.Parse("x=3"); } catch (ParseException e) { }
// evaluate it - no need to save the value returned
try { j.Evaluate(); } catch (EvaluationException e) { }

// parse a second equation
try { j.Parse("y=2"); } catch (ParseException e) { }
try { j.Evaluate(); } catch (EvaluationException e) { }

// an equation involving the above variables
try { j.Parse("x^y"); } catch (ParseException e) { }
try
{
    Object val3 = j.Evaluate();
    Console.Out.WriteLine("Value is " + val3);
}  catch (EvaluationException e) { }
Console.ReadKey(true);
top