skx / gobasic

A BASIC interpreter written in golang.
https://blog.steve.fi/tags/basic/
GNU General Public License v2.0
328 stars 27 forks source link

Support for IF cond THEN line-number #82

Closed udhos closed 5 years ago

udhos commented 5 years ago

Support for IF cond THEN line-number

Example:

210 IF D=2 THEN 260

skx commented 5 years ago

This should be a simple fix, unfortunately it is a little harder than it might be.

runIF() has the code to handle the comparison, and handle the execution of the "TRUE" or "FALSE" branches. Currently both actions result in a call to RunOnce() - that can process any kind of instruction.

So to handle "IF 1 < 10 THEN 300 [ELSE 400]" we'd need to add a special case before invoking RunOnce(). Just looking ahead one-token for token.INT should suffice. (On both branches.)

skx commented 5 years ago

Probably best to add a boolean parameter :

RunOnce( int_is_goto bool )

That will change the behavior of the default branch.

Then after that invoke runGOTOTarget(value) which will handle the line-lookup, and change of control-flow. (Moving code from runGOTO() into there.)

skx commented 5 years ago

I left this for a week to see if a volunteer would handle it; since they didn't I'll do it this evening.

It occurred to me I can implement it in a slightly hacky way, while requiring fewer changes :)