TPaxCompiler Methods
TPaxCompiler.AddCode
Adds source code to module.
procedure AddCode(const ModuleName, Text: String);
Example
AddCode('MyModule', 'begin');
AddCode('MyModule', 'writeln(123);');
AddCode('MyModule', 'end.');
TPaxCompiler.AddCodeFromFile
Adds source code from text file to module.
procedure AddCodeFromFile(const ModuleName, FileName: String);
Example
AddCodeFromFile('MyModule', 'MyFile.pas');
TPaxCompiler.AddModule
Adds source code module to compiler.
procedure AddModule(const Name, LanguageName: String);
Example
AddModule('MyModule', 'Pascal');
TPaxCompiler.Compile
Compiles program.
function Compile(PaxProgram: TPaxProgram): boolean;
Arguments
PaxProgram
TPaxProgram instance that saves result of compilation.
Example
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
PaxCompiler1: TPaxCompiler;
PaxPascalLanguage1: TPaxPascalLanguage;
PaxProgram1: TPaxProgram;
begin
PaxCompiler1 := TPaxCompiler.Create(nil);
PaxPascalLanguage1 := TPaxPascalLanguage.Create(nil);
PaxProgram1 := TPaxProgram.Create(nil);
try
PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
// register routine 'ShowMessage'
H_ShowMessage := PaxCompiler1.RegisterRoutine(0, 'ShowMessage', _typeVOID, _ccREGISTER);
PaxCompiler1.RegisterParameter(H_ShowMessage, _typeSTRING, _Unassigned);
// register variable 'S'
H_S := PaxCompiler1.RegisterVariable(0, 'S', _typeSTRING);
PaxCompiler1.AddModule('1', PaxPascalLanguage1.LanguageName);
PaxCompiler1.AddCode('1', 'begin');
PaxCompiler1.AddCode('1', ' ShowMessage(S);');
PaxCompiler1.AddCode('1', 'end.');
if PaxCompiler1.Compile(PaxProgram1) then
begin
PaxProgram1.SaveToFile('1.bin');
ShowMessage('Compiled script has been created!');
end
else
for I:=0 to PaxCompiler1.ErrorCount - 1 do
ShowMessage(PaxCompiler1.ErrorMessage[I]);
finally
PaxCompiler1.Free;
PaxPascalLanguage1.Free;
PaxProgram1.Free;
end;
end;
TPaxCompiler.CompileExpression
Compiles expression.
function CompileExpression(const Expression: String;
PaxProgram: TPaxProgram): Boolean;
Example
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, PaxCompiler;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
arr_x, arr_y: array[1..3] of Double;
h_norm, h_x, h_y: Integer;
buff: array[1..4096] of Byte;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function Norm(x, y: Double): Double;
begin
result := Sqrt(x * x + y * y);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
PaxCompiler1: TPaxCompiler;
PaxPascalLanguage1: TPaxPascalLanguage;
PaxProgram1: TPaxProgram;
I: Integer;
begin
PaxCompiler1 := TPaxCompiler.Create(nil);
PaxPascalLanguage1 := TPaxPascalLanguage.Create(nil);
PaxProgram1 := TPaxProgram.Create(nil);
try
PaxCompiler1.Reset;
PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
h_norm := PaxCompiler1.RegisterRoutine(0, 'Norm', _typeDOUBLE, _ccREGISTER);
PaxCompiler1.RegisterParameter(h_norm, _typeDOUBLE, Unassigned);
PaxCompiler1.RegisterParameter(h_norm, _typeDOUBLE, Unassigned);
h_x := PaxCompiler1.RegisterVariable(0, 'x', _typeDOUBLE);
h_y := PaxCompiler1.RegisterVariable(0, 'y', _typeDOUBLE);
if PaxCompiler1.CompileExpression('Norm(x, y)', PaxProgram1) then
begin
PaxProgram1.SaveToBuff(buff);
ShowMessage('Compiled expression has been created!');
end
else
for I:=0 to PaxCompiler1.ErrorCount - 1 do
ShowMessage(PaxCompiler1.ErrorMessage[I]);
finally
PaxCompiler1.Free;
PaxPascalLanguage1.Free;
PaxProgram1.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
PaxProgram1: TPaxProgram;
ResValue: Double;
I: Integer;
begin
{$O-}
if h_x <> 0 then
begin
PaxProgram1 := TPaxProgram.Create(nil);
try
PaxProgram1.LoadFromBuff(buff);
PaxProgram1.SetAddress(h_norm, @norm);
for I:=1 to 3 do
begin
PaxProgram1.SetAddress(h_x, @arr_x[I]);
PaxProgram1.SetAddress(h_y, @arr_y[I]);
PaxProgram1.Run;
ResValue := Double(PaxProgram1.ResultPtr^);
ShowMessage(FloatToStr(ResValue));
end;
finally
PaxProgram1.Free;
end;
end
else
ShowMessage('Press the first button to create compiled script.');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
h_x := 0; h_y := 0; h_norm := 0;
arr_x[1] := 4.2; arr_y[1] := -5.2;
arr_x[2] := -0.4; arr_y[2] := 3.2;
arr_x[3] := 2.0; arr_y[3] := 3;
end;
end.
TPaxCompiler.Create
Constructor of TPaxCompiler class.
constructor Create(AOwner: TComponent); override;
TPaxCompiler.Destroy
Destructor of TPaxCompiler class.
destructor Destroy; override;
TPaxCompiler.GetHandle
Returns id of a script-defined variable, procedure or function.
function GetHandle(LevelId: Integer; const Name: String; Upcase: Boolean): Integer;
Arguments
LevelId
Id of namespace.
Name
Name of the script-defined variable, procedure or function.
Upcase
If 'false', the search of id will be case sensitive.
Example
procedure TForm1.Button1Click(Sender: TObject);
var
H_ShowMessage, H_IntToStr, H_X: Integer;
P: Pointer;
I: Integer;
begin
PaxCompiler1.Reset;
PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
H_ShowMessage := PaxCompiler1.RegisterRoutine(0, 'ShowMessage', _typeVOID, _ccREGISTER);
PaxCompiler1.RegisterParameter(H_ShowMessage, _typeSTRING, _Unassigned);
H_IntToStr := PaxCompiler1.RegisterRoutine(0, 'IntToStr', _typeSTRING, _ccREGISTER);
PaxCompiler1.RegisterParameter(H_IntToStr, _typeINTEGER, _Unassigned);
PaxCompiler1.AddModule('1', PaxPascalLanguage1.LanguageName);
PaxCompiler1.AddCode('1', 'var x: Integer = 5;');
PaxCompiler1.AddCode('1', 'begin');
PaxCompiler1.AddCode('1', ' ShowMessage(''script:'' + IntToStr(x));');
PaxCompiler1.AddCode('1', 'end.');
if PaxCompiler1.Compile(PaxProgram1) then
begin
H_X := PaxCompiler1.GetHandle(0, 'x', true);
PaxProgram1.SetAddress(H_ShowMessage, @ShowMessage);
PaxProgram1.SetAddress(H_IntToStr, @IntToStr);
PaxProgram1.Run; // the first run
if H_X <> 0 then
begin
P := PaxProgram1.GetAddress(H_X);
ShowMessage('host:' + IntToStr(Integer(P^))); // show script-defined variable
end;
Integer(P^) := 30; // change script-defind variable
PaxProgram1.Run; // the second run
end
else
for I:=0 to PaxCompiler1.ErrorCount do
ShowMessage(PaxCompiler1.ErrorMessage[I]);
end;
TPaxCompiler.RegisterTypeDeclaration
Allows you to register enumeration type, subrange type, set type, shortstring type by its declaration.
function RegisterTypeDeclaration(LevelId: Integer;
const Declaration: String): Integer;
Arguments
LevelId
Id of namespace.
Declaration
Type declaration.
Example
RegisterTypeDeclaration(0, 'TMyEnum = (one, two, three);');
TPaxCompiler.RegisterInterfaceType
Registeres an interface type.
function RegisterInterfaceType(LevelId: Integer;
const TypeName: String; const GUID: TGUID): Integer;
Arguments
LevelId
Id of namespace.
TypeName
Name of type.
GUID
GUID of interface type.
TPaxCompiler.RegisterSupportedInterface
Registeres supported interface type.
procedure RegisterSupportedInterface(TypeId: Integer;
const GUID: TGUID);
Arguments
TypeId
Id of interface type.
GUID
GUID of suppported interface type.
TPaxCompiler.RegisterClassType
Registeres class type for paxCompiler.
function RegisterClassType(LevelId: Integer; onst TypeName: String; AncestorId: Integer): Integer; overload;
function RegisterClassType(LevelId: Integer; C: TClass): Integer; overload;
Example
H_HostClass := compiler.RegisterClassType(0, THostClass);
TPaxCompiler.RegisterClassReferenceType
Registeres class reference type for paxCompiler.
function RegisterClassReferenceType(LevelId: Integer; const TypeName: String; OriginClassId: Integer): Integer;
Example
H := RegisterNamespace(0, 'Classes');
G := RegisterClassType(H, TPersistent);
RegisterClassReferenceType(H, 'TPersistentClass', G);
TPaxCompiler.RegisterClassTypeField
Registers field of class type for paxCompiler.
function RegisterClassTypeField(TypeId: Integer; const FieldName: String; FieldTypeID: Integer; FieldShift: Integer = -1): Integer;
Example
type
THostClass = class(TComponent)
Z: Integer;
end;
.............................
H_HostClass := compiler.RegisterClassType(0, THostClass);
compiler.RegisterHeader(H_HostClass, 'constructor Create(AOwner: TComponent);', @THostClass.Create);
H_Z := compiler.RegisterClassTypeField(H_HostClass, 'Z', typeINTEGER, Integer(@THostClass(nil).Z));
TPaxCompiler.RegisterProperty
Registeres public property for paxCompiler.
function RegisterProperty(LevelId: Integer; const PropName: String; PropTypeID, ReadId, WriteId: Integer;IsDefault: Boolean): Integer; overload;
function RegisterProperty(LevelId: Integer; const Header: String): Integer; overload;
Example
H_HostClass := compiler.RegisterClassType(0, THostClass);
compiler.RegisterHeader(H_HostClass, 'constructor Create(AOwner: TComponent);', @THostClass.Create);
H_GetItem := compiler.RegisterHeader(H_HostClass, 'function GetItem(I, J: Integer): Integer;', @THostClass.GetItem);
H_SetItem := compiler.RegisterHeader(H_HostClass, 'procedure SetItem(I, J: Integer; Value: Integer);', @THostClass.SetItem);
compiler.RegisterProperty(H_HostClass, 'property Items[I, J: Integer]: Integer read GetItem write SetItem; default;');
TPaxCompiler.RegisterEnumType
Registeres enumeration type for paxCompiler.
function RegisterEnumType(LevelId: Integer; const TypeName: String; TypeBaseId: Integer = _typeINTEGER): Integer;
Example
H_TMyEnum := RegisterEnumType(0, 'TMyEnum');
RegisterEnumValue(H_TMyEnum, 'Green', 0);
RegisterEnumValue(H_TMyEnum, 'Red', 1);
TPaxCompiler.RegisterEnumValue
Registeres a value of enumeration type.
function RegisterEnumValue(EnumTypeId: Integer; const FieldName: String; const Value: Integer): Integer;
Example
H_TMyEnum := RegisterEnumType(0, 'TMyEnum');
RegisterEnumValue(H_TMyEnum, 'Green', 0);
RegisterEnumValue(H_TMyEnum, 'Red', 1);
TPaxCompiler.RegisterRTTIType
Registeres a Delphi type that has RTTI (Run time type information) for paxCompiler.
function RegisterRTTIType(LevelId: Integer; pti: PTypeInfo): Integer;
Example
RegisterRTTIType(TypeInfo(TMyEnum));
TPaxCompiler.RegisterTypeAlias
Registeres type alias for paxCompiler.
function RegisterTypeAlias(LevelId:Integer;
const TypeName: String; OriginTypeId: Integer): Integer;
Example
RegisterTypeAlias(H, 'Longint', _typeINTEGER);
TPaxCompiler.RegisterArrayType
Registeres array type for paxCompiler.
function RegisterArrayType(LevelId: Integer; const TypeName: String; RangeTypeId, ElemTypeId: Integer): Integer;
Arguments
LevelId
Id of namespace.
TypeName
Name of array type.
RangeTypeId
Id of ordinal type that specifies range of array.
ElemType
Id of type that specifies type of array element.
Example
H_Range := PaxCompiler1.RegisterSubrangeType(0, 'MySubrangeType', _typeCHAR, ord('A'), ord('Z'));
H_TMyPoint := PaxCompiler1.RegisterRecordType(0, 'TMyPoint');
PaxCompiler1.RegisterRecordTypeField(H_TMyPoint, 'X', _typeINTEGER);
PaxCompiler1.RegisterRecordTypeField(H_TMyPoint, 'Y', _typeINTEGER);
H_Array := PaxCompiler1.RegisterArrayType(0, 'MyArray', H_Range, H_TMyPoint);
TPaxCompiler.RegisterDynamicArrayType
Registeres dynamic array type. Returns id of type.
function RegisterDynamicArrayType(LevelId: Integer;
const TypeName: String; ElemTypeId: Integer): Integer;
Arguments
LevelId
Id of namespace.
TypeName
Name of type.
ElemTypeId
Id of type of array element.
TPaxCompiler.RegisterLanguage
Registeres component that represents programming language for paxCompiler.
procedure RegisterLanguage(L: TPaxCompilerLanguage);
Example
var PaxCompiler1: TPaxCompiler;
PaxPascalLanguage1: TPaxPascalLanguage;
begin
PaxCompiler1 := TPaxCompiler.Create(nil);
PaxPascalLanguage1 := TPaxPascalLanguage.Create(nil);
PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
................
TPaxCompiler.RegisterNamespace
Registeres a namespace for the paxCompiler.
function RegisterNamespace(LevelId: Integer; const NamespaceName: String): Integer;
Arguments
LevelId
Id of owner namespace.
NamespaceName
Name of namespace.
Example
id := RegisterNamespace(0, 'MyNamespace'); // 0 represents noname namespace
RegisterVariable(id, 'MyVar', _typeINTEGER, @MyVar);
TPaxCompiler.RegisterRecordType
Registeres a record type.
function RegisterRecordType(LevelId: Integer; const TypeName: String): Integer;
Arguments
LevelId
Id of owner record type or id of namespace.
TypeName
Name of record type.
Example
H_TMyPoint := PaxCompiler1.RegisterRecordType(0, 'TMyPoint');
PaxCompiler1.RegisterRecordTypeField(H_TMyPoint, 'X', _typeINTEGER);
PaxCompiler1.RegisterRecordTypeField(H_TMyPoint, 'Y', _typeINTEGER);
TPaxCompiler.RegisterRoutine
Registeres header of function or procedure for paxCompiler.
function RegisterRoutine(LevelId: Integer; const Name: String; ResultTypeID: Integer; CallConvention: Integer): Integer;
Arguments
LevelId
Id of owner namespace.
Name
Name of procedure or function.
ResultTypeID
Id of result type.
CallConvention
_ccSTDCALL = 1
_ccREGISTER = 2
Example
H_IntToStr := PaxCompiler1.RegisterRoutine(0, 'IntToStr', _typeSTRING, _ccREGISTER);
PaxCompiler1.RegisterParameter(H_IntToStr, _typeINTEGER, _Unassigned);
TPaxCompiler.RegisterMethod
Registeres method of Delphi class.
function RegisterMethod(LevelId: Integer; const MethodName: String; ResultTypeID: Integer; CallConvention: Integer; Address: Pointer = nil; IsShared: Boolean = false): Integer;
Arguments
LevelId
Id of class.
MethodName
Name of method.
ResultTypeID
Id of type of result.
CallConvention
Call convention.
IsShared
True, if it is static (shared) method.
Example
compiler.RegisterMethod(H_HostClass, 'MyMethod', _typeINTEGER, ccREGISTER, @THostClass.MyMethod, false);
TPaxCompiler.RegisterConstructor
Registeres construcor of a Delphi class for paxCompiler.
function RegisterConstructor(LevelId: Integer; const SubName: String; Address: Pointer = nil): Integer;
Example
H := compiler.RegisterConstructor(H_HostClass, 'Create', @THostClass.Create);
TPaxCompiler.RegisterParameter
Registeres parameter of a host-defined procedure of function.
function RegisterParameter(HSub: Integer; ParamTypeID: Integer; const DefaultValue: Variant; ByRef: Boolean = false): Integer;
Arguments
HSub
Id of routine.
ParamTypeId
Id of type of parameter.
DefaultValue
Default value of parameter.
ByRef
If 'true', this is the variable parameter, otherwise this is the value parameter.
Example
H_IntToStr := PaxCompiler1.RegisterRoutine(0, 'IntToStr', _typeSTRING, _ccREGISTER);
PaxCompiler1.RegisterParameter(H_IntToStr, _typeINTEGER, _Unassigned);
TPaxCompiler.RegisterPointerType
Registeres a pointer type for paxCompiler.
function RegisterPointerType(LevelId: Integer;
const TypeName: String; OriginTypeId: Integer): Integer;
Arguments
LevelId
Id of namespace.
TypeName
Name of pointer type.
OriginTypeId
Id of origin type.
Example
H_TMyPoint := PaxCompiler1.RegisterRecordType(0, 'TMyPoint');
PaxCompiler1.RegisterRecordTypeField(H_TMyPoint, 'X', _typeINTEGER);
PaxCompiler1.RegisterRecordTypeField(H_TMyPoint, 'Y', _typeINTEGER);
H_PMyPoint := PaxCompiler1.RegisterPointerType(0, 'PMyPoint', H_TMyPoint);
TPaxCompiler.RegisterProceduralType
Registeres a procedural type for paxCompiler.
function RegisterProceduralType(LevelId: Integer; const TypeName: String; SubId: Integer): Integer;
Arguments
LevelId
Id of namespace.
TypeName
Name of type.
SubId
Id of header of a subroutine.
Example
H_IntToStr := PaxCompiler1.RegisterRoutine(0, 'IntToStr', _typeSTRING, _ccREGISTER);
PaxCompiler1.RegisterParameter(H_IntToStr, _typeINTEGER, _Unassigned);
H_FuncType := PaxCompiler1.RegisterProceduralType(0, 'TFuncType', H_IntToStr);
TPaxCompiler.RegisterSetType
Registeres a set type for paxCompiler.
function RegisterSetType(LevelId: Integer; const TypeName: String; OriginTypeId: Integer): Integer;
Arguments
LevelId
Id of namespace.
TypeName
Name of set type.
OriginTypeId
Id of origin type.
Example
RegisterSetType(0, 'TMySet', _typeCHAR);
TPaxCompiler.RegisterSubrangeType
Registeres a subrange type for the paxCompiler.
function RegisterSubrangeType(LevelId: Integer; const TypeName: String; TypeBaseId: Integer; const B1, B2: Integer): Integer;
Arguments
LevelId
Id of namespace.
TypeName
Name of subrange type.
TypeBaseId
Id of base type.
B1
Low bound.
B2
High bound.
Example
PaxCompiler1.RegisterSubrangeType(0, 'MySubrangeType', _typeCHAR, ord('A'), ord('Z'));
TPaxCompiler.RegisterConstant
Registeres a constant for paxCompiler.
function RegisterConstant(LevelId: Integer; const ConstName: String; typeID: Integer; const Value: Variant): Integer; overload;
function RegisterConstant(LevelId: Integer; const ConstName: String; const Value: Variant): Integer; overload;
Example
RegisterConstant(H, 'fmOpenRead', fmOpenRead);
TPaxCompiler.RegisterVariable
Registeres a host-defined variable for paxCompiler.
function RegisterVariable(LevelId: Integer; const Name: String; TypeId: Integer; Address: Pointer = nil): Integer;
Arguments
LevelId
Id of namespace
Name
Name of variable.
TypeId
Id of variables's type.
Address
Optional. Address of variable.
Example
H_MyVar := PaxCompiler1.RegisterVariable(0, 'MyVar', _typeSINGLE);
TPaxCompiler.RegisterHeader
Registeres header of host-defined procedure, function, constructor or method for paxCompiler.
function RegisterHeader(LevelId: Integer; const Header: String; Address: Pointer = nil;
MethodIndex: Integer = 0): Integer;
Example 1
RegisterHeader(H, 'function UpperCase(const S: string): string;', @UpperCase);
Example 2
RegisterHeader(H_TList, 'function Add(Item: Pointer): Integer;', @TList.Add);
Example 3
RegisterHeader(H_TList, 'constructor Create;', @TList.Create);
TPaxCompiler.Reset
Removes all source code modules and registered items from compiler.
procedure Reset;
TPaxCompiler.ResetCompilation
Removes source code modules.
procedure ResetCompilation;
Copyright © 2006-2008
VIRT Laboratory. All rights reserved.