-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAspenSyntaxTree.pas
More file actions
330 lines (267 loc) · 7.38 KB
/
AspenSyntaxTree.pas
File metadata and controls
330 lines (267 loc) · 7.38 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
unit AspenSyntaxTree;
interface
uses SysUtils, Generics.Collections;
type TSingleType = class (TObject)
protected
fValue: Single;
public
function Value: Single;
constructor Create(Value: Single);
end;
type
TAbstractNode = class;
TAbstractNodeChildrenEnumerator = class;
{ Implemented by the TAbstractNode class so that we can use Delphi's
for..in loop. We explicitly make the process type stronger by
requiring a new Add() method to be implemented; }
IAbstractNodeChildrenEnumerator = interface
['{4ACB6E5C-1835-4458-B83F-EC2EDF10A171}']
function GetCurrent: TAbstractNode;
function MoveNext: Boolean;
property Current: TAbstractNode read GetCurrent;
procedure Add(Element: TAbstractNode);
end;
TAbstractNodeChildrenEnumerator = class (TInterfacedObject,
IAbstractNodeChildrenEnumerator)
private
fIndex: Integer;
fRefList: TList<TAbstractNode>;
public
{ IAbstractNodeChildrenEnumerator }
function GetCurrent: TAbstractNode;
function MoveNext: Boolean;
property Current: TAbstractNode read GetCurrent;
procedure Add(Element: TAbstractNode);
constructor Create;
destructor Destroy; override;
end;
TAbstractNode = class abstract (TInterfacedObject)
protected
fParent: TAbstractNode;
public
function GetEnumerator: TAbstractNodeChildrenEnumerator;
procedure PrepareEnumerator(Enumerator: TAbstractNodeChildrenEnumerator); virtual; abstract;
function HasChildren: Boolean; virtual; abstract;
function SupportsChildren: Boolean; virtual; abstract;
function RequiredChildren: Integer; virtual; abstract;
function Count: Integer; virtual; abstract;
{ Returns an evaluated result not managed by this class! }
function Evaluate: TSingleType; virtual; abstract;
procedure Clear; virtual; abstract;
function HasParent: Boolean;
function Parent: TAbstractNode;
destructor Destroy; override;
end;
type TTerminalNode = class (TAbstractNode)
protected
fValue: TSingleType;
fValueIsManaged: Boolean;
public
procedure PrepareEnumerator(Enumerator: TAbstractNodeChildrenEnumerator); override;
function HasChildren: Boolean; override;
function SupportsChildren: Boolean; override;
function RequiredChildren: Integer; override;
function Count: Integer; override;
function Evaluate: TSingleType; override;
procedure Clear; override;
constructor Create(Value: TSingleType);
constructor CreateFromPrimitive(Value: Single);
destructor Destroy; override;
end;
type TBinaryNode = class abstract (TAbstractNode)
protected
fLeftNode: TAbstractNode;
fRightNode: TAbstractNode;
public
procedure PrepareEnumerator(Enumerator: TAbstractNodeChildrenEnumerator); override;
function HasChildren: Boolean; override;
function SupportsChildren: Boolean; override;
function RequiredChildren: Integer; override;
function Count: Integer; override;
function Evaluate: TSingleType; override;
procedure Clear; override;
function ComputeSpecifics(A, B: TSingleType): TSingleType; virtual; abstract;
procedure AddLeftNode(Node: TAbstractNode);
procedure AddRightNode(Node: TAbstractNode);
function LeftNode: TAbstractNode;
function RightNode: TAbstractNode;
constructor Create; overload;
constructor Create(Left, Right: TAbstractNode); overload;
end;
type TAspenEvaluationError = class (Exception);
implementation
function TSingleType.Value: Single;
begin
Result := fValue;
end;
constructor TSingleType.Create(Value: Single);
begin
fValue := Value;
end;
function TAbstractNodeChildrenEnumerator.GetCurrent: TAbstractNode;
begin
Result := fRefList[fIndex];
end;
function TAbstractNodeChildrenEnumerator.MoveNext: Boolean;
begin
Inc(fIndex);
Result := fIndex < fRefList.Count;
end;
procedure TAbstractNodeChildrenEnumerator.Add(Element: TAbstractNode);
begin
fRefList.Add(Element);
end;
constructor TAbstractNodeChildrenEnumerator.Create;
begin
fRefList := TList<TAbstractNode>.Create;
fIndex := -1;
end;
destructor TAbstractNodeChildrenEnumerator.Destroy;
begin
fRefList.Free;
end;
function TAbstractNode.GetEnumerator: TAbstractNodeChildrenEnumerator;
var
Enumerator: TAbstractNodeChildrenEnumerator;
begin
Enumerator := TAbstractNodeChildrenEnumerator.Create;
PrepareEnumerator(Enumerator);
Result := Enumerator;
end;
function TAbstractNode.HasParent: Boolean;
begin
Result := nil <> fParent;
end;
function TAbstractNode.Parent: TAbstractNode;
begin
Result := fParent;
end;
destructor TAbstractNode.Destroy;
begin
Clear;
inherited;
end;
procedure TTerminalNode.PrepareEnumerator(Enumerator: TAbstractNodeChildrenEnumerator);
begin
;
end;
function TTerminalNode.HasChildren: Boolean;
begin
Result := False;
end;
function TTerminalNode.SupportsChildren: Boolean;
begin
Result := False;
end;
function TTerminalNode.RequiredChildren: Integer;
begin
Result := 0;
end;
function TTerminalNode.Count: Integer;
begin
Result := 0;
end;
function TTerminalNode.Evaluate: TSingleType;
begin
Result := TSingleType.Create(fValue.Value);
end;
procedure TTerminalNode.Clear;
begin
;
end;
constructor TTerminalNode.Create(Value: TSingleType);
begin
fValue := Value;
end;
constructor TTerminalNode.CreateFromPrimitive(Value: Single);
var
NewValue: TSingleType;
begin
NewValue := TSingleType.Create(Value);
fValue := NewValue;
fValueIsManaged := True;
end;
destructor TTerminalNode.Destroy;
begin
if (fValueIsManaged) then
fValue.Free;
inherited;
end;
procedure TBinaryNode.PrepareEnumerator(Enumerator: TAbstractNodeChildrenEnumerator);
begin
if Assigned(fLeftNode) then
Enumerator.Add(fLeftNode);
if Assigned(fRightNode) then
Enumerator.Add(fRightNode);
end;
function TBinaryNode.HasChildren: Boolean;
begin
Result := (nil <> fLeftNode) or (nil <> fRightNode);
end;
function TBinaryNode.SupportsChildren: Boolean;
begin
Result := True;
end;
function TBinaryNode.RequiredChildren: Integer;
begin
Result := 2;
end;
function TBinaryNode.Count: Integer;
begin
Result := Ord(nil <> fLeftNode) + Ord(nil <> fRightNode);
end;
function TBinaryNode.Evaluate: TSingleType;
var
ValueA, ValueB: TSingleType;
Evaluation: TSingleType;
begin
if (Self.Count = Self.RequiredChildren) then begin
{ It's very important to note that all Evaluation()
calls return objects that we are responsible for freeing }
try
ValueA := fLeftNode.Evaluate;
ValueB := fRightNode.Evaluate;
Evaluation := ComputeSpecifics(ValueA, ValueB);
Result := Evaluation;
finally
ValueA.Free;
ValueB.Free;
end;
end else
raise TAspenEvaluationError.Create
('Not enough nodes are present to evaluate.');
end;
procedure TBinaryNode.Clear;
begin
if Assigned(fLeftNode) then
FreeAndNil(fLeftNode);
if Assigned(fRightNode) then
FreeAndNil(fRightNode);
end;
procedure TBinaryNode.AddLeftNode(Node: TAbstractNode);
begin
fLeftNode := Node;
end;
procedure TBinaryNode.AddRightNode(Node: TAbstractNode);
begin
fRightNode := Node;
end;
function TBinaryNode.LeftNode: TAbstractNode;
begin
Result := fLeftNode;
end;
function TBinaryNode.RightNode: TAbstractNode;
begin
Result := fRightNode;
end;
constructor TBinaryNode.Create;
begin
fLeftNode := nil;
fRightNode := nil;
end;
constructor TBinaryNode.Create(Left, Right: TAbstractNode);
begin
fLeftNode := Left;
fRightNode := Right;
end;
end.