-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.Language.Formula.Solver.pas
293 lines (261 loc) · 7.79 KB
/
GS.Language.Formula.Solver.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
unit GS.Language.Formula.Solver;
///
/// Independant, RAPI ("Raw AlgoProgram Interpreter") VM-based solver.
/// This solver will not used in compiler : It is a independant implemnentation.
/// it is just an fully functionnal solver witch can be used as independant
/// numerical solver in a any program. It is a good example of what we can
/// achieve with all the tokenizer, parser, and formula tools.
///
/// Note :
/// - This solver DO NOT perform syntax analyze, your formula must
/// be syntaxicaly correct.
/// - This solver use only var to perfom all calculation. Const, will be
/// change in var on the fly. You can check IR Code by the commands
/// FlushCompiledCode/FlushPostFixFormula after compilation step.
///
interface
uses classes, sysutils, contnrs,
GS.Language.WordTokenizer, //Full code string to token.
GS.Language.Formula, //Forumla parser and types
GS.Language.AlgoProgram, //Abstract program layer.
GS.Language.VM; //We use RIPA VM : runing raw AlgoProgram code.
Type
TFormulaSolver = class
private
fformula: string;
fResult: string;
fResultAsNumber: double;
fLastError: string;
fprog : TAlgoProgram;
fMachine : TVirtualMachineRAPI;
fPostFixFormula : String;
protected
procedure InternalEmitProgram(_postFixedFormula : TTokenItemArray; var TargetProgram : TAlgoProgram); virtual;
public
constructor Create; virtual;
destructor Destroy; override;
procedure updateVar(_varName : string; value : integer);
function getVarValueAsInteger(_varName : string) : integer;
function getVarValueAsFloat(_varName : string) : double;
function getVarValueAsString(_varName : string) : string;
function FlushCompiledCode : string;
function FlushPostFixFormula : string;
function Compile(const _formula : string) : boolean;
procedure Run;
property Formula : string read fformula write fformula;
property SolvedResult : string read FResult;
property SolvedResultAsNumber : double read FResultAsNumber;
property LastError : string read FLastError;
end;
implementation
{ TFormulaSolver }
constructor TFormulaSolver.Create;
begin
inherited create;
fprog := TAlgoProgram.Create;
fMachine := TVirtualMachineRAPI.Create;
fPostFixFormula := '';
end;
destructor TFormulaSolver.Destroy;
begin
FreeAndNil(fprog);
FreeAndNil(fMachine);
inherited;
end;
function TFormulaSolver.FlushCompiledCode: string;
begin
result := fprog.flushCode;
end;
function TFormulaSolver.FlushPostFixFormula: string;
begin
result := fPostFixFormula;
end;
function TFormulaSolver.getVarValueAsFloat(_varName: string): double;
begin
result := fMachine.GetVarAsDouble(_varName);
end;
function TFormulaSolver.getVarValueasInteger(_varName: string): integer;
begin
result := fMachine.GetVarAsInteger(_varName);
end;
function TFormulaSolver.getVarValueAsString(_varName: string): string;
begin
result := fMachine.GetVarAsString(_varName);
end;
procedure TFormulaSolver.InternalEmitProgram(_postFixedFormula: TTokenItemArray;
var TargetProgram: TAlgoProgram);
var i: integer;
l,m : TList;
c : integer;
t,g : TTokenItem;
memory : TObjectList;
v : String;
vl, vr : string;
LastWord : string; //Who have the last word ? ;) This keep last variable used, which is the formula solution.
procedure copyListWithoutOp;
var j : integer;
begin
m.Clear;
for j := 0 to l.Count-1 do
begin
if (j<>i-2) and (j<>i-1) and (j<>i) then
m.Add(l[j]);
if j=i then
begin
g := TTokenItem.create(ttword,v,-1,-1);
m.Add(g);
memory.Add(g);
end;
end;
l.Clear;
for j := 0 to m.Count-1 do
begin
l.Add(m[j]);
end;
end;
procedure OperandProcess; //change const into var. (formula based process is var based.)
begin
//Left
if TTokenItem(l[i-2]).TokType = TTokenType.ttnumber then
begin
vl := 'CST'+IntToStr(c); inc(c);
TargetProgram.emit_createVar(vl,'double');
TargetProgram.emit_assignVarConst(vl,'double',TTokenItem(l[i-2]).item);
end
else
begin
vl := TTokenItem(l[i-2]).item;
end;
//Right
if TTokenItem(l[i-1]).TokType = TTokenType.ttnumber then
begin
vr := 'CST'+IntToStr(c); inc(c);
TargetProgram.emit_createVar(vr,'double');
TargetProgram.emit_assignVarConst(vr, 'double', TTokenItem(l[i-1]).item);
end
else
begin
vr := TTokenItem(l[i-1]).item;
end;
end;
begin
l := TList.Create;
m := TList.Create;
memory := TObjectList.Create;
try
for i := Low(_postFixedFormula) to High(_postFixedFormula) do
l.Add(_postFixedFormula[i]);
TargetProgram.emit_createVar('result','double');
//Pass throught Token and emit program's instruction.
i := 0;
c := 1;
while (i<l.Count) do
begin
t := TTokenItem(l[i]);
if t.TokType = TTokenType.ttsymbol then
begin
if i>1 then
begin
if t.item = '+' then
begin
v := 'R'+IntToStr(c); inc(c);
TargetProgram.emit_createVar(v,'double');
OperandProcess;
TargetProgram.emit_baseAssignOp(TAlgo_AssignVarEqualVarPlusVar,v,vl,vr);
copyListWithoutOp;
i := -1;
end
else
if t.item = '-' then
begin
v := 'R'+IntToStr(c); inc(c);
TargetProgram.emit_createVar(v,'double');
OperandProcess;
TargetProgram.emit_baseAssignOp(TAlgo_AssignVarEqualVarMinusVar,v,vl,vr);
copyListWithoutOp;
i := -1;
end
else
if t.item = '*' then
begin
v := 'R'+IntToStr(c); inc(c);
TargetProgram.emit_createVar(v,'double');
OperandProcess;
TargetProgram.emit_baseAssignOp(TAlgo_AssignVarEqualVarMulVar,v,vl,vr);
copyListWithoutOp;
i := -1;
end
else
if t.item = '/' then
begin
v := 'R'+IntToStr(c); inc(c);
TargetProgram.emit_createVar(v,'double');
OperandProcess;
TargetProgram.emit_baseAssignOp(TAlgo_AssignVarEqualVarDivVar,v,vl,vr);
copyListWithoutOp;
i := -1;
end
else
if t.item = '^' then
begin
v := 'R'+IntToStr(c); inc(c);
TargetProgram.emit_createVar(v,'double');
OperandProcess;
TargetProgram.emit_baseAssignOp(TAlgo_AssignVarEqualVarMulVar,v,vl,vr);
copyListWithoutOp;
i := -1;
end;
end;
end
else
begin
lastWord := t.item;
end;
inc(i);
end;
TargetProgram.emit_assignVar('result',LastWord);
finally
freeAndNil(l);
freeAndNil(m);
FreeAndNil(memory); //erase supplementary tokenItems
end;
end;
function TFormulaSolver.Compile(const _formula: string) : boolean;
var t : String;
l : TWordTokenizer;
r : TTokenItemArray;
fparser : TFormulaParser;
begin
FLastError := '';
t := trim(_formula);
if t<>'' then
fformula := t;
l := TWordTokenizer.Create;
fparser := TFormulaParser.Create;
try
l.Tokenize(fformula);
if fparser.ParseFormula(l.getAsTokenItemArray,r) then
begin
fPostFixFormula := fparser.TokenItemsToString(r);
InternalEmitProgram(r,fprog);
result := fLastError='';
end
else
fLastError := 'Parser Error : '+fparser.LastError;
finally
result := fLastError='';
FreeAndNil(l);
FreeAndNil(fParser);
end;
end;
procedure TFormulaSolver.Run;
begin
fMachine.load(fprog);
fMachine.run;
end;
procedure TFormulaSolver.updateVar(_varName: string; value: integer);
begin
fprog.emit_createVar(_varName);
fprog.emit_assignVarConst(_varName,'integer',inttostr(value));
end;
end.