tick

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2016 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

TICKscript is a simple invocation chaining DSL.

See the examples for how its used and example syntax of the DSL.

A reflection based evaluation of an AST.

Index

Examples

Constants

View Source
const (
	TokenError tokenType = iota
	TokenEOF
	TokenVar
	TokenAsgn
	TokenDot
	TokenIdent
	TokenReference
	TokenLambda
	TokenNumber
	TokenString
	TokenDuration
	TokenLParen
	TokenRParen
	TokenComma
	TokenNot
	TokenTrue
	TokenFalse
	TokenRegex

	TokenPlus
	TokenMinus
	TokenMult
	TokenDiv
	TokenMod

	TokenAnd
	TokenOr
	TokenEqual
	TokenNotEqual
	TokenLess
	TokenGreater
	TokenLessEqual
	TokenGreaterEqual
	TokenRegexEqual
	TokenRegexNotEqual
)

Variables

View Source
var ErrEmptyStack = errors.New("stack is empty")
View Source
var ErrInvalidExpr = errors.New("expression is invalid, could not evaluate")
View Source
var ErrNotFloat = errors.New("value is not a float")

Functions

func Evaluate

func Evaluate(script string, scope *Scope) (err error)

Parse and evaluate a given script for the scope. This evaluation method uses reflection to call methods on objects within the scope.

Example
package main

import (
	"fmt"
)

type Process struct {
	Name     string
	Children []*Process
}

func (p *Process) Spawn() *Process {
	child := &Process{}
	p.Children = append(p.Children, child)
	return child
}

func (p *Process) String() string {
	return fmt.Sprintf("{%q %s}", p.Name, p.Children)
}

func main() {

	//Run a test that evaluates the DSL against the Process struct.
	script := `
//Name the parent
parent.name('parent')

// Spawn a first child
var child1 = parent.spawn()

// Name the first child
child1.name('child1')

//Spawn a grandchild and name it
child1.spawn().name('grandchild')

//Spawn a second child and name it
parent.spawn().name('child2')
`

	scope := NewScope()
	parent := &Process{}
	scope.Set("parent", parent)

	err := Evaluate(script, scope)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(parent)
}
Output:

{"parent" [{"child1" [{"grandchild" []}]} {"child2" []}]}

Types

type BinaryNode

type BinaryNode struct {
	Left     Node
	Right    Node
	Operator tokenType
	// contains filtered or unexported fields
}

binaryNode holds two arguments and an operator.

func (BinaryNode) Position

func (p BinaryNode) Position() int

func (*BinaryNode) String

func (b *BinaryNode) String() string

type BoolNode

type BoolNode struct {
	Bool bool
	// contains filtered or unexported fields
}

boolNode holds one argument and an operator.

func (BoolNode) Position

func (p BoolNode) Position() int

func (*BoolNode) String

func (b *BoolNode) String() string

type DurationNode

type DurationNode struct {
	Dur time.Duration //the duration
	// contains filtered or unexported fields
}

durationNode holds a number: signed or unsigned integer or float. The value is parsed and stored under all the types that can represent the value. This simulates in a small amount of code the behavior of Go's ideal constants.

func (DurationNode) Position

func (p DurationNode) Position() int

func (*DurationNode) String

func (d *DurationNode) String() string

type DynamicMethod added in v0.10.0

type DynamicMethod func(self interface{}, args ...interface{}) (interface{}, error)

type Func

type Func interface {
	Reset()
	Call(...interface{}) (interface{}, error)
}

A callable function from within the expression

type Funcs

type Funcs map[string]Func

Lookup for functions

func NewFunctions

func NewFunctions() Funcs

Return set of built-in Funcs

type FunctionNode

type FunctionNode struct {
	Func string // The identifier
	Args []Node
	// contains filtered or unexported fields
}

Holds the a function call with its args

func (FunctionNode) Position

func (p FunctionNode) Position() int

func (*FunctionNode) String

func (f *FunctionNode) String() string

type IdentifierNode

type IdentifierNode struct {
	Ident string // The identifier
	// contains filtered or unexported fields
}

Holds the textual representation of an identifier

func (IdentifierNode) Position

func (p IdentifierNode) Position() int

func (*IdentifierNode) String

func (i *IdentifierNode) String() string

type LambdaNode

type LambdaNode struct {
	Node Node
	// contains filtered or unexported fields
}

Represents the begining of a lambda expression

func (LambdaNode) Position

func (p LambdaNode) Position() int

func (*LambdaNode) String

func (l *LambdaNode) String() string

type ListNode

type ListNode struct {
	Nodes []Node
	// contains filtered or unexported fields
}

Holds a function call with its args

func (*ListNode) Add

func (l *ListNode) Add(n Node)

func (ListNode) Position

func (p ListNode) Position() int

func (*ListNode) String

func (l *ListNode) String() string

type Node

type Node interface {
	String() string
	Position() int // byte position of start of node in full original input string
}

type NumberNode

type NumberNode struct {
	IsInt   bool    // Number has an integral value.
	IsFloat bool    // Number has a floating-point value.
	Int64   int64   // The integer value.
	Float64 float64 // The floating-point value.
	// contains filtered or unexported fields
}

numberNode holds a number: signed or unsigned integer or float. The value is parsed and stored under all the types that can represent the value. This simulates in a small amount of code the behavior of Go's ideal constants.

func (NumberNode) Position

func (p NumberNode) Position() int

func (*NumberNode) String

func (n *NumberNode) String() string

type ReferenceNode

type ReferenceNode struct {
	Reference string // The field reference
	// contains filtered or unexported fields
}

Holds the textual representation of an identifier

func (ReferenceNode) Position

func (p ReferenceNode) Position() int

func (*ReferenceNode) String

func (r *ReferenceNode) String() string

type ReflectionDescriber added in v0.10.0

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

Wraps any object as a SelfDescriber using reflection.

func NewReflectionDescriber added in v0.10.0

func NewReflectionDescriber(obj interface{}) *ReflectionDescriber

func (*ReflectionDescriber) CallMethod added in v0.10.0

func (r *ReflectionDescriber) CallMethod(name string, args ...interface{}) (interface{}, error)

func (*ReflectionDescriber) Desc added in v0.10.0

func (r *ReflectionDescriber) Desc() string

func (*ReflectionDescriber) HasMethod added in v0.10.0

func (r *ReflectionDescriber) HasMethod(name string) bool

Using reflection check if the object has the method or field. A field is a valid method because we can set it via reflection too.

func (*ReflectionDescriber) HasProperty added in v0.10.0

func (r *ReflectionDescriber) HasProperty(name string) bool

Using reflection check if the object has a field with the property name.

func (*ReflectionDescriber) Property added in v0.10.0

func (r *ReflectionDescriber) Property(name string) interface{}

func (*ReflectionDescriber) SetProperty added in v0.10.0

func (r *ReflectionDescriber) SetProperty(name string, value interface{}) error

type RegexNode

type RegexNode struct {
	Regex *regexp.Regexp
	// contains filtered or unexported fields
}

Holds the textual representation of a regex literal

func (RegexNode) Position

func (p RegexNode) Position() int

func (*RegexNode) String

func (s *RegexNode) String() string

type Scope

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

Contains a set of variables references and their values.

func NewScope

func NewScope() *Scope

Initialize a new Scope object.

func (*Scope) DynamicMethod added in v0.10.0

func (s *Scope) DynamicMethod(name string) DynamicMethod

func (*Scope) Get

func (s *Scope) Get(name string) (interface{}, error)

Get returns the value of 'name'.

func (*Scope) Set

func (s *Scope) Set(name string, value interface{})

Set defines a name -> value pairing in the scope.

func (*Scope) SetDynamicMethod added in v0.10.0

func (s *Scope) SetDynamicMethod(name string, m DynamicMethod)

type SelfDescriber added in v0.10.0

type SelfDescriber interface {
	//A description the object
	Desc() string

	HasMethod(name string) bool
	CallMethod(name string, args ...interface{}) (interface{}, error)

	HasProperty(name string) bool
	Property(name string) interface{}
	SetProperty(name string, arg interface{}) error
}

Interface for interacting with objects. If an object does not self describe via this interface than a reflection based implemenation will be used.

type StarNode

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

Represents a standalone '*' token.

func (StarNode) Position

func (p StarNode) Position() int

func (*StarNode) String

func (s *StarNode) String() string

type StatefulExpr

type StatefulExpr struct {
	Node  Node
	Funcs Funcs
}

Expression functions are stateful. Their state is updated with each call to the function. A StatefulExpr is a Node and its associated function state.

func NewStatefulExpr

func NewStatefulExpr(n Node) *StatefulExpr

func (*StatefulExpr) EvalBool

func (s *StatefulExpr) EvalBool(scope *Scope) (bool, error)

func (*StatefulExpr) EvalNum

func (s *StatefulExpr) EvalNum(scope *Scope) (float64, error)

func (*StatefulExpr) Reset

func (s *StatefulExpr) Reset()

Reset the state

type StringNode

type StringNode struct {
	Literal string // The string literal
	// contains filtered or unexported fields
}

Holds the textual representation of a string literal

func (StringNode) Position

func (p StringNode) Position() int

func (*StringNode) String

func (s *StringNode) String() string

type UnaryNode

type UnaryNode struct {
	Node     Node
	Operator tokenType
	// contains filtered or unexported fields
}

unaryNode holds one argument and an operator.

func (UnaryNode) Position

func (p UnaryNode) Position() int

func (*UnaryNode) String

func (u *UnaryNode) String() string

Directories

Path Synopsis
cmd
tickdoc
Tickdoc is a simple utility similiar to godoc that generates documentation from comments.
Tickdoc is a simple utility similiar to godoc that generates documentation from comments.

Jump to

Keyboard shortcuts

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