-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluator.pas
275 lines (249 loc) · 7.06 KB
/
evaluator.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
{*
* Evaluator: Math expression evaluator written in Pascal using pointers.
* Jonas Raoni Soares da Silva <http://raoni.org>
* https://github.com/jonasraoni/math-expression-evaluator
*}
unit evaluator;
interface
uses
dialogs, SysUtils, Classes, Math;
const
OPERATORS = '^~:*/:%:+-';
type
TTokenType = ( ttNoToken, ttNumber, ttSignal, ttParen );
TToken = record
offset: pchar;
content: pchar;
length: integer;
token: TTokenType;
end;
function greaterSignal( const a, b: char ): boolean;
function operate( const left, right: extended; const operator: char ): extended;
function getNumber( offset: pchar ): integer;
function getParen( offset: pchar ): integer;
function nextToken( offset: pchar ): TToken;
procedure freeToken( var t: TToken );
function getString( offset: pchar; const length: integer ): pchar;
function evalExpression( expression: pchar ): extended;
function floatToChar( value: extended ): pchar;
implementation
function floatToChar( value: extended ): pchar;
var
i: integer;
begin
getMem( result, 65 );
i := floatToText( result, value, fvExtended, ffGeneral, 18, 0 );
reallocMem( result, i+1 );
(result+i)^ := #0;
end;
function greaterSignal( const a, b: char ): boolean;
var
posA, posB, l, h: integer;
begin
result := false;
posA := pos( a, OPERATORS );
posB := pos( b, OPERATORS );
l := min( posA , posB );
h := max( posA, posB ) - 1;
while l < h do begin
inc( l );
if OPERATORS[l] = ':' then begin
result := h + 1 = posB;
break;
end;
end;
end;
function operate( const left, right: extended; const operator: char ): extended;
begin
case operator of
'^': result := power( left, right );
'~':
if right = 0 then
raise exception.create( 'Não existe raiz de 0' )
else
result := power( left, 1 / right );
'*': result := left * right;
'/':
if right = 0 then
raise exception.create( 'Não existe divisão por 0' )
else
result := left / right;
'%': result := trunc( left ) mod trunc( right );
'+': result := left + right;
'-': result := left - right;
else
raise exception.create( 'operador desconhecido.' );
end;
end;
function getNumber( offset: pchar ): integer;
begin
result := integer( offset );
while ( ( offset^ >= '0' ) and ( offset^ <= '9' ) ) or ( offset^ = DecimalSeparator ) do
inc( offset );
result := integer( offset ) - result;
end;
function getParen( offset: pchar ): integer;
var
i: integer;
begin
result := integer( offset );
i := 1;
inc( offset );
while offset^ <> #0 do begin
case offset^ of
'(': inc( i );
')': begin
dec( i );
if i = 0 then
break;
end;
end;
inc( offset );
end;
result := integer( offset ) - result;
end;
procedure freeToken( var t: TToken );
begin
freeMem( t.content );
end;
function nextToken( offset: pchar ): TToken;
procedure setResult( sign: TTokenType; const len: integer );
begin
result.length := len;
result.offset := offset;
result.content := getString( offset, len );
result.token := sign;
end;
begin
while offset^ <> #0 do begin
if ( offset^ >= '0' ) and ( offset^ <= '9' ) then begin
setResult( ttNumber, getNumber( offset ) );
exit;
end
else if pos( offset^ , OPERATORS ) <> 0 then begin
setResult( ttSignal, 1 );
exit;
end
else if offset^ = '(' then begin
inc( offset );
setResult( ttParen, getParen( offset ) );
exit;
end;
inc( offset );
end;
setResult( ttNoToken, 0 );
end;
function getString( offset: pchar; const length: integer ): pchar;
begin
getMem( result, length + 1 );
strLCopy( result, offset, length );
end;
function evalExpression( expression: pchar ): extended;
var
x: TToken;
exp, expBegin: pchar;
function read: TTokenType;
begin
x := nextToken( exp );
with x do begin
exp := offset + length;
result := token;
end;
end;
function getNumber: extended;
begin
result := 0;
case x.token of
ttNumber: result := strToFloat( x.content );
ttParen: result := evalExpression( x.content );
ttSignal:
if x.content^ = '-' then begin
freeToken( x );
read;
result := getNumber * -1;
end
else if x.content^ = '+' then begin
freeToken( x );
read;
result := getNumber;
end;
end;
end;
var
left, right: extended;
op: char;
tempExp, remainingExp: pchar;
begin
getMem( exp, strLen( expression ) + 1 );
exp := strCopy( exp, expression );
expBegin := exp;
result := 0;
while exp^ <> #0 do begin
remainingExp := nil;
if read = ttNoToken then begin
freeToken( x );
break;
end;
left := getNumber;
freeToken( x );
if read <> ttSignal then begin
freeToken( x );
result := left;
break;
end;
op := x.content^;
freeToken( x );
if read = ttNoToken then begin
freeToken( x );
freeMem( expBegin );
raise exception.create( 'erro! operando direito esperado' );
end;
right := getNumber;
freeToken( x );
if read <> ttSignal then begin
freeToken( x );
result := result + operate( left, right, op );
break;
end
else if not greaterSignal( x.content^ , op ) then begin
tempExp := floatToChar( operate( left, right, op ) );
strCopy( expBegin, x.offset );
reallocMem( expBegin, strLen( tempExp ) + strLen( expBegin ) + 1 );
exp := strMove( expBegin + strLen( tempExp ), expBegin, strLen( expBegin ) + 1 );
exp := strMove( expBegin, tempExp, strLen( tempExp ) ); //exp - expBegin
freeMem( tempExp );
freeToken( x );
end
else begin
remainingExp := allocMem( 1 );
repeat
tempExp := floatToChar( left );
reallocMem( remainingExp, strLen( remainingExp ) + strLen( tempExp ) + {strLen( @op )} 1 + 1 );
strCat( remainingExp, tempExp );
StrLCat( remainingExp, @op, strLen( remainingExp ) + 1 );
freeMem( tempExp );
left := right;
op := x.content^;
freeToken( x );
if read = ttNoToken then begin
freeToken( x );
freeMem( remainingExp );
freeMem( expBegin );
raise exception.Create( 'erro! operando direito esperado' );
end;
right := getNumber;
freeToken( x );
until ( read <> ttSignal ) or not greaterSignal( x.content^, op );
tempExp := floatToChar( operate( left, right, op ) );
reallocMem( remainingExp, strLen( remainingExp ) + strlen( tempExp ) + strlen( x.offset ) + 1 );
strCat( strCat( remainingExp, tempExp ), x.offset );
freeMem( expBegin );
expBegin := remainingExp;
exp := expBegin;
freeMem( tempExp );
freeToken( x );
end;
end;
freeMem( expBegin );
end;
end.