Author Topic: EVAL in Yabasic  (Read 3547 times)

Galileo

  • Guest
EVAL in Yabasic
« 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$)
« Last Edit: July 08, 2016, 04:35:33 PM by Galileo »

ScriptBasic

  • Guest
Re: EVAL in Yabasic
« Reply #1 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.

B+

  • Guest
Re: EVAL in Yabasic
« Reply #2 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.
« Last Edit: July 17, 2017, 09:18:02 PM by B+ »

Richly

  • Guest
Re: EVAL in Yabasic
« Reply #3 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

Richly

  • Guest
Re: EVAL in Yabasic
« Reply #4 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.

ZXDunny

  • Guest
Re: EVAL in Yabasic
« Reply #5 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.