RetroBASIC

Basicprogramming(.org) => Code and examples => Topic started by: Galileo on July 08, 2016, 04:32:57 PM

Title: EVAL in Yabasic
Post by: Galileo on July 08, 2016, 04:32:57 PM
The Yabasic 2.763 interpreter does not have an equivalent to the EVAL function than other BASIC dialects.

But it can be implemented, thanks to the instructions COMPILE and EXECUTE. Here is the code that allows it.

Code: [Select]
//=====================

export sub eval(c$)
static linea

linea=linea+1
c$="sub s"+str$(linea+1000000,"#######")+"():return "+c$+":end sub"
compile c$
return execute(mid$(c$,5,8))
end sub


//=====================

print eval("3*5")
print eval("5^2*(pi/sin(7))")
input "Formula to evaluate: " formula$
print eval(formula$)
Title: Re: EVAL in Yabasic
Post by: ScriptBasic on July 08, 2016, 05:15:41 PM
I enabled a EVAL like ability in Script BASIC by using the multi-threading feature of the language and creating a thread calling itself from within the interpreter.
Title: Re: EVAL in Yabasic
Post by: B+ on July 09, 2016, 02:50:07 AM
This is my quick calculator, it just uses CHAIN and an Environment variable EVAL in an eval function.

Code: [Select]
'quick calculator.bas  SmallBASIC 0.12.2 [B+=MGA] 2016-04-12
'Thanks shian!

repeat
  input "enter string to evaluate ";es
  if es <> "" then ? eval(es)
until es = ""

func eval(s)
  Chain "env " + enclose("EVAL=") + " + Str(" + s + ")"
  eval = env("eval")
end

Append: screen shot 2017-07-17  Note x and e did not have values assigned, PI is constant in SmallBASIC, and the radians /degrees switch that is used with this test code from another FB EVAL function was not set to convert degrees to radians.
Title: Re: EVAL in Yabasic
Post by: Richly on July 09, 2016, 07:48:34 PM
The Yabasic 2.763 interpreter does not have an equivalent to the EVAL function than other BASIC dialects.

But it can be implemented, thanks to the instructions COMPILE and EXECUTE. Here is the code that allows it.

Code: [Select]
//=====================

export sub eval(c$)
static linea

linea=linea+1
c$="sub s"+str$(linea+1000000,"#######")+"():return "+c$+":end sub"
compile c$
return execute(mid$(c$,5,8))
end sub


//=====================

print eval("3*5")
print eval("5^2*(pi/sin(7))")
input "Formula to evaluate: " formula$
print eval(formula$)

BBC BASIC for Windows has the EVAL function and so this works:
Code: [Select]
      PRINT EVAL("3*5")
      PRINT EVAL("5^2*(PI/SIN(7))")
      INPUT "Formula to evaluate: " formula$
      PRINT EVAL(formula$)
      END
Title: Re: EVAL in Yabasic
Post by: Richly on July 09, 2016, 07:56:50 PM
BBC BASIC for Windows has the EVAL function

Sinclair BASIC and SpecBAS use the VAL command to do the same thing.
Title: Re: EVAL in Yabasic
Post by: ZXDunny on July 10, 2016, 01:27:25 PM
BBC BASIC for Windows has the EVAL function

Sinclair BASIC and SpecBAS use the VAL command to do the same thing.

Yes, VAL will convert a string to VM-Bytecode and execute it to get a result - you can also precompile using TOKEN$:

LET a$=TOKEN$ "my-expression"
PRINT VAL a$

Or you can just use VAL "my-expression" but it will be slightly slower in tight loops due to compilation being done every time. VAL$ works identically, but returns the result as a string.

EXECUTE can be used to run user-built code rather than just expressions.

See my example code from the Efil demo:

http://www.zxspectrum4.net/forum/viewtopic.php?f=8&t=157

But that's a pretty extreme example of using EXECUTE to create self-modifying code.