uses console

string xps="100 + ( 200 * 3 ) * 10 + 4" 'expression=6104
string xrs="100 200 3 * 10 * 4 + +"     'expression=6104
'STACKS
/*
pushv 100
pusha +
pushb (
pushv 200
pusha *
pushv 3
popb  )
popax *    close backet
pusha *
pushv 10
popax *    precedence over+
pusha +
pushv 4
popax    + 
popax    +
*/

'INFIX
======
'nestings
double acc[16] 'accumulators
int aop[16]    'operators
int abl[16]    'bracket level
int apl[16]    'precedence level
'int afn[16]   'functions
double n       'number to operate on
int aci=1      'accum index
int le=len xps ' length of expression
int bl=0       'bracket level
int pl=1       'precedence level
int op=0       'operator
int po=0       'previos operator
int i=1        'char indexer
int ppl=1      'previous precedence level
string ns      'digit collector
'
'read bytes left to right
byte b at strptr xps
do
  if i>le
    gosub operate
    while aci>1
      gosub unnest : gosub opstep
    wend
    exit do
  endif
  select b[i]
    case 0 to 32
    case 45
      'itr 4.0E-3 etc
      if po=0 
        ns+=chr(b[i]) 'neg numbers
        i++ : continue do
      endif
    case 46, 48 to 57, 65 to 90, 97 to 122
      ns+=chr(b[i])
      i++ : continue do
    case else
      n=val ns
      ns=""
      po=op
      ppl=pl
  end select
  '
  select b[i]
    case 43: op=43 : pl=1 : gosub operate
    case 45:
      if po 
        op=45 : pl=1 : gosub operate
      endif
    case 42: op=42 : pl=2 : gosub operate
    case 47: op=47 : pl=2 : gosub operate
    case 40: gosub nest : bl++
    case 41: gosub unnest : gosub operate
  end select
  i++
loop
print "result: " acc[1] cr
pause
end

operate:
========
if pl>ppl
  gosub nest
  acc[aci]=n
  n=0
  ret
endif
gosub opstep
n=0
if pl<ppl
  gosub unnest
endif
ret

opstep:
=======
select po
  case 0  : acc(aci) =n
  case 43 : acc(aci)+=n
  case 45 : acc(aci)-=n
  case 42 : acc(aci)*=n
  case 47 : acc(aci)/=n
end select
ret

nest:
=====
apl(aci)=ppl
abl(aci)=bl
aop(aci)=po
if aci<16
  aci++
else
  'nesting err
endif
'
po=0
ppl=pl 
acc[aci]=0
ret

unnest:
=======
n=acc(aci)
if aci>1
  aci--
else
  'nesting errerr
endif
ppl=apl(aci)
bl=abl(aci)
po=aop(aci)
ret


