Author |
Topic: Curious 'expression' in EVAL tokenizer (Read 105 times) |
|
treehouse
New Member
member is offline
Posts: 13
|
|
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?
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 803
|
|
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: 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.
|
|
Logged
|
|
|
|
treehouse
New Member
member is offline
Posts: 13
|
|
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.
|
|
Logged
|
|
|
|
|