Proposal for assigning an address to a structure

for discussion of bbc basic for windows/sdl, brandy and more
Post Reply
Deleted User 9295

Proposal for assigning an address to a structure

Post by Deleted User 9295 »

One of the few things that irritates me about BBC BASIC is that there is no built-in way of assigning a memory address to a structure, something that you commonly want to do if (for example) you are using structures to implement a linked list. It wasn't too bad in the 32-bit-only days because one could write:

Code: Select all

      !(^struct{}+4) = address%
but with compatibility with 64-bits now being so important you typically end up using a procedure:

Code: Select all

      DEF PROC_setptr(RETURN d{},p%%)
      IF INKEY(-256)=&73 IF @platform% AND &40 ](^d{}+8)=p%% ELSE !(^d{}+4)=!^p%%
      ENDPROC
Whilst this is effective at hiding the ugliness, it's not fast or elegant. Therefore I'm wondering about implementing this extension to the language:

Code: Select all

      PTR(struct{}) = address%%
There are no significant compatibility implications (currently this syntax will result in a 'Missing #' error) and I don't think it would be too difficult to achieve, in BBCSDL at least (it could be considered for BB4W at a future date).

Thoughts?
User avatar
BigEd
Posts: 6261
Joined: Sun Jan 24, 2010 10:24 am
Location: West Country
Contact:

Re: Proposal for assigning an address to a structure

Post by BigEd »

Looks a little weird to me because that LHS doesn't look like an l-value. Perhaps I misunderstand?
Deleted User 9295

Re: Proposal for assigning an address to a structure

Post by Deleted User 9295 »

BigEd wrote: Sat Aug 08, 2020 3:56 pm Looks a little weird to me because that LHS doesn't look like an l-value. Perhaps I misunderstand?
Why is it any less like an l-value than, for example:

Code: Select all

      PTR#(chan) = n%%
or

Code: Select all

      LEFT$(a$,3) = "BBC"
Indeed one of the attractions of the syntax is that it overloads a token that is by definition an l-value (&CF, the left form of PTR).

I'm open to alternative suggestions, but as always they must pass the compatibility test (no existing program must be broken) so you're pretty much stuck with a syntax that currently results in an error.
User avatar
BigEd
Posts: 6261
Joined: Sun Jan 24, 2010 10:24 am
Location: West Country
Contact:

Re: Proposal for assigning an address to a structure

Post by BigEd »

Good point about the string functions being an lvalue: I've never used them that way, but I do see that this is now established.

It seems to me the desire is for something like a cast (in C) whereas I was reading it somewhat like a function.
Deleted User 9295

Re: Proposal for assigning an address to a structure

Post by Deleted User 9295 »

BigEd wrote: Sat Aug 08, 2020 5:00 pm Good point about the string functions being an lvalue: I've never used them that way, but I do see that this is now established.
They've been in BBC BASIC as l-values since Sophie's BASIC V, and in Microsoft BASICs about as long.
It seems to me the desire is for something like a cast (in C) whereas I was reading it somewhat like a function.
It's not easy to extend BBC BASIC without adding a new keyword (and that isn't compatible with existing programs which may have used it as a variable name). For example we know from when 64-bit indirection was added that there are literally no spare (single character) symbols left that we can press into service; every 7-bit ASCII symbol already has a function - and several have two or more - in 'modern' BBC BASIC!

So overloading an existing operator or keyword is really the only option, and PTR has the advantage of already having a 'left' form and of implying the required functionality ('set something's pointer'). So whilst of course I want to remain flexible and keep as many people on board as possible, it seems to me unlikely that a better syntax is available.
User avatar
BigEd
Posts: 6261
Joined: Sun Jan 24, 2010 10:24 am
Location: West Country
Contact:

Re: Proposal for assigning an address to a structure

Post by BigEd »

I'm fine with your proposal (now I understand the background and constraints)
Soruk
Posts: 1136
Joined: Mon Jul 09, 2018 11:31 am
Location: Basingstoke, Hampshire
Contact:

Re: Proposal for assigning an address to a structure

Post by Soruk »

Very clean and tidy. I like it!

(I guess it could be extended to get or set the address of any variable?)
Matrix Brandy BASIC VI (work in progress) The Distillery (another work in progress) Note Quiz (New educational software for the BBC and modern kit)
BBC Master 128, PiTubeDirect (Pi 3B), Pi1MHz, 5.25+3.5in dual floppy.
Deleted User 9295

Re: Proposal for assigning an address to a structure

Post by Deleted User 9295 »

Soruk wrote: Wed Aug 12, 2020 8:16 am (I guess it could be extended to get or set the address of any variable?)
There's been a discussion elsewhere about extending it to strings and arrays (it's only appropriate to objects which are 'double indirected' so it can't really be applied to scalar variables). I'm not entirely convinced but I'm thinking about it.

Wanting to change the address of a string or an array is, in my experience, far less common - and potentially dangerous in the case of a string because it could break the 'garbage collection'. But if you look at it as a 32/64-bit-agnostic way of achieving indirection I can see the argument.
alex_farlie
Posts: 179
Joined: Sun Jul 07, 2013 10:46 pm
Contact:

Re: Proposal for assigning an address to a structure

Post by alex_farlie »

Interesting.

So would this allow a construction like PTR (struct{})= DIM prototype{} ? to set up a pointer to a newly allocated amount of memory based on a existing record/structure declaration?

(Elsewhere on the wiki , you wrote an item about de-referencing.)
Post Reply

Return to “modern implementations of classic programming languages”