BBC BASIC
« Curious 'expression' in EVAL tokenizer »

Welcome Guest. Please Login or Register.
Mar 31st, 2018, 10:39pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Cross-platform BBC BASIC (Windows, Linux x86, Mac OS-X, Android, iOS, Raspberry Pi)
BBC BASIC Resources
BBC BASIC Help Documentation
BBC BASIC for Windows Home Page
BBC BASIC Programmers' Reference
BBC BASIC Beginners' Tutorial
BBC BASIC for SDL 2.0 Home Page
BBC BASIC Discussion Group

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Curious 'expression' in EVAL tokenizer  (Read 103 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: 803
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 »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls