Documentation ¶
Overview ¶
Package sabre provides data structures, reader for reading LISP source into data structures and functions for evluating forms against a context.
Index ¶
- Variables
- func Compare(v1, v2 Value) bool
- type Any
- type Bool
- type Character
- type EvalError
- type Float64
- type Fn
- type HashMap
- type Int64
- type Invokable
- type Keyword
- type List
- type MapScope
- type Module
- type MultiFn
- type Nil
- type Position
- type ReadError
- type Reader
- func (rd *Reader) All() (Value, error)
- func (rd *Reader) IsTerminal(r rune) bool
- func (rd *Reader) NextRune() (rune, error)
- func (rd *Reader) One() (Value, error)
- func (rd Reader) Position() Position
- func (rd *Reader) SetMacro(init rune, macro ReaderMacro, isDispatch bool)
- func (rd *Reader) SkipSpaces() error
- func (rd *Reader) Unread(runes ...rune)
- type ReaderMacro
- type Scope
- type Seq
- type Set
- type SpecialForm
- type String
- type Symbol
- type Type
- type Value
- type Values
- func (vals Values) Compare(v Value) bool
- func (vals Values) Conj(args ...Value) Seq
- func (vals Values) Cons(v Value) Seq
- func (vals Values) Eval(_ Scope) (Value, error)
- func (vals Values) First() Value
- func (vals Values) Next() Seq
- func (vals Values) Size() int
- func (vals Values) String() string
- func (vals Values) Uniq() []Value
- type Vector
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSkip can be returned by reader macro to indicate a no-op form which // should be discarded (e.g., Comments). ErrSkip = errors.New("skip expr") // ErrEOF is returned when stream ends prematurely to indicate that more // data is needed to complete the current form. ErrEOF = errors.New("unexpected EOF") )
var ( // Def implements (def symbol value) form for defining bindings. Def = SpecialForm{ Name: "def", Parse: parseDef, } // Lambda defines an anonymous function and returns. Must have the form // (fn* name? [arg*] expr*) or (fn* name? ([arg]* expr*)+) Lambda = SpecialForm{ Name: "fn*", Parse: fnParser(false), } // Macro defines an anonymous function and returns. Must have the form // (macro* name? [arg*] expr*) or (fn* name? ([arg]* expr*)+) Macro = SpecialForm{ Name: "macro*", Parse: fnParser(true), } // Let implements the (let [binding*] expr*) form. expr are evaluated // with given local bindings. Let = SpecialForm{ Name: "let", Parse: parseLet, } // Do special form evaluates args one by one and returns the result of // the last expr. Do = SpecialForm{ Name: "do", Parse: parseDo, } // If implements if-conditional flow using (if test then else?) form. If = SpecialForm{ Name: "if", Parse: parseIf, } // SimpleQuote prevents a form from being evaluated. SimpleQuote = SpecialForm{ Name: "quote", Parse: parseSimpleQuote, } // SyntaxQuote recursively applies the quoting to the form. SyntaxQuote = SpecialForm{ Name: "syntax-quote", Parse: parseSyntaxQuote, } Recur = SpecialForm{ Name: "recur", Parse: parseRecur, } )
var ErrResolving = errors.New("unable to resolve symbol")
ErrResolving is returned when a scope implementation fails to resolve a binding for given symbol.
Functions ¶
Types ¶
type Character ¶
type Character rune
Character represents a character literal. For example, \a, \b, \1, \∂ etc are valid character literals. In addition, special literals like \newline, \space etc are supported by the reader.
type Float64 ¶
type Float64 float64
Float64 represents double precision floating point numbers represented using decimal or scientific number formats.
type Fn ¶
type Fn struct { Args []string Variadic bool Body Value Func func(scope Scope, args []Value) (Value, error) }
Fn represents a function or macro definition.
func (*Fn) Compare ¶
Compare returns true if 'other' is also a function and has the same signature and body.
type HashMap ¶
HashMap represents a container for key-value pairs.
func (*HashMap) Eval ¶
Eval evaluates all keys and values and returns a new HashMap containing the evaluated values.
func (*HashMap) Get ¶
Get returns the value associated with the given key if found. Returns def otherwise.
type Int64 ¶
type Int64 int64
Int64 represents integer values represented using decimal, octal, radix and hexadecimal formats.
type Invokable ¶
Invokable represents any value that supports invocation. Vector, Fn etc support invocation.
type Keyword ¶
type Keyword string
Keyword represents a keyword literal.
type List ¶
List represents an list of forms/vals. Evaluating a list leads to a function invocation.
type MapScope ¶
type MapScope struct {
// contains filtered or unexported fields
}
MapScope implements Scope using a Go native hash-map.
func NewScope ¶
NewScope returns an instance of MapScope with no bindings. If you need builtin special forms, pass result of New() as argument.
func (*MapScope) BindGo ¶
BindGo is similar to Bind but handles conversion of Go value 'v' to
Value type. See `ValueOf()`
type Module ¶
type Module []Value
Module represents a group of forms. Evaluating a module leads to evaluation of each form in order and result will be the result of last evaluation.
func (Module) Compare ¶
Compare returns true if the 'v' is also a module and all forms in the module are equivalent.
type MultiFn ¶
MultiFn represents a multi-arity function or macro definition.
func (MultiFn) Compare ¶
Compare returns true if 'v' is also a MultiFn and all methods are equivalent.
type Position ¶
Position represents the positional information about a value read by reader.
type Reader ¶
type Reader struct { File string // contains filtered or unexported fields }
Reader provides functions to parse characters from a stream into symbolic expressions or forms.
func NewReader ¶
NewReader returns a lisp reader instance which can read forms from rs. Reader behavior can be customized by using SetMacro to override or remove from the default read table. File name will be inferred from the reader value and type information or can be set manually on the Reader.
func (*Reader) All ¶
All consumes characters from stream until EOF and returns a list of all the forms parsed. Any no-op forms (e.g., comment) returned will not be included in the result.
func (*Reader) IsTerminal ¶
IsTerminal returns true if the rune should terminate a form. ReaderMacro trigger runes defined in the read table and all space characters including "," are considered terminal.
func (*Reader) One ¶
One consumes characters from underlying stream until a complete form is parsed and returns the form while ignoring the no-op forms like comments. Except EOF, all errors will be wrapped with ReaderError type along with the positional information obtained using Position().
func (Reader) Position ¶
Position returns information about the stream including file name and the position of the reader.
func (*Reader) SetMacro ¶
func (rd *Reader) SetMacro(init rune, macro ReaderMacro, isDispatch bool)
SetMacro sets the given reader macro as the handler for init rune in the read table. Overwrites if a macro is already present. If the macro value given is nil, entry for the init rune will be removed from the read table. isDispatch decides if the macro is a dispatch macro and takes effect only after a '#' sign.
func (*Reader) SkipSpaces ¶
SkipSpaces consumes and discards runes from stream repeatedly until a character that is not a whitespace is identified. Along with standard unicode white-space characters "," is also considered a white-space and discarded.
type ReaderMacro ¶
ReaderMacro implementations can be plugged into the Reader to extend, override or customize behavior of the reader.
type Scope ¶
type Scope interface { Parent() Scope Bind(symbol string, v Value) error Resolve(symbol string) (Value, error) }
Scope implementation is responsible for managing value bindings.
type Seq ¶
type Seq interface { Value // First should return first value of the sequence or nil if the // sequence is empty. First() Value // Next should return the remaining sequence when the first value // is excluded. Next() Seq // Cons should add the value to the beginning of the sequence and // return the new sequence. Cons(v Value) Seq // Conj should join the given values to the sequence and return a // new sequence. Conj(vals ...Value) Seq }
Seq implementations represent a sequence/list of values.
type Set ¶
Set represents a list of unique values. (Experimental)
type SpecialForm ¶
SpecialForm is a Value type for representing special forms that will be subjected to an intermediate Parsing stage before evaluation.
func (SpecialForm) Eval ¶
func (sf SpecialForm) Eval(_ Scope) (Value, error)
Eval always returns error since it is not allowed to directly evaluate a special form.
func (SpecialForm) String ¶
func (sf SpecialForm) String() string
type String ¶
type String string
String represents double-quoted string literals. String Form represents the true string value obtained from the reader. Escape sequences are not applicable at this level.
func (String) Conj ¶
Conj joins the given values to list of characters of the string and returns the new sequence.
func (String) Cons ¶
Cons converts the string to character sequence and adds the given value to the beginning of the list.
type Symbol ¶
Symbol represents a name given to a value in memory.
func (Symbol) Compare ¶
Compare compares this symbol to the given value. Returns true if the given value is a symbol with same data.
type Type ¶
Type represents the type value of a given value. Type also implements Value type.
type Value ¶
type Value interface { // String should return the LISP representation of the value. String() string // Eval should evaluate this value against the scope and return // the resultant value or an evaluation error. Eval(scope Scope) (Value, error) }
Value represents data/forms in spirit. This includes those emitted by Reader, values obtained as result of an evaluation etc.
func MacroExpand ¶
MacroExpand expands the macro invocation form.
func ReadEval ¶
ReadEval consumes data from reader 'r' till EOF, parses into forms and evaluates all the forms obtained and returns the result.
func ReadEvalStr ¶
ReadEvalStr is a convenience wrapper for Eval that reads forms from string and evaluates for result.
func ValueOf ¶
func ValueOf(v interface{}) Value
ValueOf converts a Go value to spirit Value type. If 'v' is already a Value type, it is returned as is. Primitive Go values like string, rune, int, float, bool are converted to the right spirit Value types. Functions are converted to the wrapper 'Fn' type. Value of type 'reflect.Type' will be wrapped as 'Type' which enables initializing a value of that type when invoked. All other types will be wrapped using 'Any' type.
type Values ¶
type Values []Value
Values represents a list of values and implements the Seq interface.
func (Values) Compare ¶
Compare compares the values in this sequence to the other sequence. other sequence will be realized for comparison.
func (Values) First ¶
First returns the first value in the list if the list is not empty. Returns Nil{} otherwise.
type Vector ¶
Vector represents a list of values. Unlike List type, evaluation of vector does not lead to function invoke.
func (Vector) Eval ¶
Eval evaluates each value in the vector form and returns the resultant values as new vector.