Brandy BASIC non-standard array declaration

Here you can talk about anything related to BBC BASIC, not covered in another category
Richard Russell
Posts: 665
Joined: Tue 18 Jun 2024, 09:32

Brandy BASIC non-standard array declaration

Post by Richard Russell »

Does anybody happen to know why Brandy BASIC (specifically Matrix Brandy BASIC but I expect it applies to both) accepts a non-standard array declaration using square brackets as well as the conventional syntax using parentheses:

Code: Select all

      DIM a[10]
The resultant array isn't obviously any different from that created by the standard DIM a(10) but such a deliberate departure from conventional BBC BASIC syntax must surely have some purpose.

This feature doesn't seem to extend to accessing individual array elements - you can't write a[5] = PI for example - so it's a bit of a mystery.
Ric
Posts: 310
Joined: Tue 17 Apr 2018, 21:03

Re: Brandy BASIC non-standard array declaration

Post by Ric »

Sorry Richard, I've never used brandy basic. I realise that in a world of exacts, it is unlikely, but is it possible it is an oversight?
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Richard Russell
Posts: 665
Joined: Tue 18 Jun 2024, 09:32

Re: Brandy BASIC non-standard array declaration

Post by Richard Russell »

Ric wrote: Fri 03 Apr 2026, 18:07 is it possible it is an oversight?
It's hard to imagine how a programmer could 'accidentally' write code to accept an alternative character. It's not as though '(' and '[' are consecutive in the ASCII table, or indeed related in any simple way ('(' is &28, '[' is &5B). Presumably the check for two characters rather than one must actually slow the code down, if only slightly. All that screams 'deliberate' to me.
I've never used brandy basic
I'm surprised. It's hugely more popular than my BASICs, not least because it's so much faster.
Richard Russell
Posts: 665
Joined: Tue 18 Jun 2024, 09:32

Re: Brandy BASIC non-standard array declaration

Post by Richard Russell »

Richard Russell wrote: Fri 03 Apr 2026, 18:41 I'm surprised. It's hugely more popular than my BASICs, not least because it's so much faster.
More than three times faster, on average. :cry:

timing1.png
timing2.png
You do not have the required permissions to view the files attached to this post.
Richard Russell
Posts: 665
Joined: Tue 18 Jun 2024, 09:32

Re: Brandy BASIC non-standard array declaration

Post by Richard Russell »

Richard Russell wrote: Fri 03 Apr 2026, 21:02 More than three times faster, on average. :cry:
Comparing those two timing lists is instructive, if depressing: every single measurement is slower in BBCSDL than it is in Brandy!

However there's a big variation in how much slower it is: the closest I can see on a quick visual scan is A%=C%^D% which is 59 ns in Brandy and 64 ns in BBCSDL, so there's not always a huge difference.

By contrast the biggest difference I can see (excluding the loops) is for A%=B%/C% which is 25 ns in Brandy and 141 ns in BBCSDL, a ratio of about 5.6. That probably indicates a difference of approach between the two when it comes to division.

My BASICs assume that / always means floating-point division (because there's a specific operator DIV for integer division) so internally B% and C% will be converted to floats, a floating-point division performed, and then the result will be converted back to an integer to be stored in A%.

Judging by how similar the timings of A%=B%/C% and A%=B%DIVC% are in Brandy, I would guess it doesn't do any conversions and simply treats both as an integer division in this case.

Checking the speed of Sophie's original 6502 BASIC it shows the same sort of difference between / and DIV as mine, suggesting that in hers / is also always floating point. That I followed her lead in this respect wouldn't be surprising.

The really huge anomaly is that in Brandy loops (particularly NEXT) take basically no time at all! That must indicate some extreme optimisation; I know that unlike 'traditional' BBC BASIC (including mine) Brandy doesn't execute - i.e. interpret - the original source code but performs some translation to an intermediate semi-compiled form first.

Whether anything could be learnt from Brandy to help improve the speed of mine is uncertain, but given that both are Open Source anybody so inclined is welcome to take a look and propose (or implement) changes.