sexp

package
v0.0.0-...-9dcd13a Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2017 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package sexp provides a high level intermediate representation that contains both Go and Emacs Lisp traits.

Index

Constants

This section is empty.

Variables

View Source
var (
	EmptyForm    = &emptyForm{}
	EmptyBlock   = Block(nil)
	ContinueGoto = &Goto{LabelName: "continue"}
	BreakGoto    = &Goto{LabelName: "break"}

	Nil Form = Symbol{Val: "nil"}
)

Functions

func CostOf

func CostOf(forms ...Form) int

CostOf is a convenience wrapper over CostOfList.

func CostOfList

func CostOfList(forms []Form) int

CostOfList calls each form Cost method and returns accumulated cost sum.

func IsEmptyForm

func IsEmptyForm(form Form) bool

func IsStmt

func IsStmt(form Form) bool

IsStmt returns true for statement form.

func Walk

func Walk(form Form, fn walkFunc)

Walk provides convenient way to traverse AST.

Types

type And

type And struct {
	X Form
	Y Form
}

And = "X && Y".

func (*And) Copy

func (form *And) Copy() Form

func (*And) Cost

func (form *And) Cost() int

func (*And) Type

func (form *And) Type() types.Type

type ArrayIndex

type ArrayIndex struct {
	Array Form
	Index Form
}

ArrayIndex is array index expression.

func (*ArrayIndex) Copy

func (form *ArrayIndex) Copy() Form

func (*ArrayIndex) Cost

func (form *ArrayIndex) Cost() int

func (*ArrayIndex) Type

func (form *ArrayIndex) Type() types.Type

type ArrayLit

type ArrayLit struct {
	Vals []Form
	Typ  *types.Array
}

ArrayLit = "[N]T{Vals...}".

func (*ArrayLit) Copy

func (lit *ArrayLit) Copy() Form

func (*ArrayLit) Cost

func (lit *ArrayLit) Cost() int

func (*ArrayLit) Type

func (lit *ArrayLit) Type() types.Type

type ArraySlice

type ArraySlice struct {
	Array Form
	Typ   *types.Slice
	Span
}

ArraySlice is array slicing (subslice) expression.

func NewArraySlice

func NewArraySlice(array, low, high Form) *ArraySlice

func (*ArraySlice) Copy

func (form *ArraySlice) Copy() Form

func (*ArraySlice) Cost

func (form *ArraySlice) Cost() int

func (*ArraySlice) Type

func (form *ArraySlice) Type() types.Type

type ArrayUpdate

type ArrayUpdate struct {
	Array Form
	Index Form
	Expr  Form
}

ArrayUpdate is array index expression with assignment.

func (*ArrayUpdate) Copy

func (form *ArrayUpdate) Copy() Form

func (*ArrayUpdate) Cost

func (form *ArrayUpdate) Cost() int

func (*ArrayUpdate) Type

func (form *ArrayUpdate) Type() types.Type

type Bind

type Bind struct {
	Name string
	Init Form
}

Bind associates name with expression (initializer). Introduces local variable.

func (*Bind) Copy

func (form *Bind) Copy() Form

func (*Bind) Cost

func (form *Bind) Cost() int

func (*Bind) Type

func (form *Bind) Type() types.Type

type Block

type Block []Form

Block is a list of statements. Unlike FormList, it creates a new lexical scope.

func (Block) Copy

func (form Block) Copy() Form

func (Block) Cost

func (form Block) Cost() int

func (Block) Type

func (form Block) Type() types.Type

type Bool

type Bool bool

Bool = true or false literal.

func (Bool) Copy

func (atom Bool) Copy() Form

func (Bool) Cost

func (atom Bool) Cost() int

func (Bool) Type

func (atom Bool) Type() types.Type

type Call

type Call struct {
	Fn   *Func
	Args []Form
}

Call expression is normal (direct) function invocation.

func NewCall

func NewCall(fn *Func, args ...Form) *Call

func (*Call) Copy

func (call *Call) Copy() Form

func (*Call) Cost

func (call *Call) Cost() int

func (*Call) Type

func (call *Call) Type() types.Type

type CaseClause

type CaseClause struct {
	Expr Form
	Body Block
}

CaseClause is a part of SwitchBody.

type DoTimes

type DoTimes struct {
	N    Form
	Iter Local
	Step Form
	Body Block
}

DoTimes is like Repeat, but: - N is not necessary a constant. - Has inductive variable inside loop body (Iter).

func (*DoTimes) Copy

func (form *DoTimes) Copy() Form

func (*DoTimes) Cost

func (form *DoTimes) Cost() int

func (*DoTimes) Type

func (form *DoTimes) Type() types.Type

type DynCall

type DynCall struct {
	Callable Form
	Args     []Form
	Typ      types.Type
}

DynCall is indirect function invocation.

func (*DynCall) Copy

func (call *DynCall) Copy() Form

func (*DynCall) Cost

func (call *DynCall) Cost() int

func (*DynCall) Type

func (call *DynCall) Type() types.Type

type ExprStmt

type ExprStmt struct{ Expr Form }

ExprStmt is a Call which discards returned results.

func (*ExprStmt) Copy

func (form *ExprStmt) Copy() Form

func (*ExprStmt) Cost

func (form *ExprStmt) Cost() int

func (*ExprStmt) Type

func (form *ExprStmt) Type() types.Type

type Float

type Float float64

Float = floating point literal (of any supported format).

func (Float) Copy

func (atom Float) Copy() Form

func (Float) Cost

func (atom Float) Cost() int

func (Float) Type

func (atom Float) Type() types.Type

type Form

type Form interface {
	// Type evaluates Form result type.
	// Statement-like forms return "types.Typ[types.Invalid]".
	Type() types.Type

	// Copy creates a deep copy of Form object.
	Copy() Form

	// Cost returns a value that approximates computational complexity.
	//
	// If it is impossible to predict complexity, -1 is returned.
	// Examples of failing forms are loops with dynamic conditions.
	Cost() int
}

Form = universal S-expression node (akin to Go ast.Node).

func CopyList

func CopyList(forms []Form) []Form

func Rewrite

func Rewrite(form Form, fn rewriteFunc) Form

Rewrite provides convenient way to update AST.

type FormList

type FormList []Form

FormList packs multiple forms together (like "progn").

func (FormList) Copy

func (form FormList) Copy() Form

func (FormList) Cost

func (form FormList) Cost() int

func (FormList) Type

func (form FormList) Type() types.Type

type Func

type Func struct {
	Name     string
	Body     Block
	Params   []string
	Variadic bool

	// Maps param index to interface type.
	// Nil if function have no interface parameters at all.
	InterfaceInputs map[int]types.Type

	Results *types.Tuple

	DocString string
	// contains filtered or unexported fields
}

Func represents goism function object.

func (*Func) IsInlineable

func (fn *Func) IsInlineable() bool

IsInlineable tells if function can be inlined (not the same as *should* be inlined).

func (*Func) IsNoinline

func (fn *Func) IsNoinline() bool

IsNoinline returns true for functions that should not be inlined. Ever.

func (*Func) IsSubst

func (fn *Func) IsSubst() bool

IsSubst tells if function is "always inline".

func (*Func) LoadDirective

func (fn *Func) LoadDirective(directive string)

LoadDirective parses function comment directive and updates function info correspondingly.

func (*Func) SetInlineable

func (fn *Func) SetInlineable(inlineable bool)

SetInlineable sets function inlineable flag to true or false.

type Goto

type Goto struct{ LabelName string }

Goto = "goto LabelName".

func (*Goto) Copy

func (form *Goto) Copy() Form

func (*Goto) Cost

func (form *Goto) Cost() int

func (*Goto) Type

func (form *Goto) Type() types.Type

type If

type If struct {
	Cond Form
	Then Block
	Else Form // Can be EmptyForm
}

If statement evaluates test expression and, depending on the result, one of the branches gets executed. Else branch is optional.

func (*If) Copy

func (form *If) Copy() Form

func (*If) Cost

func (form *If) Cost() int

func (*If) Type

func (form *If) Type() types.Type

type Int

type Int int64

Int = rune constant or integer literal.

func (Int) Copy

func (atom Int) Copy() Form

func (Int) Cost

func (atom Int) Cost() int

func (Int) Type

func (atom Int) Type() types.Type

type Label

type Label struct{ Name string }

Label = "Name:".

func (*Label) Copy

func (form *Label) Copy() Form

func (*Label) Cost

func (form *Label) Cost() int

func (*Label) Type

func (form *Label) Type() types.Type

type LambdaCall

type LambdaCall struct {
	Args []*Bind
	Body Block
	Typ  *types.Tuple
}

LambdaCall is IIFE (Immediately Invoked Function Expression). Used for immediately invoked functions "func() {...}()" and inlining.

func (*LambdaCall) Copy

func (call *LambdaCall) Copy() Form

func (*LambdaCall) Cost

func (call *LambdaCall) Cost() int

func (*LambdaCall) Type

func (call *LambdaCall) Type() types.Type

type Let

type Let struct {
	Bindings []*Bind

	// Either of these two is set.
	// Let wraps expression OR statement.
	Expr Form
	Stmt Form
}

Let introduces bindings that are visible to a statement or expression. Bindings are destroyed after wrapped form is evaluated.

func (*Let) Copy

func (form *Let) Copy() Form

func (*Let) Cost

func (form *Let) Cost() int

func (*Let) Type

func (form *Let) Type() types.Type

type LispCall

type LispCall struct {
	Fn   *lisp.Func
	Args []Form
}

LispCall is extern function call. Lisp call is never inlined.

func NewAdd

func NewAdd(x, y Form) *LispCall

func NewAdd1

func NewAdd1(x Form) *LispCall

func NewBitAnd

func NewBitAnd(x, y Form) *LispCall

func NewBitOr

func NewBitOr(x, y Form) *LispCall

func NewBitXor

func NewBitXor(x, y Form) *LispCall

func NewConcat

func NewConcat(x, y Form) *LispCall

func NewLispCall

func NewLispCall(fn *lisp.Func, args ...Form) *LispCall

func NewMul

func NewMul(x, y Form) *LispCall

func NewNeg

func NewNeg(x Form) *LispCall

func NewNot

func NewNot(x Form) *LispCall

func NewNumEq

func NewNumEq(x, y Form) *LispCall

func NewNumGt

func NewNumGt(x, y Form) *LispCall

func NewNumGte

func NewNumGte(x, y Form) *LispCall

func NewNumLt

func NewNumLt(x, y Form) *LispCall

func NewNumLte

func NewNumLte(x, y Form) *LispCall

func NewNumNeq

func NewNumNeq(x, y Form) *LispCall

func NewQuo

func NewQuo(x, y Form) *LispCall

func NewShl

func NewShl(x, y Form) *LispCall

func NewShr

func NewShr(x, y Form) *LispCall

func NewStrEq

func NewStrEq(x, y Form) *LispCall

func NewStrGt

func NewStrGt(x, y Form) *LispCall

func NewStrLt

func NewStrLt(x, y Form) *LispCall

func NewStrNeq

func NewStrNeq(x, y Form) *LispCall

func NewSub

func NewSub(x, y Form) *LispCall

func NewSub1

func NewSub1(x Form) *LispCall

func NewSubstr

func NewSubstr(array, low, high Form) *LispCall

func (*LispCall) Copy

func (call *LispCall) Copy() Form

func (*LispCall) Cost

func (call *LispCall) Cost() int

func (*LispCall) Type

func (call *LispCall) Type() types.Type

type Local

type Local struct {
	Name string
	Typ  types.Type
}

Local - reference to a local variable. Shadowing is possible with lexical variables, so Name alone is not enough to distinguish identity.

func (Local) Copy

func (atom Local) Copy() Form

func (Local) Cost

func (atom Local) Cost() int

func (Local) Type

func (atom Local) Type() types.Type

type Loop

type Loop struct {
	Init Form // Can be EmptyForm
	Post Form // Can be EmptyForm
	Body Block
}

Loop = "while true".

func (*Loop) Copy

func (form *Loop) Copy() Form

func (*Loop) Cost

func (form *Loop) Cost() int

func (*Loop) Type

func (form *Loop) Type() types.Type

type Or

type Or struct {
	X Form
	Y Form
}

Or = "X || Y".

func (*Or) Copy

func (form *Or) Copy() Form

func (*Or) Cost

func (form *Or) Cost() int

func (*Or) Type

func (form *Or) Type() types.Type

type Rebind

type Rebind struct {
	Name string
	Expr Form
}

Rebind changes local symbol value.

func (*Rebind) Copy

func (form *Rebind) Copy() Form

func (*Rebind) Cost

func (form *Rebind) Cost() int

func (*Rebind) Type

func (form *Rebind) Type() types.Type

type Repeat

type Repeat struct {
	N    int64
	Body Block
}

Repeat is the simplest loop, it executes body N times.

Note that it is always unrolled. If unrolling is not optimal, optimizer should replace it with While.

func (*Repeat) Copy

func (form *Repeat) Copy() Form

func (*Repeat) Cost

func (form *Repeat) Cost() int

func (*Repeat) Type

func (form *Repeat) Type() types.Type

type Return

type Return struct{ Results []Form }

Return statement exits the function and returns one or more values to the caller.

func (*Return) Copy

func (form *Return) Copy() Form

func (*Return) Cost

func (form *Return) Cost() int

func (*Return) Type

func (form *Return) Type() types.Type

type SliceIndex

type SliceIndex struct {
	Slice Form
	Index Form
}

SliceIndex is slice index expression.

func (*SliceIndex) Copy

func (form *SliceIndex) Copy() Form

func (*SliceIndex) Cost

func (form *SliceIndex) Cost() int

func (*SliceIndex) Type

func (form *SliceIndex) Type() types.Type

type SliceLit

type SliceLit struct {
	Vals []Form
	Typ  *types.Slice
}

SliceLit = "[]T{Vals...}".

func (*SliceLit) Copy

func (lit *SliceLit) Copy() Form

func (*SliceLit) Cost

func (lit *SliceLit) Cost() int

func (*SliceLit) Type

func (lit *SliceLit) Type() types.Type

type SliceSlice

type SliceSlice struct {
	Slice Form
	Span
}

SliceSlice = "Slice[Low:High]".

func NewSubslice

func NewSubslice(slice, low, high Form) *SliceSlice

func (*SliceSlice) Copy

func (form *SliceSlice) Copy() Form

func (*SliceSlice) Cost

func (form *SliceSlice) Cost() int

func (*SliceSlice) Type

func (form *SliceSlice) Type() types.Type

type SliceUpdate

type SliceUpdate struct {
	Slice Form
	Index Form
	Expr  Form
}

SliceUpdate is slice index expression with assignment.

func (*SliceUpdate) Copy

func (form *SliceUpdate) Copy() Form

func (*SliceUpdate) Cost

func (form *SliceUpdate) Cost() int

func (*SliceUpdate) Type

func (form *SliceUpdate) Type() types.Type

type Span

type Span struct {
	Low  Form // [!] Can be nil
	High Form // [!] Can be nil
}

func (*Span) Kind

func (span *Span) Kind() SpanKind

type SpanKind

type SpanKind int
const (
	SpanLowOnly SpanKind = iota
	SpanHighOnly
	SpanBoth
	SpanWhole
)

type SparseArrayLit

type SparseArrayLit struct {
	Ctor    Form
	Vals    []Form
	Indexes []int
	Typ     *types.Array
}

SparseArrayLit is like ArrayLit, but does not store zero values.

func (*SparseArrayLit) Copy

func (lit *SparseArrayLit) Copy() Form

func (*SparseArrayLit) Cost

func (lit *SparseArrayLit) Cost() int

func (*SparseArrayLit) Type

func (lit *SparseArrayLit) Type() types.Type

type Str

type Str string

Str = raw/normal string literal.

func (Str) Copy

func (atom Str) Copy() Form

func (Str) Cost

func (atom Str) Cost() int

func (Str) Type

func (atom Str) Type() types.Type

type StructIndex

type StructIndex struct {
	Struct Form
	Index  int
	Typ    *types.Struct
}

StructIndex is struct selector expression.

func (*StructIndex) Copy

func (form *StructIndex) Copy() Form

func (*StructIndex) Cost

func (form *StructIndex) Cost() int

func (*StructIndex) Type

func (form *StructIndex) Type() types.Type

type StructLit

type StructLit struct {
	Vals []Form
	Typ  *types.Named
}

StructLit is an expression that yields struct object. Each struct member (field) has explicit initializer.

func (*StructLit) Copy

func (lit *StructLit) Copy() Form

func (*StructLit) Cost

func (lit *StructLit) Cost() int

func (*StructLit) Type

func (form *StructLit) Type() types.Type

type StructUpdate

type StructUpdate struct {
	Struct Form
	Index  int
	Expr   Form
	Typ    *types.Struct
}

StructUpdate = "Struct.[Index] = Expr".

func (*StructUpdate) Copy

func (form *StructUpdate) Copy() Form

func (*StructUpdate) Cost

func (form *StructUpdate) Cost() int

func (*StructUpdate) Type

func (form *StructUpdate) Type() types.Type

type Switch

type Switch struct {
	Expr Form
	SwitchBody
}

Switch is "expression switch statement" defined by Go spec.

func (*Switch) Copy

func (form *Switch) Copy() Form

func (*Switch) Cost

func (form *Switch) Cost() int

func (*Switch) Type

func (form *Switch) Type() types.Type

type SwitchBody

type SwitchBody struct {
	Clauses     []CaseClause
	DefaultBody Block // Can be EmptyBlock (no default clause)
}

SwitchBody represents switch statement case sequence with optional default clause.

type SwitchTrue

type SwitchTrue struct{ SwitchBody }

SwitchTrue is like Switch, but Expr is fixed to "true".

func (*SwitchTrue) Copy

func (form *SwitchTrue) Copy() Form

func (*SwitchTrue) Cost

func (form *SwitchTrue) Cost() int

func (*SwitchTrue) Type

func (form *SwitchTrue) Type() types.Type

type Symbol

type Symbol struct{ Val string }

Symbol = lisp.Symbol literal.

func (Symbol) Copy

func (atom Symbol) Copy() Form

func (Symbol) Cost

func (atom Symbol) Cost() int

func (Symbol) Type

func (atom Symbol) Type() types.Type

type TypeAssert

type TypeAssert struct {
	Expr Form
	Typ  types.Type
}

TypeAssert coerces expression to specified type; panics on failure.

func (*TypeAssert) Copy

func (form *TypeAssert) Copy() Form

func (*TypeAssert) Cost

func (form *TypeAssert) Cost() int

func (*TypeAssert) Type

func (form *TypeAssert) Type() types.Type

type TypeCast

type TypeCast struct {
	Form Form
	Typ  types.Type
}

TypeCast overrides wrapped form type.

func (*TypeCast) Copy

func (form *TypeCast) Copy() Form

func (*TypeCast) Cost

func (form *TypeCast) Cost() int

func (*TypeCast) Type

func (form *TypeCast) Type() types.Type

type Var

type Var struct {
	Name string
	Typ  types.Type
}

Var - reference to a global variable.

func (Var) Copy

func (atom Var) Copy() Form

func (Var) Cost

func (atom Var) Cost() int

func (Var) Type

func (atom Var) Type() types.Type

type VarUpdate

type VarUpdate struct {
	Name string
	Expr Form
}

VarUpdate changes global variable value.

func (*VarUpdate) Copy

func (form *VarUpdate) Copy() Form

func (*VarUpdate) Cost

func (form *VarUpdate) Cost() int

func (*VarUpdate) Type

func (form *VarUpdate) Type() types.Type

type While

type While struct {
	Init Form // Can be EmptyForm
	Cond Form
	Post Form // Can be EmptyForm
	Body Block
}

While is a generic (low level) looping construct.

func (*While) Copy

func (form *While) Copy() Form

func (*While) Cost

func (form *While) Cost() int

func (*While) Type

func (form *While) Type() types.Type

Jump to

Keyboard shortcuts

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