-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpress.pas
365 lines (273 loc) · 10.8 KB
/
express.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
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
(*----------------------------------------------------------------------------*)
(* Author: Joachim Pimiskern, 1994-2004 *)
(*----------------------------------------------------------------------------*)
unit express;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms,
lspglobl, lsplock, lspmain, lspbasic, lspinf, lspcreat, lsplists;
type
TLispExpression = class(TComponent)
private
(*--- Das letzte Ergebnis als Lisp-Knoten ---*)
ResultNode : pNode;
MyOwnEnvironment : pNode;
Representation : pNode;
Parameters : pNode;
FExpression : TStrings;
FText : PChar;
FExpressionBinary: PChar;
procedure SetExpression(Value: TStrings);
procedure ExpressionChanged(Sender: TObject);
procedure DefineProperties(Filer: TFiler); override;
procedure ReadBinaryData(Stream: TStream);
procedure WriteBinaryData(Stream: TStream);
function ParamByName(s: string): pNode;
protected
public
constructor Create(AOwner: TComponent); override;
procedure Loaded; override;
destructor Destroy; override;
(*--- Text als Lisp-Ausdruck liefern ---*)
function InternalRepresentation: pNode;
(*--- Die textuelle Darstellung in e. Lisp-Repraesentation uebersetzen ---*)
procedure Prepare;
(*--- Die Lisp-Ausdruecke der Reihe nach ausfuehren, analog zu (load ...) ---*)
procedure Execute;
procedure SetIntegerParam(s: string; value: longint);
procedure SetFloatParam(s: string; value: double);
procedure SetStringParam(s: string; value: string);
procedure SetSymbolParam(s: string; value: string);
procedure SetBooleanParam(s: string; value: boolean);
procedure SetNodeParam(s: string; value: pNode);
(*--- Den RESULTNODE in diverse andere Typen wandeln ---*)
function AsNode : pNode;
function AsInteger: longint;
function AsFloat : double;
function AsString : string;
function AsSymbol : string;
function AsBoolean: boolean;
property Text: PChar read FText;
property ExpressionBinary: PChar read FExpressionBinary write FExpressionBinary;
published
property Expression: TStrings read FExpression write SetExpression;
end;
implementation
uses
Eval, lspmath, lspstrng, lsppredi,
EditLsp;
{$r *.dcr}
procedure TLispExpression.SetExpression(Value: TStrings);
begin
(* Disconnect; *)
TStringList(Expression).OnChange := nil;
Expression.Assign(Value);
TStringList(Expression).OnChange := ExpressionChanged;
ExpressionChanged(nil);
end;
procedure TLispExpression.ExpressionChanged(Sender: TObject);
begin
StrDispose(FText);
FText := Expression.GetText;
StrDispose(ExpressionBinary);
ExpressionBinary := nil;
end;
constructor TLispExpression.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FExpression := TStringList.Create;
FText := nil;
end;
destructor TLispExpression.Destroy;
begin
if (MainTask = nil) then
raise Exception.Create('TLispExpression.Destroy: Lisp system not initialized');
(* Destroying; *)
(* Disconnect; *)
LspUnlockNodeAddress(@ResultNode);
LspUnlockNodeAddress(@Parameters);
LspUnlockNodeAddress(@Representation);
LspUnlockNodeAddress(@MyOwnEnvironment);
Expression.Free;
StrDispose(FText);
StrDispose(ExpressionBinary);
inherited Destroy;
end;
procedure TLispExpression.Loaded;
begin
inherited loaded;
if (MainTask = nil) then
raise Exception.Create('TLispExpression.Loaded: Lisp system not initialized');
MyOwnEnvironment := nil;
Representation := nil;
Parameters := nil;
ResultNode := nil;
LspLockNodeAddress(@MyOwnEnvironment);
LspLockNodeAddress(@Representation);
LspLockNodeAddress(@Parameters);
LspLockNodeAddress(@ResultNode);
end;
procedure TLispExpression.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineBinaryProperty('Data', ReadBinaryData, WriteBinaryData, ExpressionBinary <> nil);
end;
procedure TLispExpression.ReadBinaryData(Stream: TStream);
begin
ExpressionBinary := StrAlloc(Stream.Size);
Stream.ReadBuffer(ExpressionBinary^, Stream.Size);
end;
procedure TLispExpression.WriteBinaryData(Stream: TStream);
begin
Stream.WriteBuffer(ExpressionBinary^, StrBufSize(ExpressionBinary));
end;
(*----------------------------------------------------------------------------*)
(* Die textuelle Darstellung der Lisp-Ausdruecke in eine Lisp-Darstellung *)
(* umwandeln. *)
(*----------------------------------------------------------------------------*)
function TLispExpression.InternalRepresentation: pNode;
var m: TMemoryStream;
begin
m := TMemoryStream.Create;
Expression.SaveToStream(m);
result := GetListFromStream(m);
m.Free;
end;
(*----------------------------------------------------------------------------*)
(* Von den Strings, also dem Text, in Representation einlesen. *)
(* Alle Variablen, also Symbole der Art ?a, ?test, usw. werden in der Liste *)
(* Parameters gespeichert. Die lokale Umgebung von TLispExpression ergibt *)
(* sich aus dem Append von den Parametern und der globalen Umgebung. *)
(*----------------------------------------------------------------------------*)
procedure TLispExpression.Prepare;
var loop,ListOfVariables,Pair: pNode;
begin
if (MainTask = nil) then
raise Exception.Create('TLispExpression: Lisp system not started');
Representation := InternalRepresentation;
MyOwnEnvironment := MainTask.Environment; (* wird noch verlaengert *)
Parameters := nil;
ListOfVariables := LspGetVariables(Representation,nil);
loop := ListOfVariables;
while (loop <> nil) do
begin
Pair := LspList2(LspCar(loop),nil);
Parameters := LspCons(Pair,Parameters);
MyOwnEnvironment := LspCons(Pair,MyOwnEnvironment);
loop := LspCdr(loop);
end;
(*
LspPrint(Representation,nil);
LspPrint(Parameters,nil);
LspPrint(MyOwnEnvironment,nil);
*)
end;
(*----------------------------------------------------------------------------*)
(* Die Liste Representation laden. Der letzte Wert steht dann in Result *)
(* Mit Laden ist der Load-Befehl von Lisp gemeint, also alle Anweisungen *)
(* der Reihe nach ausfuehren. *)
(*----------------------------------------------------------------------------*)
procedure TLispExpression.Execute;
var loop: pNode;
begin
ResultNode := nil;
loop := Representation;
while (loop <> nil) do
begin
ResultNode := LspEval(LspCar(loop),MyOwnEnvironment);
loop := LspCdr(loop);
end;
end;
(*----------------------------------------------------------------------------*)
(* Einen Zeiger auf die interne Repraesentation eines Parameters liefern. *)
(* Ein Ergebnis <> nil wird nur geliefert, wenn der Parameter auch in der *)
(* Liste Parameters vorkommt. *)
(*----------------------------------------------------------------------------*)
function TLispExpression.ParamByName(s: string): pNode;
var AsNode: pNode;
begin
AsNode := LspMakeSymbol(s);
result := LspAssoc(AsNode,Parameters);
if (result <> nil) then
result := LspCar(result);
end;
(*----------------------------------------------------------------------------*)
(* Einen Parameter setzen. *)
(*----------------------------------------------------------------------------*)
procedure TLispExpression.SetIntegerParam(s: string; value: longint);
begin
LspSetq(ParamByName(s),LspMakeInteger(value),MyOwnEnvironment);
end;
(*----------------------------------------------------------------------------*)
(* Einen Parameter setzen. *)
(*----------------------------------------------------------------------------*)
procedure TLispExpression.SetFloatParam(s: string; value: double);
begin
LspSetq(ParamByName(s),LspMakeFloat(value),MyOwnEnvironment);
end;
(*----------------------------------------------------------------------------*)
(* Einen Parameter setzen. *)
(*----------------------------------------------------------------------------*)
procedure TLispExpression.SetStringParam(s: string; value: string);
begin
LspSetq(ParamByName(s),LspMakeString(value),MyOwnEnvironment);
end;
(*----------------------------------------------------------------------------*)
(* Einen Parameter setzen. *)
(*----------------------------------------------------------------------------*)
procedure TLispExpression.SetSymbolParam(s: string; value: string);
begin
LspSetq(ParamByName(s),LspMakeSymbol(value),MyOwnEnvironment);
end;
(*----------------------------------------------------------------------------*)
(* Einen Parameter setzen. *)
(*----------------------------------------------------------------------------*)
procedure TLispExpression.SetBooleanParam(s: string; value: boolean);
begin
LspSetq(ParamByName(s),BoolToNode(value),MyOwnEnvironment);
end;
(*----------------------------------------------------------------------------*)
(* Einen Parameter setzen. *)
(*----------------------------------------------------------------------------*)
procedure TLispExpression.SetNodeParam(s: string; value: pNode);
begin
LspSetq(ParamByName(s),value,MyOwnEnvironment);
end;
function TLispExpression.AsNode: pNode;
begin
result := ResultNode;
end;
function TLispExpression.AsInteger: longint;
begin
if (LspIntegerp(ResultNode)) then
result := LspGetIntegerVal(ResultNode)
else
result := 0;
end;
function TLispExpression.AsFloat: double;
begin
if (LspNumberp(ResultNode)) then
result := LspGetFloatVal(ResultNode)
else
result := 0.0;
end;
function TLispExpression.AsString: string;
begin
if (LspStringp(ResultNode)) then
result := LspGetStringVal(ResultNode)
else
result := '';
end;
function TLispExpression.AsSymbol: string;
begin
if (LspSymbolp(ResultNode)) then
result := LspGetStringVal(LspSymbolName(ResultNode))
else
result := '';
end;
function TLispExpression.AsBoolean: boolean;
begin
result := ResultNode <> nil;
end;
end.