糖炒栗子

cheap to talk,show muscle would be better...

导航

使用innerfuse脚本

使用innerfuse脚本语言
innerfuse脚本语言(或交互式财务计划系统)是建立从不同的2个部分:
–编译器
–运行
编译器,可以发现在ifpscomp.pas单元,运行时在ifps3.pas单位,不依赖于对方。可以使用编译器和运行时直接使用组件,或它的包装,该tifps3compexec组件可以在ifps3compexec单位和包装的编译器和执行在一个易于使用的类。使用组件的版本ifps3必须把它放在窗体或数据模块。设置或指定脚本属性,调用编译方法和执行方法。任何错误警告或提示发生在编译可以发现在该compilermessages数组属性和运行时错误可以通过阅读execerrortostring属性。
以下示例将编译和执行空脚本(开始的):
var
   Messages: string;
   compiled: boolean;
 begin
   ce.Script.Text := 'begin end.';
   Compiled := Ce.Compile;
   for i := 0 to ce.CompilerMessageCount -  do
     Messages := Messages + ce.CompilerMessages[i].MessageToString + # 3# 0;
   if Compiled then
     Messages := Messages + 'Succesfully compiled'# 3# 0;
   ShowMessage('Compiled Script: '# 3# 0+Messages);
   if Compiled then
   begin
     if Ce.Execute then
          ShowMessage('Succesfully Executed')
        else
          ShowMessage('Error while executing script: '+Ce.ExecErrorToString);
   end;
 end;
默认组件不仅增加了一些标准功能的脚本引擎(确切的名单在ifpscomp.pas顶部)。除了标准功能有一些库文件包含在ifps3:
tifps3dllplugin:  允许脚本使用动态链接库函数的语法是:function FindWindo (C1, C2: PChar): Longint;external 'FindWindo  A@user32.dll stdcall';
TIFPS3CE_ Controls: 进出库controls.pas和graphics.pas。
TIFPS3CE_DB:   进出库db.pas。
TIFPS3CE_DateUtils: 进出库的日期/时间相关函数。
TIFPS3CE_Std:   进出库tobject和类单位。
TIFPS3CE_StdCtrls:  进出库extctrls和按钮。
TIFPS3CE_Forms:  导入库的形式和菜单单元。
使用这些库,将它们添加到您的窗体或数据模块,选择TIFPS3CompExec组件的插件属性旁的[……],设置插件组件的插件属性。除了标准的库文件,可以方便地添加新的功能的脚本引擎。例如,创建一个新的方法来揭露脚本引擎:
procedure TForm .ShowNewMessage(const Message: string);
 begin
   ShowMessage('ShowNewMessage invoked:'# 13#10+Message);
 end;
接下来你须要分配一个事件处理器(event handler)给OnCompile事件,并使用TIFPS3CompExec的AddMethod 方法去添加的实际方法:
procedure TForm .CECompile(Sender: TIFPS3CompExec);
 begin
   Sender.AddMethod(Self, @TForm .ShowNewMessage, 'procedure ShowNewMessage
 (const Message: string);');
 end;
一个使用这个函数的简单样本,可以看起来像这样:
begin
   ShowNewMessage('Show This !');
 end.
高级功能:
IFPS3包括预处理,允许你使用定义({$IFDEF}, {$ELSE}, {$ENDIF}),并且包含其他文件在你的脚本。({$I filename.inc}),要使用这些功能,得把UsePreprocessor属性设置为true,还必须得设置MainFileName为脚本的名字放在你的脚本属性里。这些定义属性指定默认的设置,当需要一个包含文件时要调用OnNeedFile事件。
function TForm .ceNeedFile(Sender: TObject; const OrginFileName: String;
   var FileName, Output: String): Boolean;
 var
   path: string;
   f: TFileStream;
 begin
   Path := ExtractFilePath(ParamStr(0))  + FileName;
   try
     F := TFileStream.Create(Path, fmOpenRead or fmShareDenyWrite);
   except
     Result := false;
     exit;
   end;
   try
     SetLength(Output, f.Size);
     f.Read(Output[ ], Length(Output));
   finally
     f.Free;
   end;
   Result := True;
 end;
当这些属性设置好后,CompilerMessages数组属性将包含出现在这些讯息的文件名字。
IFPS3的另一特色是可能从Delphi调用方法脚本。下面的示例将作为一个脚本:

function TestFunction(Param : Double; Data: String): Longint;
 begin
   ShowNewMessage('Param : '+FloatToString(param )+# 13# 10+'Data: '+Data);
   Result :=  1234567;
 end;
 begin
 end.
在使用这些脚本函数之前,必须检查匹配的参数和结果类型,这些可以在OnVerifyProc事件中完成。
procedure TForm .CEVerifyProc(Sender: TIFPS3CompExec;
   Proc: TIFPSInternalProcedure; const Decl: String; var Error: Boolean);
 begin
   if Proc.Name = 'TESTFUNCTION' then
   begin
     if not ExportCheck(Sender.Comp, Proc, [btU8, btDouble, btString], [pmIn,
 pmIn]) then
     begin
       Sender.Comp.MakeError('', ecCustomError, 'Function header for
 TestFunction does not match.');
       Error := True;
     end else
     begin
       Proc.aExport := etExportDecl;
       Error := False;
     end;
   end else
     Error := False;
 end;
ExportCheck测试参数匹配,在这种情况下btu8是一个布尔值(结果型),btdouble是第一个参数和btString第二个参数。[pmIn pmIn]说明这两个参数都是in参数。可以使用这个脚本函数,必须为这个函数创建一个事件声明并调用。
type
   TTestFunction = function (Param : Double; Data: String): Longint of object;
 ...
 var
   Meth: TTestFunction;
   Meth := TTestFunction(ce.GetProcMethod('TESTFUNCTION'));
   if @Meth = nil then
     raise Exception.Create('Unable to call TestFunction');
   ShowMessage('Result: '+IntToStr(Meth(pi, DateTimeToStr(Now))));
这也可以在脚本引擎中添加在脚本内使用的变量。要做到这一点,必须使用AddRegisteredVariable函数。在OnExecute事件可以这样设置:
procedure TForm .ceExecute(Sender: TIFPS3CompExec);
 begin
   CE.SetVarToInstance('SELF', Self); // For class variables
   VSetInt(CE.GetVariable('MYVAR'),  1234567);
 end;
读完这个变量,脚本执行完成后,你可以使用
OnAfterExecute事件,并用VGetInt(CE.GetVariable(“MYVAR”)。


注:
一些函数和常数,可能需要补充:ifpscomp,ifps3和ifps3utl到uses中。
——脚本引擎应用本身从来不调用Application.ProcessMessages,可能将会申请挂起,当脚本在运行时,为了避免如此,可以添加Application.ProcessMessages到TIFPS3CompExec.OnLine event。
——可能会在脚本引擎肿引用你自己的类。IFPS3 includes一个工具,在/ Unit-Importing /directory下创建导入库。
——源码部分为界面每一个method/function包含一个小的描述。这些说明,也可以/Help/Units 目录.
——一些小例子都能在/Help/下找到
—— / DUnit /目录,包含一个DUnit test application for IFPS3。你需要http://dunit.sourceforge.net/去运行这个。
——例如:如何分别使用编译器和运行时,看Demo_Import /和Demo_Kylix。
——Ide-demo和unit-import SynEdit需要http://synedit.sourceforge.net/

(以上为本人拙略翻译过来的文档,借鉴时应该谨慎)

posted on 2011-11-30 17:36  糖炒栗子  阅读(334)  评论(0编辑  收藏  举报