Version: v0.0.0-...-99d8a64 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2022 License: GPL-2.0 Imports: 13 Imported by: 3



Package eval contains the evaluator which executes the BASIC programs.

The interpreter is intentionally simple:

1. The input program is parsed into a series of tokens.

2. Each token is executed sequentially.

There are distinct handlers for each kind of built-in primitive such as REM, DATA, READ, etc. Things that could be pushed outside the core, such as the maths-primitives (SIN, COS, TAN, etc) have been moved into their own package to keep this as simple and readable as possible.



This section is empty.


This section is empty.


This section is empty.


type ForLoop

type ForLoop struct {
	// contains filtered or unexported fields

ForLoop is the structure used to record a for-loop

type Interpreter

type Interpreter struct {

	// STDIN is an input-reader used for the INPUT statement
	STDIN *bufio.Reader

	// STDOUT is the writer used for PRINT and DUMP statements
	STDOUT *bufio.Writer

	// STDERR is the writer used for user facing errors during program execution
	STDERR *bufio.Writer

	// LINEEND defines any additional characters to output when printing
	// to the output or error streams.
	LINEEND string
	// contains filtered or unexported fields

Interpreter holds our state.

func FromString

func FromString(input string) (*Interpreter, error)

FromString is a constructor which takes a string, and constructs an Interpreter from it - rather than requiring the use of the tokenizer.

func New

func New(stream *tokenizer.Tokenizer) (*Interpreter, error)

New is our constructor.

Given a lexer we store all the tokens it produced in our array, and initialise some other state.

func NewWithContext

func NewWithContext(ctx context.Context, stream *tokenizer.Tokenizer) (*Interpreter, error)

NewWithContext is a constructor which allows a context to be specified.

It will defer to New for the basic constructor behaviour.

func (*Interpreter) Data

func (e *Interpreter) Data() interface{}

Data returns a reference to this underlying Interpreter

func (*Interpreter) GetArrayVariable

func (e *Interpreter) GetArrayVariable(id string, index []int) object.Object

GetArrayVariable gets the contents of the specified array value.

Useful for testing/embedding

func (*Interpreter) GetTrace

func (e *Interpreter) GetTrace() bool

GetTrace returns a boolean result indicating whether debugging information is output to STDOUT during the course of execution.

func (*Interpreter) GetVariable

func (e *Interpreter) GetVariable(id string) object.Object

GetVariable returns the contents of the given variable.

Useful for testing/embedding.

func (*Interpreter) LineEnding

func (e *Interpreter) LineEnding() string

LineEnding defines an additional characters to write after PRINT commands

func (*Interpreter) RegisterBuiltin

func (e *Interpreter) RegisterBuiltin(name string, nArgs int, ft builtin.Signature)

RegisterBuiltin registers a function as a built-in, so that it can be called from the users' BASIC program.

Useful for embedding.

func (*Interpreter) Run

func (e *Interpreter) Run() error

Run launches the program, and does not return until it is over.

A program will terminate when the control reaches the end of the final-line, or when the "END" token is encountered.

func (*Interpreter) RunOnce

func (e *Interpreter) RunOnce() error

RunOnce executes a single statement.

func (*Interpreter) SetArrayVariable

func (e *Interpreter) SetArrayVariable(id string, index []int, val object.Object) error

SetArrayVariable sets the contents of the specified array value.

Useful for testing/embedding

func (*Interpreter) SetTrace

func (e *Interpreter) SetTrace(val bool)

SetTrace allows the user to enable output of debugging-information to STDOUT when the intepreter is running.

func (*Interpreter) SetVariable

func (e *Interpreter) SetVariable(id string, val object.Object)

SetVariable sets the contents of a variable in the interpreter environment.

Useful for testing/embedding.

func (*Interpreter) StdError

func (e *Interpreter) StdError() *bufio.Writer

StdError allows access to the error-writing object.

func (*Interpreter) StdInput

func (e *Interpreter) StdInput() *bufio.Reader

StdInput allows access to the input-reading object.

func (*Interpreter) StdOutput

func (e *Interpreter) StdOutput() *bufio.Writer

StdOutput allows access to the output-writing object.

type Loops

type Loops struct {
	// contains filtered or unexported fields

Loops is the structure which holds ForLoop entries

func NewLoops

func NewLoops() *Loops

NewLoops creates a new for-loop holder

func (*Loops) Add

func (l *Loops) Add(x ForLoop)

Add stores a reference to a for-loop in our map

func (*Loops) Empty

func (l *Loops) Empty() bool

Empty returns true if we have no open FOR loop-references.

func (*Loops) Get

func (l *Loops) Get(id string) ForLoop

Get returns a reference to a for-loop.

func (*Loops) Remove

func (l *Loops) Remove(id string)

Remove removes a reference to a for-loop.

type Stack

type Stack struct {
	// contains filtered or unexported fields

Stack holds the stack-data, protected by a mutex

func NewStack

func NewStack() *Stack

NewStack returns a new stack (for holding integers)

func (*Stack) Empty

func (s *Stack) Empty() bool

Empty returns `true` if our stack is empty.

func (*Stack) Pop

func (s *Stack) Pop() (int, error)

Pop returns an item from our stack.

func (*Stack) Push

func (s *Stack) Push(v int)

Push adds a new item to our stack.

type Variables

type Variables struct {
	// contains filtered or unexported fields

Variables holds our state

func NewVars

func NewVars() *Variables

NewVars handles a new variable-holder.

func (*Variables) Get

func (v *Variables) Get(name string) object.Object

Get returns the value stored against the specified name.

func (*Variables) Set

func (v *Variables) Set(name string, val object.Object)

Set stores the given value against the specified name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL