...

Package expr

import "github.com/chenzhuoyu/iasm/expr"
Overview
Index

Overview ▾

type Expr

Expr represents an expression node.

type Expr struct {
    Type  Type
    Term  Term
    Op    Operator
    Left  *Expr
    Right *Expr
    Const int64
}

func Int

func Int(v int64) (p *Expr)

Int creates an expression from an integer.

func Ref

func Ref(t Term) (p *Expr)

Ref creates an expression from a Term.

func (*Expr) Add

func (self *Expr) Add(v *Expr) *Expr

func (*Expr) And

func (self *Expr) And(v *Expr) *Expr

func (*Expr) Div

func (self *Expr) Div(v *Expr) *Expr

func (*Expr) Evaluate

func (self *Expr) Evaluate() (int64, error)

Evaluate evaluates the expression into an integer. It also implements the Term interface.

func (*Expr) Free

func (self *Expr) Free()

Free returns the Expr into pool. Any operation performed after Free is undefined behavior.

func (*Expr) Mod

func (self *Expr) Mod(v *Expr) *Expr

func (*Expr) Mul

func (self *Expr) Mul(v *Expr) *Expr

func (*Expr) Neg

func (self *Expr) Neg() *Expr

func (*Expr) Not

func (self *Expr) Not() *Expr

func (*Expr) Or

func (self *Expr) Or(v *Expr) *Expr

func (*Expr) Pow

func (self *Expr) Pow(v *Expr) *Expr

func (*Expr) Shl

func (self *Expr) Shl(v *Expr) *Expr

func (*Expr) Shr

func (self *Expr) Shr(v *Expr) *Expr

func (*Expr) Sub

func (self *Expr) Sub(v *Expr) *Expr

func (*Expr) Xor

func (self *Expr) Xor(v *Expr) *Expr

type Operator

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 (Operator) String

func (self Operator) String() string

String returns the string representation of a Type.

type Parser

Parser parses an expression string to it's AST representation.

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

func (*Parser) Parse

func (self *Parser) Parse(repo Repository) (*Expr, error)

Parse parses the expression, and returns it's AST tree.

func (*Parser) SetSource

func (self *Parser) SetSource(src string) *Parser

SetSource resets the expression parser and sets the expression source.

type Repository

Repository represents a repository of Term's.

type Repository interface {
    Get(name string) (Term, error)
}

type RuntimeError

RuntimeError is an error which would occure at run time.

type RuntimeError struct {
    Reason string
}

func (*RuntimeError) Error

func (self *RuntimeError) Error() string

type SyntaxError

SyntaxError represents a syntax error in the expression.

type SyntaxError struct {
    Pos    int
    Reason string
}

func (*SyntaxError) Error

func (self *SyntaxError) Error() string

type Term

Term represents a value that can Evaluate() into an integer.

type Term interface {
    Free()
    Evaluate() (int64, error)
}

type Type

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 (Type) String

func (self Type) String() string

String returns the string representation of a Type.