Documentation ¶
Overview ¶
Package twik implements a tiny embeddable language for Go.
For details, see the blog post:
http://blog.labix.org/2013/07/16/twik-a-tiny-language-for-go
Index ¶
- Variables
- func NewFileSet() *ast.FileSet
- func Parse(fset *ast.FileSet, name string, code []byte) (ast.Node, error)
- func ParseString(fset *ast.FileSet, name string, code string) (ast.Node, error)
- type DefaultScope
- func (s *DefaultScope) Branch() Scope
- func (s *DefaultScope) Create(symbol string, value interface{}) error
- func (s *DefaultScope) Enclose(parent Scope) error
- func (s *DefaultScope) Eval(node ast.Node) (value interface{}, err error)
- func (s *DefaultScope) Get(symbol string) (value interface{}, err error)
- func (s *DefaultScope) Set(symbol string, value interface{}) error
- type Error
- type Scope
Constants ¶
This section is empty.
Variables ¶
var Globals = []struct { Name string value interface{} }{ {"true", true}, {"false", false}, {"nil", nil}, {"error", errorFn}, {"==", eqFn}, {"!=", neFn}, {"+", plusFn}, {"-", minusFn}, {"*", mulFn}, {"/", divFn}, {">", gtFn}, {">=", gteFn}, {"<", ltFn}, {"<=", lteFn}, {"or", orFn}, {"and", andFn}, {"if", ifFn}, {"when", whenFn}, {"unless", unlessFn}, {"var", varFn}, {"set", setFn}, {"do", doFn}, {"func", funcFn}, {"for", forFn}, {"range", rangeFn}, {"split", splitFn}, {"nth", nthFn}, {"length", lengthFn}, }
Functions ¶
func NewFileSet ¶
NewFileSet returns a new FileSet to hold positioning information for a set of parsed twik sources.
Types ¶
type DefaultScope ¶
type DefaultScope struct {
// contains filtered or unexported fields
}
DefaultScope contains standard functions (symbols) like 'if', 'and' etc. Implements Scope interface.
func (*DefaultScope) Branch ¶
func (s *DefaultScope) Branch() Scope
Branch returns a new scope that has s as a parent.
func (*DefaultScope) Create ¶
func (s *DefaultScope) Create(symbol string, value interface{}) error
Create defines a new symbol with the given value in the s scope. It is an error to redefine an existent symbol.
func (*DefaultScope) Enclose ¶
func (s *DefaultScope) Enclose(parent Scope) error
func (*DefaultScope) Eval ¶
func (s *DefaultScope) Eval(node ast.Node) (value interface{}, err error)
Eval evaluates node in the s scope and returns the resulting value.
func (*DefaultScope) Get ¶
func (s *DefaultScope) Get(symbol string) (value interface{}, err error)
Get returns the value of symbol in the shallowest scope it is defined in. It is an error to get the value of an undefined symbol.
func (*DefaultScope) Set ¶
func (s *DefaultScope) Set(symbol string, value interface{}) error
Set sets symbol to the given value in the shallowest scope it is defined in. It is an error to set an undefined symbol.
type Scope ¶
type Scope interface { Create(string, interface{}) error Set(string, interface{}) error Get(string) (interface{}, error) Branch() Scope Eval(ast.Node) (interface{}, error) Enclose(Scope) error }
Scope is an environment where twik logic may be evaluated in.
func NewDefaultScope ¶
NewScope returns a new scope for evaluating logic that was parsed into fset.