Author Topic: Expression evaluator  (Read 19467 times)

0 Members and 2 Guests are viewing this topic.

Peter

  • Guest
Re: Expression evaluator
« Reply #15 on: November 29, 2013, 03:01:46 AM »
Code: [Select]
Turkey is the focus on the day before Black Friday.
Turkey

It's really funny.

Turkey    =   Pute   <  German
to roast a turkey

Turkey    =  Türkei  < German
therewith, Berlin is the largest Turkish community outside of Turkey.

What is now 'TURKEY' ?

Charles Pegge

  • Guest
Re: Expression evaluator
« Reply #16 on: November 29, 2013, 03:52:59 AM »
ThanksGiving survivors, special edition:




This version supports variable names, and lookup array.

Code: [Select]
'SIMPLE EVALUATOR

' supporting:
' +-*/
' floating ppoint values
' variables a..z
' brackets
' multiple statements and lines

indexbase 0

% maxvar 1024

string vn[maxvar] ' variable name
double vv[maxvar] ' variable store
double st[16]     ' stack value
sys    sp[16]     ' stack operator

sys    vnb=1      ' base of var lists
sys    vne=1      ' end of var lists


function wordbound(byte*b) as sys
=================================
do
  select b
  case "0" to "9"
  case "A" to "Z"
  case "a" to "z"
  case else : exit do
  end select
  @b++
end do
return @b
end function


function newvar(string wr,double v) as sys
==========================================
vn[vne]=wr 'new variable name
vv[vne]=0  'value
vne++
if vne>maxvar then vne=maxvar 'clamp
return vne-1
end function


function lookup(string wr) as sys
=================================
sys i=vnb,f=0
do
  if i>=vne then exit do 'end of var list
  if wr=vn[i] then f=i : exit do
  i++
end do
return f
end function


function lookupv(sys p) as sys
==============================
byte b at (*p)
byte e at (wordbound b)
sys lw=@e-@b
if lw=0 then return 0 'empty word
*p=@e-1 'update source position
string wr=nuls lw
copy strptr(wr),@b,lw
sys f=lookup(wr)
if not f then f=newvar(wr,0)
return f
end function


function evalnm(sys *dp, double *v,sys b)
=========================================
b-=48
if dp=0
  v=v*10+b
else
  dp*=10
  v=v+b/dp
end if
end function


function evalop(sys op, double *a,v)
====================================
select op
case 0   : a=v
case "+" : a+=v
case "-" : a-=v
case "*" : a*=v
case "/" : a/=v
case
end select
end function


function eval(string s) as double
=================================
byte b at (strptr s) 'source string
double a       'accum
double v       'value
sys    op      'operator
sys    ai      'accum index
sys    si      'stack index
sys    vi      'variable index
sys    dp      'decimal point
do
  select b
  case 0          : evalop(op,a,v) : return a
  case 10 to 13   : evalop(op,a,v) : vv[ai]=a : a=0 : v=0 : op=0 : dp=0
  case ":"        : evalop(op,a,v) : vv[ai]=a : a=0 : v=0 : op=0 : dp=0
  case "0" to "9" : evalnm(dp,v,b)
  case "A" to "Z" : vi=lookupv(@@b) : v=vv(vi) : dp=0
  case "a" to "z" : vi=lookupv(@@b) : v=vv(vi) : dp=0
  case "="        : ai=vi
  case "."        : dp=1
  case 42 to 47   : evalop(op,a,v) : op=b : v=0 : dp=0
  case "("        : st[si]=a : sp[si]=op : a=0 : v=0 : op=0 : dp=0 : si++
  case ")"        : evalop(op,a,v) : si-- : v=a : a=st[si] : op=sp[si] : dp=0
  end select
  @b++
end do
end function

print eval("av=32 : bv=16.25 : 2*(av+bv) ") '96.5


JRS

  • Guest
Re: Expression evaluator
« Reply #17 on: November 29, 2013, 09:44:06 AM »
Quote
What is now 'TURKEY' ?

That definition is determined by the number of credit cards in your wallet, what you think is a deal and how much you like crowded stores.

Aurel

  • Guest
Re: Expression evaluator
« Reply #18 on: November 29, 2013, 10:08:43 AM »
Hi Charles..
Wow it is cool piece of code and i need some time to figure what is what
it looks that most important operation here is :
Code: [Select]
string wr=nuls lw
copy strptr(wr),@b,lw
print "WORD:" + wr
so finally i get string (word)..i don't know nothing about copy command  ???
if i understand this ...
this function copy byte @b with len of word lw into string(pointer) wr ...right?
and of course i add:
Code: [Select]
string wr=nuls lw
copy strptr(wr),@b,lw
print "WORD:" + wr
float f = GetIntValue(wr)
'print "F:" + f
return f

and change in Eval()

Code: [Select]
case "a" to "z"     
     v=lookupv(@@b) :dp=0
and variable value is properly calculated...

thanks  :)

PS.
source code for my interpreter look like this:
Code: [Select]
defINT n,start1,e,i,x,r
Set n=1,start1 = 2,e=3
wForm 100,0,400,400,#MMS,0,"New Window!"
Set r = start1*(3+5)
wtext 10,50,"Result is: "
wtext 140,50,r

Peter

  • Guest
Re: Expression evaluator
« Reply #19 on: November 30, 2013, 03:14:43 AM »
Quote
That definition is determined by the number of credit cards in your wallet, what you think is a deal and how much you like crowded stores.

Sir John,

Are you feeling well?  :D

JRS

  • Guest
Re: Expression evaluator
« Reply #20 on: November 30, 2013, 08:52:54 AM »
I have learned my lesson.

Don't try to be funny on international forums. You will spend your whole life explaining the joke.  :-\

Charles Pegge

  • Guest
Re: Expression evaluator
« Reply #21 on: November 30, 2013, 10:17:30 AM »
People go shopping-mad after eating all those turkeys:


JRS

  • Guest
Re: Expression evaluator
« Reply #22 on: November 30, 2013, 10:33:03 AM »
Dictionary:

Turkey

Slang
a. A person considered inept or undesirable.
b. A failure, especially a failed theatrical production or movie.

On another note. (pun intended)

Quote from: Charles - JRS forum
I suspect the Bitcoin market is vulnerable to price manipulation - for instance, indirect purchase of own bitcoins at a high price to inflate the perceived market value.

I think Theo sold his cow for a few magic beans. Fools breed like rabbits.

It doesn't get much sadder than this. Why would anyone buy EZGUI when there are plenty of free libraries (with source) available that do a much better job? EZGUI is the Windows 3.1 of GUI toolkits.

Quote
Sorry if this is an inappropriate post, but I'm at the end of my rope.

Just to say I'm a long term customer of PowerBASIC from using Turbo Basic in the Borland days, to PowerBASIC when it was Spectra publishing and then PowerBASIC 3.5 for DOS, PowerBASIC Console Compiler 3 and Classic PowerBASIC 8 Windows Compiler.

On the 8th November I ordered PowerBASIC Console Compiler 5, on the 13th November I had an Invoice which I duly paid, then nothing....

I tried emailing sales@powerbasic.com, and again nothing. After mailing again and hearing nothing I raised a dispute through PayPal, this at least seem to do something as I had a reply from PowerBASIC on the 25th November apologising for the delay and now nothing again.

My emails are still going unanswered. Now before I escalate the dispute further I'm hoping someone in PowerBASIC will see this post and try and resolve whatever the issue is. The order number is 388264.

Thanks

Rob

Chris,

It makes it a little hard to sell add-ons when your potential customers are unable to buy the compiler. Really, find something else to do and stop abusing yourself.



A metaphor for effect.


 
« Last Edit: December 03, 2013, 01:17:28 AM by John »

Aurel

  • Guest
Re: Expression evaluator
« Reply #23 on: December 05, 2013, 08:07:48 AM »
Back to topic ..please  :)

Charles...
I have a question for you.
I tried few methods for expression evaluator and all of them in my case( in my interpreter)...
when expression is inside FOR/looop constantly increase memory usage.
If loop is  bigger,i mean more iteration use more memory .
Why is that?
If is anything else inside loop like (like printing counter on window) then there is no
memory up.
Is this maybe because string operations or something else( recursion maybe ??????

Charles Pegge

  • Guest
Re: Expression evaluator
« Reply #24 on: December 05, 2013, 09:30:10 PM »
Your code may be dumping lots of old strings.

If you can avoid string expressions like s=s+t in iterations, and use a buffer expression, it will make a big difference in performance:

string s=nuls(1000)
sys i=1
...
mid(s,i)=t : i+=len(t)
...
return left(s,i-1)



Aurel

  • Guest
Re: Expression evaluator
« Reply #25 on: December 06, 2013, 09:40:38 AM »
ok i see
But how explain that if i use your evaluator or few other methods all of them
produce memory usage growth when calculation is inside loop.
If this same loop just show counter then there is no change in memory consuption ::)
it looks like mistery to me ::)

Charles Pegge

  • Guest
Re: Expression evaluator
« Reply #26 on: December 07, 2013, 01:32:33 AM »
Are you returning eval() into a string?

Aurel

  • Guest
Re: Expression evaluator
« Reply #27 on: December 07, 2013, 03:25:13 AM »
No Charles...
I return to float
it is under subroutine ParseExpr()
float rf
string cExpr
rf = Eval(cExpr)

ok i add code into attachment so you can try...
here is example;
Code: [Select]
defINT n,s
Set n = 1,s=2
wForm 100,0,400,400,#MMS,0,"New Window!"
LoopTo n,1,120000,1
  set s = n+1
wtext 100,100,s
Shift n
wtext 250,40,"Test TEXT"
« Last Edit: December 12, 2013, 11:44:09 AM by Aurel »

Aurel

  • Guest
Re: Expression evaluator
« Reply #28 on: December 07, 2013, 06:00:15 AM »
Charles
what you mean ...is maybe a problem in recursion,because in most cases those
functions are called recursive.?
i have also found this:
Quote
Assigning an empty string to a variable

PA This is the usual way to clear a string variable.

Text$ = ""

What a waste! First of all, the string "" takes 6 bytes of RAM each time you use it. Consider the alternative:

Text$ = vbNullString

So what is this? vbNullString is a special VB constant that denotes a null string. The "" literal is an empty string. There's an important difference. An empty string is a real string. A null string is not. It is just a zero. If you know the C language, vbNullString is the equivalent of NULL.
« Last Edit: December 07, 2013, 06:08:00 AM by Aurel »

Charles Pegge

  • Guest
Re: Expression evaluator
« Reply #29 on: December 08, 2013, 01:55:23 AM »
Hi Aurel,

No problem with recursion.

Oxygen accepts null strings as well as empty strings.
dim'ed strings are initially null.

string s
print strptr s 'result: 0



I found two potential memory leaks in OxygenBasic:

  autoconverting from a numeric function to a string

  autoconverting from a string to a number

I should have both of these fixed later today