mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-07 15:49:23 +02:00
* fixed bug 42
This commit is contained in:
parent
d72ca253fe
commit
df5ca5be09
@ -451,6 +451,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
Function TExprParse.Evaluate(Expr : String):longint;
|
Function TExprParse.Evaluate(Expr : String):longint;
|
||||||
Var
|
Var
|
||||||
I : Integer;
|
I : Integer;
|
||||||
@ -462,78 +463,78 @@ begin
|
|||||||
Token := '';
|
Token := '';
|
||||||
|
|
||||||
For I := 1 to Length(Expr) DO
|
For I := 1 to Length(Expr) DO
|
||||||
|
begin
|
||||||
if Expr[I] in ['0'..'9'] then
|
if Expr[I] in ['0'..'9'] then
|
||||||
begin { Build multi-digit numbers }
|
begin { Build multi-digit numbers }
|
||||||
Token := Token + Expr[I];
|
Token := Token + Expr[I];
|
||||||
if I = Length(Expr) then { Send last one to calculator }
|
if I = Length(Expr) then { Send last one to calculator }
|
||||||
RPNCalc(Token,false);
|
RPNCalc(Token,false);
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
|
if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
|
||||||
begin
|
begin
|
||||||
if Token <> '' then
|
if Token <> '' then
|
||||||
begin { Send last built number to calc. }
|
begin { Send last built number to calc. }
|
||||||
RPNCalc(Token,false);
|
RPNCalc(Token,false);
|
||||||
Token := '';
|
Token := '';
|
||||||
|
end;
|
||||||
|
|
||||||
|
Case Expr[I] OF
|
||||||
|
'(' : OpPush('(',false);
|
||||||
|
')' : begin
|
||||||
|
While OpStack[OpTop].ch <> '(' DO
|
||||||
|
Begin
|
||||||
|
OpPop(opr);
|
||||||
|
RPNCalc(opr.ch,opr.is_prefix);
|
||||||
|
end;
|
||||||
|
OpPop(opr); { Pop off and ignore the '(' }
|
||||||
|
end;
|
||||||
|
'+','-','~' : Begin
|
||||||
|
{ workaround for -2147483648 }
|
||||||
|
if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
|
||||||
|
begin
|
||||||
|
token:='-';
|
||||||
|
expr[i]:='+';
|
||||||
|
end;
|
||||||
|
{ if start of expression then surely a prefix }
|
||||||
|
{ or if previous char was also an operator }
|
||||||
|
if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
|
||||||
|
OpPush(Expr[I],true)
|
||||||
|
else
|
||||||
|
Begin
|
||||||
|
{ Evaluate all higher priority operators }
|
||||||
|
While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
|
||||||
|
Begin
|
||||||
|
OpPop(opr);
|
||||||
|
RPNCalc(opr.ch,opr.is_prefix);
|
||||||
|
end;
|
||||||
|
OpPush(Expr[I],false);
|
||||||
|
End;
|
||||||
|
end;
|
||||||
|
'*', '/',
|
||||||
|
'^','|','&',
|
||||||
|
'%','<','>' : begin
|
||||||
|
While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
|
||||||
|
Begin
|
||||||
|
OpPop(opr);
|
||||||
|
RPNCalc(opr.ch,opr.is_prefix);
|
||||||
|
end;
|
||||||
|
OpPush(Expr[I],false);
|
||||||
|
end;
|
||||||
|
end; { Case }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
Error(invalid_op); { Handle bad input error }
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Case Expr[I] OF
|
{ Pop off the remaining operators }
|
||||||
'(' : OpPush('(',false);
|
While OpTop > 0 do
|
||||||
')' :
|
Begin
|
||||||
begin
|
OpPop(opr);
|
||||||
While OpStack[OpTop].ch <> '(' DO
|
RPNCalc(opr.ch,opr.is_prefix);
|
||||||
Begin
|
end;
|
||||||
OpPop(opr);
|
|
||||||
RPNCalc(opr.ch,opr.is_prefix);
|
|
||||||
end;
|
|
||||||
OpPop(opr); { Pop off and ignore the '(' }
|
|
||||||
end;
|
|
||||||
|
|
||||||
'+','-','~': Begin
|
{ The result is stored on the top of the stack }
|
||||||
While (OpTop > 0) AND
|
|
||||||
(Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
|
|
||||||
Begin
|
|
||||||
OpPop(opr);
|
|
||||||
RPNCalc(opr.ch,opr.is_prefix);
|
|
||||||
end;
|
|
||||||
{ if start of expression then surely a prefix }
|
|
||||||
{ or if previous char was also an operator }
|
|
||||||
{ push it and don't evaluate normally }
|
|
||||||
{ workaround for -2147483648 }
|
|
||||||
if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
|
|
||||||
begin
|
|
||||||
token:='-';
|
|
||||||
expr[i]:='+';
|
|
||||||
end;
|
|
||||||
if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
|
|
||||||
OpPush(Expr[I],true)
|
|
||||||
else
|
|
||||||
OpPush(Expr[I],false);
|
|
||||||
end;
|
|
||||||
|
|
||||||
'*', '/','^','|','&','%','<','>' :
|
|
||||||
begin
|
|
||||||
While (OpTop > 0) AND
|
|
||||||
(Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
|
|
||||||
Begin
|
|
||||||
OpPop(opr);
|
|
||||||
RPNCalc(opr.ch,opr.is_prefix);
|
|
||||||
end;
|
|
||||||
OpPush(Expr[I],false);
|
|
||||||
end;
|
|
||||||
end; { Case }
|
|
||||||
end
|
|
||||||
else Error(invalid_op);
|
|
||||||
{ Handle bad input error }
|
|
||||||
|
|
||||||
While OpTop > 0 do { Pop off the remaining operators }
|
|
||||||
Begin
|
|
||||||
OpPop(opr);
|
|
||||||
RPNCalc(opr.ch,opr.is_prefix);
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ The result is stored on the top of the stack }
|
|
||||||
Evaluate := RPNPop;
|
Evaluate := RPNPop;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -1628,7 +1629,10 @@ end;
|
|||||||
end.
|
end.
|
||||||
{
|
{
|
||||||
$Log$
|
$Log$
|
||||||
Revision 1.6 1998-08-10 14:49:40 peter
|
Revision 1.7 1998-08-18 20:51:32 peter
|
||||||
|
* fixed bug 42
|
||||||
|
|
||||||
|
Revision 1.6 1998/08/10 14:49:40 peter
|
||||||
+ localswitches, moduleswitches, globalswitches splitting
|
+ localswitches, moduleswitches, globalswitches splitting
|
||||||
|
|
||||||
Revision 1.5 1998/07/14 21:46:38 peter
|
Revision 1.5 1998/07/14 21:46:38 peter
|
||||||
|
Loading…
Reference in New Issue
Block a user