A twisted question about BASIC

What should happen when this code is run?

100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 10
1510 print i
1520 goto 900
1530 next i
1540 print 1540
2000 rem

Now what would you expect to see when this is run?

100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 0
1510 print i
1520 goto 900
1530 next i
1540 print 1540
2000 rem

The final question is how would you handle this if you were writing a BASIC compiler?

It takes a twisted mind to try to write a compiler; it requires a really twisted mind to thoroughly test one.

2 Likes

1
2
3
4
5
6
7
8
9
10
910

1540

1 Like

I see your issue.

Is NEXT i a goto to the inc/compare/branch logic of the FOR statement
Or is the NEXT i where the inc/compare/branch happens?
Does the NEXT i save it’s location until after compare?

Thinking about it now, I don’t think NEXT should save it’s location and instead should just be unconditional jump back the logic in the FOR.
Then the FOR could jump past the 1530 NEXT when it is done
This would be a simple approach but I don’t know if that is how various basics work.

The plot thickens…

First this code:

100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 2
1510 print i
1520 goto 900
1530 next i
1540 print 1540
2000 rem

with these interpreters:

https://yohan.es/swbasic/

1
2
910

6800 FLEX BASIC

1
2
910

GWBASIC

1
NEXT without FOR in 900

Then this code:

100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 0
1510 print i
1520 goto 900
1530 next i
1540 print 1540
2000 rem

with these interpreters:

https://yohan.es/swbasic/

1540

6800 FLEX BASIC

1
910

GWBASIC

1540

And then this code:

100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 0
1510 print i
1520 goto 1700
1530 next i
1540 print 1540
1550 goto 2000
1700 next i
1710 print 1710
2000 rem

with these interpreters:

https://yohan.es/swbasic/

1540

6800 FLEX BASIC

1
1710

GWBASIC

1540

And finally this code:

100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 2
1510 print i
1520 goto 1700
1530 next i
1540 print 1540
1550 goto 2000
1700 next i
1710 print 1710
2000 rem

with these interpreters:

https://yohan.es/swbasic/

1
2
1710

6800 FLEX BASIC

1
2
1710

GWBASIC

1
NEXT without FOR in 1700

Two different interpretations of “FOR I = 1 TO 0”:

  • find the next “NEXT”; that is the bottom of the loop
  • the loop always executes at least once

It is interesting that GWBASIC only considers the first NEXT following the FOR to be valid.

1 Like

Dartmouth TeleBASIC on telehack.com just prints 1-10 one numeral per line.

                                                                                                                                                           
Connected to TELEHACK port 81                                                                                                                              
                                                                                                                                                           
It is 2:19 pm on Tuesday, June 23, 2020 in Mountain View, California, USA.                                                                                 
There are 55 local users. There are 26639 hosts on the network.                                                                                            
                                                                                                                                                           
  Type HELP for a detailed command list.                                                                                                                   
  Type NEWUSER to create an account.                                                                                                                       
                                                                                                                                                           
May the command line live forever.                                                                                                                         
                                                                                                                                                           
Command, one of the following:                                                                                                                             
  2048        ?           a2          ac          advent      basic                                                                                        
  bf          c8          cal         calc        ching       clear                                                                                        
  clock       cowsay      date        ddate       echo        eliza                                                                                        
  factor      figlet      finger      fnord       geoip       help                                                                                         
  hosts       ipaddr      joke        login       mac         md5                                                                                          
  morse       newuser     notes       octopus     phoon       pig                                                                                          
  ping        primes      privacy     qr          rain        rand                                                                                         
  rfc         rig         roll        rot13       sleep       starwars                                                                                     
  traceroute  units       uptime      usenet      users       uumap                                                                                        
  uupath      uuplot      weather     when        zc          zork                                                                                         
  zrun                                                                                                                                                     
                                                                                                                                                           
.basic                                                                                                                                                     
Dartmouth DTSS TeleBASIC (c) 1964,1966,1969,1970,1971,1979        

>100 goto 1500                                                                                                                                             
>900 next i                                                                                                                                                
>920 goto 2000                                                                                                                                             
>1500 for i = 1 to 10                                                                                                                                      
>1510 print i                                                                                                                                              
>1520 goto 900                                                                                                                                             
>1530 next i                                                                                                                                               
>1540 print 1540                                                                                                                                           
>2000 rem                                                                                                                                                  
>run                                                                                                                                                       
 1                                                                                                                                                         
 2                                                                                                                                                         
 3                                                                                                                                                         
 4                                                                                                                                                         
 5                                                                                                                                                         
 6                                                                                                                                                         
 7                                                                                                                                                         
 8                                                                                                                                                         
 9                                                                                                                                                         
 10 

ahh… that’s why.

The branching takes it to 900 then back to 1500. Steps to 1510. Steps to 1520 Jumps to 900 and when i == 10 is true jumps to 2000 then halts from lack of further instructions.

1 Like

It is interesting that GWBASIC only considers the first NEXT following the FOR to be valid.

Yeah GWBASIC was the start of smarter parsing and took some queues from pascal and c compilers. QuickBASIC/QBasic took it a step further. But basically by that time the microsoft interpreter stop look at for loops as aliases to a set of do while jumps and looked at for loops the same way as this c-code:

for (int x=0; x<n; x++) 
{
    // user instructional code block here
} // next i

I guess to answer the question:

The final question is how would you handle this if you were writing a BASIC compiler?

The approach I’d go with is maybe something more akin to quickbasic or PowerBASIC; unless I’m strapped for clock cycles and couldn’t write the pre-bootstrapped interpreter in something like c otherwise I’d go with dartmouth’s approach but maybe introduce virtual machine like zmachine.

Not that I’m crazy enough to do such; YACC makes me want to pull my hair out when I even try to dig into it more than a simple layer 7 protocol generator or dsl for configs.

The latest version of my BASIC torture code is:

100 a = 0
200 gosub 1500
300 a = 1
400 gosub 1500
500 end
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 2
1510 print i
1520 if a = 0 then goto 900
1530 goto 1700
1540 next i
1550 print 1550
1560 goto 2000
1700 next i
1710 print 1710
2000 return

Running it with https://yohan.es/swbasic/ yields:

1
2
910
1
2
1710

It appears that the NEXT statement “tells” the FOR statement the location of the bottom of the loop.

Interesting they have the starwars ASCII text movie.

Must resist advent…

1 Like

Let’s add another version of BASIC to the mix. I have a DuinoMite-Mega board that has a Microchip PIC32 processor on board. It uses MMBASIC, Version 4.5, written by Geoff Graham. I have a copy of the source code for this MMBASIC interpreter, it is written in C. Several years ago I requested a copy of the source code and the author sent it to me.

Here are the results of your short programs…





This sure brings back some old memories…

3 Likes

Apple IIe - Applewin emulator
bill2-appleiie-applewin-emulator

Atari 800 - Altima emulator
bill2-atari-800-altima-emulator

Commodore Pet - Vice emulator

Commodore 128 - Vice emulator

Commodore 64 - Vice emulator

1 Like

1500 For I=1 to 0

1 Like

Around the same time, I bought these development boards:

http://www.ic0nstrux.com/xgs-avr-8-bit-development-system

http://www.ic0nstrux.com/products/development-kits/xgs-pic-16-bit-development-system

They had a BASIC cross compiler, but nothing to program them natively. I started to write a BASIC interpreter for them. The project derailed when I did not figure out a way to handle the FOR statement. Now that the idea of always running the loop at least once provides the answer to the “where is NEXT” problem, it will probably go back on the projects pile.

Thanks for the correction. Here is the correct listing.


This is on a DuinoMite-Mega Board running MMBasic, Version 4.5 by Geoff Graham using a 32 bit Microchip PIC 32 processor.

1 Like