Hi Aurel,
I tried to recreate Richard's code from the code at LBB without the ().
I found out my VAL() and MID() do not work the same as his. I like his, better!
VAL() returns something until it hits a non number thing, NOT 0 if say there is a + sign in the string, like Val("99 bottles of beer.") = 99
NOT 0
MID("thisIsTooShort", 99) = ""
NOT Crash with Error message.
So with those fixed, I can't tell if his can handle -3--2? because my modified one doesn't but does OK with all other things, I think. No! negative signs aren't working eg 1-2 no, but 1 -2 OK?
' EVAL trans & mod of Richard.bas SmallBASIC 0.12.9 (B+=MGA) 2017-07-20
' Dang it! JB deleted Richard's LBB translation and mod for ()
' Here it is before () handled? can I recreate Richard's with ()
' copied from LBB forum 2017-07-20
' hmm... how much do I remember from my short study?
' this thing is barely working need to fix -- and spaces
dim pcp(5)
pcp(1) = 1 : pcp(2) = 1 : pcp(3) = 2 : pcp(4) = 2 : pcp(5) = 3
' Test of expression evaluator
'e = "-3*-2" 'wow OK
'e = "-3--2" '2? nope
'e = "-3/-2" 'OK
e = "2 ^ 4" 'nope 1
e = "2^4" 'OK
e = "1-2" '+3-4+5-6+7-8
e = "1 -2+3 -4+5 -6+7 -8" 'O
op = 0 : pr = 1 : x = 99 '???
r = evaluate(e, op, pr, x)
? r
pause
func evaluate(byref e, byref posOper, byref precedence, x)
local copyPosOper, copyPrecedence, copyX, i, getValPos
? "evaluate gets ";e
repeat
copyPosOper = posOper
copyPrecedence = precedence
copyX = x
'x = val(e) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this is returning 0 if any operators are present
getValPos = len(e)
for i = 1 to len(e)
if instr("-.0123456789", mid(e, i, 1)) = 0 then getValPos = i - 1 : exit for
next
x = val(mid(e, 1, getValPos))
'print "x = ";x
repeat
'? "Inner repeat e = ";e
if len(e) < 2 then
posOper = 0
else
e = mid(e, 2)
posOper = instr(1, "+-*/^", left(e, 1))
fi
until posOper<>0 or len(e) < 2
if len(e) > 1 then e = mid(e, 2) ' Skip past operator
precedence = pcp(posOper)
while precedence > copyPrecedence : x = evaluate(e, posOper, precedence, x) : wend
'temp = copyX : copyX = x : x = temp
swap x, copyX
select case copyPosOper
case 0: x = copyX
case 1: x = x + copyX
case 2: x = x - copyX
case 3: x = x * copyX
case 4: x = x / copyX
case 5: x = x ^ copyX
end select
until precedence < copyPrecedence
evaluate = x
end