builtin

package
v0.0.0-...-437b54e Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: GPL-2.0 Imports: 10 Imported by: 3

Documentation

Overview

Package builtin contains the implementation for the core functions implemented for BASIC, such as SIN, COS, RND, PRINT, etc.

The builtin package also provides an interface which with you can extent the interpreter to supply new primitives, for example you might implement RANDOM, PEEK, POKE, or something entirely different.

Examples of extending the interpreter exist within the `goserver/` and `embed/` packages within the distribution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ABS

func ABS(env Environment, args []object.Object) object.Object

ABS implements ABS

func ACS

func ACS(env Environment, args []object.Object) object.Object

ACS (arccosine)

func ASN

func ASN(env Environment, args []object.Object) object.Object

ASN (arcsine)

func ATN

func ATN(env Environment, args []object.Object) object.Object

ATN (arctan)

func BIN

func BIN(env Environment, args []object.Object) object.Object

BIN converts a number from binary.

func CHR

func CHR(env Environment, args []object.Object) object.Object

CHR returns the character specified by the given ASCII code.

func CODE

func CODE(env Environment, args []object.Object) object.Object

CODE returns the integer value of the specified character.

func COS

func COS(env Environment, args []object.Object) object.Object

COS implements the COS function..

func DUMP

func DUMP(env Environment, args []object.Object) object.Object

DUMP just displays the only argument it received.

func EXP

func EXP(env Environment, args []object.Object) object.Object

EXP x=e^x EXP

func INT

func INT(env Environment, args []object.Object) object.Object

INT implements INT

func LEFT

func LEFT(env Environment, args []object.Object) object.Object

LEFT returns the N left-most characters of the string.

func LEN

func LEN(env Environment, args []object.Object) object.Object

LEN returns the length of the given string

func LN

func LN(env Environment, args []object.Object) object.Object

LN calculates logarithms to the base e - LN

func MID

func MID(env Environment, args []object.Object) object.Object

MID returns the N characters from the given offset

func PI

func PI(env Environment, args []object.Object) object.Object

PI returns the value of PI

func PRINT

func PRINT(env Environment, args []object.Object) object.Object

PRINT handles displaying strings, integers, and errors.

func RIGHT(env Environment, args []object.Object) object.Object

RIGHT returns the N right-most characters of the string.

func RND

func RND(env Environment, args []object.Object) object.Object

RND implements RND

func SGN

func SGN(env Environment, args []object.Object) object.Object

SGN is the sign function (sometimes called signum).

func SIN

func SIN(env Environment, args []object.Object) object.Object

SIN operats the sin function.

func SPC

func SPC(env Environment, args []object.Object) object.Object

SPC returns a string containing the given number of spaces

func SQR

func SQR(env Environment, args []object.Object) object.Object

SQR implements square root.

func STR

func STR(env Environment, args []object.Object) object.Object

STR converts a number to a string

func TAN

func TAN(env Environment, args []object.Object) object.Object

TAN implements the tan function.

func TL

func TL(env Environment, args []object.Object) object.Object

TL returns a string, minus the first character.

func VAL

func VAL(env Environment, args []object.Object) object.Object

VAL converts a string to a number

Types

type Builtins

type Builtins struct {
	// contains filtered or unexported fields
}

Builtins holds our state.

func New

func New() *Builtins

New returns a new helper/holder for builtin functions.

func (*Builtins) Get

func (b *Builtins) Get(name string) (int, Signature)

Get the values associated with the given built-in.

func (*Builtins) Register

func (b *Builtins) Register(name string, nArgs int, ft Signature)

Register records a built-in function. The three arguments are:

NAME  - The thing that the BASIC program will call
nARGS - The number of arguments the built-in requires.
        NOTE: Arguments are comma-separated in the BASIC program,
        but commas are stripped out.
FT    - The function which provides the implementation.

type Environment

type Environment interface {
	// StdInput is a handle to a reader-object, allowing input to
	// be processed by the built-ins.
	StdInput() *bufio.Reader

	// StdOutput is a handle to a writer-object, allowing output to
	// be generated by the built-ins.
	StdOutput() *bufio.Writer

	// StdError is a handle to a writer-object, allowing user errors to
	// be communicated by the built-ins.
	StdError() *bufio.Writer

	// LineEnding specifies any additional characters that should be
	// appended to PRINT commands - for example '\n'
	LineEnding() string

	// Data allows the builtins to get a reference to the intepreter.
	Data() interface{}
}

Environment is an interface which is passed to all built-in functions.

type Signature

type Signature func(env Environment, args []object.Object) object.Object

Signature is the signature of a builtin-function.

Each built-in will receive an array of objects, and will return a single object back to the caller.

In the case of an error then the object will be an error-object.

Jump to

Keyboard shortcuts

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