eval assign

Discussions related to the extensions to the language available in LBB
tsh73
Posts: 44
Joined: Fri Apr 06, 2018 7:58 pm

eval assign

Post by tsh73 »

sorry I lost source by Richard - cannot find on forum or on Wiki - so I reinvented it and post here for posterity
I consider this to be really handy ability.

Code: Select all

'EVAL ASSIGN
'Assigns variable by passed name
'LBB only

z=1
call evalAssign "z", 1.23
print z

'-------------------------------
sub evalAssign aVar$, aVal
	'use LBB pane to see user function assign
	'to be called as FNassign
	dummy= eval("FNassign(";aVar$;",";aVal;")")
end sub

'helper function
function assign(byRef dest, src)
    'print dest, src
    dest=src
end function
tsh73
Posts: 44
Joined: Fri Apr 06, 2018 7:58 pm

Re: eval assign

Post by tsh73 »

Sorry. I cannot understand WHY it works.

if we have variable "x" at main level,
then in the sub level "x" is undefined
and EVAL("x") should not work, is it?
To fix it I supposed to make "x" global, am I?

Here some test code that shows results inconsistent with my understanding (LB 3.10)
See difference in sub test2 and sub test3
sub test3 has one line remmed out - but it magically makes EVAL work

Code: Select all

'understanding EVAL and scope

xx1$="xx1 is defined"

print "xx1$:";xx1$
print "eval:";eval$("xx1$")
print "expected: defined, defined"
print "got: (defined as expected)"
print 

call test1
print "expected: empty, empty"
print "(empty as expected)"
print 


call test2 "dummy$"
print "expected: empty, empty, dummy$, empty"
print "got: empty, empty, dummy$, 0"
print 

call test2 "xx1$"
print "expected: empty, empty, xx1$, empty"
print "got: empty, empty, xx1$, empty"
print 

call test3 "xx1$"
print "expected: empty, xx1$, empty"
print "got: defined, xx1$, defined "
print 


end



sub test1
'xx1$ is not visible here, does it? and val(xx1$ is empty)
print "xx1$:";xx1$
print "eval:";eval$("xx1$")
end sub

sub test2 aVar$
print "xx1$:";xx1$
print "eval xx1$:";eval$("xx1$")
print "aVar$:";aVar$
print "eval aVar$:";eval$(aVar$)
end sub

sub test3 aVar$
'Exactly like test2 but comment first line. 
'WHOA! Eval on "xx1$" suddenly work
'print "xx1$:";xx1$
print "eval xx1$:";eval$("xx1$")
print "aVar$:";aVar$
print "eval aVar$:";eval$(aVar$)
end sub
results

Code: Select all

xx1$:xx1 is defined
eval:xx1 is defined
expected: defined, defined
got: (defined as expected)

xx1$:
eval:
expected: empty, empty
(empty as expected)

xx1$:
eval xx1$:
aVar$:dummy$
eval aVar$:0
expected: empty, empty, dummy$, empty
got: empty, empty, dummy$, 0

xx1$:
eval xx1$:
aVar$:xx1$
eval aVar$:
expected: empty, empty, xx1$, empty
got: empty, empty, xx1$, empty

eval xx1$:xx1 is defined
aVar$:xx1$
eval aVar$:xx1 is defined
expected: empty, xx1$, empty
got: defined, xx1$, defined