-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathi3d_scriptengine.pas
More file actions
560 lines (513 loc) · 12.6 KB
/
i3d_scriptengine.pas
File metadata and controls
560 lines (513 loc) · 12.6 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//------------------------------------------------------------------------------
//
// I3D_Viewer - Model Viewer for Speed Haste models
// Copyright (C) 2020-2021 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// Script Engine
//
//------------------------------------------------------------------------------
// Site : https://sourceforge.net/projects/i3dviewer/
//------------------------------------------------------------------------------
unit i3d_scriptengine;
interface
uses
SysUtils, Classes;
//
// JVAL
//
// Script Engine
//
// TScriptEngine
const
MAX_STRING_SIZE = 1024;
type
TScriptEngine = class
private
sc_String: PChar;
sc_Integer: integer;
sc_Single: single;
sc_Line: integer;
sc_End: boolean;
fNewLine: boolean;
fBracketLevel: integer;
fParenthesisLevel: integer;
ScriptBuffer: PChar;
ScriptPtr: PChar;
ScriptEndPtr: PChar;
StringBuffer: array [0..MAX_STRING_SIZE] of char;
ScriptSize: integer;
AlreadyGot: boolean;
ignonelist: TStringList;
protected
function fToken: string;
public
constructor Create(const tx: string); virtual;
destructor Destroy; override;
procedure AddIgnoreToken(const s: string);
procedure Clear;
procedure SetText(const tx: string); virtual;
procedure ScriptError(const err: string); overload;
procedure ScriptError(const Fmt: string; const Args: array of const); overload;
function GetString: boolean;
function GetStringEOL: string;
function GetStringEOLWithQuotes: string;
function GetTokensEOL: TStringList;
function GetStringEOLUnChanged: string;
procedure MustGetString;
procedure MustGetStringName(const name: string);
function GetInteger: boolean;
procedure MustGetInteger;
function GetFloat: boolean;
procedure MustGetFloat;
procedure UnGet;
function MatchString(const strs: TStringList): integer; overload;
function MatchString(const str: string): boolean; overload;
function MatchPosString(const str: string): boolean;
function MustMatchString(strs: TStringList): integer;
function Compare(const txt: string): boolean;
property _Integer: integer read sc_Integer;
property _Float: single read sc_Single;
property _String: string read fToken;
property _Finished: boolean read sc_End;
property _Line: integer read sc_Line;
property NewLine: boolean read fNewLine;
property BracketLevel: integer read fBracketLevel;
property ParenthesisLevel: integer read fParenthesisLevel;
end;
implementation
const
ASCII_QUOTE = '"';
ASCII_COMMENT1 = '/';
ASCII_COMMENT = $2F2F; // = '//'
// TScriptEngine
constructor TScriptEngine.Create(const tx: string);
begin
Inherited Create;
ignonelist := TStringList.Create;
fBracketLevel := 0;
fParenthesisLevel := 0;
fNewLine := false;
Clear;
SetText(tx);
end;
destructor TScriptEngine.Destroy;
begin
Clear;
ignonelist.Free;
Inherited;
end;
procedure TScriptEngine.AddIgnoreToken(const s: string);
begin
ignonelist.Add(UpperCase(s));
end;
function TScriptEngine.fToken: string;
begin
result := string(sc_String);
end;
procedure TScriptEngine.Clear;
begin
if ScriptSize > 0 then
FreeMem(ScriptBuffer, ScriptSize);
ScriptBuffer := nil;
ScriptSize := 0;
ScriptPtr := ScriptBuffer;
ScriptEndPtr := ScriptPtr + ScriptSize;
sc_Line := 1;
sc_End := false;
sc_String := @StringBuffer[0];
AlreadyGot := false;
end;
procedure TScriptEngine.SetText(const tx: string);
var
p: Pointer;
size: integer;
begin
size := Length(tx);
GetMem(p, size);
Move(tx[1], p^, size);
Clear;
ScriptBuffer := p;
ScriptSize := size;
ScriptPtr := ScriptBuffer;
ScriptEndPtr := ScriptPtr + ScriptSize;
sc_Line := 1;
sc_End := false;
sc_String := @StringBuffer[0];
AlreadyGot := false;
end;
procedure TScriptEngine.ScriptError(const err: string);
begin
// I_Warning('%s'#13#10, [err]);
end;
procedure TScriptEngine.ScriptError(const fmt: string; const Args: array of const);
var
s: string;
begin
s := Format(fmt, Args);
ScriptError(s);
end;
procedure TScriptEngine.MustGetString;
begin
if not GetString then
begin
ScriptError('TScriptEngine.MustGetString(): Missing string at Line %d', [sc_Line]);
end;
end;
procedure TScriptEngine.MustGetStringName(const name: string);
begin
MustGetString;
if not Compare(name) then
begin
ScriptError('TScriptEngine.MustGetStringName(): "%s" expected at Line %d', [name, sc_Line]);
end;
end;
function TScriptEngine.GetInteger: boolean;
var
code: integer;
begin
if GetString then
begin
val(string(sc_String), sc_Integer, code);
if code <> 0 then
begin
ScriptError(
'TScriptEngine.GetInteger(): Bad numeric constant "%s" at Line %d',
[sc_String, sc_Line]);
result := false;
exit;
end;
result := true;
end
else
result := true;
end;
procedure TScriptEngine.MustGetInteger;
begin
if not GetInteger then
begin
ScriptError('TScriptEngine.MustGetInteger(): Missing integer at Line %d', [sc_Line]);
end;
end;
function TScriptEngine.GetFloat: boolean;
var
code: integer;
i: integer;
str: string;
begin
if GetString then
begin
str := string(sc_String);
val(str, sc_Single, code);
if code <> 0 then
begin
for i := 1 to Length(str) do
if str[i] in ['.', ','] then
str[i] := '.';
val(str, sc_Single, code);
if code <> 0 then
begin
for i := 1 to Length(str) do
if str[i] = '.' then
str[i] := ',';
val(str, sc_Single, code);
if code <> 0 then
begin
ScriptError(
'TScriptEngine.GetFloat(): Bad numeric constant "%s" at Line %d',
[sc_String, sc_Line]);
result := false;
exit;
end;
end;
end;
result := true;
end
else
result := true;
end;
procedure TScriptEngine.MustGetFloat;
begin
if not GetFloat then
begin
ScriptError('TScriptEngine.MustGetFloat(): Missing single at Line %d', [sc_Line]);
end;
end;
procedure TScriptEngine.UnGet;
// Assumes there is a valid string in sc_String.
begin
AlreadyGot := true;
end;
function TScriptEngine.MatchString(const str: string): boolean;
begin
result := Compare(str);
end;
function TScriptEngine.MatchString(const strs: TStringList): integer;
// Returns the index of the first match to sc_String from the passed
// array of strings, or -1 if not found.
var
i: integer;
begin
for i := 0 to strs.Count - 1 do
begin
if Compare(strs.Strings[i]) then
begin
result := i;
exit;
end;
end;
result := -1;
end;
function TScriptEngine.MatchPosString(const str: string): boolean;
begin
result := Pos(UpperCase(str), UpperCase(string(sc_String))) > 0;
end;
function TScriptEngine.MustMatchString(strs: TStringList): integer;
var
i: integer;
begin
i := MatchString(strs);
if i = -1 then
begin
ScriptError('TScriptEngine.MustMatchString(): List'#13#10'%s'#13#10'expected at Line %d', [strs.Text, sc_Line]);
end;
result := i;
end;
function TScriptEngine.Compare(const txt: string): boolean;
begin
result := UpperCase(txt) = UpperCase(fToken);
end;
function TScriptEngine.GetString: boolean;
var
txt: PChar;
foundToken: boolean;
begin
if AlreadyGot then
begin
AlreadyGot := false;
result := true;
exit;
end;
fNewLine := false;
foundToken := false;
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
while not foundToken do
begin
while (ScriptPtr^ <= Chr(32)) or (ScriptPtr^ in ['{', '}', '(', ')', ',']) do
begin
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
if ScriptPtr^ = '{' then
inc(fBracketLevel)
else if ScriptPtr^ = '}' then
begin
dec(fBracketLevel);
if fBracketLevel < 0 then
begin
ScriptError('TScriptEngine.GetString(): Closing bracket "}" found at line %d without opening bracket "{"', [sc_Line]);
fBracketLevel := 0;
end;
end
else if ScriptPtr^ = '(' then
inc(fParenthesisLevel)
else if ScriptPtr^ = ')' then
begin
dec(fParenthesisLevel);
if fParenthesisLevel < 0 then
begin
ScriptError('TScriptEngine.GetString(): Closing parenthesis ")" found at line %d without opening parenthesis "("', [sc_Line]);
fParenthesisLevel := 0;
end;
end
else if ScriptPtr^ = Chr(10) then
begin
inc(sc_Line);
fNewLine := true;
end;
inc(ScriptPtr);
end;
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
if ScriptPtr^ <> ASCII_COMMENT1 then
begin // Found a token
foundToken := true;
end
else
begin // Skip comment
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
inc(ScriptPtr);
if ScriptPtr^ = ASCII_COMMENT1 then
begin
while (ScriptPtr^ <> Chr(13)) and (ScriptPtr^ <> Chr(10)) do
begin
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
inc(ScriptPtr);
end;
inc(sc_Line);
fNewLine := true;
end;
end;
end;
txt := sc_String;
if ScriptPtr^ = ASCII_QUOTE then
begin // Quoted string
inc(ScriptPtr);
while ScriptPtr^ <> ASCII_QUOTE do
begin
txt^ := ScriptPtr^;
inc(txt);
inc(ScriptPtr);
if (ScriptPtr = ScriptEndPtr) or
(txt = @sc_String[MAX_STRING_SIZE - 1]) then
break;
end;
inc(ScriptPtr);
end
else
begin // Normal string
while (ScriptPtr^ > Chr(32)) and (ScriptPtr < ScriptEndPtr) and
(PWord(ScriptPtr)^ <> ASCII_COMMENT) and (not (ScriptPtr^ in ['{', '}', '(', ')', ','])) do
begin
txt^ := ScriptPtr^;
inc(txt);
inc(ScriptPtr);
if (ScriptPtr = ScriptEndPtr) or
(txt = @sc_String[MAX_STRING_SIZE - 1]) then
break;
end;
end;
txt^ := Chr(0);
if ignonelist.IndexOf(UpperCase(string(sc_String))) < 0 then
result := true
else
Result := GetString;
end;
function TScriptEngine.GetStringEOL: string;
begin
result := '';
if not GetString then
exit;
if fNewLine then
begin
AlreadyGot := true;
exit;
end;
result := string(sc_string);
while not sc_End and not fNewLine do
begin
GetString;
if fNewLine then
begin
AlreadyGot := true;
exit;
end;
result := result + ' ' + string(sc_string);
end;
end;
function TScriptEngine.GetStringEOLWithQuotes: string;
begin
result := '';
if not GetString then
exit;
if fNewLine then
begin
AlreadyGot := true;
exit;
end;
result := '"' + string(sc_string) + '"';
while not sc_End and not fNewLine do
begin
GetString;
if fNewLine then
begin
AlreadyGot := true;
exit;
end;
result := result + ' ' + '"' + string(sc_string) + '"';
end;
end;
function TScriptEngine.GetTokensEOL: TStringList;
begin
result := TStringList.Create;
if not GetString then
exit;
if fNewLine then
begin
AlreadyGot := true;
exit;
end;
result.Add(sc_string);
while not sc_End and not fNewLine do
begin
GetString;
if fNewLine then
begin
AlreadyGot := true;
exit;
end;
result.Add(sc_string);
end;
end;
function TScriptEngine.GetStringEOLUnChanged: string;
begin
result := '';
if ScriptPtr < ScriptEndPtr then
if ScriptPtr^ = #10 then
begin
Inc(ScriptPtr);
if ScriptPtr < ScriptEndPtr then
begin
result := ScriptPtr^;
end;
end;
while ScriptPtr^ <> #10 do
begin
Inc(ScriptPtr);
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
exit;
end;
if not (ScriptPtr^ in [#10, #13]) then
result := result + ScriptPtr^;
end;
end;
end.