sxpf

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: EUPL-1.2 Imports: 8 Imported by: 5

README

sxpf - S-Expression Framework

This is a framework to work with s-expressions.

If you are looking to the previous work, please take a look at branch v0.

Types

Sxpf support the following atomic, immutable types:

  • Numbers contain numeric values. Currently, only integer values are supported. There is no maximum or minimum integer value. They optionally start with a + or - sign and contain only digits 0, ..., 9.
  • Strings are UTF-8 encoded Unicode character sequences. They are delimited by " characters. Special characters inside the string, like the " character itself, are escaped by the \ character.
  • Symbols are sequences of printable / visible Unicode characters. They are typically used to bind them to values within an environment. Another use case is symbolic computation. A symbol allows to store additional values by providing an association list (a-list), a list of cons cells. Symbols can be compared for equality quite efficients, since they are interned by their associated symbol factory.
  • Boolean are the symbols True and False, interpreted as boolean values. Therefore, there are no symbols with this name.
  • Keywords are sequences of printable / visible Unicode characters, starting with a & character. They are somehow in between of strings and symbols. They have a string-like value, but cannot contain special Unicode characters. They are simple enough to be like symbols, but do not contain an a-list, are not interned. They always evaluate to themself. Their use case is to act like a marker, a special value.

Sxpf supports nested lists. A list is delimited by parentheses: ( ... ). Within a list, all values are separated by space characters, including new line. Lists can be nested. Internally, lists are created by cons cells, also called pairs. The first part of a cons cell, called "car", contains the actual value stored at the beginning of a list. The second part, called "cdr", typically links to the next cons cell This allows multiple lists to share elements. A proper list is a list, where the last elements second part is the empty list. The last element of a list may be a cons cell, where the second part references a values except a list. Such lists are improper lists. Since the second part may reference any value, even earlier elements of a list, lists may be circular. Single cons cells are denoted as (X . Y), where the car references S and the cdr references Y (Y is not a list).

All other types supported by Sxpf cannot be specified via the reader.

Documentation

Overview

Package sxpf allows to build a custom REPL for s-expressions.

Index

Constants

View Source
const (
	True  = Boolean(true)
	False = Boolean(false)

	TrueString  = "True"
	FalseString = "False"
)

The two boolean values, Do not use other constants. There are defined string values other code must respect (e.g. symbol factory, reader, ...)

View Source
const MappedFactorySize = 128

MappedFactorySize is the base size of a new maped symbol factory. If more symbols are entered into the factory, it must be re-sized internally, which will consume some time.

View Source
const RootEnvironmentSize = 128

RootEnvironmentSize is the base size of the root environment. If more bindings are entered, it must be re-sized, which may consume some time.

Variables

View Source
var ErrZeroNotAllowed = errors.New("number zero not allowed")

ErrZeroNotAllowed

Functions

func IsFalse

func IsFalse(obj Object) bool

IsFalse returns true, if object is a false value.

A nil object, the False object or an empty string are false values.

func IsList

func IsList(obj Object) bool

IsList returns true, if the object is a list, e.g. with nil at the last cdr.

func IsNil

func IsNil(obj Object) bool

IsNil return true, if the given object is the nil object.

func IsPair

func IsPair(obj Object) bool

IsPair returns true, if the object is a cons pair.

func IsTrue

func IsTrue(obj Object) bool

IsTrue returns true, if object is a true value.

Everything except a nil object, the False object, and the empty string, is a true value.

func IsUndefined

func IsUndefined(obj Object) bool

IsUndefined returns true iff the object is a undefined value

func NumCmp

func NumCmp(x, y Number) int

NumCmp compares the two number and returns -1 if x < y, 0 if x = y, and 1 if x > y.

func Print

func Print(w io.Writer, obj Object) (int, error)

Print writes the string representation to a io.Writer.

func Repr

func Repr(obj Object) string

Repr returns the string representation of the given object.

func WriteStrings

func WriteStrings(w io.Writer, sl ...string) (int, error)

WriteStrings is a helper function to write multiple strings at once.

Types

type Boolean

type Boolean bool

Boolean represents a boolean object.

func GetBoolean

func GetBoolean(obj Object) (Boolean, bool)

GetBoolean returns the object as a boolean, if possible.

func MakeBoolean

func MakeBoolean(b bool) Boolean

MakeBoolean creates a new Boolean object.

func Negate

func Negate(obj Object) Boolean

Negate returns the negation of the true value of the given object.

func (Boolean) IsAtom

func (Boolean) IsAtom() bool

func (Boolean) IsEql

func (b Boolean) IsEql(other Object) bool

IsEql compares two objects for equivalence.

func (Boolean) IsEqual

func (b Boolean) IsEqual(other Object) bool

IsEqual is the same a IsEqv for strings.

func (Boolean) IsNil

func (Boolean) IsNil() bool

IsNil return true, if it is a nil boolean value.

func (Boolean) Negate

func (b Boolean) Negate() Boolean

Negate returns the other boolean value.

func (Boolean) Repr

func (b Boolean) Repr() string

Repr returns the value representation.

func (Boolean) String

func (b Boolean) String() string

String returns the Go string representation.

type Environment

type Environment interface {
	// An environment is an object by itself
	Object

	// String returns the local name of this environment.
	String() string

	// Parent allows to retrieve the parent environment. Environment is the root
	// environment, nil is returned. Lookups that cannot be satisfied in an
	// environment are delegated to the parent envrionment.
	Parent() Environment

	// Bind creates a local mapping with a given symbol and object.
	// A previous mapping will be overwritten.
	Bind(*Symbol, Object) error

	// Lookup will search for a local binding of the given symbol. If not
	// found, the search will *not* be continued in the parent environment.
	// Use the global `Resolve` function, if you want a search up to the parent.
	Lookup(*Symbol) (Object, bool)

	// Bindings returns all bindings as an a-list in some random order.
	Bindings() *List

	// Unbind removes the mapping of the given symbol to an object.
	Unbind(*Symbol) error

	// Freeze sets the environment in a read-only state.
	Freeze()
}

Environment maintains a mapping between symbols and values. Form are evaluated within environments.

func GetEnvironment

func GetEnvironment(obj Object) (Environment, bool)

GetEnvironment returns the object as an environment, if possible.

func MakeChildEnvironment

func MakeChildEnvironment(parent Environment, name string, baseSize int) Environment

MakeChildEnvironment creates a new environment with a given parent.

func MakeRootEnvironment

func MakeRootEnvironment() Environment

MakeRootEnvironment creates a new root environment.

func RootEnv

func RootEnv(env Environment) Environment

RootEnv returns the root environment of the given environment.

type ErrEnvFrozen

type ErrEnvFrozen struct{ Env Environment }

ErrEnvFrozen is returned when trying to update a frozen environment.

func (ErrEnvFrozen) Error

func (err ErrEnvFrozen) Error() string

type ErrImproper

type ErrImproper struct{ Lst *List }

ErrImproper is signalled if an improper list is found where it is not appropriate.

func (ErrImproper) Error

func (err ErrImproper) Error() string

type Int64

type Int64 int64

Int64 is a number that store 64 bit integer values.

func (Int64) IsAtom

func (Int64) IsAtom() bool

func (Int64) IsEql

func (i Int64) IsEql(other Object) bool

IsEql compare two objects.

func (Int64) IsEqual

func (i Int64) IsEqual(other Object) bool

IsEqual is currently the same as IsEqv, since we support only integer values.

func (Int64) IsNil

func (i Int64) IsNil() bool

IsNil return true, if it is a nil integer value.

func (Int64) IsZero

func (i Int64) IsZero() bool

func (Int64) Repr

func (i Int64) Repr() string

Repr returns the value representation.

func (Int64) String

func (i Int64) String() string

String returns the Go string representation.

type Keyword

type Keyword string

Keyword represents a symbolic value.

A keyword is like a string, but contains only printable characters. A keyword is like a symbol, but does not allow to associate additional data. A keyword is often used as a key of an association list, as it is more lightweight compared to a string and to a symbol.

func GetKeyword

func GetKeyword(obj Object) (Keyword, bool)

GetKeyword returns the object as a keyword, if possible

func (Keyword) IsAtom

func (Keyword) IsAtom() bool

func (Keyword) IsEql

func (kw Keyword) IsEql(other Object) bool

IsEql compare two objects.

func (Keyword) IsEqual

func (kw Keyword) IsEqual(other Object) bool

IsEqual is the same a IsEqv for keywords.

func (Keyword) IsNil

func (Keyword) IsNil() bool

IsNil return true, if it is a nil keyword object.

func (Keyword) Print

func (kw Keyword) Print(w io.Writer) (int, error)

Print write the string representation to the given Writer.

func (Keyword) Repr

func (kw Keyword) Repr() string

Repr returns the value representation.

func (Keyword) String

func (kw Keyword) String() string

String returns the Go string representation.

type List

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

A list is a node containing a value for the element and a pointer to the tail. In other lisps it is often called "cons", "cons-cell", or "cell".

func AllBindings

func AllBindings(env Environment) *List

AllBindings returns an a-list of all bindings in the given environment and its parent environments.

func Cons

func Cons(car, cdr Object) *List

func GetList

func GetList(obj Object) (*List, bool)

GetList returns the object as a list.

func MakeList

func MakeList(objs ...Object) *List

MakeList creates a new list with the given objects.

func Nil

func Nil() *List

Nil() returns the nil list.

func (*List) AppendBang

func (lst *List) AppendBang(obj Object) *List

AppendBang updates the given list by appending a new element after its end.

func (*List) Assoc

func (lst *List) Assoc(obj Object) *List

Assoc returns the first list where the car IsEqv to the given object.

func (*List) Car

func (lst *List) Car() Object

Car returns the head object of a list.

func (*List) Cdr

func (lst *List) Cdr() Object

Cdr returns the second object of a list.

func (*List) Cons

func (cdr *List) Cons(car Object) *List

Cons prepends a value in front of a given listreturning the new list.

func (*List) ExtendBang

func (lst *List) ExtendBang(obj *List) *List

ExtendBang updates the given list by extending it with the second 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 (*List) Head

func (lst *List) Head() *List

Head returns the first object as a list, if possible.

func (*List) IsAtom

func (lst *List) IsAtom() bool

func (*List) IsEql

func (lst *List) IsEql(other Object) bool

IsEql compares two objects for equivalence. Two lists are eqv iff they are the same lists.

func (*List) IsEqual

func (lst *List) IsEqual(other Object) bool

IsEqual compare two objects.

func (*List) IsNil

func (lst *List) IsNil() bool

IsNil return true, if it is a nil pair object.

func (*List) Last

func (lst *List) Last() (Object, error)

Last returns the last element of a non-empty list.

func (*List) LastPair

func (lst *List) LastPair() *List

LastPair returns the last list cell of the given list, or nil.

func (*List) Length

func (lst *List) Length() int

Length returns the length of the list.

func (*List) Print

func (lst *List) Print(w io.Writer) (int, error)

Print write the string representation to the given Writer.

func (*List) Repr

func (lst *List) Repr() string

Repr returns the value representation.

func (*List) Reverse

func (lst *List) Reverse() (*List, error)

Reverse returns a reversed list.

func (*List) SetCdr

func (lst *List) SetCdr(obj Object)

SetCdr sets the cdr of the list to the given object.

func (*List) String

func (lst *List) String() string

String returns the Go string representation.

func (*List) Tail

func (lst *List) Tail() *List

Tail returns the tail of a list, if the tail is a list.

type Number

type Number interface {
	Object

	// Returns true iff the number is equal to zero.
	IsZero() bool
}

Number value store numbers.

func GetNumber

func GetNumber(obj Object) (Number, bool)

GetNumber returns the object as a number, if possible.

func NumAdd

func NumAdd(x, y Number) Number

NumAdd adds the two numbers.

func NumDiv

func NumDiv(x, y Number) (Number, error)

NumDiv divides the first by the second number.

func NumMod

func NumMod(x, y Number) (Number, error)

NumMod divides the first by the second number.

func NumMul

func NumMul(x, y Number) Number

NumMul multiplies the two numbers.

func NumNeg

func NumNeg(x Number) Number

NumNeg negates the given number.

func NumSub

func NumSub(x, y Number) Number

NumSub subtracts the two numbers.

func ParseInteger

func ParseInteger(s string) (Number, error)

ParseInteger parses the string as an integer value and returns its value as a number.

type Object

type Object interface {
	fmt.Stringer

	// 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

	// IsEql compares two objects for atomic / shallow equality.
	IsEql(Object) bool

	// IsEqual compare two objects for deep equality.
	IsEqual(Object) bool

	// Repr returns the object representation.
	Repr() string
}

Object is the generic value all s-expressions must fulfill.

func Resolve

func Resolve(env Environment, sym *Symbol) (Object, bool)

Resolve a symbol is an environment and all of its parent environment.

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 String

type String string

String represents a string object.

func GetString

func GetString(obj Object) (String, bool)

GetString returns the object as a string, if possible

func MakeString

func MakeString(s string) String

MakeString creates a new string object.

func (String) IsAtom

func (String) IsAtom() bool

func (String) IsEql

func (s String) IsEql(other Object) bool

IsEql compares two objects for equivalence.

func (String) IsEqual

func (s String) IsEqual(other Object) bool

IsEqual is the same a IsEqv for strings.

func (String) IsNil

func (String) IsNil() bool

IsNil return true, if it is a nil string value.

func (String) Print

func (s String) Print(w io.Writer) (int, error)

Print write the string representation to the given Writer.

func (String) Repr

func (s String) Repr() string

Repr returns the value representation.

func (String) String

func (s String) String() string

String returns the Go string representation.

type Symbol

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

Symbol represent a symbol value.

Every symbol can store metadata with the help of Cons(). It can be retrieved using Assoc().

func GetSymbol

func GetSymbol(obj Object) (*Symbol, bool)

GetSymbol returns the object as a symbol if possible.

func (*Symbol) Assoc

func (sy *Symbol) Assoc(key Object) *List

Assoc retrieves the formerly bound key/value pair.

func (*Symbol) Cons

func (sy *Symbol) Cons(key, obj Object) *List

Cons a key to a value, to store metadata for the symbol.

func (*Symbol) Factory

func (sy *Symbol) Factory() SymbolFactory

Factory returns the symbol factory that created this symbol.

func (*Symbol) IsAtom

func (sy *Symbol) IsAtom() bool

func (*Symbol) IsEql

func (sy *Symbol) IsEql(other Object) bool

IsEqual compare two objects.

Two symbols are equal, if the are created by the same factory and have the same same.

func (*Symbol) IsEqual

func (sy *Symbol) IsEqual(other Object) bool

IsEqual is the same a IsEqv for symbols.

func (*Symbol) IsNil

func (sy *Symbol) IsNil() bool

IsNil return true, if it is a nil symbol value.

func (*Symbol) Name

func (sy *Symbol) Name() string

Name returns the canonical name the symbol factory assigned to the symbol.

func (*Symbol) Repr

func (sy *Symbol) Repr() string

Repr returns the object representation.

func (*Symbol) String

func (sy *Symbol) String() string

String returns the Go string representation.

type SymbolFactory

type SymbolFactory interface {
	// Make produces a singleton symbol from the given string.
	// If the string denotes an invalid name, an error will be returned.
	Make(string) (*Symbol, error)

	// MustMake will produce a singleton symbol and panic if that does not work.
	MustMake(string) *Symbol

	// IsValidName returns true, if given name is a valid name for a symbol.
	//
	// The empty string is always an invalid name.
	IsValidName(string) bool

	// Symbols returns a sequence of all symbols managed by this factory.
	Symbols() []*Symbol

	// ReprSymbol returns the factory-specific representation of the given symbol.
	ReprSymbol(*Symbol) string
}

SymbolFactory creates new symbols and ensures locally that there is only one symbol with a given string value. It encapsulates case-sensitiveness, and is the only way to produce a valid symbol.

func FindSymbolFactory

func FindSymbolFactory(obj Object) SymbolFactory

FindSymbolFactory searches for a symbol an returns its symbol factory.

Typically, the search is done depth-first.

func MakeMappedFactory

func MakeMappedFactory() SymbolFactory

MakeMappedFactory creates a new factory.

type Undefined

type Undefined struct{}

Undefined is an object that signal a 'no value'.

func MakeUndefined

func MakeUndefined() Undefined

MakeUndefined creates an undefined objact.

func (Undefined) IsAtom

func (Undefined) IsAtom() bool

func (Undefined) IsEql

func (kw Undefined) IsEql(other Object) bool

func (Undefined) IsEqual

func (udef Undefined) IsEqual(other Object) bool

func (Undefined) IsNil

func (Undefined) IsNil() bool

func (Undefined) Print

func (udef Undefined) Print(w io.Writer) (int, error)

func (Undefined) Repr

func (udef Undefined) Repr() string

func (Undefined) String

func (udef Undefined) String() string

Directories

Path Synopsis
Package builtins contains functions that help to build builtin functions.
Package builtins contains functions that help to build builtin functions.
binding
Package binding contains builtins and syntax to bind values.
Package binding contains builtins and syntax to bind values.
boolean
Package boolean contains builtins and syntax for boolean values.
Package boolean contains builtins and syntax for boolean values.
callable
Package callable provides syntaxes and builtins to work with callables / functions / procedure.
Package callable provides syntaxes and builtins to work with callables / functions / procedure.
cond
Package cond provides some special/builtin functions to conditionally evaluate values.
Package cond provides some special/builtin functions to conditionally evaluate values.
define
Package define contains all syntaxes and builtins to bind values to symbols.
Package define contains all syntaxes and builtins to bind values to symbols.
env
Package env provides some special/builtin functions to work with environments.
Package env provides some special/builtin functions to work with environments.
equiv
Package equiv contains function to test for equivalence of objects.
Package equiv contains function to test for equivalence of objects.
list
Package list contains all list-related builtins
Package list contains all list-related builtins
number
Package number contains builtins to work with numbers.
Package number contains builtins to work with numbers.
pprint
Package pprint provides some function to pretty-print objects.
Package pprint provides some function to pretty-print objects.
quote
Package quote contains functions to use quotations These are: quote, quasiquote, unquote, unquote-splicing.
Package quote contains functions to use quotations These are: quote, quasiquote, unquote, unquote-splicing.
timeit
Package timeit provides functions to measure evaluation.
Package timeit provides functions to measure evaluation.
Package eval allows to evaluate s-expressions.
Package eval allows to evaluate s-expressions.

Jump to

Keyboard shortcuts

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