Expr represents an expression node.
type Expr struct { Type Type Term Term Op Operator Left *Expr Right *Expr Const int64 }
func Int(v int64) (p *Expr)
Int creates an expression from an integer.
func Ref(t Term) (p *Expr)
Ref creates an expression from a Term.
func (self *Expr) Add(v *Expr) *Expr
func (self *Expr) And(v *Expr) *Expr
func (self *Expr) Div(v *Expr) *Expr
func (self *Expr) Evaluate() (int64, error)
Evaluate evaluates the expression into an integer. It also implements the Term interface.
func (self *Expr) Free()
Free returns the Expr into pool. Any operation performed after Free is undefined behavior.
func (self *Expr) Mod(v *Expr) *Expr
func (self *Expr) Mul(v *Expr) *Expr
func (self *Expr) Neg() *Expr
func (self *Expr) Not() *Expr
func (self *Expr) Or(v *Expr) *Expr
func (self *Expr) Pow(v *Expr) *Expr
func (self *Expr) Shl(v *Expr) *Expr
func (self *Expr) Shr(v *Expr) *Expr
func (self *Expr) Sub(v *Expr) *Expr
func (self *Expr) Xor(v *Expr) *Expr
Operator represents an operation to perform when Type is EXPR.
type Operator uint8
const ( // ADD performs "Add Expr.Left and Expr.Right". ADD Operator = iota // SUB performs "Subtract Expr.Left by Expr.Right". SUB // MUL performs "Multiply Expr.Left by Expr.Right". MUL // DIV performs "Divide Expr.Left by Expr.Right". DIV // MOD performs "Modulo Expr.Left by Expr.Right". MOD // AND performs "Bitwise AND Expr.Left and Expr.Right". AND // OR performs "Bitwise OR Expr.Left and Expr.Right". OR // XOR performs "Bitwise XOR Expr.Left and Expr.Right". XOR // SHL performs "Bitwise Shift Expr.Left to the Left by Expr.Right Bits". SHL // SHR performs "Bitwise Shift Expr.Left to the Right by Expr.Right Bits". SHR // POW performs "Raise Expr.Left to the power of Expr.Right" POW // NOT performs "Bitwise Invert Expr.Left". NOT // NEG performs "Negate Expr.Left". NEG )
func (self Operator) String() string
String returns the string representation of a Type.
Parser parses an expression string to it's AST representation.
type Parser struct {
// contains filtered or unexported fields
}
func (self *Parser) Parse(repo Repository) (*Expr, error)
Parse parses the expression, and returns it's AST tree.
func (self *Parser) SetSource(src string) *Parser
SetSource resets the expression parser and sets the expression source.
Repository represents a repository of Term's.
type Repository interface { Get(name string) (Term, error) }
RuntimeError is an error which would occure at run time.
type RuntimeError struct { Reason string }
func (self *RuntimeError) Error() string
SyntaxError represents a syntax error in the expression.
type SyntaxError struct { Pos int Reason string }
func (self *SyntaxError) Error() string
Term represents a value that can Evaluate() into an integer.
type Term interface { Free() Evaluate() (int64, error) }
Type is tyep expression type.
type Type int
const ( // CONST indicates that the expression is a constant. CONST Type = iota // TERM indicates that the expression is a Term reference. TERM // EXPR indicates that the expression is a unary or binary expression. EXPR )
func (self Type) String() string
String returns the string representation of a Type.