value

package
v0.0.0-...-8cefd3a Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: ISC Imports: 4 Imported by: 2

Documentation

Overview

Package value implements Lisp values and their evaluation.

Index

Constants

This section is empty.

Variables

View Source
var (
	EOF = Error("EOF")
	T   = Intern("t")
	NIL = Intern("nil") // also: empty list
)
View Source
var SystemEnvironment = &env{
	env: map[string]Value{
		"atom":   Func1(atom),
		"null":   Func1(null),
		"eq":     equalFn,
		"equal":  equalFn,
		"car":    Func1(car),
		"cdr":    Func1(cdr),
		"caar":   Func1(caar),
		"cadr":   Func1(cadr),
		"cddr":   Func1(cddr),
		"caddr":  Func1(caddr),
		"cadar":  Func1(cadar),
		"caddar": Func1(caddar),
		"cons":   Func2(func(x, y Value) Value { return Cons(x, y) }),
		"list":   FuncN(list),
		"apply":  FuncN(apply),

		"error":         FuncN(raiseError),
		"ignore-errors": Func1(trapError),

		"environment-bindings": EnvFunc(bindings),
	},
}

SystemEnvironment is the toplevel environment where primitives are defined.

Functions

func Errorf

func Errorf(format string, a ...interface{})

Errorf raises an error with a formatted message.

Types

type Atom

type Atom struct {
	Name string
}

func Intern

func Intern(name string) *Atom

func (*Atom) Equal

func (v *Atom) Equal(x Value) Value

func (Atom) String

func (v Atom) String() string

type Cell

type Cell struct {
	Car Value
	Cdr Value
}

Cell is an object that holds two values.

func Cons

func Cons(x, y Value) *Cell

Cons constructs an object that holds two values.

Lists can be represented by consing x onto another cons. The y value of the last cons must be NIL.

func (*Cell) Equal

func (c *Cell) Equal(cmp Value) Value

func (*Cell) String

func (c *Cell) String() string

func (*Cell) Walk

func (c *Cell) Walk(fn func(Value))

Walk traverses calls fn(Car) for each cell in a list.

type Environment

type Environment interface {
	Value
	// Returns the list of defined symbols.
	Names() []string
	// Lookup the value of a symbol.
	Lookup(name string) (Value, bool)
	// Define a new symbol.
	Define(name string, value Value)
	// Update the value of a symbol.
	Update(name string, value Value) error
}

An environment maintains a set of bindings that are typically referenced when evaluating a Lisp expression.

func NewEnv

func NewEnv(parent Environment) Environment

NewEnv returns a new environment that extends the bindings of parent. If parent is nil, then this is a toplevel environment.

type Error

type Error string

Error is a value used to represent runtime errors.

func (Error) Equal

func (e Error) Equal(v Value) Value

Equal implements the Value interface.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

func (Error) String

func (e Error) String() string

type Function

type Function interface {
	Value
	Invoke([]Value) Value
}

func EnvFunc

func EnvFunc(fn func(Environment) Value) Function

FuncEnv creates a Function value from a native Go function that only accepts environments.

func Func1

func Func1(fn func(Value) Value) Function

Func1 creates a Function value from a native Go function that accepts a single argument.

func Func2

func Func2(fn func(Value, Value) Value) Function

Func2 creates a Function value from a native Go function that accepts two arguments.

func FuncN

func FuncN(fn func(vs []Value) Value) Function

FuncN creates a Function value from a native Go function that accepts a variable number of arguments.

func FuncX

func FuncX(n int, fn func([]Value) Value) Function

FuncX creates a Function value from a native Go function that accepts a specified number of arguments.

type Value

type Value interface {
	Equal(Value) Value
	// If the value has a valid written representation, then this
	// should output an external representation suitable for read.
	String() string
}

Value is a runtime representation of Lisp data.

Jump to

Keyboard shortcuts

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