---------- The LiveBindings technology introduced in Delphi XE2, includes a set of interfaces, classes and methods to evaluate and compile expressions. ----------
begin LScope:= BasicOperators; LCompiledExpr:= Compile('Form1.Memo2.Lines.Strings[strtoint(Form1.ComboBox1.Text)]', LScope); LResult:=LCompiledExpr.Evaluate(LScope, nil, nil).GetValue; if not LResult.IsEmpty then Form1.Memo1.Lines.add(LResult.ToString); end; procedure DoIt2; Var LScope : IScope; LCompiledExpr : ICompiledBinding; LResult : TValue; LDictionaryScope: TDictionaryScope; begin LScope:= TNestedScope.Create(BasicOperators, BasicConstants); LDictionaryScope := TDictionaryScope.Create; //add a set of constants to the Scope LDictionaryScope.Map.Add('MinsPerHour', TValueWrapper.Create(MinsPerHour)); LDictionaryScope.Map.Add('MinsPerDay', TValueWrapper.Create(MinsPerDay)); LDictionaryScope.Map.Add('MSecsPerSec', TValueWrapper.Create(MSecsPerSec)); LDictionaryScope.Map.Add('MSecsPerDay', TValueWrapper.Create(MSecsPerDay)); LDictionaryScope.Map.Add('myvariant', TValueWrapper.Create(myvariant)); //merge the scopes LScope:= TNestedScope.Create(LScope, LDictionaryScope); ; LCompiledExpr:= Compile(Form1.Memo2.Lines.Strings[strtoint(Form1.ComboBox1.Text)], LScope); LResult:=LCompiledExpr.Evaluate(LScope, nil, nil).GetValue; if not LResult.IsEmpty then Form1.Memo1.Lines.add(LResult.ToString);
end;
procedure DoIt3; Var LScope : IScope; LCompiledExpr : ICompiledBinding; LResult : TValue; LDictionaryScope: TDictionaryScope; begin LScope:= TNestedScope.Create(BasicOperators, BasicConstants); //add the registered methods LScope := TNestedScope.Create(LScope, TBindingMethodsFactory.GetMethodScope); LCompiledExpr:= Compile(Form1.Memo2.Lines.Strings[strtoint(Form1.ComboBox1.Text)], LScope); LResult:=LCompiledExpr.Evaluate(LScope, nil, nil).GetValue; if not LResult.IsEmpty then Form1.Memo1.Lines.add(LResult.ToString); end;
procedure TForm1.Button1Click(Sender: TObject); begin try DoIt2; except on E:Exception do Form1.Memo1.Lines.add(E.Classname+ ':'+ E.Message); end; //Writeln('Press Enter to exit'); //Readln; end;