Jump to content
Sign in to follow this  
rje

Adding While to BASIC 2.0

Recommended Posts

Just out of curiosity, what sort of pain would be involved in adding a major syntactic feature like WHILE?

I understand that a WHILE is little more than

	W_LABEL:   
IF expr GOTO W_CONTINUE
	GOTO W_DONE
	W_CONTINUE:
	...do stuff..
GOTO W_LABEL
	W_DONE:
	

...but that's not trivial, injecting a new thing into BASIC 2.0.

I also understand that C's FOR construct is just an initial condition and a WHILE with a "last" statement in the loop.

F_BEGIN:
	<initialization step>
	F_WHILE:
	IF <test step> GOTO F_CONTINUE
	GOTO F_DONE
	F_CONTINUE:
	...do stuff..
	<last step>
	GOTO F_WHILE
	F_DONE
	

  • Like 1

Share this post


Link to post
Share on other sites

Thinking a little about it, it seems that WHILE can be modeled after BASIC 2.0's FOR statement, right?  Because the FOR has an embedded test for stopping the loop.  So it's kind of like a WHILE.

Or, put another way, the two share some common logic patterns, if not actual code.

 

Edited by rje

Share this post


Link to post
Share on other sites

GW-Basic (and subsequently QBasic) had a nice, simple syntax for WHILE:

10 X = 0

20 WHILE X < 5

30 PRINT X

40 X = X + 1

50 WEND

Of course, this is a bad code example, as you should really just use FOR in this case, but there is a good precedent for clean, terse while loops in other Microsoft BASICs.

  • Like 3

Share this post


Link to post
Share on other sites

So maybe I should ask... is FORTH good for writing things like BASIC interpreters?

 

 

Share this post


Link to post
Share on other sites

Certainly ... it makes it much easier to experiment with interpretation models and work out how to eliminate bottlenecks, and then when the model is in place, convert the Basic keyword actions into assembly for speed.  But that would be for a Basic loaded from disk, for extending the ROM Basic, the tools are a good assembler and a good breakpoint/watchpoints single step capable monitor.

 

Share this post


Link to post
Share on other sites
10 hours ago, SlithyMatt said:

GW-Basic (and subsequently QBasic) had a nice, simple syntax for WHILE:

10 X = 0

20 WHILE X < 5

30 PRINT X

40 X = X + 1

50 WEND

Of course, this is a bad code example, as you should really just use FOR in this case, but there is a good precedent for clean, terse while loops in other Microsoft BASICs.

A DO ...WHILE from QBasic would be easier to implement At DO, push an "impossible" FOR entry, then WHILE executes the test, if it is true, use the for loop entry to repeat, if it fails, continue. It's the same as if you say: FOR I = 1 TO 2 ... IF <test> THEN I=I-1 ... NEXT I.

WHILE ... WEND can borrow the FOR stack too, but the extra work with WHILE ... WEND is when it falls out of the loop, you have to scan through each statement looking for the matching WEND ... which means maintaining a depth count starting at 1 that you increment with each WHILE token and decrement with each WEND token until it hits zero, then continue executing.

But like GW-BASIC, CBM V2 BASIC is a little less flexible about having the same keyword do different things in different contexts. Therefore I would advise someone looking to add WHILE WEND to start out with DO ... UNTIL ... when they have the evaluation of an expression and the FOR stack management and looping worked out, that is everything for WHILE ... WEND except scanning the program to find the WEND that closes the current WHILE. And then just leave off the other three versions of DO ... LOOP, so the only closer for DO loops is UNTIL and WHILE only occurs in a WHILE ... WEND loop.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

Please review our Terms of Use