ast

package
v0.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2021 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StringTerminateNone  StringTerminate = 0
	StringTerminateBegin                 = 1
	StringTerminateEnd                   = 2
	StringTerminateBoth                  = StringTerminateBegin | StringTerminateEnd
)
View Source
const (
	TargetInitialize = "initialize"
	TargetFinalize   = "finalize"
	TargetAll        = "all"
)
View Source
const (
	TransformSlice reflect.Kind = reflect.UnsafePointer + 1000 + iota
	TransformMap
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayLiteral

type ArrayLiteral struct {
	*Base
	Multiline bool
	Values    []Node
}

A node represent array or list literal

func (*ArrayLiteral) Evaluate

func (al *ArrayLiteral) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

ArrayLiteral Evaluate return value of array or list

func (*ArrayLiteral) String

func (al *ArrayLiteral) String() string

func (*ArrayLiteral) Visit

func (al *ArrayLiteral) Visit(cb CodeBuilder)

type AssignStatement

type AssignStatement struct {
	*Base
	Ident SettableNode
	Op    token.Token
	Value Node
}

AssignStatement implement Node and handle all assign operation

func (*AssignStatement) Evaluate

func (as *AssignStatement) Evaluate(ctx Context) (err error)

func (*AssignStatement) String

func (as *AssignStatement) String() string

func (*AssignStatement) Visit

func (as *AssignStatement) Visit(cb CodeBuilder)

type Base

type Base struct {
	Offset int
	File   *token.File
}

provide basic info related to ast node

func (*Base) ErrPos

func (b *Base) ErrPos() string

func (*Base) Position

func (b *Base) Position() token.Position

type BasicLit

type BasicLit struct {
	*Base
	Kind token.Token
	Mark byte
	Lit  string
}

A node represent literal value

func (*BasicLit) Evaluate

func (bl *BasicLit) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

BasicLit Evaluate convert a string lit value to it corresponding type value such as integer, float, bool ...etc

func (*BasicLit) String

func (bl *BasicLit) String() string

func (*BasicLit) Visit

func (bl *BasicLit) Visit(cb CodeBuilder)

type Binary

type Binary struct {
	*Base
	L  Node
	Op token.Token
	R  Node
}

A node represent binary tree operator

func (*Binary) Evaluate

func (b *Binary) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

Binary Evaluate return result of pair operation

func (*Binary) String

func (b *Binary) String() string

func (*Binary) Visit

func (b *Binary) Visit(cb CodeBuilder)

type BlockStatement

type BlockStatement struct {
	*Base

	Stmts []Statement
	// contains filtered or unexported fields
}

BlockStatement implement Node contain multiple Statement

func (*BlockStatement) Append

func (bs *BlockStatement) Append(stmt Statement)

func (*BlockStatement) Evaluate

func (bs *BlockStatement) Evaluate(ctx Context) (err error)

func (*BlockStatement) String

func (bs *BlockStatement) String() string

func (*BlockStatement) Visit

func (bs *BlockStatement) Visit(cb CodeBuilder)

type BreakContinueStatement

type BreakContinueStatement struct {
	*Base
	Label string
	Op    token.Token // break or continue
}

func (*BreakContinueStatement) Evaluate

func (bcs *BreakContinueStatement) Evaluate(ctx Context) error

func (*BreakContinueStatement) String

func (bcs *BreakContinueStatement) String() string

func (*BreakContinueStatement) Visit

func (bcs *BreakContinueStatement) Visit(cb CodeBuilder)

type Call

type Call struct {
	*Base
	Kind         token.Token
	Args         []Node
	Name         string
	OutputResult bool
	FuncLit      *Function
}

A node represent call expression or statement

func (*Call) Evaluate

func (c *Call) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

Call Evaluate execute one of the following type an external command line, a target or a function

func (*Call) String

func (c *Call) String() string

func (*Call) Visit

func (c *Call) Visit(cb CodeBuilder)

type Code

type Code interface {
	String() string
	Visit(cb CodeBuilder)
}

type CodeBuilder

type CodeBuilder interface {
	String() string
	io.Writer
	io.ByteWriter
	io.StringWriter
	WriteRune(r rune) (int, error)
	WriteIndent()
	WriteQuoteString(s string, quote byte, stm StringTerminate)
	GetIdent() int
	SetIdent(c int)
	IdentBy(c int)
	Len() int
}

func NewCodeBuilder

func NewCodeBuilder(indent string, formatter bool, maxLength int) CodeBuilder

type Conditional

type Conditional struct {
	*Base
	Cond  Node
	True  Node
	False Node
}

A node represent short if or ternary expression

func (*Conditional) Evaluate

func (cd *Conditional) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

Conditional Evaluate check the condition boolean result, if the result is true is return result of True expression's evaluation otherwise a result from False expression's evalute is return instread. This expression is known as short if or ternary expression.

func (*Conditional) String

func (cd *Conditional) String() string

func (*Conditional) Visit

func (cd *Conditional) Visit(cb CodeBuilder)

type Context

type Context interface {
	Scope
	EnterBlock(forLoop bool, loopLabel string) (Scope, int)
	ExitBlock(index int)
	ShouldBreak(fromLoop bool) bool
	ResetBreakContinue()
	Break(label string) error
	Continue(label string) error
	GetCommand(name string) function.Function
	GetTarget(name string) *Target
	GetFunction(name string) *Function
}

type Cook

type Cook interface {
	Code
	Block() *BlockStatement
	AddFunction(fn *Function)
	AddTarget(base *Base, name string) (*Target, error)
	Execute(pargs map[string]interface{}) error
	ExecuteWithTarget(pargs map[string]interface{}, names ...string) error
	Scope() Scope
}

func NewCook

func NewCook() Cook

type Delete

type Delete struct {
	*Base
	Indexes []Node
	End     Node // if End is not nil, Var must be array and it's delete range.
	X       *Ident
}

A node represent delete statement

func (*Delete) Evaluate

func (d *Delete) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

Delete Evaluate delete item from array or map. Delete return error if there is an error occorred otherwise the result value is always nil

func (*Delete) String

func (d *Delete) String() string

func (*Delete) Visit

func (d *Delete) Visit(cb CodeBuilder)

type ElseStatement

type ElseStatement struct {
	*Base
	IfStmt *IfStatement
	Insts  *BlockStatement
}

func (*ElseStatement) Evaluate

func (efst *ElseStatement) Evaluate(ctx Context) error

func (*ElseStatement) String

func (efst *ElseStatement) String() string

func (*ElseStatement) Visit

func (efst *ElseStatement) Visit(cb CodeBuilder)

type Exit

type Exit struct {
	*Base
	ExitCode Node
}

A node represent exit expression statement

func (*Exit) Evaluate

func (e *Exit) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

Exit Evaluate will exit the execution with the given code.

func (*Exit) String

func (e *Exit) String() string

func (*Exit) Visit

func (e *Exit) Visit(cb CodeBuilder)

type ExprWrapperStatement

type ExprWrapperStatement struct {
	X Node
}

func (*ExprWrapperStatement) ErrPos

func (ews *ExprWrapperStatement) ErrPos() string

func (*ExprWrapperStatement) Evaluate

func (ews *ExprWrapperStatement) Evaluate(ctx Context) (err error)

IncDecExpr Evaluate increase or delete value by 1

func (*ExprWrapperStatement) Position

func (ews *ExprWrapperStatement) Position() token.Position

func (*ExprWrapperStatement) String

func (ews *ExprWrapperStatement) String() string

func (*ExprWrapperStatement) Visit

func (ews *ExprWrapperStatement) Visit(cb CodeBuilder)

type Fallback

type Fallback struct {
	*Base
	Primary Node
	Default Node
}

A node represent fallback expression

func (*Fallback) Evaluate

func (fb *Fallback) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

Fallback Evaluate return the primary result if no error

func (*Fallback) String

func (fb *Fallback) String() string

func (*Fallback) Visit

func (fb *Fallback) Visit(cb CodeBuilder)

type ForStatement

type ForStatement struct {
	*Base
	Label string
	I     *Ident
	Value *Ident
	Oprnd Node      // must be map, array or string
	Range *Interval // {1..2}, (0..3), [1..2]:1
	Insts *BlockStatement
}

func (*ForStatement) Evaluate

func (fst *ForStatement) Evaluate(ctx Context) error

func (*ForStatement) String

func (fst *ForStatement) String() string

func (*ForStatement) Visit

func (fst *ForStatement) Visit(cb CodeBuilder)

type Function

type Function struct {
	Insts  *BlockStatement
	Name   string
	Lambda token.Token
	X      Node
	Args   []*Ident
}

func (*Function) Execute

func (fn *Function) Execute(ctx Context, pargs []Node) (v interface{}, kind reflect.Kind, err error)

func (*Function) String

func (fn *Function) String() string

func (*Function) Visit

func (fn *Function) Visit(cb CodeBuilder)

type Ident

type Ident struct {
	*Base
	Name string
}

A node represent a variable

func (*Ident) Evaluate

func (id *Ident) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

Ident Evaluate read value from current scope always to global scope. If no variable exist, it will look into environment varaible and return it value if founded otherwise a nil and invalid kind is return instead.

func (*Ident) IsEqual

func (id *Ident) IsEqual(sn SettableNode) bool

func (*Ident) Set

func (id *Ident) Set(ctx Context, v interface{}, k reflect.Kind, bubble func(v interface{}, k reflect.Kind) error) (err error)

func (*Ident) String

func (id *Ident) String() string

func (*Ident) VariableName

func (id *Ident) VariableName() string

func (*Ident) Visit

func (id *Ident) Visit(cb CodeBuilder)

type IfStatement

type IfStatement struct {
	*Base
	Cond  Node
	Insts *BlockStatement
	Else  *ElseStatement
}

func (*IfStatement) Evaluate

func (ifst *IfStatement) Evaluate(ctx Context) error

func (*IfStatement) String

func (ifst *IfStatement) String() string

func (*IfStatement) Visit

func (ifst *IfStatement) Visit(cb CodeBuilder)

type IncDec

type IncDec struct {
	*Base
	Op token.Token
	X  Node
}

A node represent incremenet or decrement expression

func (*IncDec) Evaluate

func (idc *IncDec) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

IncDecExpr Evaluate increase or delete value by 1

func (*IncDec) String

func (idc *IncDec) String() string

func (*IncDec) Visit

func (idc *IncDec) Visit(cb CodeBuilder)

type Index

type Index struct {
	*Base
	Index Node
	X     Node
}

A node represent index expression

func (*Index) Evaluate

func (ix *Index) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

Index Evaluate return item of array or map

func (*Index) IsEqual

func (ix *Index) IsEqual(sn SettableNode) bool

func (*Index) Set

func (ix *Index) Set(ctx Context, v interface{}, k reflect.Kind, _ func(v interface{}, k reflect.Kind) error) (err error)

func (*Index) String

func (ix *Index) String() string

func (*Index) VariableName

func (ix *Index) VariableName() string

func (*Index) Visit

func (ix *Index) Visit(cb CodeBuilder)

type Interval

type Interval struct {
	*Base
	A        Node
	AInclude bool
	B        Node
	BInclude bool
	Step     Node
}

A node represent range value

func (*Interval) Evaluate

func (r *Interval) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

Range Evaluate return a result of slice of 2 items value if not error occurred during evaluation

func (*Interval) String

func (r *Interval) String() string

func (*Interval) Visit

func (r *Interval) Visit(cb CodeBuilder)

type IsType

type IsType struct {
	*Base
	X     Node
	Types []token.Token
}

A node represent type check expression

func (*IsType) Evaluate

func (it *IsType) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

IsType Evaluate return a boolean value. It return true if variable X value is satified the given type Token.

func (*IsType) String

func (it *IsType) String() string

func (*IsType) Visit

func (it *IsType) Visit(cb CodeBuilder)

type MapLiteral

type MapLiteral struct {
	*Base
	Multiline bool
	Keys      []Node
	Values    []Node
}

A node represent map literal

func (*MapLiteral) Evaluate

func (ml *MapLiteral) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

MapLiteral Evaluate return value of map or directionary

func (*MapLiteral) String

func (ml *MapLiteral) String() string

func (*MapLiteral) Visit

func (ml *MapLiteral) Visit(cb CodeBuilder)

type MergeMap

type MergeMap struct {
	*Base
	Op    token.Token // must be < or ?
	Value Node        // either a variable holding map or a map literal
}

A new represent map merge operation A += < {...} or A += ? {...}

func (*MergeMap) Evaluate

func (mm *MergeMap) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

func (*MergeMap) Set

func (mm *MergeMap) Set(ctx Context, setVal interface{}, _ reflect.Kind) error

func (*MergeMap) String

func (mm *MergeMap) String() string

func (*MergeMap) VariableName

func (mm *MergeMap) VariableName() string

func (*MergeMap) Visit

func (mm *MergeMap) Visit(cb CodeBuilder)

type Node

type Node interface {
	Code
	ErrPos() string
	Position() token.Position
	Evaluate(ctx Context) (interface{}, reflect.Kind, error)
}

type Paren

type Paren struct {
	*Base
	Inner Node
}

A node represent parenthese expression

func (*Paren) Evaluate

func (p *Paren) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

Paran Evaluate execute inner node and return it's response

func (*Paren) String

func (p *Paren) String() string

func (*Paren) Visit

func (p *Paren) Visit(cb CodeBuilder)

type ReadFrom

type ReadFrom struct {
	*Base
	File Node
}

A node represent redirect read file expression <

func (*ReadFrom) Evaluate

func (rf *ReadFrom) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

ReadFrom Evaluate return content of a file

func (*ReadFrom) String

func (rf *ReadFrom) String() string

func (*ReadFrom) Visit

func (rf *ReadFrom) Visit(cb CodeBuilder)

type RedirectTo

type RedirectTo struct {
	*Base
	Files  []Node
	Caller Node
	Append bool
}

A node represent redirect write to or append to one or more file expression

func (*RedirectTo) Evaluate

func (rt *RedirectTo) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

WriteTo Evaluate write/append the data to the file

func (*RedirectTo) String

func (rt *RedirectTo) String() string

func (*RedirectTo) Visit

func (rt *RedirectTo) Visit(cb CodeBuilder)

type ReturnStatement

type ReturnStatement struct {
	*Base
	X Node
}

A statement represent return expression return

func (*ReturnStatement) Evaluate

func (r *ReturnStatement) Evaluate(ctx Context) error

Return Evaluate return/forward value of a literal value or a value of a variable

func (*ReturnStatement) String

func (rs *ReturnStatement) String() string

func (*ReturnStatement) Visit

func (rs *ReturnStatement) Visit(cb CodeBuilder)

type Scope

type Scope interface {
	GetVariable(name string) (value interface{}, kind reflect.Kind, fromEnv bool)
	SetVariable(name string, value interface{}, kind reflect.Kind, bubble func(v interface{}, k reflect.Kind) error) bool
	SetReturnValue(v interface{}, kind reflect.Kind)
	GetReturnValue() (v interface{}, kind reflect.Kind)
}

type SettableNode

type SettableNode interface {
	Node
	VariableName() string
	Set(ctx Context, v interface{}, k reflect.Kind, bubble func(v interface{}, k reflect.Kind) error) error
	IsEqual(sn SettableNode) bool
}

type SizeOf

type SizeOf struct {
	*Base
	X Node
}

A node represent size of a variable or literal value expression

func (*SizeOf) Evaluate

func (sf *SizeOf) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

SizeOf Evaluate return size of variable or literal value such as string, array, map

func (*SizeOf) String

func (sf *SizeOf) String() string

func (*SizeOf) Visit

func (sf *SizeOf) Visit(cb CodeBuilder)

type Statement

type Statement interface {
	Code
	Evaluate(ctx Context) error
}

type StringInterpolation

type StringInterpolation struct {
	*Base
	// contains filtered or unexported fields
}

func (*StringInterpolation) Evaluate

func (si *StringInterpolation) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

func (*StringInterpolation) String

func (si *StringInterpolation) String() string

func (*StringInterpolation) Visit

func (si *StringInterpolation) Visit(cb CodeBuilder)

type StringInterpolationBuilder

type StringInterpolationBuilder interface {
	io.StringWriter
	AddExpression(node Node)
	Build(offset int, file *token.File) Node
}

func NewStringInterpolationBuilder

func NewStringInterpolationBuilder(mark byte) StringInterpolationBuilder

type StringTerminate

type StringTerminate uint8

type SubValue

type SubValue struct {
	*Base
	Range *Interval
	X     Node
}

A node represent sub value of original value

func (*SubValue) Evaluate

func (sv *SubValue) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

SubValue Evaluate return sub string or a slice of original slice

func (*SubValue) String

func (sv *SubValue) String() string

func (*SubValue) Visit

func (sv *SubValue) Visit(cb CodeBuilder)

type Target

type Target struct {
	*Base

	Insts *BlockStatement
	// contains filtered or unexported fields
}

func (*Target) Execute

func (t *Target) Execute(ctx Context, args []*args.FunctionArg) error

func (*Target) SetCallAll

func (t *Target) SetCallAll()

func (*Target) String

func (t *Target) String() string

func (*Target) Visit

func (t *Target) Visit(cb CodeBuilder)

func (*Target) Vist

func (t *Target) Vist(cb CodeBuilder)

type Targets

type Targets []*Target

func (Targets) Len

func (t Targets) Len() int

func (Targets) Less

func (t Targets) Less(i, j int) bool

func (Targets) Swap

func (t Targets) Swap(i, j int)

type Transformation

type Transformation struct {
	*Base
	Ident SettableNode
	Fn    *Function
	// contains filtered or unexported fields
}

A node represent a transform expression

func (*Transformation) Evaluate

func (t *Transformation) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

Transformation Evaluate apply tranform on an array or map

func (*Transformation) String

func (t *Transformation) String() string

func (*Transformation) Visit

func (t *Transformation) Visit(cb CodeBuilder)

type TypeCast

type TypeCast struct {
	*Base
	X  Node
	To token.Token
}

A node represent type cast expression

func (*TypeCast) Evaluate

func (tc *TypeCast) Evaluate(ctx Context) (v interface{}, k reflect.Kind, err error)

TypeCast Evaluate return convertible value converted from string to string

func (*TypeCast) String

func (tc *TypeCast) String() string

func (*TypeCast) Visit

func (tc *TypeCast) Visit(cb CodeBuilder)

type Unary

type Unary struct {
	*Base
	Op token.Token
	X  Node
}

A node represent unary expression

func (*Unary) Evaluate

func (un *Unary) Evaluate(ctx Context) (interface{}, reflect.Kind, error)

func (*Unary) String

func (un *Unary) String() string

func (*Unary) Visit

func (un *Unary) Visit(cb CodeBuilder)

Jump to

Keyboard shortcuts

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