Menu

Compiling BASIC to Go to Binary

2019-01-20
2019-02-03
  • Everton da Silva Marques

    Hi guys,

    First of all, thanks to Rob for PC-BASIC. It is amazing. It brings sweet old memories of fun with BASIC.

    Currently I am sketching a toy compiler to translate BASIC programs to Go. Then the Go code can be compiled to native binary. It works like this:

    basgo-build < program.bas > program.go ;# translates to Go
    go build program.go ;# compiles to executable
    ./program ;# executes the binary program
    

    Right now, it can handle only simple BASIC programs.

    I use the awesome PC-BASIC as authoritative reference for the implementation of that BASIC-to-Go toy compiler. :-)

    One can find the full toy compiler here: https://github.com/udhos/basgo

    If you find fun in compiling old BASIC code to executable code, let me know. :-)

    Cheers,
    Everton

     
  • Rob Hagemans

    Rob Hagemans - 2019-02-02

    Hi Everton, that's great! I'll give it a try.

    I noted that you introduce an extension keyword GOFUNC - could I suggest naming it _GOFUNC (with a leading underscore)?

    The reason is that the GW-BASIC syntax forbids using keywords as identifiers, so adding a keyword will make the compiler incompatible with any program that has for example something like GOFUNC$="GO" or DEF FNGOFUNC(x)=x+1. The underscore is not otherwise legal in GW-BASIC identifiers , so this avoids a namespace clash. It's the same approach taken by QB64 and I've borrowed the idea for PC-BASIC extension functions as well.

     
    • Everton da Silva Marques

      Thanks for the _GOFUNC hint.

      I filed an issue to fix it asap:

      https://github.com/udhos/basgo/issues/3

       
    • Everton da Silva Marques

      Rob, I've added your _GOFUNC suggestion. It looks like this now:

      everton@homeubu:~/basgo$ more gofunc/rad.bas 
      110 rem Using _GOIMPORT and _GODECL to embed Go code within BASIC code
      120 rem
      130 _goimport("math")
      140 _godecl("func degToRad(d float64) float64 {")
      150 _godecl("    return d*math.Pi/180")
      160 _godecl("}")
      170 rem
      180 rem Now using _GOFUNC to call that Go function from BASIC code
      190 rem
      200 d = 180
      210 r = _gofunc("degToRad", d)
      220 print d;"degrees in radians is";r
      everton@homeubu:~/basgo$ 
      everton@homeubu:~/basgo$ basgo-build < gofunc/rad.bas > a.go && go run a.go
       180 degrees in radians is 3.141592653589793 
      everton@homeubu:~/basgo$ 
      
       
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.