• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

在delphi中嵌入脚本语言--(译)RemObjectsPascalScript使用说明(1)(译)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
翻譯這篇文章源於我的一個通用工資計算平台的想法,在工資的計算中,不可避免的需要使用到自定義公式,然而對於自定義公式的實現,我自己想了一些,也在網上搜索了很多,解決辦法大致有以下幾種:
1. 自己寫代碼去解析公式。這種方法的缺點是,解析的代碼很難實現,如果公式的功能比較完整,如增加條件判斷或自定義函數。不亞於實現了一個簡單的語言編譯囂或解釋囂。所以,只能實現一些諸如加減乘除之類的簡單公式。
2. 打包成SQL傳給數據庫去執行。這顯然不是一種好辦法。而且需要與特定的數據庫和表結構進行適應。
3. 我想到在foxpro中有宏替換功能&,那不如就借用它的這個功能,即利用foxpro寫一個dll,在這個dll中實現了將字符串轉換成指令執行的功能,然後在delphi中加載這個dll,將公式傳入dll中的函數執行。這應該是一個辦法,但我還沒有去實現它。
4. 內嵌腳本語言。
 
也只有第四種辦法比較理想的,於是我就找到了RemObjects Pascal Script,安裝,並翻譯了這篇使用說明。
再把應用范圍擴大一點,其實在編譯型程序中嵌入腳本語言可以解決很多應用程序自動化的問題,在了解並實際寫了幾個RemObjects Pascal Script的實從程序後。內心還是蠻興奮的。
 
PS01 - Using the RemObjects Pascal Script
使用RemObjects Pascal Script
 
This article provides an overview of the new RemObjects Pascal Script and explains how to create some simple scripts.
這篇文章提供了RemObjects Pascal Script的一個概覽,以及說明了如何去創建一些簡單的腳本。
 
Pascal Script comprises two different parts:
Compiler (uPSCompiler.pas)
Runtime (uPSRuntime.pas)
Pascal Script由兩個部分組成:
編譯囂(UPSCompiler.pas)
運行時(uPSRuntime.pas)
 
The two parts have no interdependencies on each other. You can use them directly, or you can use them in the TPSScript component, which can be found in the uPSComponent.pas unit, and wraps them both in one easy to use class.
這兩部分之間是沒有相互依賴的。你可以直接使用她們,或才你可以透過TPSSCript組件來使用她們,TPSSCript組件在uPSComponent.pas單元中,她對上述兩個部分進行一些包裝以便我們可以很容易的使用。
 
To use the component version of Pascal Script, you must first place it on your form or data module, set or assign the script property, call the Compile method, and call the Execute method. Compile errors, warnings or hints can be found in the CompilerMessages array property, while runtime errors can be found by reading the ExecErrorToString property.
要使用Pascal Script組件,你首先要將它從組件面板中拖置窗體或module中,然後設置它的script屬性,然後調用它的Compile方法進行編譯,再然後調用它的Execute方法來執行腳本。編譯的errors,hints,warnings可以通過其屬性CompilerMessages取得,這個屬性是一個數組。如果是運行時的錯誤,則可以通過屬性ExecErrorToString取得。
 
The following example will compile and execute an empty script ("begin end."):
下面的例子將編譯並執行一個空腳本("begin end."):
 
var
 Messages: string;
 compiled: boolean;
begin
 ce.Script.Text := 'begin end.';
 Compiled := Ce.Compile;
 for i := 0 to ce.CompilerMessageCount -1 do
    Messages := Messages +
                ce.CompilerMessages[i].MessageToString +
                #13#10;
 if Compiled then
    Messages := Messages + 'Succesfully compiled'#13#10;
 ShowMessage('Compiled Script: '#13#10+Messages);
 if Compiled then begin
    if Ce.Execute then
      ShowMessage('Succesfully Executed')
    else
      ShowMessage('Error while executing script: '+
                  Ce.ExecErrorToString);
 end;
end;
 
By default, the component only adds a few standard functions to the scripting engine (the exact list can be found at the top of uPSComponents.pas).
缺省情況下,組件只加入一少部分標准的functions到腳本引擎中(具體可以在uPSComponents.pas單元頭中找到)
 
Besides the standard functions, there are a few libraries included with Pascal Script:
     TPSDllPlugin Allow scripts to use dll functions, the syntax is like:
function FindWindow(C1, C2: PChar): Longint; external '[email protected] stdcall';
     TPSImport_Classes Import library for TObject and the Classes unit.
     TPSImport_DateUtils    Import library for date/time related functions.
     TPSImport_ComObj Access COM Objects from your scripts.
     TPSImport_DB Import library for db.pas.
     TPSImport_Forms  Import library for the Forms & Menus units.
     TPSImport_Controls     Import library to Controls.pas and Graphics.pas.
     TPSImport_StdCtrls     Import library for ExtCtrls and Buttons.
 
除了這些標准的functions之外,Pascal Script還包含了一少部分程式庫:
TPSDllPlugin      允許腳本可以使用外部DLL函數,其調用語法類似下例:
function FindWindow(C1, C2: PChar): Longint; external '[email protected] stdcall';
TPSImport_Classes 導入對應於TObject和Classes單元的libraries;
TPSImport_DateUtils    導入日期時間相關的libraries;
TPSImport_ComObj 在腳本中訪問COM對象;
TPSImport_DB      導入對應於db.pas單元的libraries;
TPSImport_Forms   導入對應於Forms和Menus單元的libraries;
TPSImport_Controls 導入對應於Controls.pas和Graphics.pas單元的libraries;
TPSImport_StdCtrls 導入對應於ExtCtrls和Buttons的libraries.
 
To use these libraries, add them to your form or data module, select the [...] button next to the plugins property of the TPSCompiler component, add a new item and set the Plugin property to the plugin component.
要使用這些libraries,將它們從組件面板中拖至窗體數據data module中,然後設置TPSCompiler的plugins屬性,在其中增加條目,並將條目指向這些plugin組件。
 
Besides the standard libraries, you can easily add new functions to the scripting engine. In order to do that, create a new method you would like to expose to the scripting engine, for example:
除了這些標准的libraries之外,你還可以很方便地向腳本引擎中添加新的函數。要做到這一點,創建一個你要加入到腳本中的method,如下例:
 
procedure TForm1.ShowNewMessage(const Message: string);
begin
 ShowMessage('ShowNewMessage invoked:'#13#10+Message);
end;
 
Then, assign an event handler to the OnCompile event and use the AddMethod method of TPSCompiler to add the actual method:
然後,在TPSCompiler 的OnCompile事件中將該方法添加入腳本中:
 
procedure TForm1.CECompile(Sender: TPSScript);
begin
 Sender.AddMethod(Self, @TForm1.ShowNewMessage,
                   'procedure ShowNewMessage
                   (const Message: string);');
end;
 
A sample script that uses this function could look like this:
這樣, 你就可以在腳本中使用這個函數,如下:
 
begin
 ShowNewMessage('Show This !');
end.
 
Advanced Features
高級功能
 
Pascal Script includes a preprocessor that allows you to use defines ({$IFDEF}, {$ELSE}, {$ENDIF}) and include other files in your script ({$I filename.inc}). To enable these features, you must set the UsePreprocessor property to true and the MainFileName property to match the name of the script in the Script property. The Defines property specifies which defines are set by default, and the OnNeedFile event is called when an include file is needed.
Pascal Script包含了一個預處理程序,以便你可以在腳本中使用編譯預定義(defines)({$IFDEF}, {$ELSE}, {$ENDIF}) 以及在腳本中包含其它腳本 ({$I filename.inc})。要達到這個功能,你需要設置UsePreprocessor屬性為true,and the MainFileName property to match the name of the script in the Script property. 。Defines屬性指定要缺省時定義哪些defines;OnNeedFile事件代碼在需要包含的文件時被執行。
 
function TForm1.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[1], Length(Output));
 finally
 f.Free;
 end;
 Result := True;
end;
 
When these properties are set, the CompilerMessages array property will include the file name these messages occur in.
當這些屬性被設置以後,CompilerMessages屬性可就將可能包含這些文件名。
 
Additionally, you can call scripted functions from Delphi. The following sample will be used as a script:
另外,你可以在Delphi中調用腳本裡的函數。如下函數被定義在腳本中,後面將會在delphi中被調用:
 
function TestFunction(Param1: Double; Data: String): Longint;
begin
 ShowNewMessage('Param1: '+FloatToString(param1)
                 +#13#10+'Data: '+Data);
 Result := 1234567;
end;
 
begin
end.
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB·提取图像中多个目标发布时间:2022-07-18
下一篇:
基于matlab的经典图像边缘检测算法发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap