-
Notifications
You must be signed in to change notification settings - Fork 3
/
expression.pas
305 lines (260 loc) · 6.82 KB
/
expression.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
{
Crafting Interpreters
https://craftinginterpreters.com/
}
unit expression;
{$ifdef FPC}
{$mode delphi}
{$interfaces corba}
{$endif}
{$IFOPT D+}
{$DEFINE DEBUG}
{$ENDIF}
//{$DEFINE TRACE}
interface
uses
Classes, SysUtils
, token;
type
IExpressionVisitor = interface;
TExpression = class
abstract
public
function Accept(ev: IExpressionVisitor): TObject; virtual; abstract;
end;
{ TLiteralExpression }
TLiteralExpression = class(TExpression)
private
FValue: TObject;
public
constructor Create(v: TObject);
destructor Destroy; override;
function Accept(ev: IExpressionVisitor): TObject; override;
property value: TObject read FValue;
end;
{ TUnaryExpression }
TUnaryExpression = class(TExpression)
private
FOp: TToken;
FRight: TExpression;
public
constructor Create(op: TToken; right: TExpression);
destructor Destroy; override;
function Accept(ev: IExpressionVisitor): TObject; override;
property op: TToken read FOp;
property right: TExpression read FRight;
end;
{ TBinaryExpression }
TBinaryExpression = class(TExpression)
private
FOp: TToken;
FLeft, FRight: TExpression;
public
constructor Create(op: TToken; left, right: TExpression);
destructor Destroy; override;
function Accept(ev: IExpressionVisitor): TObject; override;
property op: TToken read FOp;
property left: TExpression read FLeft;
property right: TExpression read FRight;
end;
{ TLogicalExpression }
TLogicalExpression = class(TBinaryExpression)
public
function Accept(ev: IExpressionVisitor): TObject; override;
end;
{ TGroupingExpression }
TGroupingExpression = class(TExpression)
private
FExpr: TExpression;
public
constructor Create(expr: TExpression);
destructor Destroy; override;
function Accept(ev: IExpressionVisitor): TObject; override;
property expr: TExpression read FExpr;
end;
{ TVariableExpression }
TVariableExpression = class(TExpression)
private
// TO DO Rename
FVarName: TToken;
public
constructor Create(varName: TToken);
destructor Destroy; override;
function Accept(ev: IExpressionVisitor): TObject; override;
property varName: TToken read FVarName;
end;
{ TAssignmentExpression }
TAssignmentExpression = class(TExpression)
private
FVarName: TToken;
// TO DO Rename
FValue: TExpression;
public
constructor Create(varName: TToken; val: TExpression);
destructor Destroy; override;
function Accept(ev: IExpressionVisitor): TObject; override;
property varName: TToken read FVarName;
property value: TExpression read FValue;
end;
IExpressionVisitor = interface
['{8A3D7767-D1C5-49F5-BC15-F6C975FBAC67}']
function VisitLit(expr: TLiteralExpression): TObject;
function VisitUn(expr: TUnaryExpression): TObject;
function VisitBin(expr: TBinaryExpression): TObject;
function VisitLogic(expr: TLogicalExpression): TObject;
function VisitGroup(expr: TGroupingExpression): TObject;
function VisitVar(expr: TVariableExpression): TObject;
function VisitAssign(expr: TAssignmentExpression): TObject;
end;
implementation
{$IFDEF TRACE}
uses ptypes;
(* ObjToStr *)
{$ENDIF}
constructor TLiteralExpression.Create(v: TObject);
begin
self.FValue := v;
end;
destructor TLiteralExpression.Destroy;
begin
//FreeAndNil(self.FValue);
inherited Destroy;
end;
function TLiteralExpression.Accept(ev: IExpressionVisitor): TObject;
{$IFDEF TRACE}
const
__currMethodName = 'TLiteralExpression.Accept';
{$ENDIF}
begin
{$IFDEF TRACE}
Writeln(Format('[TRACE] (%s) Accepting literal expression: %s',
[__currMethodName, ObjToStr(FValue)]));
{$ENDIF}
// CLONE HERE ?
Result := ev.VisitLit(self);
end;
constructor TUnaryExpression.Create(op: TToken; right: TExpression);
begin
self.FOp := op;
self.FRight := right;
end;
destructor TUnaryExpression.Destroy;
begin
//FreeAndNil(self.FOp);
FreeAndNil(self.FRight);
inherited Destroy;
end;
function TUnaryExpression.Accept(ev: IExpressionVisitor): TObject;
{$IFDEF TRACE}
const
__currMethodName = 'TUnaryExpression.Accept';
{$ENDIF}
begin
{$IFDEF TRACE}
Writeln(Format('[TRACE] (%s) Accepting unary expression: %s',
[__currMethodName, self.FOp.lexeme]));
{$ENDIF}
Result := ev.VisitUn(self);
end;
constructor TBinaryExpression.Create(op: TToken; left, right: TExpression);
begin
self.FOp := op;
self.FLeft := left;
self.FRight := right;
end;
destructor TBinaryExpression.Destroy;
begin
//FreeAndNil(self.FOp);
FreeAndNil(self.FLeft);
FreeAndNil(self.FRight);
inherited Destroy;
end;
function TBinaryExpression.Accept(ev: IExpressionVisitor): TObject;
{$IFDEF TRACE}
const
__currMethodName = 'TBinaryExpression.Accept';
{$ENDIF}
begin
{$IFDEF TRACE}
WriteLn(Format('[TRACE] (%s) Accepting binary expression: %s',
[__currMethodName, self.FOp.lexeme]));
{$ENDIF}
Result := ev.VisitBin(self);
end;
function TLogicalExpression.Accept(ev: IExpressionVisitor): TObject;
{$IFDEF TRACE}
const
__currMethodName = 'TLogicalExpression.Accept';
{$ENDIF}
begin
{$IFDEF TRACE}
WriteLn(Format('[TRACE] (%s) Accepting logical expression: %s',
[__currMethodName, self.FOp.lexeme]));
{$ENDIF}
Result := ev.VisitLogic(self);
end;
constructor TGroupingExpression.Create(expr: TExpression);
begin
self.FExpr := expr;
end;
destructor TGroupingExpression.Destroy;
begin
FreeAndNil(self.FExpr);
inherited Destroy;
end;
function TGroupingExpression.Accept(ev: IExpressionVisitor): TObject;
{$IFDEF TRACE}
const
__currMethodName = 'TGroupingExpression.Accept';
{$ENDIF}
begin
{$IFDEF TRACE}
WriteLn(Format('[TRACE] (%s) Accepting grouping expression', [__currMethodName]));
{$ENDIF}
Result := ev.VisitGroup(self);
end;
constructor TVariableExpression.Create(varName: TToken);
begin
self.FVarName := varName;
end;
destructor TVariableExpression.Destroy;
begin
//FreeAndNil(self.FVarName);
inherited Destroy;
end;
function TVariableExpression.Accept(ev: IExpressionVisitor): TObject;
{$IFDEF TRACE}
const
__currMethodName = 'TVariableExpression.Accept';
{$ENDIF}
begin
{$IFDEF TRACE}
WriteLn(Format('[TRACE] (%s) Accepting variable expression: %s',
[__currMethodName, self.FVarName.lexeme]));
{$ENDIF}
Result := ev.VisitVar(self);
end;
constructor TAssignmentExpression.Create(varName: TToken; val: TExpression);
begin
self.FVarName := varName;
self.FValue := val;
end;
destructor TAssignmentExpression.Destroy;
begin
//FreeAndNil(self.FVarName);
FreeAndNil(self.FValue);
inherited Destroy;
end;
function TAssignmentExpression.Accept(ev: IExpressionVisitor): TObject;
{$IFDEF TRACE}
const
__currMethodName = 'TAssignmentExpression.Accept';
{$ENDIF}
begin
{$IFDEF TRACE}
WriteLn(Format('[TRACE] (%s) Accepting assignment expression: %s',
[__currMethodName, self.FVarName.lexeme]));
{$ENDIF}
Result := ev.VisitAssign(self);
end;
end.