forked from jdehaan2014/GearLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuarray.pas
325 lines (280 loc) · 8.54 KB
/
uarray.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
unit uArray;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, uAST, uError, uToken, Variants, uClass, uInterpreter,
uCallable, uArrayIntf, uMembers;
type
TArrayClass = class(TInterfacedObject, IArrayable)
private
Ident: TIdent;
Elements: TArrayElements;
Methods: TMethodTable;
Values: TValueTable;
function FindMethod(Instance: IArrayInstance; const AName: String): ICallable;
function FindValuable(const AName: String): IValuable;
public
constructor Create(AIdent: TIdent; AElements: TArrayElements;
Members: TMembers);
function Call(Token: TToken; Interpreter: TInterpreter;
ArgList: TArgList): Variant;
function toString: string; override;
procedure ExtendWith(Members: TMembers);
end;
TArrayInstance = class(TInterfacedObject, IArrayInstance)
private
ArrayClass: TArrayClass;
FElements: TArrayElements;
function getCount: LongInt;
function getElements: TArrayElements;
function getItem(i: integer): Variant;
procedure setItem(i: integer; AValue: Variant);
public
property Elements: TArrayElements read getElements;
property Count: LongInt read getCount;
property Items[i:integer]: Variant read getItem write setItem; default;
constructor Create(AnArrayClass: TArrayClass);
destructor Destroy; override;
function toString: string; override;
function GetMember(Ident: TIdent): Variant;
function getTypeName: String;
function getArrayClass: IArrayable;
function Get(i: Integer; Token: TToken): Variant;
procedure Put(i: Integer; Value: Variant; Token: TToken);
// array standard functions
procedure Init(const ALength: Integer; const AFillChar: Variant);
procedure Add(const AValue: Variant);
procedure AddList(constref AList: TArrayElements);
procedure Insert(const AIndex: Integer; const AValue: Variant; Token: TToken);
procedure Delete(const AIndex: Integer; Token: TToken);
function Remove(const AValue: Variant; Token: TToken): Variant;
function Extract(const AValue: Variant; Token: TToken): Variant;
function Contains(const AValue: Variant; Token: TToken): Boolean;
function IndexOf(const AValue: Variant; Token: TToken): Variant;
function Retrieve(const AIndex: Integer; Token: TToken): Variant;
procedure Clear;
function First: Variant;
function Last: Variant;
end;
implementation
uses uFunc , uVariantSupport;
function TArrayClass.FindMethod(Instance: IArrayInstance; const AName: String
): ICallable;
begin
Result := Nil;
if Methods.ContainsKey(AName) then
Result := (ICallable(Methods[AName]) as TFunc).Bind(Instance);
end;
function TArrayClass.FindValuable(const AName: String): IValuable;
begin
Result := Nil;
if Values.ContainsKey(AName) then
Result := Values[AName]
end;
constructor TArrayClass.Create
(AIdent: TIdent; AElements: TArrayElements; Members: TMembers);
begin
Ident := AIdent;
Elements := AElements;
Methods := Members.Methods;
Values := Members.Values;
end;
function TArrayClass.Call(Token: TToken; Interpreter: TInterpreter; ArgList: TArgList
): Variant;
var
Instance: IArrayInstance;
i: Integer;
begin
Instance := IArrayInstance(TArrayInstance.Create(Self));
if ArgList.Count = 0 then
Instance.Elements.AddRange(Self.Elements)
else if ArgList.Count > 1 then
for i := 0 to ArgList.Count-1 do
Instance.Elements.Add(ArgList[i].Value)
else if VarSupports(ArgList[0].Value, IArrayInstance) then
Instance.Elements.AddRange(IArrayInstance(ArgList[0].Value).Elements)
else if VarIsStr(ArgList[0].Value) then
for i := 1 to Length(String(ArgList[0].Value)) do
Instance.Elements.Add(String(ArgList[0].Value)[i])
else
Raise ERuntimeError.Create(Token,
'Array member of this type not allowed.');
Result := Instance;
end;
function TArrayClass.toString: string;
begin
Result := Ident.Text;
end;
procedure TArrayClass.ExtendWith(Members: TMembers);
var
Key: String;
begin
for Key in Members.Methods.Keys do
Methods.AddOrSetValue(Key, Members.Methods[Key]);
for Key in Members.Values.Keys do
Values.AddOrSetValue(Key, Members.Values[Key]);
end;
{ TArrayInstance }
function TArrayInstance.getCount: LongInt;
begin
Result := FElements.Count;
end;
function TArrayInstance.getElements: TArrayElements;
begin
Result := FElements;
end;
function TArrayInstance.getItem(i: integer): Variant;
begin
Result := FElements[i];
end;
procedure TArrayInstance.setItem(i: integer; AValue: Variant);
begin
FElements[i] := AValue;
end;
constructor TArrayInstance.Create(AnArrayClass: TArrayClass);
begin
ArrayClass := AnArrayClass;
FElements := TArrayElements.Create;
end;
destructor TArrayInstance.Destroy;
begin
FElements.Free;
inherited Destroy;
end;
function TArrayInstance.GetMember(Ident: TIdent): Variant;
var
Method: ICallable;
Valuable: IValuable;
begin
Method := ArrayClass.FindMethod(IArrayInstance(Self), Ident.Text);
if Method <> Nil then
Exit(Method);
Valuable := ArrayClass.FindValuable(Ident.Text);
if Valuable <> Nil then
Exit(Valuable);
Raise ERuntimeError.Create(Ident.Token,
'Undefined array member "' + Ident.Text + '".');
end;
function TArrayInstance.getTypeName: String;
begin
Result := ArrayClass.Ident.Text;
end;
function TArrayInstance.getArrayClass: IArrayable;
begin
Result := IArrayable(ArrayClass);
end;
function TArrayInstance.Get(i: Integer; Token: TToken): Variant;
begin
if getCount = 0 then
Result := Unassigned
else if (i >= 0) and (i < getCount) then
Result := FElements[i]
else
Raise ERuntimeError.Create(Token,
'Index ('+ IntToStr(i) + ') out of range ('+ '0,' + IntToStr(getCount-1) + ').');
end;
procedure TArrayInstance.Put(i: Integer; Value: Variant; Token: TToken);
begin
if (i >= 0) and (i < getCount) then
FElements[i] := Value
else
Raise ERuntimeError.Create(Token,
'Index ('+ IntToStr(i) + ') out of range ('+ '0,' + IntToStr(getCount-1) + ').');
end;
procedure TArrayInstance.Init(const ALength: Integer; const AFillChar: Variant);
var
i: Integer;
begin
for i := 1 to ALength do
FElements.Add(AFillChar);
end;
procedure TArrayInstance.Add(const AValue: Variant);
begin
FElements.Add(AValue);
end;
procedure TArrayInstance.AddList(constref AList: TArrayElements);
begin
FElements.AddRange(AList);
end;
procedure TArrayInstance.Insert(const AIndex: Integer; const AValue: Variant;
Token: TToken);
begin
if (AIndex >= 0) and (AIndex <= getCount) then
FElements.Insert(AIndex, AValue)
else
Raise ERuntimeError.Create(Token,
'Index ('+ IntToStr(AIndex) + ') out of range ('+ '0,' +
IntToStr(getCount) + ') during insert action.');
end;
procedure TArrayInstance.Delete(const AIndex: Integer; Token: TToken);
begin
if (AIndex >= 0) and (AIndex < getCount) then
FElements.Delete(AIndex)
else
Raise ERuntimeError.Create(Token,
'Index ('+ IntToStr(AIndex) + ') out of range ('+ '0,' +
IntToStr(getCount-1) + ') during delete action.');
end;
function TArrayInstance.Remove(const AValue: Variant; Token: TToken): Variant;
begin
Result := FElements.Find(AValue, Token);
if Result >= 0 then
FElements.Delete(Result)
else
Result := Unassigned;
end;
function TArrayInstance.Extract(const AValue: Variant; Token: TToken): Variant;
begin
if FElements.Find(AValue, Token) >= 0 then
Result := FElements.Extract(AValue)
else
Result := Unassigned;
end;
function TArrayInstance.Contains(const AValue: Variant; Token: TToken): Boolean;
begin
Result := FElements.Find(AValue, Token) >= 0;
end;
function TArrayInstance.IndexOf(const AValue: Variant; Token: TToken): Variant;
begin
Result := FElements.Find(AValue, Token);
if Result < 0 then
Result := Unassigned;
end;
function TArrayInstance.Retrieve(const AIndex: Integer; Token: TToken): Variant;
begin
if (AIndex >= 0) and (AIndex < getCount) then
Result := FElements[AIndex]
else
Result := Unassigned;
end;
procedure TArrayInstance.Clear;
begin
FElements.Clear;
end;
function TArrayInstance.First: Variant;
begin
if getCount>0 then
Result := FElements[0]
else
Result := Unassigned;
end;
function TArrayInstance.Last: Variant;
begin
if getCount>0 then
Result := FElements[Pred(getCount)]
else
Result := Unassigned;
end;
function TArrayInstance.toString: string;
var
i: Integer;
begin
Result := '[';
if FElements.Count > 0 then begin
for i := 0 to FElements.Count-2 do
Result += FElements[i].toString + ', ';
Result += FElements[FElements.Count-1].toString;
end;
Result += ']';
end;
end.