Repository navigation
Expand file tree
/
Copy pathFunDu.pas
More file actions
604 lines (512 loc) · 11.8 KB
/
Copy pathFunDu.pas
File metadata and controls
604 lines (512 loc) · 11.8 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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
unit FunDu;
{$I config.inc}
interface
uses
Classes, SysUtils;
const
SYNTAX_ERROR = 1;
SYMBOL_NOT_FOUND = 2;
type
TEnv = class;
TAtom = class;
TIntAtom = class;
TStrAtom = class;
TSymbolAtom = class;
TListAtom = class;
TNativeFunction = class;
TDict = class;
TDictItem = class;
TAtomType = (atStr, atSymbol, atInt, atFloat, atList, atDict, atNativeFunction);
TNativeFunctionPointer = function(env: TEnv; args: TListAtom): TAtom;
TError = record
Raised: Boolean;
Message: String;
Code: Integer;
end;
TEnv = class
private
_mainScope: TDict;
_error: Integer;
_errorMessage: String;
function LoadFile(fileName: String): String;
function BuildSyntaxTree(code: String): TAtom;
public
constructor Create();
procedure ExecFile(fileName: String);
function EvalCode(code: String): TAtom;
procedure ResetError;
procedure RaiseError(AError: Integer; AMessage: String);
property MainScope: TDict read _mainScope;
property Error: Integer read _error;
property ErrorMessage: String read _errorMessage;
end;
TAtom = class
private
_id: Integer;
_type: TAtomType;
public
property AtomType: TAtomType read _type;
property Id: Integer read _id;
function StrValue: String; virtual;
function Eval(env: TEnv): TAtom; virtual;
constructor Create(AType: TAtomType);
destructor Destroy; override;
function Copy: TAtom; virtual;
end;
TIntAtom = class(TAtom)
private
_int: Integer;
public
property IntValue: Integer read _int write _int;
function StrValue: String; override;
constructor Create(AIntValue: Integer);
constructor Create(AStrValue: String);
function Copy: TAtom; override;
end;
TStrAtom = class(TAtom)
private
_str: String;
public
function StrValue: String; override;
constructor Create(AType: TAtomType);
constructor Create(AStr: String);
function Copy: TAtom; override;
end;
TSymbolAtom = class(TStrAtom)
private
_hash: Longint;
public
constructor Create(AStr: String);
function Eval(env: TEnv): TAtom; override;
property HashValue: Longint read _hash;
end;
TListAtom = class(TAtom)
private
_list: TFPList; //TODO: replace with own implementation
public
property Atoms: TFPList read _list;
function StrValue: String; override;
procedure Add(AValue: TAtom);
function Get(index: Integer): TAtom;
function Eval(env: TEnv): TAtom; override;
constructor Create;
destructor Destroy; override;
function Copy: TAtom; override;
end;
TNativeFunction = class(TAtom)
private
_fun: TNativeFunctionPointer;
public
constructor Create(AFun: TNativeFunctionPointer);
function Exec(env: TEnv; args: TListAtom): TAtom;
function Copy: TAtom; override;
end;
TDict = class(TAtom)
private
_items: TFPList;
function FindItem(AName: String; AHash: Longint): TDictItem;
function FindItem(AName: String): TDictItem;
public
constructor Create;
procedure Put(AName: String; AValue: TAtom);
function Get(AName: String; AHash: Longint): TAtom;
function Get(AName: String): TAtom;
end;
TDictItem = class
private
_name: String;
_hash: Longint;
_value: TAtom;
public
constructor Create(AName: String; AValue: TAtom);
property Name: String read _name;
property Hash: Longint read _hash;
property Value: TAtom read _value write _value;
end;
{$IFDEF DEBUG_INFO}
var
Atoms: TFPList;
IdCounter: Integer = 0;
procedure ShowDebugInfo();
{$ENDIF}
implementation
uses
FunUtils, Parser;
{$IFDEF DEBUG_INFO}
procedure ShowDebugInfo();
var
i: Integer;
begin
writeln('---------------------------');
for i := 0 to Pred(Atoms.Count) do
with TAtom(Atoms.Items[i]) do
begin
writeln(' Type: ', AtomType, '; StrValue: ', StrValue, '; Id: ', Id);
end;
writeln('---------------------------');
end;
{$ENDIF}
{--== TEnv implementation ==--}
//TODO: move that thing to Lib
function TEnv.LoadFile(fileName: String): String;
var
f: TextFile;
b: Char;
begin
Result := '';
writeln('Using generic file loader');
AssignFile(f, fileName);
Reset(f);
repeat
Read(f, b);
Result := Result + b;
until EOF(f);
CloseFile(f);
end;
function TEnv.BuildSyntaxTree(code: String): TAtom;
var
stack: TStack;
root, current, newList: TListAtom;
begin
stack := TStack.Create;
root := TListAtom.Create;
root.Add(TSymbolAtom.Create('do'));
current := root;
with TParser.Create(code) do
begin
while not Finished do
begin
Step;
case TockenType of
tcBracketOpen:
begin
newList := TListAtom.Create;
current.Add(newList);
stack.Push(current);
current := newList;
end;
tcSymbol:
begin
TListAtom(current).Add(TSymbolAtom.Create(Tocken));
end;
tcString:
begin
TListAtom(current).Add(TStrAtom.Create(Tocken));
end;
tcBracketClose:
begin
current := TListAtom(stack.Pop);
if current = nil then
begin
RaiseError(SYNTAX_ERROR, 'Something wrong with brackets1!');
break;
end;
end;
end;
end;
if stack.pop <> nil then
RaiseError(SYNTAX_ERROR, 'Something really wrong with brackets2!');
writeln('Syntax tree: ', root.StrValue);
Result := TAtom(root);
Free;
end;
stack.Free;
end;
constructor TEnv.Create();
begin
writeln('Creating toy env');
_mainScope := TDict.Create;
ResetError;
end;
procedure TEnv.ResetError;
begin
_error := 0;
end;
procedure TEnv.RaiseError(AError: Integer; AMessage: String);
begin
_error := AError;
_errorMessage := AMessage;
end;
procedure TEnv.ExecFile(fileName: String);
var
contents: String;
begin
contents := LoadFile(fileName);
EvalCode(contents);
end;
//TODO: числовые типы!
function TEnv.EvalCode(code: String): TAtom;
var
root: TListAtom;
begin
Result := nil;
root := TListAtom(BuildSyntaxTree(code));
if (Error = 0) and (Assigned(root)) then
begin
writeln('==========================================');
Result := root.Eval(Self);
writeln('==========================================');
end;//TODO: else some message
root.Free;
{$IFDEF DEBUG_INFO}
ShowDebugInfo();
{$ENDIF}
end;
{--== TAtom implementation==--}
constructor TAtom.Create(AType: TAtomType);
begin
_type := AType;
{$IFDEF DEBUG_INFO}
_id := IdCounter;
Inc(IdCounter);
{$ENDIF}
writeln('Creating atom: ', _type, '; Id: ', _id);
//writeln('Creating ', _type);
{$IFDEF DEBUG_INFO}
Atoms.Add(Pointer(Self));
writeln(' Atoms total: ', Atoms.Count);
{$ENDIF}
end;
destructor TAtom.Destroy;
begin
writeln('Destroying atom: ', _type);
{$IFDEF DEBUG_INFO}
Atoms.Remove(Pointer(Self));
writeln(' Atoms total: ', Atoms.Count);
{$ENDIF}
inherited Destroy;
end;
function TAtom.Copy: TAtom;
begin
Result := TAtom.Create(_type);
end;
function TAtom.StrValue: String;
begin
Result := 'Value';
end;
function TAtom.Eval(env: TEnv): TAtom;
begin
Result := Copy;
end;
{--== TIntAtom implementation ==--}
constructor TIntAtom.Create(AIntValue: Integer);
begin
inherited Create(atInt);
_int := AIntValue;
end;
constructor TIntAtom.Create(AStrValue: String);
begin
Create(StrToInt(AStrValue));
end;
function TIntAtom.StrValue: String;
begin
Result := IntToStr(_int);
end;
function TIntAtom.Copy: TAtom;
begin
Result := TIntAtom.Create(_int);
end;
{--== TStrAtom implementation ==--}
constructor TStrAtom.Create(AType: TAtomType);
begin
inherited Create(AType);
end;
constructor TStrAtom.Create(AStr: String);
begin
inherited Create(atStr);
_str := AStr;
end;
function TStrAtom.StrValue: String;
begin
Result := _str;
end;
function TStrAtom.Copy: TAtom;
begin
Result := TAtom(TStrAtom.Create(_str));
end;
{--== TSymbolAtom implementation==--}
constructor TSymbolAtom.Create(AStr: String);
begin
inherited Create(atSymbol);
_str := AStr;
_hash := hashString(AStr);
end;
function TSymbolAtom.Eval(env: TEnv): TAtom;
var
FoundAtom: TAtom;
begin
FoundAtom := env.MainScope.Get(StrValue, HashValue);
if Assigned(FoundAtom) then
Result := FoundAtom //Copy?
else
begin
Result := nil;
env.RaiseError(SYMBOL_NOT_FOUND, 'Symbol not found: ' + StrValue);
end;
end;
{--== TListAtom implementation==--}
constructor TListAtom.Create;
begin
inherited Create(atList);
_list := TFPList.Create;
end;
destructor TListAtom.Destroy;
var
i: Integer;
begin
for i := 0 to Pred(_list.Count) do
TAtom(_list.Items[i]).Free;
FreeAndNil(_list);
inherited Destroy;
end;
function TListAtom.StrValue: String;
var
i: Integer;
begin
Result := '[';
for i := 0 to _list.Count - 1 do
begin
Result := Result + TAtom(_list[i]).StrValue;
if i <> _list.Count - 1 then
Result := Result + ' ';
end;
Result := Result + ']';
end;
procedure TListAtom.Add(AValue: TAtom);
begin
_list.Add(AValue);
end;
function TListAtom.Get(index: Integer): TAtom;
begin
if _list.Count >= index then
Result := TAtom(_list.Items[index])
else
Result := nil;
end;
function TListAtom.Eval(env: TEnv): TAtom;
var
Head, Evaluated: TAtom;
begin
Result := nil;
Head := Get(0);
if (Assigned(Head)) and (Head.AtomType = atSymbol) then
begin
Evaluated := Head.Eval(env);
if Assigned(Evaluated) then
case Evaluated.AtomType of
atNativeFunction: Result := TNativeFunction(Evaluated).Exec(env, Self);
end;
end;
end;
function TListAtom.Copy: TAtom;
var
i: Integer;
ListCopy: TListAtom;
begin
ListCopy := TListAtom.Create;
for i := 0 to Pred(_list.Count) do
begin
ListCopy.Add(TAtom(_list.Items[i]).Copy);
end;
Result := TAtom(ListCopy);
end;
{--== TNativeFunction implementation==--}
constructor TNativeFunction.Create(AFun: TNativeFunctionPointer);
begin
inherited Create(atNativeFunction);
_fun := AFun;
end;
function TNativeFunction.Exec(env: TEnv; args: TListAtom): TAtom;
var
Evaluated: TAtom;
begin
if (args <> nil) then
writeln(args.StrValue);
Evaluated := _fun(env, args);
if Evaluated <> nil then
begin
Result := Evaluated.Copy;
Evaluated.Free;
end
else
Result := nil;
end;
function TNativeFunction.Copy: TAtom;
begin
Result := TAtom(TNativeFunction.Create(_fun));
end;
{--== TDict implementation==--}
constructor TDict.Create;
begin
inherited Create(atDict);
_items := TFPList.Create;
end;
procedure TDict.Put(AName: String; AValue: TAtom);
var
FoundItem: TDictItem;
begin
FoundItem := FindItem(AName);
if FoundItem <> nil then
FoundItem.Value := AValue
else
begin
FoundItem := TDictItem.Create(AName, AValue);
_items.Add(FoundItem);
end;
end;
function TDict.Get(AName: String; AHash: Longint): TAtom;
var
FoundItem: TDictItem;
begin
FoundItem := FindItem(AName, AHash);
if FoundItem <> nil then
Result := FoundItem.Value
else
Result := nil;
end;
function TDict.Get(AName: String): TAtom;
var
FoundItem: TDictItem;
begin
FoundItem := FindItem(AName);
if Assigned(FoundItem) then
Result := FoundItem.Value
else
Result := nil;
end;
function TDict.FindItem(AName: String; AHash: Longint): TDictItem;
var
i: Integer;
begin
Result := nil;
for i := 0 to Pred(_items.Count) do
begin
with TDictItem(_items[i]) do
begin
if (AHash = Hash) and (AName = Name) then
begin
Result := TDictItem(_items[i]);
break;
end;
end;
end;
end;
function TDict.FindItem(AName: String): TDictItem;
var
FindHash: Longint;
begin
FindHash := hashString(AName);
Result := FindItem(AName, FindHash);
end;
{--== TDictItem implementation==--}
constructor TDictItem.Create(AName: String; AValue: TAtom);
begin
_name := AName;
_hash := hashString(AName);
_value := AValue;
end;
begin
{$IFDEF DEBUG_INFO}
Atoms := TFPList.Create;
{$ENDIF}
end.