-
Notifications
You must be signed in to change notification settings - Fork 28
/
lputils.pas
403 lines (350 loc) · 16.1 KB
/
lputils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
{
Author: Niels A.D
Project: Lape (https://github.com/nielsAD/lape)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
Lape utilities/extra's.
}
unit lputils;
{$I lape.inc}
interface
uses
lptypes, lpvartypes, lpcompiler;
type
PSInit = (psiSettings, psiTypeAlias, psiMagicMethod, psiFunctionWrappers, psiExceptions, psiUselessTypes);
PSInitSet = set of PSInit;
EExposeGlobalsMethod = (egmName, egmPtr, egmValue, egmInvoke);
EExposeGlobalsMethods = set of EExposeGlobalsMethod;
TTraverseCallback = procedure(v: TLapeGlobalVar; AName: lpString; Compiler: TLapeCompilerBase; var Arg);
procedure InitializePascalScriptBasics(Compiler: TLapeCompiler; Initialize: PSInitSet = [psiSettings, psiTypeAlias, psiMagicMethod, psiFunctionWrappers, psiExceptions]);
procedure TraverseGlobals(Compiler: TLapeCompilerBase; Callback: TTraverseCallback; var Arg; BaseName: lpString = ''; Decls: TLapeDeclarationList = nil);
procedure ExposeGlobals(Compiler: TLapeCompiler; Methods: EExposeGlobalsMethods = [egmName, egmPtr, egmValue, egmInvoke]); overload;
const
LapePascalScriptCompilerOptions = [lcoLooseSemicolon, lcoAutoInvoke];
implementation
uses
lpparser, lpeval,
SysUtils, Classes;
procedure InitializePascalScriptBasics(Compiler: TLapeCompiler; Initialize: PSInitSet = [psiSettings, psiTypeAlias, psiMagicMethod, psiFunctionWrappers, psiExceptions]);
begin
if (Compiler = nil) then
Exit;
with Compiler do
begin
if (psiSettings in Initialize) then
Compiler.Options := Compiler.Options + LapePascalScriptCompilerOptions;
if (psiMagicMethod in Initialize) then
begin
InternalMethodMap['GetArrayLength'] := InternalMethodMap['Length'];
InternalMethodMap['SetArrayLength'] := InternalMethodMap['SetLength'];
end;
if (psiTypeAlias in Initialize) then
begin
addGlobalType(getBaseType(DetermineIntType(SizeOf(Byte), False)).createCopy(), 'Byte');
addGlobalType(getBaseType(DetermineIntType(SizeOf(ShortInt), True)).createCopy(), 'ShortInt');
addGlobalType(getBaseType(DetermineIntType(SizeOf(Word), False)).createCopy(), 'Word');
addGlobalType(getBaseType(DetermineIntType(SizeOf(SmallInt), True)).createCopy(), 'SmallInt');
addGlobalType(getBaseType(DetermineIntType(SizeOf(LongWord), False)).createCopy(), 'LongWord');
addGlobalType(getBaseType(DetermineIntType(SizeOf(LongInt), True)).createCopy(), 'LongInt');
addGlobalType(getBaseType(DetermineIntType(SizeOf(Cardinal), False)).createCopy(), 'Cardinal');
addGlobalType(getBaseType(DetermineIntType(SizeOf(Integer), True)).createCopy(), 'Integer');
addGlobalType(getPointerType(ltChar, False).createCopy(), 'PChar');
end;
if (psiUselessTypes in Initialize) then
begin
addGlobalType(getBaseType({$IFDEF Lape_Unicode}ltUnicodeString{$ELSE}ltAnsiString{$ENDIF}).createCopy(), 'NativeString');
addGlobalType(getBaseType(ltString).createCopy(), 'AnyString');
addGlobalType(getBaseType(ltString).createCopy(), 'tbtString');
addGlobalType(getBaseType(ltPointer).createCopy(), '___Pointer');
addGlobalType('array of Variant', 'TVariantArray');
end;
if (psiFunctionWrappers in Initialize) then
addDelayedCode(
LapeDelayedFlags +
'function StrGet(var s: string; Index: SizeInt): Char; begin Result := s[Index]; end;' + LineEnding +
'function StrGet2(s: string; Index: SizeInt): Char; begin Result := s[Index]; end;' + LineEnding +
'procedure StrSet(c: Char; Index: SizeInt; var s: string); begin s[Index] := c; end;' + LineEnding +
'function WStrGet(var s: WideString; Index: SizeInt): WideChar; begin Result := s[Index]; end;' + LineEnding +
'function VarArrayGet(var s: Variant; Index: Int32): Variant; overload; begin Result := VarArrayGet(s, [Index]); end;' + LineEnding +
'procedure VarArraySet(c: Variant; Index: Int32; var s: Variant); overload; begin VarArraySet(s, c, [Index]); end;' + LineEnding +
'function PadZ(s: string; Len: SizeInt): string; begin Result := PadL(s, Len, '#39'0'#39'); end;' + LineEnding +
'function Replicate(c: Char; l: SizeInt): string; begin Result := StringOfChar(c, l); end;' + LineEnding +
'function Int64ToStr(i: Int64): string; begin Result := IntToStr(i); end;' + LineEnding +
'function UInt64ToStr(i: UInt64): string; begin Result := IntToStr(i); end;'
, '!addDelayedPSUseless');
if (psiExceptions in Initialize) then
begin
addGlobalType('(' +
'erNoError, erCannotImport, erInvalidType, erInternalError,' +
'erInvalidHeader, erInvalidOpcode, erInvalidOpcodeParameter,' +
'erNoMainProc, erOutOfGlobalVarsRange, erOutOfProcRange, erOutOfRange,' +
'erOutOfStackRange, erTypeMismatch, erUnexpectedEof, erVersionError,' +
'erDivideByZero, erMathError, erCouldNotCallProc, erOutofRecordRange,' +
'erOutOfMemory, erException, erNullPointerException, erNullVariantError,' +
'erInterfaceNotSupported, erCustomError)',
'TIFException');
addDelayedCode(
LapeDelayedFlags +
'function ExceptionToString(Ex: TIFException; Param: string): string; begin ' +
'Result := ToString(Ex);' +
'if (Param <> '#39#39') then Result := Result + '#39'('#39' + Param + '#39')'#39';' +
'end;' + LineEnding +
'procedure RaiseException(Ex: TIFException; Param: string); overload; begin RaiseException(ExceptionToString(Ex, Param)); end;'
, '!addDelayedPSExceptions');
end;
end;
end;
procedure TraverseGlobals(Compiler: TLapeCompilerBase; Callback: TTraverseCallback; var Arg; BaseName: lpString = ''; Decls: TLapeDeclarationList = nil);
procedure TraverseBaseTypes;
var
BaseType: ELapeBaseType;
Typ: TLapeType;
begin
for BaseType := Succ(ltUnknown) to High(ELapeBaseType) do
begin
Typ := Compiler.getBaseType(BaseType);
if (Typ <> nil) then
TraverseGlobals(Compiler, Callback, Arg, Typ.Name, Typ.ManagedDeclarations);
end;
end;
var
n: lpString;
Decl: TLapeDeclaration;
begin
if (BaseName = 'System') then
Exit;
if (Decls = nil) then
if (Compiler = nil) then
Exit
else
begin
TraverseBaseTypes();
Decls := Compiler.GlobalDeclarations;
end;
for Decl in Decls.ExportToArray() do
begin
try
if (Decl.Name = '') then
if (Decl is TLapeGlobalVar) and (TLapeGlobalVar(Decl).VarType is TLapeType_Method) then
n := BaseName + '[' + lpString(IntToStr(Decls.IndexOf(Decl))) + ']'
else
Continue
else if (BaseName <> '') then
n := BaseName + '.' + Decl.Name
else
n := Decl.Name;
if (Decl is TLapeType) then
TraverseGlobals(Compiler, Callback, Arg, n, TLapeType(Decl).ManagedDeclarations)
else if (Decl is TLapeGlobalvar) then
with TLapeGlobalVar(Decl) do
begin
Callback(TLapeGlobalVar(Decl), n, Compiler, Arg);
if (VarType is TLapeType_Type) or (VarType is TLapeType_OverloadedMethod) then
TraverseGlobals(Compiler, Callback, Arg, n, VarType.ManagedDeclarations);
end;
except
{catch exception}
end;
end;
end;
procedure _ExposeGlobals_Invoke_Callback(v: TLapeGlobalVar; AName: lpString; Compiler: TLapeCompilerBase; var Arg);
var
VariantType: TLapeType;
function ParamsCompatible(Params: TLapeParameterList): Boolean;
var
i: Integer;
begin
Result := True;
for i := 0 to Params.Count - 1 do
if (Params[i].VarType = nil) or (not (Params[i].ParType in Lape_ValParams)) then
Exit(False)
else if (not Params[i].VarType.CompatibleWith(VariantType)) then
Exit(False);
end;
var
i: Integer;
Temp: lpString;
begin
Temp := '';
if (AName = '') or (AName[1] = '!') or (not v.HasType()) or
(not (v.VarType is TLapeType_Method)) or MethodOfObject(v.VarType)
then
Exit;
AName := LapeCase(AName);
VariantType := Compiler.getBaseType(ltVariant);
with TLapeType_Method(v.VarType) do
begin
if (not ParamsCompatible(Params)) then
Exit;
Temp := #39 + AName + #39': begin ' +
'Assert(ParamsLen = ' + lpString(IntToStr(Params.Count)) + ', ErrParamCount); ';
if (Res <> nil) and VariantType.CompatibleWith(Res) then
Temp := Temp + 'Result := ';
Temp := Temp + AName + '(';
for i := 0 to Params.Count - 1 do
begin
if (i > 0) then
Temp := Temp + ', ';
Temp := Temp + 'Params[' + lpString(IntToStr(i)) + ']';
end;
Temp := Temp + '); end';
end;
TLapeStringList(Arg).Add(Temp);
end;
procedure _ExposeGlobals_GetPtr_Callback(v: TLapeGlobalVar; AName: lpString; Compiler: TLapeCompilerBase; var Arg);
var
Temp: lpString;
begin
if (AName = '') or (AName[1] = '!') or (not v.HasType()) then
Exit
else if (v.VarType is TLapeType_Method) and (TLapeType_Method(v.VarType).MethodDef = mdProperty) then
Exit
else if (v.VarType is TLapeType_Method) then
Temp := ''
else if (v.VarType.EvalRes(op_Addr) <> nil) then
Temp := '@'
else
Exit;
AName := LapeCase(AName);
TLapeStringList(Arg).Add(#39 + AName + #39': Result := ' + Temp + AName);
end;
procedure _ExposeGlobals_GetName_Callback(v: TLapeGlobalVar; AName: lpString; Compiler: TLapeCompilerBase; var Arg);
var
Temp: lpString;
begin
if (AName = '') or (AName[1] = '!') or (not v.HasType()) then
Exit
else if (v.VarType is TLapeType_Method) and (TLapeType_Method(v.VarType).MethodDef = mdProperty) then
Exit
else if (v.VarType is TLapeType_Method) then
Temp := 'ConstPointer(' + AName + ')'
else if (v.VarType.EvalRes(op_Addr) <> nil) then
Temp := '@' + AName
else
Exit;
TLapeStringList(Arg).Add(Temp + ': Result := '#39 + AName + #39);
end;
procedure _ExposeGlobals_GetVal_Callback(v: TLapeGlobalVar; AName: lpString; Compiler: TLapeCompilerBase; var Arg);
begin
if (AName = '') or (AName[1] = '!') or (not v.HasType()) or
(not v.Readable) or (not Compiler.getBaseType(ltVariant).CompatibleWith(v.VarType))
then
Exit;
AName := LapeCase(AName);
TLapeStringList(Arg).Add(#39 + AName + #39': Result := ' + AName);
end;
function TraverseGlobals_String(Compiler: TLapeCompiler; Callback: TTraverseCallback): lpString;
var
Builder: TLapeStringList;
begin
Builder := TLapeStringList.Create('', dupIgnore, True, False);
try
TraverseGlobals(Compiler, Callback, Builder);
Builder.Add('');
Result := Builder.Implode(';' + LineEnding);
finally
Builder.Free();
end;
end;
procedure _ExposeGlobals_AddInvoke(Compiler: TLapeCompiler);
begin
Compiler.addDelayedCode(
LapeDelayedFlags + LineEnding +
'function VariantInvoke(Name: string; Params: array of Variant = []): Variant; override;' + LineEnding +
'const ErrParamCount = '#39'Parameter count mismatch'#39';' + LineEnding +
'var ParamsLen: SizeInt := Length(Params);' + LineEnding +
'begin' + LineEnding +
' Result := Unassigned;' + LineEnding +
{$IFDEF Lape_CaseSensitive}
' case Name of ' + LineEnding +
{$ELSE}
' case LowerCase(Name) of ' + LineEnding +
{$ENDIF}
' ' + TraverseGlobals_String(Compiler, @_ExposeGlobals_Invoke_Callback) + LineEnding +
' else raise '#39'Cannot invoke "'#39' + Name + '#39'" using VariantInvoke'#39';' + LineEnding +
' end;' + LineEnding +
'end;',
'!VariantInvoke');
end;
procedure _ExposeGlobals_AddPtr(Compiler: TLapeCompiler);
begin
Compiler.addDelayedCode(
LapeDelayedFlags + LineEnding +
'function GetGlobalPtr(Name: string): ConstPointer; override;' + LineEnding +
'begin' + LineEnding +
' Result := nil;' + LineEnding +
{$IFDEF Lape_CaseSensitive}
' case Name of ' + LineEnding +
{$ELSE}
' case LowerCase(Name) of ' + LineEnding +
{$ENDIF}
' ' + TraverseGlobals_String(Compiler, @_ExposeGlobals_GetPtr_Callback) + LineEnding +
' end;' + LineEnding +
'end;',
'!GetGlobalPtr'
);
end;
procedure _ExposeGlobals_AddGlobalVal(Compiler: TLapeCompiler);
begin
Compiler.addDelayedCode(
LapeDelayedFlags + LineEnding +
'function GetGlobal(Name: string): Variant; override;' + LineEnding +
'begin' + LineEnding +
' Result := Unassigned;' + LineEnding +
{$IFDEF Lape_CaseSensitive}
' case Name of ' + LineEnding +
{$ELSE}
' case LowerCase(Name) of ' + LineEnding +
{$ENDIF}
' ' + TraverseGlobals_String(Compiler, @_ExposeGlobals_GetVal_Callback) + LineEnding +
' else raise '#39'Cannot get value of "'#39' + Name + '#39'" using GetGlobal'#39';' + LineEnding +
' end;' + LineEnding +
'end;',
'!GetGlobalVal'
);
end;
procedure _ExposeGlobals_AddGlobalName(Compiler: TLapeCompiler);
begin
Compiler.addDelayedCode(
LapeDelayedFlags + LineEnding +
'function GetGlobalName(Ptr: ConstPointer): string; override;' + LineEnding +
'begin' + LineEnding +
' Result := '#39#39';' + LineEnding +
' case Ptr of ' + LineEnding +
' ' + TraverseGlobals_String(Compiler, @_ExposeGlobals_GetName_Callback) + LineEnding +
' end;' + LineEnding +
'end;',
'!GetGlobalName'
);
end;
procedure ExposeGlobals(Compiler: TLapeCompiler; Methods: EExposeGlobalsMethods);
var
Method: EExposeGlobalsMethod;
begin
if (Compiler = nil) then
Exit;
for Method in Methods do
case Method of
egmInvoke:
begin
Compiler.addDelayedCode('{$H-} function VariantInvoke(Name: string; Params: array of Variant = []): Variant; begin end;', '!VariantInvoke');
Compiler.AfterParsing.AddProc(@_ExposeGlobals_AddInvoke);
end;
egmPtr:
begin
Compiler.addDelayedCode('{$H-} function GetGlobalPtr(Name: string): ConstPointer; begin end;', '!GetGlobalPtr');
Compiler.AfterParsing.AddProc(@_ExposeGlobals_AddPtr);
end;
egmValue:
begin
Compiler.addDelayedCode('{$H-} function GetGlobal(Name: string): Variant; begin end;', '!GetGlobalVal');
Compiler.AfterParsing.AddProc(@_ExposeGlobals_AddGlobalVal);
end;
egmName:
begin
Compiler.addDelayedCode('{$H-} function GetGlobalName(Ptr: ConstPointer): string; begin end;', '!GetGlobalName');
Compiler.AfterParsing.AddProc(@_ExposeGlobals_AddGlobalName);
end;
end;
Compiler.addBaseDefine('Lape_ExposeGlobals');
end;
end.