Expression Class
This class allows you to execute a given code fragment at run-time. There can be up to 4 arguments of any type passed to the Expression. The setVariable() function sets values of such arguments. For example:
Expression e0 = new Expression("133.0 * 133.0");
if(e0.isCompiled()) {
log.message("%s\n",typeinfo(e0.run()));
}
Expression e1 = new Expression("
{
int a,b,c,d;
return a + b + c + d;
}");
e1.setVariable("a",1);
e1.setVariable("b",2);
e1.setVariable("c",3);
e1.setVariable("d",4);
if(e1.isCompiled()) {
log.message("%d\n",typeinfo(e1.run()));
}
Expression e2 = new Expression("
{
string name;
File file = new File(name,\"rb\");
int size = file.getSize();
file.close();
delete file;
return size;
}
");
e2.setVariable("name","test.cpp");
if(e2.isCompiled()) {
log.message("%s\n",typeinfo(e2.run()));
}
double: 17689
int: 10
int: 1302
Expression Class
Members
int isCompiled()
Returns a value indicating if the given expression has been compiled.Return value
1 if the expression has been compiled; otherwise, 0.int getFunction(string name, int num_args)
Returns the ID of the function from the expression namespace. It can be used to call a function by its ID instead of the name (speeds up the function call; it is almost as fast the direct call).Arguments
- string name - Name of the function.
- int num_args - The number of function arguments.
Return value
Function ID.int isFunction(string name, int num_args)
Checks if a given user-defined function exists in the expression namespace.Arguments
- string name - Full name of the target function.
- int num_args - Number of arguments of the target function.
Return value
1 if the function exists; otherwise, 0.void setName(string name)
Sets a namespace name for the expression that can be used when calling expression methods.Arguments
- string name - Namespace name.
string getName()
Get a name used as a namespace name when calling expression methods.Return value
Namespace name.void setVariable(string name, variable value)
Set the value of the variable from the expression namespace by its name.Arguments
- string name - Variable name.
- variable value - Variable value to set.
variable getVariable(string name)
Returns ID of the variable from the expression namespace. It can be used to pass a variable by its ID instead of the name. It speeds up passing of the varible and can be used, for example, on mobile devices when the performance is crucial.Arguments
- string name - Variable name.
Return value
Variable, if it exists; otherwise, 0.int isVariable(string name)
Checks if a given user-defined variable exists in the expression namespace.Arguments
- string name - Name of the target variable.
Return value
1 if the variable exists; otherwise, 0.Expression(string src, int scope = 0 = 0)
Runs the expression pointed to.Arguments
- string src - Source buffer pointer.
- int scope = 0 - 1 to treat the expression namespace as the global; otherwise, 0 (by default).
variable run()
Runs the given expression.Return value
Argument value.Last update: 2018-04-26
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)