Documentation
¶
Overview ¶
Package sx provides the basic objects to work with symbolic expressions.
Index ¶
- Constants
- Variables
- func AllPackages() iter.Seq[*Package]
- func IsFalse(obj Object) bool
- func IsList(obj Object) bool
- func IsNil(obj Object) bool
- func IsTrue(obj Object) bool
- func IsUndefined(obj Object) bool
- func NumCmp(x, y Number) int
- func Print(w io.Writer, obj Object) (int, error)
- type ErrImproper
- type ErrSymbolFrozen
- type Int64
- type ListBuilder
- func (lb *ListBuilder) Add(obj Object)
- func (lb *ListBuilder) AddN(objs ...Object)
- func (lb *ListBuilder) Collect(seq iter.Seq[Object])
- func (lb *ListBuilder) ExtendBang(lst *Pair)
- func (lb *ListBuilder) IsEmpty() bool
- func (lb *ListBuilder) Last() *Pair
- func (lb *ListBuilder) List() *Pair
- func (lb *ListBuilder) Reset()
- type Number
- type Object
- type Package
- func (pkg *Package) AllSymbols() iter.Seq[*Symbol]
- func (pkg *Package) FindSymbol(name string) *Symbol
- func (pkg *Package) GoString() string
- func (pkg *Package) IsAtom() bool
- func (pkg *Package) IsEqual(other Object) bool
- func (pkg *Package) IsEqualPackage(other *Package) bool
- func (pkg *Package) IsNil() bool
- func (pkg *Package) IsTrue() bool
- func (pkg *Package) MakeSymbol(name string) *Symbol
- func (pkg *Package) Name() string
- func (pkg *Package) Size() int
- func (pkg *Package) String() string
- type Pair
- func (pair *Pair) AppendBang(obj Object) *Pair
- func (pair *Pair) Assoc(obj Object) *Pair
- func (pair *Pair) Car() Object
- func (pair *Pair) Cdr() Object
- func (pair *Pair) Cons(car Object) *Pair
- func (pair *Pair) Copy() *Pair
- func (pair *Pair) ExtendBang(obj *Pair) *Pair
- func (pair *Pair) GoString() string
- func (pair *Pair) Head() *Pair
- func (pair *Pair) IsAtom() bool
- func (pair *Pair) IsEqual(other Object) bool
- func (pair *Pair) IsNil() bool
- func (pair *Pair) IsTrue() bool
- func (pair *Pair) Last() (Object, error)
- func (pair *Pair) LastPair() *Pair
- func (pair *Pair) Length() int
- func (pair *Pair) LengthEqual(n int) bool
- func (pair *Pair) LengthGreater(n int) bool
- func (pair *Pair) LengthLess(n int) bool
- func (pair *Pair) MakeList() *Pair
- func (pair *Pair) Nth(n int) (Object, error)
- func (pair *Pair) Pairs() iter.Seq[*Pair]
- func (pair *Pair) Print(w io.Writer) (int, error)
- func (pair *Pair) RemoveAssoc(obj Object) *Pair
- func (pair *Pair) Reverse() (*Pair, error)
- func (pair *Pair) SetCar(obj Object)
- func (pair *Pair) SetCdr(obj Object)
- func (pair *Pair) String() string
- func (pair *Pair) Tail() *Pair
- func (pair *Pair) Values() iter.Seq[Object]
- type Printable
- type Sequence
- type String
- func (s String) GetValue() string
- func (s String) GoString() string
- func (String) IsAtom() bool
- func (s String) IsEqual(other Object) bool
- func (s String) IsEqualString(other String) bool
- func (String) IsNil() bool
- func (s String) IsTrue() bool
- func (s String) Print(w io.Writer) (int, error)
- func (s String) String() string
- type Symbol
- func (sym *Symbol) Bind(val Object) error
- func (sym *Symbol) Bound() (Object, bool)
- func (sym *Symbol) Freeze()
- func (sym *Symbol) GetValue() string
- func (sym *Symbol) GoString() string
- func (*Symbol) IsAtom() bool
- func (sym *Symbol) IsEqual(other Object) bool
- func (sym *Symbol) IsEqualSymbol(other *Symbol) bool
- func (sym *Symbol) IsFrozen() bool
- func (sym *Symbol) IsKeyword() bool
- func (sym *Symbol) IsNil() bool
- func (sym *Symbol) IsTrue() bool
- func (sym *Symbol) Package() *Package
- func (sym *Symbol) Print(w io.Writer) (length int, err error)
- func (sym *Symbol) String() string
- type Undefined
- type Vector
- func (v Vector) GoString() string
- func (v Vector) IsAtom() bool
- func (v Vector) IsEqual(other Object) bool
- func (v Vector) IsNil() bool
- func (v Vector) IsTrue() bool
- func (v Vector) Length() int
- func (v Vector) LengthEqual(n int) bool
- func (v Vector) LengthGreater(n int) bool
- func (v Vector) LengthLess(n int) bool
- func (v Vector) MakeList() *Pair
- func (v Vector) Nth(n int) (Object, error)
- func (v Vector) Print(w io.Writer) (int, error)
- func (v Vector) String() string
- func (v Vector) Values() iter.Seq[Object]
Constants ¶
const ( InitName = "INIT" KeywordName = "KEYWORD" )
Predefined package names.
const VectorName = "vector"
VectorName is the name of the (vector ...) builtin
Variables ¶
var ( SymbolQuote = initPackage.MakeSymbol("quote") SymbolQuasiquote = initPackage.MakeSymbol("quasiquote") SymbolUnquote = initPackage.MakeSymbol("unquote") SymbolUnquoteSplicing = initPackage.MakeSymbol("unquote-splicing") )
Names of quotation symbols.
Used in packages sxbuiltins, sxreader.
ErrZeroNotAllowed is signalled with an argument must not be zero, e.g. for division.
var T = MakeSymbol("T")
T is the default true object.
Functions ¶
func AllPackages ¶
AllPackages returns an iterator of all packages.
func IsFalse ¶
IsFalse returns true, if the given object will be interpreted as "false" in a boolean context.
func IsList ¶
IsList returns true, if the object is a list, not just a pair. A list must have a nil value at the last cdr.
func IsTrue ¶
IsTrue returns true, if the given object will be interpreted as "true" in a boolean context.
func IsUndefined ¶
IsUndefined returns true iff the object is a undefined value
func NumCmp ¶
NumCmp compares the two number and returns -1 if x < y, 0 if x = y, and 1 if x > y.
Types ¶
type ErrImproper ¶
type ErrImproper struct{ Pair *Pair }
ErrImproper is signalled if an improper list is found where it is not appropriate.
func (ErrImproper) Error ¶
func (err ErrImproper) Error() string
Error returns a textual representation for this error.
type ErrSymbolFrozen ¶
type ErrSymbolFrozen struct{ Symbol *Symbol }
ErrSymbolFrozen is returned when trying to update a frozen symbol.
func (ErrSymbolFrozen) Error ¶
func (err ErrSymbolFrozen) Error() string
type Int64 ¶
type Int64 int64
Int64 is a number that store 64 bit integer values.
func (Int64) GoString ¶
GoString returns the Go string representation.
func (Int64) IsAtom ¶
IsAtom always returns true because a symbol is an atomic value.
func (Int64) IsTrue ¶
IsTrue returns true if Int64 can be interpreted as a "true" value.
type ListBuilder ¶
type ListBuilder struct {
// contains filtered or unexported fields
}
ListBuilder is a helper to build a list sequentially from start to end.
func (*ListBuilder) AddN ¶
func (lb *ListBuilder) AddN(objs ...Object)
AddN adds multiple objects to the list builder.
func (*ListBuilder) Collect ¶
func (lb *ListBuilder) Collect(seq iter.Seq[Object])
Collect all values of the iterator into a list.
func (*ListBuilder) ExtendBang ¶
func (lb *ListBuilder) ExtendBang(lst *Pair)
ExtendBang the list by the given list, reusing the given list
func (*ListBuilder) IsEmpty ¶
func (lb *ListBuilder) IsEmpty() bool
IsEmpty returns true, if no element was added.
func (*ListBuilder) List ¶
func (lb *ListBuilder) List() *Pair
List the result, but not resetting the builder.
type Number ¶
Number value store numbers.
func GetNumber ¶
GetNumber returns the object as a number, if possible.
func NumDiv ¶
NumDiv divides the first by the second number.
func NumMod ¶
NumMod divides the first by the second number.
type Object ¶
type Object interface {
fmt.Stringer
fmt.GoStringer
// IsNil checks if the concrete object is nil.
IsNil() bool
// IsAtom returns true iff the object is an object that is not further decomposable.
IsAtom() bool
// IsTrue returns true, of the value can be considered as a "true" value.
IsTrue() bool
// IsEqual compare two objects for deep equality.
IsEqual(Object) bool
}
Object is the generic value all s-expressions must fulfill.
type Package ¶
type Package struct {
// contains filtered or unexported fields
}
Package maps symbol names to Symbols.
func CurrentPackage ¶
func CurrentPackage() *Package
CurrentPackage returns the currently selected package.
func FindPackage ¶
FindPackage returns the package with the given name.
func GetPackage ¶
GetPackage returns the object as a package if possible.
func KeywordPackage ¶
func KeywordPackage() *Package
KeywordPackage return the package that manages keyword symbols.
func MakePackage ¶
MakePackage builds a new package.
func MustMakePackage ¶
MustMakePackage builds a new package and panics if something went wrong.
func (*Package) AllSymbols ¶
AllSymbols returns an iterator of all symbols currently manages by the package.
func (*Package) FindSymbol ¶
FindSymbol returns the symbol with the given name.
func (*Package) GoString ¶
GoString returns the go string representation.
func (*Package) IsAtom ¶
IsAtom always returns true because a symbol is an atomic value.
func (*Package) IsEqual ¶
IsEqual compare the symbol with an object.
func (*Package) IsEqualPackage ¶
IsEqualPackage compare two packages.
func (*Package) IsNil ¶
IsNil may return true if a symbol pointer is nil.
func (*Package) IsTrue ¶
IsTrue returns true if package can be interpreted as a "true" value.
func (*Package) MakeSymbol ¶
MakeSymbol builds a symbol with the given string value. It tries to re-use symbols, so that symbols can be compared by their reference, not by their content.
func (*Package) Size ¶
Size returns the number of symbols created by the package.
type Pair ¶
type Pair struct {
// contains filtered or unexported fields
}
Pair is a node containing a value for the element and a pointer to the tail. In other lisps it is often called "cell", "cons", "cons-cell", or "list".
func MakeList ¶
MakeList creates a new list with the given objects.
func (*Pair) AppendBang ¶
AppendBang updates the given pair by setting a new pair with the given object and nil as its new second object.
func (*Pair) Assoc ¶
Assoc returns the first pair of a list where the car IsEqual to the given object.
func (*Pair) Cons ¶
Cons prepends a value in front of a given listreturning the new list.
func (*Pair) ExtendBang ¶
ExtendBang updates the given pair by extending it with the second pair list after its end. Returns the last list node of the newly formed list beginning with `lst`, which is also the last list node of the list starting with `val`.
func (*Pair) GoString ¶
GoString returns the go string representation.
func (*Pair) Head ¶
Head returns the first object as a pair, if possible. Otherwise it returns nil.
func (*Pair) IsTrue ¶
IsTrue returns true if pair can be interpreted as a "true" value.
func (*Pair) Last ¶
Last returns the last element of a non-empty list.
func (*Pair) LastPair ¶
LastPair returns the last pair of the given pair list, or nil.
func (*Pair) LengthEqual ¶
LengthEqual returns true if the length of the pair list is equal to the given number.
pair.LengthEqual(n) is typically much faster than pair.Length() == n.
func (*Pair) LengthGreater ¶
LengthGreater returns true if the length of the pair list is greater than the given number.
pair.LengthGreater(n) is typically much faster than pair.Length() > n.
func (*Pair) LengthLess ¶
LengthLess returns true if the length of the pair list is less than the given number.
pair.LengthLess(n) is typically much faster than pair.Length() < n.
func (*Pair) MakeList ¶
MakeList builds a list. Basically, the same pair is returned. This method is needed to make Sequence interface happy.
func (*Pair) Nth ¶
Nth returns the n'th object of the pair list. It is an error, if n < 0 or if the list length is less than n.
func (*Pair) Pairs ¶
Pairs returns an iterator of all pair nodes.
func (*Pair) Print ¶
Print write the string representation to the given Writer.
func (*Pair) RemoveAssoc ¶
RemoveAssoc deletes all pairs from the association list, where the car IsEqual to the given object. A new list is created.
func (*Pair) Reverse ¶
Reverse returns a reversed pair list.
func (*Pair) SetCar ¶
SetCar sets the car of the pair to the given object.
func (*Pair) SetCdr ¶
SetCdr sets the cdr of the pair to the given object.
func (*Pair) Tail ¶
Tail returns the second object as a pair, if possible. Otherwise it returns nil.
type Printable ¶
type Printable interface {
// Print emits the string representation on the given Writer
Print(io.Writer) (int, error)
}
Printable is a object that has is specific representation, which is different to String().
type Sequence ¶
type Sequence interface {
Object
// Length returns the length of the sequence.
Length() int
// LengthLess reports whether the length of the sequence is less than
// the given number.
LengthLess(n int) bool
// LengthGreater reports whether the length of the sequence is greater than
// the given number.
LengthGreater(n int) bool
// LengthEqual reports whether the length of the sequence is equal to the
// given number.
LengthEqual(n int) bool
// Nth returns the n-th element of a sequence.
Nth(n int) (Object, error)
// MakeList returns the sequence as a pair list. If sequence is already a
// pair list, it is returned without copying it.
MakeList() *Pair
// Values() returns an iterator over all elements of the sequence in
// natural order.
Values() iter.Seq[Object]
}
Sequence is an Object that has a finite, ordered set of elements.
type String ¶
type String struct {
// contains filtered or unexported fields
}
String represents a string object.
func GetString ¶
GetString returns the object as a string, if possible
func (String) GetValue ¶
GetValue returns the string value of a String.
func (String) GoString ¶
GoString returns the go string representation.
func (String) IsAtom ¶
IsAtom always returns true because a string is an atomic value.
func (String) IsEqual ¶
IsEqual compares two objects for equivalence.
func (String) IsEqualString ¶
IsEqualString compares two strings.
func (String) IsTrue ¶
IsTrue returns true if string can be interpreted as a "true" value.
func (String) Print ¶
Print write the string representation to the given Writer.
type Symbol ¶
type Symbol struct {
// contains filtered or unexported fields
}
Symbol represent a symbol value.
func GetSymbol ¶
GetSymbol returns the object as a symbol if possible.
func (*Symbol) Freeze ¶
func (sym *Symbol) Freeze()
Freeze the symbol so that bound value cannot be changed any more.
func (*Symbol) GetValue ¶
GetValue return the string value of the symbol.
func (*Symbol) GoString ¶
GoString returns the go string representation.
func (*Symbol) IsAtom ¶
IsAtom always returns true because a symbol is an atomic value.
func (*Symbol) IsEqual ¶
IsEqual compare the symbol with an object.
func (*Symbol) IsEqualSymbol ¶
IsEqualSymbol compare two symbols.
func (*Symbol) IsFrozen ¶
IsFrozen returns true if symbol is frozen.
func (*Symbol) IsKeyword ¶
IsKeyword returns true, if the symbols package is the keyword package.
func (*Symbol) IsNil ¶
IsNil may return true if a symbol pointer is nil.
func (*Symbol) IsTrue ¶
IsTrue returns true if symbol can be interpreted as a "true" value.
func (*Symbol) Package ¶
Package returns the Package that created the symbol.
func (*Symbol) Print ¶
Print write the string representation to the given Writer.
type Undefined ¶
type Undefined struct{}
Undefined is an object that signal a 'no value'.
func (Undefined) GoString ¶
GoString returns a string representation to be used in Go code.
func (Undefined) IsAtom ¶
IsAtom always returns false because an undefined value is never atomic.
func (Undefined) IsEqual ¶
IsEqual returns true if the other value has the same content.
func (Undefined) IsNil ¶
IsNil always returns false because an undefined value is never nil.
func (Undefined) IsTrue ¶
IsTrue returns true if undefined can be interpreted as a "true" value. Hint: it will never ;)
type Vector ¶
type Vector []Object
Vector is a sequence of Objects.
func Collect ¶
Collect values from seq into a new vector and return it.
func GetVector ¶
GetVector returns the object as a vector, if possible.
func (Vector) GoString ¶
GoString returns the string representation to be used in Go code.
func (Vector) IsAtom ¶
IsAtom signals an atomic value. Only the empty vector is atomic.
func (Vector) IsEqual ¶
IsEqual compares the vector with another object to have the same content.
func (Vector) IsNil ¶
IsNil returns true, if the vector should be treated like the Nil() object.
func (Vector) IsTrue ¶
IsTrue returns true if vector can be interpreted as a "true" value.
func (Vector) LengthEqual ¶
LengthEqual return true, if the length of the vector is equal to the given value.
func (Vector) LengthGreater ¶
LengthGreater return true, if the length of the vector is greater than the given value.
func (Vector) LengthLess ¶
LengthLess return true, if the length of the vector is less than the given value.
func (Vector) MakeList ¶
MakeList builds a pair list from the vector.
func (Vector) Nth ¶
Nth returns the object at the given position. It is an error if the position is less than zero or greater than the vector length.
func (Vector) Print ¶
Print write the string representation to the given Writer.
Source Files
¶
- boolean.go
- const.go
- list.go
- number.go
- package.go
- sequence.go
- string.go
- sx.go
- symbol.go
- undef.go
- vector.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package main provides a simple interpreter for s-expressions.
|
Package main provides a simple interpreter for s-expressions. |
|
Package sxbuiltins contains functions that help to build builtin functions.
|
Package sxbuiltins contains functions that help to build builtin functions. |
|
Package sxeval allows to evaluate s-expressions.
|
Package sxeval allows to evaluate s-expressions. |
|
Package sxreader implements a way to read symbolic expression into an internal representation.
|
Package sxreader implements a way to read symbolic expression into an internal representation. |