BBC BASIC
« Curious 'expression' in EVAL tokenizer »

Welcome Guest. Please Login or Register.
Jan 23rd, 2018, 4:34pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Curious 'expression' in EVAL tokenizer  (Read 29 times)
treehouse
New Member
Image


member is offline

Avatar




PM


Posts: 13
xx Curious 'expression' in EVAL tokenizer
« Thread started on: Jan 19th, 2018, 11:31pm »

On this page, http://bb4w.wikispaces.com/Tokeniser there's a clever use of EVAL to get keyword tokens (even from their abreviations), but it uses a syntax I've not seen before.
EVAL("1:"+A$)
It evaluates to whatever (numeric) is before the colon, it appears.
Is this a special case, the expression with a colon in it?
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 692
xx Re: Curious 'expression' in EVAL tokenizer
« Reply #1 on: Jan 20th, 2018, 12:47pm »

on Jan 19th, 2018, 11:31pm, treehouse wrote:
Is this a special case, the expression with a colon in it?

Not exactly. To avoid EVAL throwing an error the supplied string parameter must be a legal (numeric or string) expression, but at the same time we want to take advantage of the fact that the string passed to EVAL is first passed through the tokeniser.

At first sight these two requirements are contradictory: the string that we want to tokenise is almost certainly not a valid numeric or string expression. The way this is resolved is something of a cheat: we take any valid numeric expression (e.g. "1") and the code snippet we want to tokenise (e.g. "PRINT PI") and we join them using the normal statement separator character which is a colon:

Code:
"1:PRINT PI" 

With this trick we have achieved what we set out to. The overall string is a valid numeric expression (because the expression evaluator naturally terminates at a statement separator) but it is followed by the code snippet we want to tokenise. Fortunately the tokeniser does not stop at a statement separator!

So the colon isn't a "special case", because it's the regular statement separator character, but its use in the string passed to EVAL is definitely 'unusual'. This same trick, or a variant of it, works in pretty much every version of BBC BASIC. What does vary, of course, is where in memory the tokenised string can be found.

Richard.
User IP Logged

treehouse
New Member
Image


member is offline

Avatar




PM


Posts: 13
xx Re: Curious 'expression' in EVAL tokenizer
« Reply #2 on: Jan 20th, 2018, 3:24pm »

Quote:
The overall string is a valid numeric expression (because the expression evaluator naturally terminates at a statement separator) but it is followed by the code snippet we want to tokenise. Fortunately the tokeniser does not stop at a statement separator!

Ingenious! Thank you, Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls