-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.Language.AlgoProgram.pas
480 lines (388 loc) · 10.8 KB
/
GS.Language.AlgoProgram.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
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
unit GS.Language.AlgoProgram;
///
///
/// Abstract implementation of "Algorithm" program.
/// This will be used by compiler tools to emit high level descriptioncode,
/// starting from a script (Such as TFormula) or a AST (abstract Syntax Tree).
/// Unit GS.AlgoProgram.Runner implement a program runner for AlgoProgram.
///
interface
{$IFDEF FPC}
{$mode delphi}{$H+}
{$ENDIF FPC}
uses sysUtils, classes,
Generics.Collections;
type
///
///
/// Core
///
///
TAlgoProgram = class; //Pure algorithme (i.e. symbolic) program.
TAlgoVar = class; //Variable representation.
TAlgoInstruction = class; //Instruction representation.
TAlgoData = class
end;
TAlgoVar = class(TAlgoData)
protected
function _AsString : string; virtual; abstract;
function _AsDouble : double; virtual; abstract;
function _AsInteger : integer; virtual; abstract;
public
name, _Type : string;
procedure assign(source : TAlgoVar); virtual; abstract;
property AsString : String read _AsString;
property AsDouble : Double read _AsDouble;
property AsInteger : Integer read _AsInteger;
end;
TAlgoInstruction = class
protected
prog : TAlgoProgram;
public
constructor create(_prog : TAlgoProgram); reintroduce;
function AsString : String; virtual;
end;
TAlgoInstructionClass = class of TAlgoInstruction;
///
///
///
/// Variable
///
///
///
///
//Int
TAlgoInteger = class(TAlgoVar)
value : integer;
procedure assign(source : TAlgoVar); override;
function _AsString : string; override;
function _AsDouble : double; override;
function _AsInteger : integer; override;
end;
TAlgoConstInteger = class(TAlgoInteger)
procedure assign(source : TAlgoVar); override;
end;
//float
TAlgoDouble = class(TalgoVar)
value : double;
procedure assign(source : TAlgoVar); override;
function _AsString : string; override;
function _AsDouble : double; override;
function _AsInteger : integer; override;
end;
TAlgoConstDouble = class(TAlgoDouble)
procedure assign(source : TAlgoVar); override;
end;
//string
TAlgoString = class(TAlgoVar)
value : UTF8String;
function _AsString : string; override;
end;
TAlgoConstString = class(TAlgoString)
procedure assign(source : TAlgoVar); override;
end;
///
///
///
///
/// OPERATION (Intruction)
///
///
///
///
///
///
TAlgo_CreateVar = class(TAlgoInstruction)
varname,vartype : String;
function AsString : String; override;
end;
TAlgo_CreateConst = class(TAlgoInstruction)
varname,vartype : String;
function AsString : String; override;
end;
TAlgo_AssignVarVar = class(TAlgoInstruction)
varnameAssigned : String;
varnameSource : String;
function AsString : String; override;
end;
TAlgo_AssignVarConst = class(TAlgoInstruction)
varnameAssigned : String;
valueSource : string;
_type : string;
_valueAsString : string;
function AsString : String; override;
end;
TAlgo_AssignVarEqualVarOperandVar = class(TAlgoInstruction)
varnameAssigned : String;
varnameLeft : String;
varnameRight : String;
end;
TAlgo_AssignVarEqualVarPlusVar = class(TAlgo_AssignVarEqualVarOperandVar)
public
function AsString : String; override;
end;
TAlgo_AssignVarEqualVarMinusVar = class(TAlgo_AssignVarEqualVarOperandVar)
public
function AsString : String; override;
end;
TAlgo_AssignVarEqualVarMulVar = class(TAlgo_AssignVarEqualVarOperandVar)
public
function AsString : String; override;
end;
TAlgo_AssignVarEqualVarDivVar = class(TAlgo_AssignVarEqualVarOperandVar)
public
function AsString : String; override;
end;
TAlgoProgram = Class
private
function GetInstructionCode(Index: Integer): TAlgoInstruction;
function GetInstructionCount: integer;
protected
codes : TObjectList<TAlgoInstruction>;
procedure AddInstruction(_Instr : TAlgoInstruction);
public
constructor Create; virtual;
destructor Destroy; override;
procedure reset;
function flushCode : string;
procedure emit_createVar(name : String; const _type : String= 'integer');
procedure emit_createConst(name : String; const _type : String= 'integer');
procedure emit_assignVar(resultName, ValueName : String);
procedure emit_assignVarConst(resultName : String; typename : String; Value : string); overload;
procedure emit_baseAssignOp(Op : TAlgoInstructionClass; resultName, op1Name, op2Name : String);
property Instruction[Index : Integer] : TAlgoInstruction read GetInstructionCode;
property InstructionCount : integer read GetInstructionCount;
End;
implementation
{ TAlgoInstruction }
function TAlgoInstruction.AsString: String;
begin
result := className;
end;
constructor TAlgoInstruction.create(_prog: TAlgoProgram);
begin
assert(assigned(_prog));
prog := _prog;
end;
{ TAlgoProgram }
procedure TAlgoProgram.AddInstruction(_Instr: TAlgoInstruction);
begin
assert(assigned(_Instr));
codes.Add(_Instr);
end;
constructor TAlgoProgram.Create;
begin
inherited create;
codes := TObjectList<TAlgoInstruction>.Create;
end;
destructor TAlgoProgram.Destroy;
begin
FreeAndNil(codes);
inherited;
end;
procedure TAlgoProgram.emit_assignVar(resultName, ValueName : String);
var a : TAlgo_AssignVarVar;
i,o : TAlgoVar;
begin
a := TAlgo_AssignVarVar.create(self);
a.varnameAssigned := resultName;
a.varnameSource := ValueName;
AddInstruction(a);
end;
procedure TAlgoProgram.emit_assignVarConst(resultName : String; typename : String; Value : string);
var a : TAlgo_AssignVarConst;
n : string;
begin
n := format('CST_%s_%s_%s',[uppercase(resultName),typename,value]);
emit_createConst(n,typename);
a := TAlgo_AssignVarConst.create(self);
a.varnameAssigned := resultName;
a.valueSource := n;
a._type := typename;
a._valueAsString := Value;
AddInstruction(a);
end;
procedure TAlgoProgram.emit_baseAssignOp(Op: TAlgoInstructionClass; resultName,
op1Name, op2Name: String);
var a : TAlgo_AssignVarEqualVarOperandVar;
begin
a := TAlgo_AssignVarEqualVarOperandVar(Op.create(Self));
a.varnameAssigned := resultName;
a.varnameLeft := op1Name;
a.varnameRight := op2Name;
AddInstruction(a);
end;
procedure TAlgoProgram.emit_createConst(name: String; const _type: String);
var a : TAlgo_CreateConst;
n : String;
begin
assert(length(name)>0);
n := trim(name);
a := TAlgo_CreateConst.create(self);
a.varname := name;
a.vartype := _Type;
AddInstruction(a);
end;
procedure TAlgoProgram.emit_createVar(name: String; const _type : string);
var a : TAlgo_CreateVar;
n : String;
begin
assert(length(name)>0);
n := trim(name);
a := TAlgo_CreateVar.create(self);
a.varname := name;
a.vartype := _Type;
AddInstruction(a);
end;
function TAlgoProgram.flushCode: string;
var i : integer;
l : TStringList;
begin
l := TStringList.Create;
try
for I := 0 to codes.Count-1 do
begin
l.Add(codes[i].AsString);
end;
result := l.Text;
finally
FreeAndNil(l);
end;
end;
function TAlgoProgram.GetInstructionCode(Index: Integer): TAlgoInstruction;
begin
result := codes[Index];
end;
function TAlgoProgram.GetInstructionCount: integer;
begin
result := codes.Count;
end;
procedure TAlgoProgram.reset;
begin
codes.Clear;
end;
{ TAlgo_CreateVar }
function TAlgo_CreateVar.AsString: String;
begin
result := format('%s : VAR %s (%s)',[className,varname,vartype]);
end;
{ TAlgo_AssignVarVar }
function TAlgo_AssignVarVar.AsString: String;
begin
result := format('%s : %s <- %s',[className,varnameAssigned,varnameSource]);
end;
{ TAlgo_AssignVarConst }
function TAlgo_AssignVarConst.AsString: String;
begin
result := format('%s : %s <- %s (%s)',[className,varnameAssigned,valueSource,_valueAsString]);
end;
{ TAlgoInteger }
function TAlgoInteger._AsDouble: double;
begin
result := double(value);
end;
function TAlgoInteger._AsInteger: integer;
begin
result := value;
end;
procedure TAlgoInteger.assign(source: TAlgoVar);
begin
if source is TAlgoInteger then
value := TalgoInteger(source).value
else
if source is TAlgoDouble then
value := trunc(TalgoDouble(source).value)
else
if source is TAlgoString then
begin
try
value := StrToInt(TAlgoString(source).value)
Except
raise Exception.Create(ClassName+'.Assign Error : cannot convert string into integer');
end;
end
else
raise Exception.Create(ClassName+' Error : unknown source type ('+source.ClassName+')');
end;
function TAlgoInteger._AsString: string;
begin
result := IntToStr(value);
end;
{ TAlgoConstInteger }
procedure TAlgoConstInteger.assign(source: TAlgoVar);
begin
raise Exception.Create('Const integer "name" cannot be assigned');
end;
{ TAlgoDouble }
procedure TAlgoDouble.assign(source: TAlgoVar);
begin
if source is TAlgoInteger then
value := TalgoInteger(source).AsDouble
else
if source is TAlgoDouble then
value := TalgoDouble(source).value
else
if source is TAlgoString then
begin
try
value := StrToFloat(TAlgoString(source).value);
Except
raise Exception.Create(ClassName+'.Assign Error : cannot convert string into float');
end;
end
else
raise Exception.Create(ClassName+' Error : unknown source type ('+source.ClassName+')');
end;
function TAlgoDouble._AsDouble: double;
begin
result := value;
end;
function TAlgoDouble._AsInteger: integer;
begin
result := trunc(value);
end;
function TAlgoDouble._AsString: string;
begin
result := FloatToStr(value);
end;
{ TAlgoString }
function TAlgoString._AsString: string;
begin
result := String(value);
end;
{ TAlgoConstDouble }
procedure TAlgoConstDouble.assign(source: TAlgoVar);
begin
raise Exception.Create('Const double "name" cannot be assigned');
end;
{ TAlgoConstString }
procedure TAlgoConstString.assign(source: TAlgoVar);
begin
raise Exception.Create('Const string "name" cannot be assigned');
end;
{ TAlgo_CreateConst }
function TAlgo_CreateConst.AsString: String;
begin
result := format('%s : CONST %s (%s)',[className,varname,vartype]);
end;
{ TAlgo_AssignVarEqualVarPlusVar }
function TAlgo_AssignVarEqualVarPlusVar.AsString: String;
begin
result := format('%s : %s <- %s + %s',[className,varnameAssigned,varnameLeft,varnameRight]);
end;
{ TAlgo_AssignVarEqualVarMinusVar }
function TAlgo_AssignVarEqualVarMinusVar.AsString: String;
begin
result := format('%s : %s <- %s - %s',[className,varnameAssigned,varnameLeft,varnameRight]);
end;
{ TAlgo_AssignVarEqualVarMulVar }
function TAlgo_AssignVarEqualVarMulVar.AsString: String;
begin
result := format('%s : %s <- %s * %s',[className,varnameAssigned,varnameLeft,varnameRight]);
end;
{ TAlgo_AssignVarEqualVarDivVar }
function TAlgo_AssignVarEqualVarDivVar.AsString: String;
begin
result := format('%s : %s <- %s / %s',[className,varnameAssigned,varnameLeft,varnameRight]);
end;
end.