skx / gobasic

A BASIC interpreter written in golang.

Home Page:https://blog.steve.fi/tags/basic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for SWAP

udhos opened this issue · comments

commented

gobasic should support SWAP.

Documentation: http://www.antonis.de/qbebooks/gwbasman/swap.html

$ gobasic swap.bas 
Error running program:
	Line 30 : Object{Type:error, Value:The variable 'swap' doesn't exist}
$ 
$ more swap.bas 
10 let a=1
20 let b=2
30 swap a,b
40 print a
50 print b

This wouldn't be hard to implement, although the core built-ins aren't capable of being used.

Most of our functions, such as the maths/string primitives, are implemented beneath the builtin/ package/directory. However they receive the expanded version of the arguments.

So:

10 LET a = 3
20 PRINT a

Actually outputs "3", and that happens because the value 3 is passed to the PRINT-function. The print-primitive doesn't fetch the value of a, and then output 3.

That means if you want to implement SWAP it goes in eval/eval.go - just like LET, FOR, etc, etc.

It doesn't become hard. But it does become more annoying than it otherwise would be. (There will also need to be a new token-type, and lexer-update. Those are near-trivial though :) )

I might implement this, but otherwise it is a good first issue to tackle :)

I might implement this, but otherwise it is a good first issue to tackle :)

Implemented:

    frodo ~/go/src/github.com/skx/gobasic $ cat swap.bas 
    10 LET a = 3
    20 LET b = 41
    30 PRINT "A:" , a, " B:", b, "\n"
    40 SWAP a,b
    50 PRINT "A:" , a, " B:", b, "\n"
    60 PRINT "\n"

    100 DIM a(10)
    110 a[2] = 33
    120 PRINT "a[2]:", a[2], " a[3]", a[3], "\n"
    130 SWAP a[2], a[3]
    140 PRINT "a[2]:", a[2], " a[3]", a[3], "\n"
    150 PRINT "\n"

    1000 DIM a(3)
    1010 LET a[1] = "steve"
    1020 LET a[2] = "kemp"
    1030 PRINT a[1], " ", a[2], "\n"
    2000 SWAP a[1], a[2]
    2010 PRINT a[1], " ", a[2], "\n"
    2020 PRINT "\n"

Output is as-expected:

   frodo ~/go/src/github.com/skx/gobasic $ go build . ; ./gobasic  swap.bas 
   A: 3  B: 41 
   A: 41  B: 3 

   a[2]: 33  a[3] 0 
   a[2]: 0  a[3] 33 

   steve   kemp 
   kemp   steve 

It needs some cleanup, but should be ready later in the morning, then perhaps time for a new release.

Also needs tests since our coverage has slipped:

   frodo ~/go/src/github.com/skx/gobasic $ ./test-coverage 
   94.7%