ast

package
v0.0.0-...-f022186 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package ast defines the node types used by the compiler to build syntax trees

Expr
	Ident
	Constant, Symbol, Function, Block
	Unary, Binary, Trinary, Nary, In
	Mem, RangeTo, RangeLen
	Call
Statement
	ExprStmt
	Compound
	Return, Throw, Break, Continue
	TryCatch
	If, Switch
	For, ForIn, Forever, While, DoWhile

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Blocks

func Blocks(f *Function)

Blocks sets compileAsFunction if a block can be compiled as a function i.e. isn't a closure, doesn't share any variables. NOTE: This is trickier than it seems. e.g. Have to handle nested blocks and sharing between peer blocks. Does not process nested functions (they're already codegen and not Ast); they are checked as constructed bottom up.

func CantBeEmpty

func CantBeEmpty(e Expr, cols []string) bool

func IsColumn

func IsColumn(e Expr, cols []string) bool

IsColumn handles _lower!

func NewChildren

func NewChildren(list []Value) children

Types

type Arg

type Arg struct {
	SuAstNode
	Name Value // nil if not named
	E    Expr
	TwoPos
}

func (*Arg) Echo

func (a *Arg) Echo() string

func (*Arg) Get

func (a *Arg) Get(_ *Thread, m Value) Value

func (*Arg) String

func (a *Arg) String() string

type Binary

type Binary struct {
	Lhs Expr
	Rhs Expr
	Tok tok.Token
	// contains filtered or unexported fields
}

func (*Binary) CanEvalRaw

func (a *Binary) CanEvalRaw(flds []string) bool

CanEvalRaw returns true if Eval doesn't need to unpack the values. It sets evalRaw and Packed which are used later by Eval.

func (*Binary) Children

func (a *Binary) Children(fn func(Node) Node)

func (*Binary) Columns

func (a *Binary) Columns() []string

func (*Binary) CouldEvalRaw

func (a *Binary) CouldEvalRaw() bool

CouldEvalRaw is used by replaceExpr to know when to copy

func (*Binary) Echo

func (a *Binary) Echo() string

func (*Binary) Eval

func (a *Binary) Eval(c *Context) Value

func (*Binary) Get

func (a *Binary) Get(_ *Thread, m Value) Value

func (Binary) SetPos

func (Binary) SetPos(org, end int32)

func (*Binary) String

func (a *Binary) String() string

type Block

type Block struct {
	Name string
	Function
	// CompileAsFunction is set and used by codegen
	CompileAsFunction bool
}

func (*Block) CanEvalRaw

func (en *Block) CanEvalRaw([]string) bool

func (*Block) Children

func (a *Block) Children(fn func(Node) Node)

func (*Block) Columns

func (a *Block) Columns() []string

func (*Block) Echo

func (en *Block) Echo() string

func (*Block) Eval

func (a *Block) Eval(*Context) Value

func (*Block) Get

func (a *Block) Get(th *Thread, m Value) Value

func (*Block) String

func (a *Block) String() string

type Break

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

func (*Break) Get

func (a *Break) Get(_ *Thread, m Value) Value

func (*Break) Position

func (stmt *Break) Position() int

func (*Break) String

func (*Break) String() string

type Builder

type Builder interface {
	Symbol(s SuStr) Expr
	Unary(tok Token, expr Expr) Expr
	Binary(lhs Expr, tok Token, rhs Expr) Expr
	Trinary(cond Expr, t Expr, f Expr) Expr
	Nary(tok Token, exprs []Expr) Expr
	In(e Expr, exprs []Expr) Expr
	Call(fn Expr, args []Arg, end int32) Expr
}

type Call

type Call struct {
	Fn   Expr
	Args []Arg
	End  int32
	// contains filtered or unexported fields
}

func (*Call) CanEvalRaw

func (a *Call) CanEvalRaw(flds []string) bool

func (*Call) Children

func (a *Call) Children(fn func(Node) Node)

func (*Call) Columns

func (a *Call) Columns() []string

func (*Call) Echo

func (a *Call) Echo() string

func (*Call) Eval

func (a *Call) Eval(c *Context) Value

func (*Call) Get

func (a *Call) Get(_ *Thread, m Value) Value

func (Call) SetPos

func (Call) SetPos(org, end int32)

func (*Call) String

func (a *Call) String() string

type Case

type Case struct {
	SuAstNode
	Exprs []Expr
	Body  []Statement
	TwoPos
}

func (*Case) Get

func (a *Case) Get(_ *Thread, m Value) Value

func (*Case) String

func (a *Case) String() string

type Compound

type Compound struct {
	Body []Statement
	// contains filtered or unexported fields
}

func (*Compound) Children

func (a *Compound) Children(fn func(Node) Node)

func (*Compound) Get

func (a *Compound) Get(_ *Thread, m Value) Value

func (*Compound) Position

func (stmt *Compound) Position() int

func (*Compound) String

func (a *Compound) String() string

type Constant

type Constant struct {
	Val Value
	// Packed is used for queries. It is set by Binary.CanEvalRaw
	Packed string
	TwoPos
	// contains filtered or unexported fields
}

func (*Constant) CanEvalRaw

func (en *Constant) CanEvalRaw([]string) bool

func (*Constant) Columns

func (a *Constant) Columns() []string

func (*Constant) Echo

func (a *Constant) Echo() string

func (*Constant) Eval

func (a *Constant) Eval(*Context) Value

func (*Constant) Get

func (a *Constant) Get(_ *Thread, m Value) Value

func (*Constant) SetPos

func (a *Constant) SetPos(org, end int32)

func (*Constant) String

func (a *Constant) String() string

type Context

type Context struct {
	Th   *Thread
	Tran *SuTran
	Hdr  *Header
	Row  Row
}

type Continue

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

func (*Continue) Get

func (a *Continue) Get(_ *Thread, m Value) Value

func (*Continue) Position

func (stmt *Continue) Position() int

func (*Continue) String

func (*Continue) String() string

type DoWhile

type DoWhile struct {
	Body Statement
	Cond Expr
	// contains filtered or unexported fields
}

func (*DoWhile) Children

func (a *DoWhile) Children(fn func(Node) Node)

func (*DoWhile) Get

func (a *DoWhile) Get(_ *Thread, m Value) Value

func (*DoWhile) Position

func (stmt *DoWhile) Position() int

func (*DoWhile) String

func (a *DoWhile) String() string

type Expr

type Expr interface {
	Node

	Echo() string
	// Eval, CanEvalRaw, and Columns are used by queries
	Eval(*Context) Value
	// CanEvalRaw returns true if Eval doesn't need to unpack the values.
	// and it sets the Expr so future Eval's will (or will not) be raw.
	// It must be called before calling Eval.
	// It is primarily for Where comparisons.
	CanEvalRaw(flds []string) bool
	Columns() []string
	// contains filtered or unexported methods
}

Expr is implemented by expression nodes

type ExprPos

type ExprPos struct {
	SuAstNode
	Expr
	Pos int32
	End int32
}

func (*ExprPos) Get

func (a *ExprPos) Get(t *Thread, m Value) Value

type ExprStmt

type ExprStmt struct {
	E Expr
	// contains filtered or unexported fields
}

func (*ExprStmt) Children

func (a *ExprStmt) Children(fn func(Node) Node)

func (*ExprStmt) Get

func (a *ExprStmt) Get(_ *Thread, m Value) Value

func (*ExprStmt) Position

func (stmt *ExprStmt) Position() int

func (*ExprStmt) String

func (a *ExprStmt) String() string

type Factory

type Factory struct{}

Factory is a simple pass through Builder

func (Factory) Binary

func (Factory) Binary(lhs Expr, tok Token, rhs Expr) Expr

func (Factory) Call

func (Factory) Call(fn Expr, args []Arg, end int32) Expr

func (Factory) In

func (Factory) In(e Expr, exprs []Expr) Expr

func (Factory) Nary

func (Factory) Nary(tok Token, exprs []Expr) Expr

func (Factory) Symbol

func (Factory) Symbol(s SuStr) Expr

func (Factory) Trinary

func (Factory) Trinary(cond Expr, t Expr, f Expr) Expr

func (Factory) Unary

func (Factory) Unary(tok Token, expr Expr) Expr

type Folder

type Folder struct{}

Folder implements constant folding for expressions. Doing the folding as the AST is built is implicitly bottom up without requiring an explicit tree traversal. It also means we only build the folded tree. Some methods are split so they can be used by propfold.

func (Folder) Binary

func (f Folder) Binary(lhs Expr, token tok.Token, rhs Expr) Expr

func (Folder) Call

func (Folder) Call(fn Expr, args []Arg, end int32) Expr

func (Folder) In

func (f Folder) In(e Expr, exprs []Expr) Expr

func (Folder) Nary

func (f Folder) Nary(token tok.Token, exprs []Expr) Expr

func (Folder) Symbol

func (Folder) Symbol(s SuStr) Expr

func (Folder) Trinary

func (f Folder) Trinary(cond Expr, e1 Expr, e2 Expr) Expr

func (Folder) Unary

func (f Folder) Unary(token tok.Token, expr Expr) Expr

type For

type For struct {
	Cond Expr
	Body Statement
	Init []Expr
	Inc  []Expr
	// contains filtered or unexported fields
}

func (*For) Children

func (a *For) Children(fn func(Node) Node)

func (*For) Get

func (a *For) Get(_ *Thread, m Value) Value

func (*For) Position

func (stmt *For) Position() int

func (*For) String

func (a *For) String() string

type ForIn

type ForIn struct {
	E    Expr
	E2   Expr // used by for-range
	Body Statement
	Var  Ident // optional with for-range
	// contains filtered or unexported fields
}

func (*ForIn) Children

func (a *ForIn) Children(fn func(Node) Node)

func (*ForIn) Get

func (a *ForIn) Get(_ *Thread, m Value) Value

func (*ForIn) Position

func (stmt *ForIn) Position() int

func (*ForIn) String

func (a *ForIn) String() string

type Forever

type Forever struct {
	Body Statement
	// contains filtered or unexported fields
}

func (*Forever) Children

func (a *Forever) Children(fn func(Node) Node)

func (*Forever) Get

func (a *Forever) Get(_ *Thread, m Value) Value

func (*Forever) Position

func (stmt *Forever) Position() int

func (*Forever) String

func (a *Forever) String() string

type Function

type Function struct {
	Final  map[string]uint8
	Params []Param
	Body   []Statement
	Base   Gnum
	TwoPos
	Pos1        int32
	Pos2        int32
	HasBlocks   bool
	IsNewMethod bool
	// contains filtered or unexported fields
}

func PropFold

func PropFold(fn *Function) *Function

PropFold traverses an AST and does constant propagation and folding, modifying the AST

func (*Function) CanEvalRaw

func (en *Function) CanEvalRaw([]string) bool

func (*Function) Children

func (a *Function) Children(fn func(Node) Node)

func (*Function) Echo

func (en *Function) Echo() string

func (*Function) Get

func (a *Function) Get(_ *Thread, m Value) Value

func (*Function) Position

func (a *Function) Position() int

func (*Function) String

func (a *Function) String() string

type Ident

type Ident struct {
	Name     string
	Pos      int32 // for check errors
	Implicit bool  // for implicit Record, Object, this
	// contains filtered or unexported fields
}

func (*Ident) CanEvalRaw

func (en *Ident) CanEvalRaw([]string) bool

func (*Ident) Columns

func (a *Ident) Columns() []string

func (*Ident) Echo

func (a *Ident) Echo() string

func (*Ident) Eval

func (a *Ident) Eval(c *Context) Value

func (*Ident) Get

func (a *Ident) Get(_ *Thread, m Value) Value

func (*Ident) GetEnd

func (a *Ident) GetEnd() int

func (*Ident) GetPos

func (a *Ident) GetPos() int

func (*Ident) ParamName

func (a *Ident) ParamName() string

func (*Ident) SetPos

func (a *Ident) SetPos(org, end int32)

func (*Ident) String

func (a *Ident) String() string

type If

type If struct {
	Cond Expr
	Then Statement
	Else Statement // may be nil

	ElseEnd int32
	// contains filtered or unexported fields
}

func (*If) Children

func (a *If) Children(fn func(Node) Node)

func (*If) Get

func (a *If) Get(_ *Thread, m Value) Value

func (*If) Position

func (stmt *If) Position() int

func (*If) String

func (a *If) String() string

type In

type In struct {
	E     Expr
	Exprs []Expr
	// Packed is set by CanEvalRaw
	Packed []string
	// contains filtered or unexported fields
}

func (*In) CanEvalRaw

func (a *In) CanEvalRaw(flds []string) bool

CanEvalRaw returns true if Eval doesn't need to unpack the values. It sets Packed which is later used by Eval.

func (*In) Children

func (a *In) Children(fn func(Node) Node)

func (*In) Columns

func (a *In) Columns() []string

func (*In) CouldEvalRaw

func (a *In) CouldEvalRaw() bool

CouldEvalRaw is used by replaceExpr to know when to copy

func (*In) Echo

func (a *In) Echo() string

func (*In) Eval

func (a *In) Eval(c *Context) Value

func (*In) Get

func (a *In) Get(_ *Thread, m Value) Value

func (In) SetPos

func (In) SetPos(org, end int32)

func (*In) String

func (a *In) String() string

type InRange

type InRange struct {
	E      Expr
	Org    Expr // *Constant
	End    Expr // *Constant
	OrgTok tok.Token
	EndTok tok.Token
	// contains filtered or unexported fields
}

InRange is added by Folder to bypass strict compare for same type ranges

func (*InRange) CanEvalRaw

func (a *InRange) CanEvalRaw(flds []string) bool

CanEvalRaw returns true if Eval doesn't need to unpack the values. It sets Packed which is later used by Eval.

func (*InRange) Children

func (a *InRange) Children(fn func(Node) Node)

func (*InRange) Columns

func (a *InRange) Columns() []string

func (*InRange) CouldEvalRaw

func (a *InRange) CouldEvalRaw() bool

CouldEvalRaw is used by replaceExpr to know when to copy

func (*InRange) Echo

func (a *InRange) Echo() string

func (*InRange) Eval

func (a *InRange) Eval(c *Context) Value

func (InRange) SetPos

func (InRange) SetPos(org, end int32)

func (*InRange) String

func (a *InRange) String() string

type Mem

type Mem struct {
	E      Expr
	M      Expr
	DotPos int32
	// contains filtered or unexported fields
}

func (*Mem) CanEvalRaw

func (en *Mem) CanEvalRaw([]string) bool

func (*Mem) Children

func (a *Mem) Children(fn func(Node) Node)

func (*Mem) Columns

func (a *Mem) Columns() []string

func (*Mem) Echo

func (a *Mem) Echo() string

func (*Mem) Eval

func (a *Mem) Eval(c *Context) Value

func (*Mem) Get

func (a *Mem) Get(_ *Thread, m Value) Value

func (Mem) SetPos

func (Mem) SetPos(org, end int32)

func (*Mem) String

func (a *Mem) String() string

type Nary

type Nary struct {
	Exprs []Expr
	Tok   tok.Token
	// contains filtered or unexported fields
}

Nary is used for associative binary operators e.g. add, multiply, and, or

func (*Nary) CanEvalRaw

func (a *Nary) CanEvalRaw(flds []string) bool

func (*Nary) Children

func (a *Nary) Children(fn func(Node) Node)

func (*Nary) Columns

func (a *Nary) Columns() []string

func (*Nary) Echo

func (a *Nary) Echo() string

func (*Nary) Eval

func (a *Nary) Eval(c *Context) Value

func (*Nary) Get

func (a *Nary) Get(_ *Thread, m Value) Value

func (Nary) SetPos

func (Nary) SetPos(org, end int32)

func (*Nary) String

func (a *Nary) String() string

type Node

type Node interface {
	String() string
	// Children calls the given function for each child node
	Children(func(Node) Node)
	// SetPos is called by the parser
	SetPos(org, end int32)
	// Get is for the Value interface for Suneido.Parse
	Get(*Thread, Value) Value
	// contains filtered or unexported methods
}

Node is embedded by Expr and Statement

type Param

type Param struct {
	SuAstNode
	DefVal Value // may be nil
	Name   Ident // including prefix @ . _
	End    int32
	// Unused is set if the parameter was followed by /*unused*/
	Unused bool
}

func (*Param) Get

func (a *Param) Get(_ *Thread, m Value) Value

func (*Param) GetEnd

func (a *Param) GetEnd() int

func (*Param) GetPos

func (a *Param) GetPos() int

func (*Param) String

func (a *Param) String() string

type Params

type Params struct {
	SuAstNode
	Params []Param
}

func (Params) Get

func (a Params) Get(_ *Thread, m Value) Value

func (Params) String

func (a Params) String() string

type RangeLen

type RangeLen struct {
	E    Expr
	From Expr
	Len  Expr
	// contains filtered or unexported fields
}

func (*RangeLen) CanEvalRaw

func (en *RangeLen) CanEvalRaw([]string) bool

func (*RangeLen) Children

func (a *RangeLen) Children(fn func(Node) Node)

func (*RangeLen) Columns

func (a *RangeLen) Columns() []string

func (*RangeLen) Echo

func (a *RangeLen) Echo() string

func (*RangeLen) Eval

func (a *RangeLen) Eval(c *Context) Value

func (*RangeLen) Get

func (a *RangeLen) Get(_ *Thread, m Value) Value

func (RangeLen) SetPos

func (RangeLen) SetPos(org, end int32)

func (*RangeLen) String

func (a *RangeLen) String() string

type RangeTo

type RangeTo struct {
	E    Expr
	From Expr
	To   Expr
	// contains filtered or unexported fields
}

func (*RangeTo) CanEvalRaw

func (en *RangeTo) CanEvalRaw([]string) bool

func (*RangeTo) Children

func (a *RangeTo) Children(fn func(Node) Node)

func (*RangeTo) Columns

func (a *RangeTo) Columns() []string

func (*RangeTo) Echo

func (a *RangeTo) Echo() string

func (*RangeTo) Eval

func (a *RangeTo) Eval(c *Context) Value

func (*RangeTo) Get

func (a *RangeTo) Get(_ *Thread, m Value) Value

func (RangeTo) SetPos

func (RangeTo) SetPos(org, end int32)

func (*RangeTo) String

func (a *RangeTo) String() string

type Return

type Return struct {
	E Expr

	ReturnThrow bool
	// contains filtered or unexported fields
}

func (*Return) Children

func (a *Return) Children(fn func(Node) Node)

func (*Return) Get

func (a *Return) Get(_ *Thread, m Value) Value

func (*Return) Position

func (stmt *Return) Position() int

func (*Return) String

func (a *Return) String() string

type SetPosAble

type SetPosAble interface {
	SetPos(org, end int32)
}

type Statement

type Statement interface {
	Node
	Position() int
	GetPos() int
	GetEnd() int
	// contains filtered or unexported methods
}

type SuAstNode

type SuAstNode struct {
	ValueBase[SuAstNode]
}

func (SuAstNode) Equal

func (SuAstNode) Equal(any) bool

func (SuAstNode) SetConcurrent

func (SuAstNode) SetConcurrent()

func (SuAstNode) Type

func (SuAstNode) Type() types.Type

type Switch

type Switch struct {
	E       Expr
	Cases   []Case
	Default []Statement // may be nil

	Pos1   int32
	Pos2   int32
	PosDef int32
	// contains filtered or unexported fields
}

func (*Switch) Children

func (a *Switch) Children(fn func(Node) Node)

func (*Switch) Get

func (a *Switch) Get(_ *Thread, m Value) Value

func (*Switch) Position

func (stmt *Switch) Position() int

func (*Switch) String

func (a *Switch) String() string

type Symbol

type Symbol struct {
	Constant
}

func (*Symbol) CanEvalRaw

func (en *Symbol) CanEvalRaw([]string) bool

func (*Symbol) Get

func (a *Symbol) Get(th *Thread, m Value) Value

type Throw

type Throw struct {
	E Expr
	// contains filtered or unexported fields
}

func (*Throw) Children

func (a *Throw) Children(fn func(Node) Node)

func (*Throw) Get

func (a *Throw) Get(_ *Thread, m Value) Value

func (*Throw) Position

func (stmt *Throw) Position() int

func (*Throw) String

func (a *Throw) String() string

type Token

type Token = tokens.Token

type Trinary

type Trinary struct {
	Cond Expr
	T    Expr
	F    Expr
	// contains filtered or unexported fields
}

func (*Trinary) CanEvalRaw

func (en *Trinary) CanEvalRaw([]string) bool

func (*Trinary) Children

func (a *Trinary) Children(fn func(Node) Node)

func (*Trinary) Columns

func (a *Trinary) Columns() []string

func (*Trinary) Echo

func (a *Trinary) Echo() string

func (*Trinary) Eval

func (a *Trinary) Eval(c *Context) Value

func (*Trinary) Get

func (a *Trinary) Get(_ *Thread, m Value) Value

func (Trinary) SetPos

func (Trinary) SetPos(org, end int32)

func (*Trinary) String

func (a *Trinary) String() string

type TryCatch

type TryCatch struct {
	Try         Statement
	Catch       Statement
	CatchFilter string
	CatchVar    Ident

	CatchPos       int32
	CatchEnd       int32
	CatchVarUnused bool
	// contains filtered or unexported fields
}

func (*TryCatch) Children

func (a *TryCatch) Children(fn func(Node) Node)

func (*TryCatch) Get

func (a *TryCatch) Get(_ *Thread, m Value) Value

func (*TryCatch) Position

func (stmt *TryCatch) Position() int

func (*TryCatch) String

func (a *TryCatch) String() string

type TwoPos

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

func (*TwoPos) GetEnd

func (a *TwoPos) GetEnd() int

func (*TwoPos) GetPos

func (a *TwoPos) GetPos() int

func (*TwoPos) SetPos

func (a *TwoPos) SetPos(org, end int32)

type Unary

type Unary struct {
	E Expr
	TwoPos
	Tok tok.Token
	// contains filtered or unexported fields
}

func (*Unary) CanEvalRaw

func (en *Unary) CanEvalRaw([]string) bool

func (*Unary) Children

func (a *Unary) Children(fn func(Node) Node)

func (*Unary) Columns

func (a *Unary) Columns() []string

func (*Unary) Echo

func (a *Unary) Echo() string

func (*Unary) Eval

func (a *Unary) Eval(c *Context) Value

func (*Unary) Get

func (a *Unary) Get(_ *Thread, m Value) Value

func (*Unary) String

func (a *Unary) String() string

type While

type While struct {
	Cond Expr
	Body Statement
	// contains filtered or unexported fields
}

func (*While) Children

func (a *While) Children(fn func(Node) Node)

func (*While) Get

func (a *While) Get(_ *Thread, m Value) Value

func (*While) Position

func (stmt *While) Position() int

func (*While) String

func (a *While) String() string

Jump to

Keyboard shortcuts

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