ast

package
v0.6.9 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2016 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arithmetic added in v0.4.0

type Arithmetic struct {
	Op    ArithmeticOp
	Exprs []Node
	Posx  Pos
}

Arithmetic represents a node where the result is arithmetic of two or more operands in the order given.

func (*Arithmetic) Accept added in v0.4.0

func (n *Arithmetic) Accept(v Visitor) Node

func (*Arithmetic) GoString added in v0.4.0

func (n *Arithmetic) GoString() string

func (*Arithmetic) Pos added in v0.4.0

func (n *Arithmetic) Pos() Pos

func (*Arithmetic) String added in v0.4.0

func (n *Arithmetic) String() string

func (*Arithmetic) Type added in v0.4.0

func (n *Arithmetic) Type(Scope) (Type, error)

type ArithmeticOp added in v0.4.0

type ArithmeticOp int

ArithmeticOp is the operation to use for the math.

const (
	ArithmeticOpInvalid ArithmeticOp = 0
	ArithmeticOpAdd     ArithmeticOp = iota
	ArithmeticOpSub
	ArithmeticOpMul
	ArithmeticOpDiv
	ArithmeticOpMod
)

type BasicScope

type BasicScope struct {
	FuncMap map[string]Function
	VarMap  map[string]Variable
}

BasicScope is a simple scope that looks up variables and functions using a map.

func (*BasicScope) LookupFunc

func (s *BasicScope) LookupFunc(n string) (Function, bool)

func (*BasicScope) LookupVar

func (s *BasicScope) LookupVar(n string) (Variable, bool)

type Call

type Call struct {
	Func string
	Args []Node
	Posx Pos
}

Call represents a function call.

func (*Call) Accept

func (n *Call) Accept(v Visitor) Node

func (*Call) GoString added in v0.6.0

func (n *Call) GoString() string

func (*Call) Pos

func (n *Call) Pos() Pos

func (*Call) String

func (n *Call) String() string

func (*Call) Type

func (n *Call) Type(s Scope) (Type, error)

type Concat

type Concat struct {
	Exprs []Node
	Posx  Pos
}

Concat represents a node where the result of two or more expressions are concatenated. The result of all expressions must be a string.

func (*Concat) Accept

func (n *Concat) Accept(v Visitor) Node

func (*Concat) GoString

func (n *Concat) GoString() string

func (*Concat) Pos

func (n *Concat) Pos() Pos

func (*Concat) String

func (n *Concat) String() string

func (*Concat) Type

func (n *Concat) Type(Scope) (Type, error)

type Function

type Function struct {
	// ArgTypes is the list of types in argument order. These are the
	// required arguments.
	//
	// ReturnType is the type of the returned value. The Callback MUST
	// return this type.
	ArgTypes   []Type
	ReturnType Type

	// Variadic, if true, says that this function is variadic, meaning
	// it takes a variable number of arguments. In this case, the
	// VariadicType must be set.
	Variadic     bool
	VariadicType Type

	// Callback is the function called for a function. The argument
	// types are guaranteed to match the spec above by the type checker.
	// The length of the args is strictly == len(ArgTypes) unless Varidiac
	// is true, in which case its >= len(ArgTypes).
	Callback func([]interface{}) (interface{}, error)
}

Function defines a function that can be executed by the engine. The type checker will validate that the proper types will be called to the callback.

type LiteralNode

type LiteralNode struct {
	Value interface{}
	Typex Type
	Posx  Pos
}

LiteralNode represents a single literal value, such as "foo" or 42 or 3.14159. Based on the Type, the Value can be safely cast.

func (*LiteralNode) Accept

func (n *LiteralNode) Accept(v Visitor) Node

func (*LiteralNode) GoString

func (n *LiteralNode) GoString() string

func (*LiteralNode) Pos

func (n *LiteralNode) Pos() Pos

func (*LiteralNode) String

func (n *LiteralNode) String() string

func (*LiteralNode) Type

func (n *LiteralNode) Type(Scope) (Type, error)

type Node

type Node interface {
	// Accept is called to dispatch to the visitors. It must return the
	// resulting Node (which might be different in an AST transform).
	Accept(Visitor) Node

	// Pos returns the position of this node in some source.
	Pos() Pos

	// Type returns the type of this node for the given context.
	Type(Scope) (Type, error)
}

Node is the interface that all AST nodes must implement.

type Pos

type Pos struct {
	Column, Line int // Column/Line number, starting at 1
}

Pos is the starting position of an AST node

func (Pos) String

func (p Pos) String() string

type Scope

type Scope interface {
	LookupFunc(string) (Function, bool)
	LookupVar(string) (Variable, bool)
}

Scope is the interface used to look up variables and functions while evaluating. How these functions/variables are defined are up to the caller.

type Stack

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

Stack is a stack of Node.

func (*Stack) Len

func (s *Stack) Len() int

func (*Stack) Pop

func (s *Stack) Pop() Node

func (*Stack) Push

func (s *Stack) Push(n Node)

func (*Stack) Reset

func (s *Stack) Reset()

type Type

type Type uint32

Type is the type of any value.

const (
	TypeInvalid Type = 0
	TypeAny     Type = 1 << iota
	TypeString
	TypeInt
	TypeFloat
)

func (Type) String

func (i Type) String() string

type UnaryArithmetic added in v0.6.9

type UnaryArithmetic struct {
	Op   ArithmeticOp
	Expr Node
	Posx Pos
}

UnaryArithmetic represents a node where the result is arithmetic of one operands

func (*UnaryArithmetic) Accept added in v0.6.9

func (n *UnaryArithmetic) Accept(v Visitor) Node

func (*UnaryArithmetic) GoString added in v0.6.9

func (n *UnaryArithmetic) GoString() string

func (*UnaryArithmetic) Pos added in v0.6.9

func (n *UnaryArithmetic) Pos() Pos

func (*UnaryArithmetic) String added in v0.6.9

func (n *UnaryArithmetic) String() string

func (*UnaryArithmetic) Type added in v0.6.9

func (n *UnaryArithmetic) Type(Scope) (Type, error)

type Variable

type Variable struct {
	Value interface{}
	Type  Type
}

Variable is a variable value for execution given as input to the engine. It records the value of a variables along with their type.

type VariableAccess

type VariableAccess struct {
	Name string
	Posx Pos
}

VariableAccess represents a variable access.

func (*VariableAccess) Accept

func (n *VariableAccess) Accept(v Visitor) Node

func (*VariableAccess) GoString

func (n *VariableAccess) GoString() string

func (*VariableAccess) Pos

func (n *VariableAccess) Pos() Pos

func (*VariableAccess) String

func (n *VariableAccess) String() string

func (*VariableAccess) Type

func (n *VariableAccess) Type(s Scope) (Type, error)

type Visitor

type Visitor func(Node) Node

Visitors are just implementations of this function.

The function must return the Node to replace this node with. "nil" is _not_ a valid return value. If there is no replacement, the original node should be returned. We build this replacement directly into the visitor pattern since AST transformations are a common and useful tool and building it into the AST itself makes it required for future Node implementations and very easy to do.

Note that this isn't a true implementation of the visitor pattern, which generally requires proper type dispatch on the function. However, implementing this basic visitor pattern style is still very useful even if you have to type switch.

Jump to

Keyboard shortcuts

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