Adds source code to module.procedure AddCode(const ModuleName, Text: String);
Example
AddCode('MyModule', 'begin'); AddCode('MyModule', 'writeln(123);'); AddCode('MyModule', 'end.');
Adds source code from text file to module.procedure AddCodeFromFile(const ModuleName, FileName: String);
Example
AddCodeFromFile('MyModule', 'MyFile.pas');
Adds source code module to compiler.procedure AddModule(const Name, LanguageName: String);
Example
AddModule('MyModule', 'Pascal');
Compiles program.function Compile(PaxProgram: TPaxProgram; BuildAll: Boolean = false; BuildWithRuntimePackages: Boolean = false): boolean; overload;Arguments
PaxProgramTPaxProgram instance that saves result of compilation.BuildAllBuild all compiled units (pcu-files).BuildWithRuntimePackagesIf true, PaxProgram will use pcu-s as run-time packages
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;
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.
Constructor of TPaxCompiler class.constructor Create(AOwner: TComponent); override;
Destructor of TPaxCompiler class.destructor Destroy; override;
Returns id of a script-defined variable, procedure or function.function GetHandle(LevelId: Integer; const Name: String; Upcase: Boolean): Integer;Arguments
LevelIdId of namespace.NameName of the script-defined variable, procedure or function.UpcaseIf '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;
Allows you to register enumeration type, subrange type, set type, shortstring type by its declaration.function RegisterTypeDeclaration(LevelId: Integer; const Declaration: String): Integer;Arguments
LevelIdId of namespace.DeclarationType declaration.RegisterTypeDeclaration(0, 'TMyEnum = (one, two, three);');
Registeres an interface type.function RegisterInterfaceType(LevelId: Integer; const TypeName: String; const GUID: TGUID): Integer;Arguments
LevelIdId of namespace.TypeNameName of type.GUIDGUID of interface type.
Registeres supported interface type.procedure RegisterSupportedInterface(TypeId: Integer; const GUID: TGUID);Arguments
TypeIdId of interface type.GUIDGUID of suppported interface type.
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);
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);
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));
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;');
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);
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);
Registeres a Delphi type that has RTTI (Run time type information) for paxCompiler.function RegisterRTTIType(LevelId: Integer; pti: PTypeInfo): Integer;
Example
RegisterRTTIType(TypeInfo(TMyEnum));
Registeres type alias for paxCompiler.function RegisterTypeAlias(LevelId:Integer; const TypeName: String; OriginTypeId: Integer): Integer;
Example
RegisterTypeAlias(H, 'Longint', _typeINTEGER);
Registeres array type for paxCompiler.function RegisterArrayType(LevelId: Integer; const TypeName: String; RangeTypeId, ElemTypeId: Integer): Integer;Arguments
LevelIdId of namespace.TypeNameName of array type.RangeTypeIdId of ordinal type that specifies range of array.ElemTypeId 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);
Registeres dynamic array type. Returns id of type.function RegisterDynamicArrayType(LevelId: Integer; const TypeName: String; ElemTypeId: Integer): Integer;Arguments
LevelIdId of namespace.TypeNameName of type.ElemTypeIdId of type of array element.
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); ................
Registeres a namespace for the paxCompiler.function RegisterNamespace(LevelId: Integer; const NamespaceName: String): Integer;Arguments
LevelIdId of owner namespace.NamespaceNameName of namespace.
Example
id := RegisterNamespace(0, 'MyNamespace'); // 0 represents noname namespace RegisterVariable(id, 'MyVar', _typeINTEGER, @MyVar);
Registeres a record type.function RegisterRecordType(LevelId: Integer; const TypeName: String): Integer;Arguments
LevelIdId of owner record type or id of namespace.TypeNameName of record type.
Example
H_TMyPoint := PaxCompiler1.RegisterRecordType(0, 'TMyPoint'); PaxCompiler1.RegisterRecordTypeField(H_TMyPoint, 'X', _typeINTEGER); PaxCompiler1.RegisterRecordTypeField(H_TMyPoint, 'Y', _typeINTEGER);
Registeres header of function or procedure for paxCompiler.function RegisterRoutine(LevelId: Integer; const Name: String; ResultTypeID: Integer; CallConvention: Integer): Integer;Arguments
LevelIdId of owner namespace.NameName of procedure or function.ResultTypeIDId of result type.CallConvention_ccSTDCALL = 1 _ccREGISTER = 2
Example
H_IntToStr := PaxCompiler1.RegisterRoutine(0, 'IntToStr', _typeSTRING, _ccREGISTER); PaxCompiler1.RegisterParameter(H_IntToStr, _typeINTEGER, _Unassigned);
Registeres method of Delphi class.function RegisterMethod(LevelId: Integer; const MethodName: String; ResultTypeID: Integer; CallConvention: Integer; Address: Pointer = nil; IsShared: Boolean = false): Integer;Arguments
LevelIdId of class.MethodNameName of method.ResultTypeIDId of type of result.CallConventionCall convention.IsSharedTrue, if it is static (shared) method.
Example
compiler.RegisterMethod(H_HostClass, 'MyMethod', _typeINTEGER, ccREGISTER, @THostClass.MyMethod, false);
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);
Registeres parameter of a host-defined procedure of function.function RegisterParameter(HSub: Integer; ParamTypeID: Integer; const DefaultValue: Variant; ByRef: Boolean = false): Integer;Arguments
HSubId of routine.ParamTypeIdId of type of parameter.DefaultValueDefault value of parameter.ByRefIf '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);
Registeres a pointer type for paxCompiler.function RegisterPointerType(LevelId: Integer; const TypeName: String; OriginTypeId: Integer): Integer;Arguments
LevelIdId of namespace.TypeNameName of pointer type.OriginTypeIdId 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);
Registeres a procedural type for paxCompiler.function RegisterProceduralType(LevelId: Integer; const TypeName: String; SubId: Integer): Integer;Arguments
LevelIdId of namespace.TypeNameName of type.SubIdId 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);
Registeres a set type for paxCompiler.function RegisterSetType(LevelId: Integer; const TypeName: String; OriginTypeId: Integer): Integer;Arguments
LevelIdId of namespace.TypeNameName of set type.OriginTypeIdId of origin type.
Example
RegisterSetType(0, 'TMySet', _typeCHAR);
Registeres a subrange type for the paxCompiler.function RegisterSubrangeType(LevelId: Integer; const TypeName: String; TypeBaseId: Integer; const B1, B2: Integer): Integer;Arguments
LevelIdId of namespace.TypeNameName of subrange type.TypeBaseIdId of base type.B1Low bound.B2High bound.
Example
PaxCompiler1.RegisterSubrangeType(0, 'MySubrangeType', _typeCHAR, ord('A'), ord('Z'));
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);
Registeres a host-defined variable for paxCompiler.function RegisterVariable(LevelId: Integer; const Name: String; TypeId: Integer; Address: Pointer = nil): Integer;Arguments
LevelIdId of namespaceNameName of variable.TypeIdId of variables's type.AddressOptional. Address of variable.
Example
H_MyVar := PaxCompiler1.RegisterVariable(0, 'MyVar', _typeSINGLE);
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);
Removes all source code modules and registered items from compiler.procedure Reset;
Removes source code modules.procedure ResetCompilation;