Create script from script

I'm making my BASIC interpreter as I said I would last year. Except it runs really slow. I wrote a program to draw a circle

10 LET RADIUS = 100
20 LET ORIGINX = 100
40 FOR A=0 TO 360 STEP 5
50 LET AR = A / 180 * PI

(i put this here in case my project is finished so you can copy+paste the code into there)
(Also TGOTO and TDOWN are somethings I added in my implementation)

It takes about 13 seconds to finish. So I was wondering about converting that code into Snap! blocks. To do that I would need to figure out how to make a script from scripts, so I could use the run block to run it, and it wouldn't take 13 seconds to finish drawing a circle.

Just wondering, what technique are you using the parse the BASIC code?

Well first I convert it into a list of tokens, so 10 LET RADIUS = 100 would turn into

const	10
var		num		RADIUS
const	100

Then the program executes it

I suppose I should give out the source code

You can translate individual BASIC commands to blocks by using inputs, so LET RADIUS = 100 would be translated by this procedure:

The deep idea here is that the procedure created in the REPORT block has to remember the values of VARIABLE and VALUE from this particular call to TRANSLATE LET.

And note that I had to delete the default value 0 from the SET block.

Then to make a script out of a bunch of these, you just

Those can be nested to build up a script of any length recursively. Or if you have a list of individual commands' scripts, make this block take one list input and put a FOR EACH ITEM in the ring.

The thing to remember is that you don't want any of these actions to actually happen while building up a BASIC program; you want to run the whole thing when the user says RUN.The gray rings say "don't do this now, just remember what the user wants to do."

You'll have to invent something for line numbers. Maybe each of the commands like TRANSLATE LET should actually report a list with two items, the line number and the action. Then GOTO can look through the list of actions for a matching line number.


Oh right other solutions exist. I don't know why I thought that I had to make a script using scripts. I don't like posting for help because usually when I ask for help I figure it out 1-2 hours later. In fact, I was thinking of a solution similar to yours after creating the topic. But I didn't say that I solved it or anything because I still didn't know how to create a script from script. That was the reason why I made this topic in the first place. My BASIC project was the motivation.

Anyway, thanks

EDIT: Now that I think about it I don't really think it would be useful if someone figured out how to make a script from scripts. They can just use your (bh's) solution instead. So your solution would be a workaround to creating script from scripts.

1 Like

You're quite welcome. Sounds like a worthy project (i.e., one you'll learn from).

1 Like