forked from jdehaan2014/GearLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udict.pas
316 lines (265 loc) · 7.75 KB
/
udict.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
unit uDict;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, uAST, uError, uToken, Variants, uInterpreter,
uCallable, uDictIntf, uMembers, uArrayIntf;
type
TDictClass = class(TInterfacedObject, IDictionary)
private
Ident: TIdent;
Elements: TDictElements;
Methods: TMethodTable;
Values: TValueTable;
function FindMethod(Instance: IDictInstance; const AName: String): ICallable;
function FindValuable(const AName: String): IValuable;
public
constructor Create(AIdent: TIdent; AElements: TDictElements; Members: TMembers);
function Call(Token: TToken; Interpreter: TInterpreter; ArgList: TArgList): Variant;
function toString: string; override;
procedure ExtendWith(Members: TMembers);
end;
TDictInstance = class(TInterfacedObject, IDictInstance)
private
DictClass: TDictClass;
FElements: TDictElements;
function getCount: LongInt;
function getElements: TDictElements;
function getItem(i: integer): Variant;
procedure setItem(i: integer; AValue: Variant);
public
property Elements: TDictElements read getElements;
property Count: LongInt read getCount;
property Items[i:integer]: Variant read getItem write setItem; default;
constructor Create(ADictClass: TDictClass);
destructor Destroy; override;
function toString: string; override;
function GetMember(Ident: TIdent): Variant;
function TypeName: String;
function Get(Key: Variant; Token: TToken): Variant;
procedure Put(Key: Variant; Value: Variant; Token: TToken);
// standard functions
procedure Add(const AKey, AValue: Variant);
procedure AddList(constref AList: TDictElements);
function ContainsKey(const AKey: Variant; Token: TToken): Boolean;
function ContainsValue(const AValue: Variant; Token: TToken): Boolean;
function ValueOf(const AKey: Variant; Token: TToken): Variant;
function KeyOf(const AValue: Variant; Token: TToken): Variant;
procedure Delete(const AKey: Variant; Token: TToken);
procedure Sort; // sort on Keys
procedure setSorted(Sorted: Boolean=False);
procedure Clear;
function Keys: IArrayInstance;
function Values: IArrayInstance;
end;
implementation
uses uFunc, uVariantSupport, uLanguage, uArray;
{ TDictClass }
constructor TDictClass.Create
(AIdent: TIdent; AElements: TDictElements; Members: TMembers);
begin
Ident := AIdent;
Elements := AElements;
Methods := Members.Methods;
Values := Members.Values;
end;
function TDictClass.Call
(Token: TToken; Interpreter: TInterpreter; ArgList: TArgList): Variant;
var
Instance: IDictInstance;
begin
Instance := IDictInstance(TDictInstance.Create(Self));
Instance.Elements.Assign(Self.Elements);
Result := Instance;
end;
function TDictClass.toString: string;
begin
Result := Ident.Text;
end;
procedure TDictClass.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;
function TDictClass.FindMethod(Instance: IDictInstance; const AName: String): ICallable;
begin
Result := Nil;
if Methods.ContainsKey(AName) then
Result := (ICallable(Methods[AName]) as TFunc).Bind(Instance);
end;
function TDictClass.FindValuable(const AName: String): IValuable;
begin
Result := Nil;
if Values.ContainsKey(AName) then
Result := Values[AName]
end;
{ TDictInstance }
function TDictInstance.getCount: LongInt;
begin
Result := FElements.Count;
end;
function TDictInstance.getElements: TDictElements;
begin
Result := FElements;
end;
function TDictInstance.getItem(i: integer): Variant;
begin
Result := FElements[i];
end;
procedure TDictInstance.setItem(i: integer; AValue: Variant);
begin
FElements[i] := AValue;
end;
constructor TDictInstance.Create(ADictClass: TDictClass);
begin
DictClass := ADictClass;
FElements := TDictElements.Create;
end;
destructor TDictInstance.Destroy;
begin
FElements.Free;
inherited Destroy;
end;
function TDictInstance.toString: string;
var
i: Integer;
function getStr(Value: Variant): String;
begin
Result := Value.toString;
if VarType(Value) = varString then
Result := QuotedStr(Result);
end;
begin
Result := '[';
if FElements.Count > 0 then begin
for i := 0 to FElements.Count-2 do
Result += getStr(FElements.Keys[i]) + ': ' +
getStr(FElements.Data[i]) + ', ';
Result += getStr(FElements.Keys[FElements.Count-1]) + ': ' +
getStr(FElements.Data[FElements.Count-1]);
end
else Result += ':';
Result += ']';
end;
function TDictInstance.GetMember(Ident: TIdent): Variant;
var
Method: ICallable;
Valuable: IValuable;
begin
Method := DictClass.FindMethod(IDictInstance(Self), Ident.Text);
if Method <> Nil then
Exit(Method);
Valuable := DictClass.FindValuable(Ident.Text);
if Valuable <> Nil then
Exit(Valuable);
Raise ERuntimeError.Create(Ident.Token,
'Undefined dictionary member "' + Ident.Text + '".');
end;
function TDictInstance.TypeName: String;
begin
Result := DictClass.Ident.Text;
end;
function TDictInstance.Get(Key: Variant; Token: TToken): Variant;
begin
if ContainsKey(Key, Token) then
Result := FElements[Key]
else
Raise ERuntimeError.Create(Token,
'Key ('+ Key.toString + ') not found.');
end;
procedure TDictInstance.Put(Key: Variant; Value: Variant; Token: TToken);
begin
try
FElements[Key] := Value
except
Raise ERuntimeError.Create(Token,
'Key:Value ('+ Key.toString + ', ' + Value.toString + ') wrong.');
end;
end;
procedure TDictInstance.Add(const AKey, AValue: Variant);
begin
FElements.Add(AKey, AValue);
end;
procedure TDictInstance.AddList(constref AList: TDictElements);
begin
FElements.AddList(AList);
end;
function TDictInstance.ContainsKey(const AKey: Variant; Token: TToken): Boolean;
begin
Result := FElements.FindKey(AKey, Token) >= 0;
end;
function TDictInstance.ContainsValue(const AValue: Variant; Token: TToken): Boolean;
begin
Result := FElements.FindValue(AValue, Token) >= 0;
end;
function TDictInstance.ValueOf(const AKey: Variant; Token: TToken): Variant;
var
i: SizeInt;
begin
i := FElements.FindKey(AKey, Token);
if i>=0 then
Result := FElements.Data[i]
else
Result := Unassigned;
end;
function TDictInstance.KeyOf(const AValue: Variant; Token: TToken): Variant;
var
i: Integer;
begin
i := FElements.FindValue(AValue, Token);
if i >= 0 then
Result := FElements.Keys[i]
else
Result := Unassigned;
end;
procedure TDictInstance.Delete(const AKey: Variant; Token: TToken);
var
i: Integer;
begin
i := FElements.FindKey(AKey, Token);
if i>=0 then
FElements.Delete(i)
else
Raise ERuntimeError.Create(Token, 'Key to delete not found.');
end;
procedure TDictInstance.Sort;
begin
FElements.Sort;
end;
procedure TDictInstance.setSorted(Sorted: Boolean);
begin
FElements.Sorted := Sorted;
end;
procedure TDictInstance.Clear;
begin
FElements.Clear;
end;
function TDictInstance.Keys: IArrayInstance;
var
ArrayType: IArrayable;
Instance: IArrayInstance;
i: Integer;
begin
ArrayType := IArrayable(Language.Interpreter.Globals['Array']);
Instance := IArrayInstance(TArrayInstance.Create(ArrayType as TArrayClass));
for i := 0 to Elements.Count-1 do
Instance.Elements.Add(Elements.Keys[i]);
Result := Instance;
end;
function TDictInstance.Values: IArrayInstance;
var
ArrayType: IArrayable;
Instance: IArrayInstance;
i: Integer;
begin
ArrayType := IArrayable(Language.Interpreter.Globals['Array']);
Instance := IArrayInstance(TArrayInstance.Create(ArrayType as TArrayClass));
for i := 0 to Elements.Count-1 do
Instance.Elements.Add(Elements.Data[i]);
Result := Instance;
end;
end.