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

122 - Fix swallowline #123

Closed skx closed 11 months ago

skx commented 11 months ago

It was reported that the following program fails, due to issues with swallowing newlines:

REM This is a comment
PRINT "OK"

This comes about because the swallowLine implementation we have consumes the newline it uses to stop its search, however the interpreter then goes on to increment the line:

func (e *Interpreter) RunOnce() error {
  ..

case token.REM:
    err = e.swallowLine()
    case token.RETURN:
            ...

  ..

//
// Ready for the next instruction
//
e.offset++

//
// Error?
//
if err != nil {
    return err
}
return nil

As an easy fix for the moment I've updated the swallowLine implementation to actually return with the current token being the newline, not the one after that:

A similar solution would have been to add a break after the token.REM handler, but special-casing that felt wrong.

This closes #122.