parser

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package parser provides functions for turning token stream into an AST (Symbolic Expressions)

Index

Constants

This section is empty.

Variables

View Source
var ErrEOF = errors.New("end of file")

ErrEOF is returned when the parser has consumed all tokens.

Functions

This section is empty.

Types

type Expr

type Expr interface {
	Eval(env Scope) (interface{}, error)
}

Expr represents an evaluatable expression.

func Parse

func Parse(name string, src string) (Expr, error)

Parse tokenizes and parses the src to build an AST.

type KeywordExpr added in v0.0.2

type KeywordExpr struct {
	Keyword string
}

KeywordExpr represents a keyword literal.

func (KeywordExpr) Eval added in v0.0.2

func (ke KeywordExpr) Eval(scope Scope) (interface{}, error)

Eval returns the keyword itself.

func (KeywordExpr) String added in v0.0.2

func (ke KeywordExpr) String() string

type ListExpr

type ListExpr struct {
	List []Expr
}

ListExpr represents a list (i.e., a function call) expression.

func (ListExpr) Eval

func (le ListExpr) Eval(scope Scope) (interface{}, error)

Eval evaluates each s-exp in the list and then evaluates the list itself as an s-exp.

func (ListExpr) String

func (le ListExpr) String() string

type MacroFunc

type MacroFunc func(scope Scope, name string, exprs []Expr) (interface{}, error)

MacroFunc will receive un-evaluated list of s-expressions and the current scope. In addition, if the macro was accessed through a name the name will be passed as well. If the macro was not accessed by name (e.g. was result of another list etc.), name will be empty string.

type MapExpr added in v0.0.2

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

MapExpr represents a map literal expression.

func (MapExpr) Eval added in v0.0.2

func (me MapExpr) Eval(scope Scope) (interface{}, error)

Eval evaluates a map literal expression into map[string]interface{}

type ModuleExpr

type ModuleExpr struct {
	Name  string
	Exprs []Expr
}

ModuleExpr represents a list of Exprs.

func (ModuleExpr) Eval

func (me ModuleExpr) Eval(scope Scope) (interface{}, error)

Eval executes each expression in the module and returns the last result.

func (ModuleExpr) String

func (me ModuleExpr) String() string

type NumberExpr

type NumberExpr struct {
	NumStr string
	Number interface{}
}

NumberExpr represents number s-expression.

func (NumberExpr) Eval

func (ne NumberExpr) Eval(scope Scope) (interface{}, error)

Eval for a number returns itself.

func (NumberExpr) String

func (ne NumberExpr) String() string

type QuoteExpr

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

QuoteExpr implements the quote-literal form.

func (QuoteExpr) Eval

func (qe QuoteExpr) Eval(scope Scope) (interface{}, error)

Eval returns the expression itself without evaluating it.

func (QuoteExpr) String

func (qe QuoteExpr) String() string

func (QuoteExpr) UnquoteEval

func (qe QuoteExpr) UnquoteEval(scope Scope) (interface{}, error)

UnquoteEval unquotes and evaluates the underlying expression.

type Scope

type Scope interface {
	Get(name string) (interface{}, error)
	Doc(name string) string
	Bind(name string, v interface{}, doc ...string) error
	Root() Scope
}

Scope is responsible for managing bindings.

type ScopedFunc

type ScopedFunc func(scope Scope, vals ...interface{}) (interface{}, error)

ScopedFunc are like normal functions but get access to the current scope. Eval is an example of a ScopedFunc.

type StringExpr

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

StringExpr represents single and double quoted strings.

func (StringExpr) Eval

func (se StringExpr) Eval(_ Scope) (interface{}, error)

Eval returns unquoted version of the STRING token.

func (StringExpr) String

func (se StringExpr) String() string

type SymbolExpr

type SymbolExpr struct {
	Symbol string
}

SymbolExpr represents a symbol.

func (SymbolExpr) Eval

func (se SymbolExpr) Eval(scope Scope) (interface{}, error)

Eval returns the symbol name itself.

func (SymbolExpr) ExpType

func (se SymbolExpr) ExpType() string

ExpType returns s-expression type name.

func (SymbolExpr) String

func (se SymbolExpr) String() string

type VectorExpr

type VectorExpr struct {
	List []Expr
}

VectorExpr represents a vector form.

func (VectorExpr) Eval

func (ve VectorExpr) Eval(scope Scope) (interface{}, error)

Eval creates a golang slice.

func (VectorExpr) String

func (ve VectorExpr) String() string

Jump to

Keyboard shortcuts

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