BBC BASIC structures
Julie Stamp (8365) 474 posts |
What would make BBC BASIC structures work well in RISC OS? Here are some ideas.
|
Steve Drain (222) 1620 posts |
@Julie You should look at the two pages I posted links to in the BASIC thread. You can also reach them now through the link to Basalt in Sprow’s document. All you ask for has been available in Basalt for about a decade and in a BASIC library for longer. So, you could download Basalt or just the StrongHelp manual or the Structs library and see what can be done. ;-) I would definitely update the Structs library now, but it is functional. BTW, without checking again, I do not think the way structures are implemented in BB4W would satisfy your requirements. |
Steve Drain (222) 1620 posts |
Implementing structures for BASIC is fairly straightforward once you have dynamic memory. The main problem is creating a namespace for each type of structure. The BASIC module aready has linked list code for handling namespaces, but it is a bit of a bottleneck in BASIC itself, without some ‘tricks’. ;-) There is a bit of complexity in resolving strings, arrays and structures, but not more than a few lines in BASIC. Addendum: The structures I am thinking about here are not a new variable type, but a pointer, which I would always place in a float variable for readability. I have experimented with my pseudo-types – entities – but integration with SWIs is not so easy. Recognising a numeric variable as the start of a structure element is unique, I think, when it is followed by a ‘.’ (period). |
Paolo Fabio Zaino (28) 1893 posts |
@ Julie
Given that BASIC is famous for adding a “type” symbol at the end of every variable that is not a float, what would it be the symbol for a struct in BBC BASIC? (if there is going to be one). How members would be accessed? The via the ‘.’ symbol? Would a struct be represented by a pointer to it? And if so, would the ‘.’ symbol (if that is the chosen one) still be valid or would it have a second syntax when accessing members pointed by a pointer? (yeah I am refering to C “→”) If pointers to structs are allowed, would it be valid to use an integer (ptr%) as a pointer to a struct? How would it work when passing a struct to a function? ByRef or ByVal? Would it be allowed to use MyFunc(RETURN MyStruc)? I like the ideas you’re proposing, I would add that a struct should allow pointers to other variables as members. That would allow a lot of potentials, including the possibility to create jump tables in BBC BASIC. I’d propose that it should be possible to create an array of structs. Something like: DIM MyStruct(10) Struct Ok I stop here for now :) |
Theo Markettos (89) 919 posts |
How would you handle typing? You want to prevent somebody accessing a struct a as a struct b. So now you need the concept of a ‘pointer to a struct a’ which differs from a pointer to something else, and you need to check (when dereferencing, presumably) that you’re using the right pointer. Then you need to be able to embed those pointers in other structs. The alternative is dynamic typing, where you just have a generic pointer type and a pointer is determined to be a pointer to a struct a because that’s where it happens to go. You would then error if you try to access a.x when struct a doesn’t have an ‘x’ element. But that’s risky since it still allows you to mix up your types and get away with it if the fields exist. (Or maybe you call this ‘polymorphism’ and declare it to be a language feature) Steve, I had a quick look through your Structs documentation and I didn’t see much discussion about typing. Is everything just an integer pointer, or is there a way to prevent confusing one type with another? Edit: another question: should structs imply a certain organisation in memory? If you want to be able to define say a Wimp icon via a struct, you need some degree of predictability as to how the fields are laid out. That also suggests that the struct itself can’t have any typing information inside it, although it would be possible to keep it in a shadow table elsewhere. |
Rick Murray (539) 13907 posts |
Whoa, hold up a moment there. Unless we’re planning on implementing something akin to union, it could be useful to be able to assign one sort of structure to be understood as another. This could perhaps be handled by the “NEW” concept in Basalt to say “this is structure X, treat it as structure Y”. I don’t know if that’s a possibility?
I thought the BASIC way was “allow it, but don’t document it”. ;-)
Always. If you’re going to define a structure for, say, a catalogue entry or a window definition, that structure will need to be what the rest of the API is expecting, not some weird BASIC thing. Might be worth specifying, before it’s too late, that when writing strings to structures, they will be null terminated (and read likewise).
I think it would be necessary to make a formal distinction between a struct definition, and a struct containing data. |
Steve Drain (222) 1620 posts |
The first thing I say in my documents is:
I do not think you can escape that and it applies equally to C, I think. After that we are discussing the details how to reference the memory and the prototype. The way that Basalt does it is KISS. There is an integer as a handle to a block that has a pointer to the prototype as its first word, followed by the information block. If I were doing this now I would probably have a handle to a header block with pointers to information block and prototype and a size word. The information block itself would be referenced as The extension to a structure variable type is conceptually quite small, but would require much more extensive modification of the interpreter. I think that naked handles are quite widespread in RO and the effort may not be worth it. Even if there is a suitable suffix for these structure types, what about the next type, say lists or tuples? 3 After this you need to invent language to deal with these structures. I chose to distinguish prototype from structure, but BB4W use a hybrid method. So, a prototype is DEFined as a list of items in order. A NEW structure is created by reserving the block of memory and associating the prototype. An existing (OLD) block of memory such as a Window block can be associated with a prototype temporarily – @Rick. Access to structure items is going to use the ‘.’ (period) syntax, I think. That should be no problem in the BASIC interpreter, and having no suffix for a structure type could be seen as an advantage. Basalt has to break out of the interpreter and uses ‘\’ (backslash), which also supplies the correct context to the handle, but that is a singular case, The types of structure item is a choice, but it is not confined to the set of BASIC variables. I chose ones that would make intepreting a Window block easier. 1 There are other things called structures, but they could not deal with Window blocks, for instance. 2 “Prototype” is one of several terms for the same thing. 3 I did invent a pseudo-type, an ‘entity’ as a float variable – no suffix. The first 4 bytes are a handle and the 5th defines the type. This requires no deep modification of the interpreter, but an entity only has meaning in the correct context and is rather vulnerable. I did have use for these but stopped a long while back. |
Steve Drain (222) 1620 posts |
For my own amusement I recently implemented structures with unions using just BASIC, with a handful of code snippets. ;-) This library is intimately integrated with lots of others, so it is not going to see the light of day and it is not documented beyond the REMs. It does allow eg: I have used this with Toolbox structures/objects, but it is a bit over the top. ;-) |
jgharston (7770) 14 posts |
“How members would be accessed? The via the ‘.’ symbol?” You see how it’s already been done: |