Priority of unary minus and exponentiation

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Hated Moron

Re: Priority of unary minus and exponentiation

Post by Hated Moron »

Hated Moron wrote: Sat 05 Nov 2022, 09:32 No expression evaluation, no
This feature of BBC BASIC contributed to its speed. In a traditional BASIC the syntax of a built-in function call is, for example, ABS(<expression>) which means that even in a case where the expression is degenerate, i.e. consists of a single factor, the expression evaluator is called.

And with the kind of expression evaluator that BBC BASIC uses, at least, there is a significant overhead in calling it 'unnecessarily': there are at least half-a-dozen nested function calls and returns plus tests for all the possible operators.

But the ability to omit the parentheses means that the syntax becomes ABS<factor> which completely bypasses the need to call the expression evaluator and suffer its associated overheads!

I'm not sure what other BASICs give the programmer the ability to call the expression evaluator, or not, in this way. Of course you may say that omitting the parentheses impairs readability, which is true, but in speed-critical code that may be an acceptable price to pay.
Hated Moron

Re: Priority of unary minus and exponentiation

Post by Hated Moron »

On 05/11/2022 11:16, J.G.Harston wrote (cross-posted from the Discussion Group):
Looking at the code I think maybe EvalUnaryMinus could be
jsr EvaluateExpression instead of jsr EvalNumber, so '-' would
essentially imply a following '('.
As I'm sure you appreciate, that wouldn't have the right effect at all. Consider this expression:

Code: Select all

      PRINT -5-3
Which should of course print -8. If you tried to handle the unary minus by effectively adding a parenthesis it would become:

Code: Select all

      PRINT -(5-3)
which would print -2!