How to emulate Beta BASIC's KEYIN?
Posted: Wed Apr 06, 2011 2:13 pm
The BASICs written by Andy Wright (Beta BASIC for the ZX Spectrum; SAM BASIC and MasterBASIC for the SAM Coupé) have a nice command called KEYIN. It receives a string and "types" it in "virtually" into the command line. It simply evaluates the BASIC code and executes it. This feature makes it possible to write self-modifying programs (though the SAM Coupé's version of the command is quite buggy for this) and, more interesting, store BASIC code into arrays, files or DATAs and execute it at will (very useful for decision trees and many other things).
In order to use it in a program, I'm trying to replicate KEYIN in S*BASIC, without success.
First try, with DO:
Second try, with MERGE:
I tried also the MERGE version in a subroutine, but no difference: MERGE cannot be used if the return stack is not empty (no mention about this in the manuals I have).
Any suggestion will be apreciated. I'm running out of ideas at the moment. I've searched for something similar in any toolkit I know of, but found nothing.
In order to use it in a program, I'm trying to replicate KEYIN in S*BASIC, without success.
First try, with DO:
Code: Select all
defproc keyin(sbasic_code$):rem first try
rem Evaluate a string that contains S*BASIC code
rem It doesn't work, because DO stops the program.
loc file$,channel
let file$="ram1_keyin_tmp"
let channel=fop_over(file$)
rem if channel<0:fatal_error "keyin: "&channel
print #channel,sbasic_code$
close #channel
do file$
delete file$
enddef
Code: Select all
defproc keyin(sbasic_code$):rem second try
rem Evaluate a string that contains S*BASIC code
rem It doesn't work, because MERGE cannot be used inside a procedure or while any procedure is active.
loc file$,merge_line,channel
let file$="ram1_keyin_tmp"
let merge_line=32767
let channel=fop_over(file$)
rem if channel<0:fatal_error "keyin: "&channel
print #channel,merge_line&"defproc do_keyin:"&sbasic_code$&":enddef"
close #channel
merge file$
do_keyin
dline merge_line
delete file$
enddef
Any suggestion will be apreciated. I'm running out of ideas at the moment. I've searched for something similar in any toolkit I know of, but found nothing.