Dynamic allocation of BBC BASIC variables from within a BASIC program
tymaja (278) 174 posts |
I’m just teaching myself a bit of Aarch64, messing round with Xen and the like; A question that arises is : How can someone ‘set’ a variable from within a BASIC program? An example program would be: Is there a trick within RISC OS BBC BASIC that allows a programmer to do something like this? I would be extremely grateful if someone knows how to do this! (otherwise I will need to spend time messing with the BASIC stack, LVAR, and so on – and BASIC is written in extremely efficient ARM code, almost as if the creator of the ARM CPU could see the data flowing down each individual line as it went through the CPU :) |
tymaja (278) 174 posts |
Line 30 doesn’t work and wouldn’t be expected to, but is there a way to send a ‘command’ to BASIC somehow – to set a variable dynamically at runtime? |
Steve Pampling (1551) 8170 posts |
You need to be looking at the SWI for setting and reading values. That kind of thing is covered in the Programmers Reference Manuals and the specific SWI’s you’re after to deal with your question are OS_SetVarVal and OS_ReadVarVal Lots of stuff under OS SWI Calls Input for runtime variable change can be pulled from various sources, commonest is of course the keyboard. Checkout OS_Byte key scan is OS_Byte 121. Good examples of SWI use are in various of the BASIC programs that are part of the standard OS install. My best advice is do it different to me. :)
Almost like the people that designed the first ARM and BBC BASIC and the OS tied it all together and were a very small, tight-knit group. |
Steve Drain (222) 1620 posts |
The ususal way is a variation on: PROC_LET(A$,13) ... DEFPROC_LET(var$,val) IF EVAL("FN_LET("+var$+")") ENDPROC DEFFN_LET(RETURN var):var=val:=TRUE That works for integers or floats and there is the equivalent for strings. There are other ways, but ‘trickier’. ;-) |
Steve Pampling (1551) 8170 posts |
It’s the EVAL I was steering away from. I seem to recall recent discussions about EVAL and compiling… |
Steve Drain (222) 1620 posts |
You cannot do this with ABC because BTW System variable != BASIC variable. ;-) |
Steve Pampling (1551) 8170 posts |
Ah, I was pointing at the method I used to do a Multi-user front end for PopStar in BASIC. |
tymaja (278) 174 posts |
Thank you! I will try these (the EVAL way looks good for what I want for a little project I am working on!) |
Alan Adams (2486) 1149 posts |
That’s helped me out too. I don’t suppose you could explain how the magic works? |
Steve Fryatt (216) 2105 posts |
Think it through.
Since To be honest, I would suggest that if you’re doing stuff like this in production code, you’re almost certainly doing it wrong. There will be a much better solution involving conventional data structures just waiting to be implemented… If the aim is to process external data, consider what happens if the data happens to match a real variable that your program uses. If it isn’t, use an array with sensible indices. |
Steve Drain (222) 1620 posts |
I bow to your judgement, but plead that these will be much more complicated for ‘everyday use’. ;-) BASIC is not replete with data structures, so it is your arrays or Davis’s indirection, or I might revert to extended I think it interesting that the EVAL(varname$)=value
Assembly is one of those. The other is to execute the code fragment |
Alan Adams (2486) 1149 posts |
It is so much easier in PHP – $$var=$val; |