I’m using the ICompiledBinding interface to evaluate simple expressions, If I use literals like (2*5)+10 works fine , but when i try to compile something like 2*Pi the code fails with the error :
EEvaluatorError:Couldn’t find Pi
This is my current code
{$APPTYPE CONSOLE}
uses
System.Rtti,
System.Bindings.EvalProtocol,
System.Bindings.EvalSys,
System.Bindings.Evaluator,
System.SysUtils;
procedure Test;
Var
RootScope : IScope;
CompiledExpr : ICompiledBinding;
R : TValue;
begin
RootScope:= BasicOperators;
//Compile('(2*5)+10', RootScope); //works
CompiledExpr:= Compile('2*Pi', RootScope);//fails
R:=CompiledExpr.Evaluate(RootScope, nil, nil).GetValue;
if not R.IsEmpty then
Writeln(R.ToString);
end;
begin
try
Test;
except
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.
So how I can evaluate an expression which contains the Pi constant using the ICompiledBinding interface?
As far i know exists two options
1) You can initializate the
IScopeinterface using theTNestedScopeclass passing theBasicOperatorsandBasicConstantsscopes.(The BasicConstants Scope define nil, true, False, and Pi.)
2) you can add the Pi Value manually to the Scope using a sentence like this.
and the code will look like this