• Stars
    star
    311
  • Rank 129,814 (Top 3 %)
  • Language
    Go
  • License
    GNU General Publi...
  • Created over 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A BASIC interpreter written in golang.

Go Report Card license Release

05 PRINT "Index"





10 PRINT "GOBASIC!"

This repository contains a naive implementation of BASIC, written in Golang.

If you'd prefer to see a more "real" interpreted language, implemented in Go, you might prefer monkey.

The implementation is simple for two main reasons:

  • There is no UI, which means any and all graphics-primitives are ruled out.
    • However the embedded sample, described later in this file, demonstrates using BASIC to create a PNG image.
    • There is also a HTTP-based BASIC server, also described later, which allows you to create images "interactively".
  • I didn't implement the full BASIC set of primitives.
    • Although most of the commands available to the ZX Spectrum are implemented. I only excluded things relating to tape, PEEK, POKE, etc.
    • If you want to add new BASIC keywords this is easy, and the samples mentioned above do that.

The following obvious primitives work as you'd expect:

  • DIM
  • END
    • Exit the program.
  • GOTO
    • Jump to the given line.
  • GOSUB / RETURN
    • Used to call the subroutines at the specified line.
  • IF / THEN / ELSE
    • Conditional execution.
  • INPUT
    • Allow reading a string, or number (see later note about types).
  • LET
    • Assign a value to a variable, creating it if necessary.
  • FOR & NEXT
    • Looping constructs.
  • PRINT
    • Print a string, an integer, or variable.
    • Multiple arguments may be separated by commas.
  • REM
    • A single-line comment (BASIC has no notion of multi-line comments).
  • READ & DATA
  • SWAP
  • DEF FN & FN

Most of the maths-related primitives I'm familiar with are also present, for example SIN, COS, PI, ABS, along with the similar string-related primitives:

  • LEN "STEVE"
    • Returns the length of a string "STEVE" (5).
  • LEFT$ "STEVE", 2
    • Returns the left-most 2 characters of "STEVE" ("ST").
  • RIGHT$ "STEVE", 2
    • Returns the right-most 2 characters of "STEVE" ("VE").
  • CHR$ 42
    • Converts the integer 42 to a character (*). (i.e. ASCII value.)
  • CODE " "
    • Converts the given character to the integer value (32).




20 PRINT "Limitations"

This project was started as a weekend-project, although it has subsequently been improved and extended.

The code has near-total test-coverage, and has been hammered with multiple days of fuzz-testing (i.e. Feeding random programs into the interpreter to see if it will die - see FUZZING.md for more details on that.)

That said there are some (obvious) limitations:

  • Only a single statement is allowed upon each line.
  • Only a subset of the language is implemented.
    • If there are specific primitives you miss, then please report a bug.
      • The project is open to suggestions, but do bear in mind the project goals listed later on.
  • When it comes to types only floating-point and string values are permitted.
    • There is support for arrays but only one or two dimensional ones.

Arrays

Arrays are used just like normal variables, but they need to be declared using the DIM statement. Individual elements are accessed using the offsets in brackets after the variable name:

10 DIM a(10,10)
20 LET a[1,1]=10
30 PRINT a[1,1], "\n"

Arrays are indexed from 0-N, so with an array size of ten you can access eleven elements:

 10 DIM a(10)
 20 a[0] = 0
 30 a[1] = 1
 40 ..
 90 a[9] = 9
100 a[10] = 10

ZX Spectrum BASIC indexed arrays from 1, denying the ability to use the zeroth element, which I've long considered a mistake.

Line Numbers

Line numbers are mostly optional, for example the following program is valid and correct:

 10 READ a
 20 IF a = 999 THEN GOTO 100
 30 PRINT a, "\n"
 40 GOTO 10
100 END
    DATA 1, 2, 3, 4, 999

The main reason you need line-numbers is for the GOTO and GOSUB functions, if you prefer to avoid them then you're welcome to do so.

IF Statement

The handling of the IF statement is perhaps a little unusual, since I'm used to the BASIC provided by the ZX Spectrum which had no ELSE clause. The general form of the IF statement I've implemented is:

IF $CONDITIONAL THEN $STATEMENT1 [ELSE $STATEMENT2]

Only a single statement is permitted between "THEN" and "ELSE", and again between "ELSE" and NEWLINE. These are valid IF statements:

IF 1 > 0 THEN PRINT "OK"
IF 1 > 3 THEN PRINT "SOMETHING IS BROKEN": ELSE PRINT "Weird!"

In that second example you'll see that ":" was used to terminate the PRINT statement, which otherwise would have tried to consume all input until it hit a newline.

The set of comparison functions probably includes everything you need:

  • IF a < b THEN ..
  • IF a > b THEN ..
  • IF a <= b THEN ..
  • IF a >= b THEN ..
  • IF a = b THEN ..
  • IF a <> b THEN ..
  • IF a THEN ..
    • This passes if a is a number which is not zero.
    • This passes if a is a string which is non-empty.

You can see several examples of the IF statement in use in the example examples/70-if.bas.

DATA / READ Statements

The READ statement allows you to read the next value from the data stored in the program, via DATA. There is no support for the RESTORE function, so once your data is read it cannot be re-read.

Builtin Functions

You'll also notice that the primitives which are present all suffer from the flaw that they don't allow brackets around their arguments. So this is valid:

10 PRINT RND 100

But this is not:

10 PRINT RND(100)

This particular problem could be fixed, but I've not considered it significant.

Types

There are no type restrictions on variable names vs. their contents, so these statements are each valid:

  • LET a = "steve"
  • LET a = 3.2
  • LET a% = ""
  • LET a$ = "steve"
  • LET a$ = 17 + 3
  • LET a% = "string"

The sole exception relates to the INPUT statement. The INPUT statement prompts a user for input, and returns it as a value - it doesn't know whether to return a "string" or a "number". So it returns a string if it sees a $ in the variable name.

This means this reads a string:

10 INPUT "Enter a string", a$

But this prompts for a number:

10 INPUT "Enter a number", a

This seemed better than trying to return a string, unless the input looked like a number (i.e. the input matched /^([0-9\.]+)$/ we could store a number, otherwise a string).





30 PRINT "Installation"

We don't pull in any external dependencies, except for the embedded examples, so installation is simple.

git clone https://github.com/skx/gobasic
cd gobasic
go install

You can also install directly via:

go install github.com/skx/gobasic@latest

If you don't have a golang environment setup you should be able to download a binary release from our release page:





40 PRINT "Usage"

gobasic is very simple, and just requires the name of a BASIC-program to execute. Write your input in a file and invoke gobasic with the path.

For example the following program was useful to test my implementation of the GOTO primitive:

 10 GOTO 80
 20 GOTO 70
 30 GOTO 60
 40 PRINT "Hello, world!\n"
 50 END
 60 GOTO 40
 70 GOTO 30
 80 GOTO 20

Execute it like this:

$ gobasic examples/10-goto.bas

NOTE: I feel nostalgic seeing keywords in upper-case, but PRINT and print are treated identically.





50 PRINT "Implementation"

A traditional interpreter for a scripting/toy language would have a series of well-defined steps:

  • Split the input into a series of tokens ("lexing").
  • Parse those tokens and build an abstract syntax tree (AST).
  • Walk that tree, evaluating as you go.

As is common with early 8-bit home-computers this implementation is a little more BASIC:

  • We parse the input into a series of tokens, defined in token/token.go
  • We then directly execute those tokens.
    • The execution happens in eval/eval.go with a couple of small helpers:
      • eval/for_loop.go holds a simple data-structure for handling FOR/NEXT loops.
      • eval/stack.go holds a call-stack to handle GOSUB/RETURN
      • eval/vars.go holds all our variable references.
      • We have a facility to allow golang code to be made available to BASIC programs, and we use that facility to implement a bunch of our functions as "builtins".
        • Our builtin-functions are implemented beneath builtin/.
  • Because we support both strings and ints/floats in our BASIC scripts we use a wrapper to hold them on the golang-side. This can be found in object/object.go.

As there is no AST step errors cannot be detected prior to the execution of programs - because we only hit them after we've started running.





60 PRINT "Sample Code"

There are a small number of sample-programs located beneath examples/. These were written in an adhoc fashion to test various parts of the implementation.

Perhaps the best demonstration of the code are the following two samples:

  • examples/90-stars.bas
    • Prompt the user for their name and the number of stars to print.
    • Then print them. Riveting! Fascinating! A program for the whole family!
  • examples/55-game.bas
    • A classic game where you guess the random number the computer has thought of.




70 PRINT "Embedding"

The interpreter is designed to be easy to embed into your application(s) if you're crazy enough to want to do that!

You can see an example in the file embed/main.go.

The example defines several new functions which can be called by BASIC:

  • PEEK
  • POKE
  • PLOT
  • SAVE
  • CIRCLE

When the script runs it does some BASIC variable manipulation and it also creates a PNG file - the PLOT function allows your script to set a pixel and the CIRCLE primitive draws an outline of a circle. Finally the SAVE function writes out the result.

Extending this example to draw filled circles, boxes, etc, is left as an exercise ;)

Hopefully this example shows that making your own functions available to BASIC scripts is pretty simple. (This is how SIN, COS, etc are implemented in the standalone interpreter.)





75 PRINT "DoS"

When it comes to security problems the most obvious issue we might suffer from is denial-of-service attacks; it is certainly possible for this library to be given faulty programs, for example invalid syntax, or references to undefined functions. Failures such as those would be detected at parse/run time, as appropriate.

In short running user-supplied scripts should be safe, but there is one obvious exception, the following program is valid:

10 PRINT "STEVE ROCKS!"
20 GOTO 10

This program will never terminate! If you're handling untrusted user-scripts, you'll want to ensure that you explicitly setup a timeout period.

The following will do what you expect:

// Setup a timeout period of five seconds
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

// Now create the interpreter, via the tokenizer
t := tokenizer.New(string(`10 GOTO 10`))

// Ensure we pass the context over
e, err := eval.NewWithContext(ctx,t)
if err != nil {
   fmt.Printf("error creating interpreter: %s\n", err.Error())
   panic(err)   // proper handling here
}

// Now run the program
err = e.Run()
if err != nil {
  fmt.Printf("error running: %s\n", err.Error())
  panic(err)   // proper handling here
}

// Here we'll see a timeout eror

The program will be terminated with an error after five seconds, which means that your host application will continue to run rather than being blocked forever!

80 PRINT "Visual BASIC!"

Building upon the code in the embedded-example I've also implemented a simple HTTP-server which will accept BASIC code, and render images!

To run this:

cd goserver ; go build . ; ./goserver

Once running open your browser at the URL:

The view will have an area of entering code, and once you run it the result will be shown in the bottom of the screen. Something like this:

alt text

There are several included examples which you can load/launch by clicking upon them.





90 PRINT "Bugs?"

It is probable that bugs exist in this interpreter, but I've tried to do as much testing as I can. If you spot anything that seems wrong please do report an issue.

  • If the interpreter segfaults that is a bug.
    • Even if the program is invalid, bogus, or malformed the interpreter should cope with it.
  • If a valid program produces the wrong output then that is also a bug.

The project contain a number of test-cases, which you can execute like so:

$ go test ./...

Finally if our test-coverage drops beneath 95% that is a bug. The test coverage of most of our packages is 100%, unfortunately the main eval/ package is not yet completely covered.

You can see the global coverage via:

$ ./test-coverage
97.9%

In addition to the test-cases which have been manually written the interpreter has also been fuzz-tested, which has resulted in some significant improvements.

See FUZZING.md for details of how to run the fuzz-tests.





100 PRINT "Project Goals / Links"

It is never the intention of this project to support all things that are possible in the various dialects of BASIC.

There are facilities which will make porting programs useful, such as the ability to use WHILE/END loops, functions with named-parameters, and primitives such as SLEEP, BEEP, & etc.

Above all else this project is supposed to be fun, for me. Which means if there are two ways of implementing something I'll pick the way I remember back when I was 12 and computers were .. fun!

If there are feature-requests which seem less fun, and less immediately useful to me - with my biased memories of coding on a ZX Spectrum - I will tag them "wontfix". If you contribute a pull-request to support them I will accept them, but I'm probably not likely to work upon them directly.

That said there are cases where I can be persuaded, and there are a lot of other BASIC intepreters out there, so I won't feel bad if this particular project doesn't suit your needs.

One project, slightly related to this, which might be worth checking up on is this one:

Github Setup

This repository is configured to run tests upon every commit, and when pull-requests are created/updated. The testing is carried out via .github/run-tests.sh which is used by the github-action-tester action.

Releases are automated in a similar fashion via .github/build, and the github-action-publish-binaries action.

Steve

More Repositories

1

sysadmin-util

Tools for Linux/Unix sysadmins.
Perl
940
star
2

bookmarks.public

A template for self-hosted bookmarks using HTML & jQuery.
JavaScript
660
star
3

tunneller

Allow internal services, running on localhost, to be accessed over the internet..
Go
457
star
4

simple.vm

Simple virtual machine which interprets bytecode.
C
452
star
5

deployr

A simple golang application to automate the deployment of software releases.
Go
323
star
6

go.vm

A simple virtual machine - compiler & interpreter - written in golang
Go
309
star
7

simple-vpn

A simple VPN allowing mesh-like communication between nodes, over websockets
Go
276
star
8

monkey

An interpreted language written in Go
Go
248
star
9

sysbox

sysadmin/scripting utilities, distributed as a single binary
Go
205
star
10

esp8266

Collection of projects for the WeMos Mini D1
C++
162
star
11

kilua

A minimal text-editor with lua scripting.
C++
158
star
12

sos

Simple Object Storage (I wish I could call it Steve's Simple Storage, or S3 ;)
Go
145
star
13

github-action-publish-binaries

Publish binaries when new releases are made.
Shell
134
star
14

evalfilter

A bytecode-based virtual machine to implement scripting/filtering support in your golang project.
Go
110
star
15

rss2email

Convert RSS feeds to emails
Go
104
star
16

e-comments

External comments for static HTML pages, a lightweight self-hosted disqus alternative.
JavaScript
101
star
17

linux-security-modules

A place to store my toy linux-security modules.
C
87
star
18

marionette

Something like puppet, for the localhost only.
Go
84
star
19

kpie

Simple devilspie-like program for window manipulation, with Lua.
C
77
star
20

dhcp.io

Dynamic DNS - Via Redis, Perl, and Amazon Route53.
Perl
69
star
21

foth

Tutorial-style FORTH implementation written in golang
Go
67
star
22

overseer

A golang-based remote protocol tester for testing sites & service availability
Go
62
star
23

templer

A modular extensible static-site-generator written in perl.
Perl
62
star
24

math-compiler

A simple intel/AMD64 assembly-language compiler for mathematical operations
Go
58
star
25

assembler

Basic X86-64 assembler, written in golang
Go
56
star
26

lighthouse-of-doom

A simple text-based adventure game
C
56
star
27

node-reverse-proxy.js

A reverse HTTP-proxy in node.js
JavaScript
53
star
28

webmail

A golang webmail server.
Go
51
star
29

dotfiles

Yet another dotfile-repository
Emacs Lisp
50
star
30

github2mr

Export all your github repositories to a form suitable for 'myrepos' to work with.
Go
46
star
31

puppet-summary

The Puppet Summary is a web interface providing reporting features for Puppet, it replaces the Puppet Dashboard project
Go
45
star
32

org-worklog

A template for maintaining a work-log, via org-mode.
39
star
33

tweaked.io

The code behind http://tweaked.io/
JavaScript
36
star
34

rss2hook

POST to webhook(s) when new feed-items appear.
Go
35
star
35

pam_pwnd

A PAM module to test passwords against previous leaks at haveibeenpwned.com
C
34
star
36

alphavet

A golang linter to detect functions not in alphabetical order
Go
32
star
37

dns-api-go

The code behind https://dns-api.org/
Go
31
star
38

critical

A simple/minimal TCL interpreter, written in golang
Go
31
star
39

markdownshare.com

The code which was previously used at http://markdownshare.com/
Perl
29
star
40

github-action-tester

Run tests when pull-requests are opened, or commits pushed.
Shell
26
star
41

maildir-tools

Golang-based utility which can be used for scripting Maildir things, and also as a basic email client
Go
22
star
42

chronicle2

Chronicle is a simple blog compiler, written in Perl with minimal dependencies.
Perl
20
star
43

purppura

A server for receiving and processing alerts & events.
Go
20
star
44

implant

Simple utility for embedding files/resources inside golang binaries
Go
20
star
45

dns-api.org

The code which was previously used at https://dns-api.org/
Perl
19
star
46

bfcc

BrainFuck Compiler Challenge
Go
18
star
47

ephemeris

A static blog-compiler
Go
15
star
48

markdownshare

The code behind https://markdownshare.com/
Go
15
star
49

z80-examples

Z80 assembly-language programs.
Makefile
15
star
50

yal

Yet another lisp interpreter
Go
14
star
51

aws-utils

A small collection of AWS utilities, packaged as a single standalone binary.
Go
14
star
52

z80retroshield

Arduino library for driving the Z80 retro-shield.
Shell
12
star
53

Device-Osram-Lightify

Interface to the Osram Lightify system
Perl
12
star
54

github-action-build

Build a project, creating artifacts
Shell
12
star
55

webserver-attacks

Identify attacks against webservers via simple rules
Perl
12
star
56

predis

A redis-server written in Perl.
Perl
11
star
57

da-serverspec

ServerSpec.org configuration for the Debian-Administration cluster.
Ruby
10
star
58

docker-api-gateway

Trivial API-gateway for docker, via HAProxy
Go
10
star
59

http2xmpp

HTTP to XMPP (jabber) bridge.
Perl
9
star
60

nanoexec

Trigger commands over a nanomsg queue
C
9
star
61

go-experiments

Repository containing experiments as I learn about golang
Go
9
star
62

labeller

Script label addition/removal for gmail/gsuite email.
Go
8
star
63

golang-metrics

Automatic submission of system metrics to graphite, for golang applications
Go
8
star
64

ms-lite

A collection of plugins for a qpsmtpd-powered virtual-host aware SMTP system.
Perl
8
star
65

remotehttp

Magic wrapper to deny HTTP-requests to to "local" resources.
Go
8
star
66

dashboard

Redis & node.js powered dashboard skeleton
JavaScript
8
star
67

Buffalo-220-NAS

Installing NFS on a Buffalo 220 NAS device
Shell
8
star
68

asql

A toy utility to process Apache log files via SQL.
Perl
7
star
69

DockerFiles

Container for various dockerfiles.
Shell
6
star
70

yawns

Yet another weblog/news site
Perl
6
star
71

cidr_match.js

A simple module to test whether a given IPv4 address is within a particular CIDR range.
JavaScript
6
star
72

pass

password-store distribution, with plugins.
Shell
6
star
73

knownfs

A FUSE-based filesystem that exports ~/.ssh/known_hosts
Go
6
star
74

mpd-web

Simple HTTP view of an MPD server
Go
6
star
75

mod_writable

Disallow serving writable files under Apache 2.4.x
C
5
star
76

org-diary

Easily maintain a simple work-log / journal with the use of org-mode
Emacs Lisp
5
star
77

mod_blacklist

A simple Apache module to blacklist remote hosts.
C
5
star
78

arduino-mega-z80-simplest

The simplest possible project combining an Arduino Mega and a Zilog Z80 processor
C++
4
star
79

turtle

A simple turtle-implementation, using FORTH as a scripting-language
Go
4
star
80

purple

A simplified version of mauvealert
Perl
3
star
81

subcommands

Easy subcommand handling for a golang based command-line application
Go
3
star
82

thyme

A simple package-building system, using docker
Perl
2
star
83

httpd

Simple golang HTTP server
Go
2
star
84

edinburgh.io

Open pub database
JavaScript
2
star
85

run-directory

A simple application inspired by `run-parts`.
Go
2
star
86

Redis--SQLite

Redis-Compatible module which writes to SQLite
Perl
2
star
87

runme

A quick hack for running commands from README.md files
Go
2
star
88

devopswithdocker.com

Repository created for the Helsinki University course.
Dockerfile
2
star
89

aws-list

Export a dump of all running EC2 instances, along with AMI details, AMI age, etc, etc.
1
star
90

calibre-plugins

A small collection of calibre-plugins.
Python
1
star
91

WebService--Amazon--Route53--Caching

Perl module to cache the results of WebService::Amazon::Route53
Perl
1
star
92

lexing-parsing-linting-stuffs

Code to go with my talk
Python
1
star
93

Test--RemoteServer

The Perl module Test::RemoteServer
Perl
1
star
94

org-tag-cloud

Easily maintain a tag-cloud of org-mode tags.
Emacs Lisp
1
star
95

headerfile

Parse files with simple key:value headers, easily.
Go
1
star
96

z80-cpm-scripting-interpreter

A trivial I/O language, with repl, written in z80 assembler to run under CP/M.
Makefile
1
star