ast

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ast declares the syntax tree of Kukel programs (syntax.ebnf, sections 1-3).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprint

func Fprint(w io.Writer, n Node, withPos bool) error

Fprint writes n as an indented tree, one node per line. With withPos each line ends with the node's position.

func FprintFile

func FprintFile(w io.Writer, f *File, withPos bool) error

FprintFile writes a whole file as a tree (see Fprint).

func Inspect

func Inspect(n Node, f func(Node) bool)

Inspect walks the tree rooted at n in depth-first order. It calls f for each node; when f returns false, the node's children are skipped.

Types

type AssignExpr

type AssignExpr struct {
	Op  token.Kind
	LHS Expr
	RHS Expr
}

AssignExpr is "lhs op rhs" with op one of = += -= *= /= %= &= |= ^= <<= >>=.

func (*AssignExpr) Pos

func (x *AssignExpr) Pos() token.Pos

type BinaryExpr

type BinaryExpr struct {
	Op    token.Kind
	OpPos token.Pos
	X     Expr
	Y     Expr
}

BinaryExpr is "x op y".

func (*BinaryExpr) Pos

func (x *BinaryExpr) Pos() token.Pos

type Block

type Block struct {
	Lbrace token.Pos
	Stmts  []Stmt
	Rbrace token.Pos
}

Block is "{ stmts }".

func (*Block) Pos

func (s *Block) Pos() token.Pos

type BoolLit

type BoolLit struct {
	ValuePos token.Pos
	Value    bool
}

BoolLit is "true" or "false".

func (*BoolLit) Pos

func (x *BoolLit) Pos() token.Pos

type BreakStmt

type BreakStmt struct {
	Break token.Pos
	Label *Ident // nil without a label
}

BreakStmt is "break [label];".

func (*BreakStmt) Pos

func (s *BreakStmt) Pos() token.Pos

type CallExpr

type CallExpr struct {
	Fun    Expr
	Lparen token.Pos
	Args   []Expr // may contain *Spread
}

CallExpr is "fun(args)".

func (*CallExpr) Pos

func (x *CallExpr) Pos() token.Pos

type CaseLabel

type CaseLabel struct {
	Case  token.Pos
	Value Expr
}

CaseLabel is "case value:".

func (*CaseLabel) Pos

func (s *CaseLabel) Pos() token.Pos

type CharLit

type CharLit struct {
	ValuePos token.Pos
	Text     string
	Value    int64
}

CharLit is a character literal; Value is its code (negative when folded with "-").

func (*CharLit) Pos

func (x *CharLit) Pos() token.Pos

type ClassDecl

type ClassDecl struct {
	Class      token.Pos
	Name       *Ident
	BaseModule *Ident // the module of the base class, or nil
	Base       *Ident // nil without a base class
	Members    []Member
}

ClassDecl is "class Name [: [module::]Base] { members }".

func (*ClassDecl) Pos

func (d *ClassDecl) Pos() token.Pos

type CommaExpr

type CommaExpr struct {
	List []Expr
}

CommaExpr is "a, b, c": evaluated left to right, value of the last. It occurs only as the first and last part of a for loop.

func (*CommaExpr) Pos

func (x *CommaExpr) Pos() token.Pos

type CondExpr

type CondExpr struct {
	Cond Expr
	Then Expr
	Else Expr
}

CondExpr is "cond ? then : else".

func (*CondExpr) Pos

func (x *CondExpr) Pos() token.Pos

type ConstDecl

type ConstDecl struct {
	Const token.Pos
	Vars  []*VarSpec
}

ConstDecl is "const A = 1, B = A + 1;". Every Value is set.

func (*ConstDecl) Pos

func (d *ConstDecl) Pos() token.Pos

type ContinueStmt

type ContinueStmt struct {
	Continue token.Pos
	Label    *Ident
}

ContinueStmt is "continue [label];".

func (*ContinueStmt) Pos

func (s *ContinueStmt) Pos() token.Pos

type Decl

type Decl interface {
	Node
	// contains filtered or unexported methods
}

Decl is a top-level definition.

type DefaultLabel

type DefaultLabel struct {
	Default token.Pos
}

DefaultLabel is "default:".

func (*DefaultLabel) Pos

func (s *DefaultLabel) Pos() token.Pos

type DoStmt

type DoStmt struct {
	Do   token.Pos
	Body Stmt
	Cond Expr
}

DoStmt is "do body while (cond);".

func (*DoStmt) Pos

func (s *DoStmt) Pos() token.Pos

type EmptyStmt

type EmptyStmt struct {
	Semi token.Pos
}

EmptyStmt is ";".

func (*EmptyStmt) Pos

func (s *EmptyStmt) Pos() token.Pos

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

Expr is an expression.

type ExprStmt

type ExprStmt struct {
	X Expr
}

ExprStmt is "expr;".

func (*ExprStmt) Pos

func (s *ExprStmt) Pos() token.Pos

type FieldDecl

type FieldDecl struct {
	Start  token.Pos
	Static bool
	Names  []*Ident
}

FieldDecl declares (static) data members: "[static] a, b;".

func (*FieldDecl) Pos

func (m *FieldDecl) Pos() token.Pos

type File

type File struct {
	Name   string
	Module string // the module the file is, "" for the program's own files (set by the loader)
	Decls  []Decl // the use declarations come first

	// Compiled is set for a module loaded from a .kkc file (kukel build
	// -module): the file holds the module's header, the compiler links
	// this code instead of compiling function bodies.
	Compiled *bytecode.Module
}

File is one parsed source file.

type FloatLit

type FloatLit struct {
	ValuePos token.Pos
	Text     string
	Value    float64
}

FloatLit is a floating-point literal (possibly folded with "-").

func (*FloatLit) Pos

func (x *FloatLit) Pos() token.Pos

type ForEachStmt

type ForEachStmt struct {
	For   token.Pos
	Key   *Ident // the only name with one name
	Value *Ident // nil with one name
	X     Expr
	Body  Stmt
}

ForEachStmt is "for (local key[, value] in x) body".

func (*ForEachStmt) Pos

func (s *ForEachStmt) Pos() token.Pos

type ForStmt

type ForStmt struct {
	For      token.Pos
	InitVars []*VarSpec // "for (local i = 0, j; ...)"
	Init     Expr
	Cond     Expr // nil: loop until break
	Post     Expr
	Body     Stmt
}

ForStmt is "for (init; cond; post) body". The initializer is either a local list (InitVars) or an expression (Init); both may be absent.

func (*ForStmt) Pos

func (s *ForStmt) Pos() token.Pos

type FuncDecl

type FuncDecl struct {
	Class  *Ident // nil for a plain function
	Name   *Ident
	Params *ParamList
	Body   *Block
}

FuncDecl is a function definition "name(params) { ... }" or, with Class set, a member function definition "Class::name(params) { ... }".

func (*FuncDecl) Pos

func (d *FuncDecl) Pos() token.Pos

type FuncLit

type FuncLit struct {
	Fn     token.Pos
	Params *ParamList
	Body   *Block
}

FuncLit is "fn (params) { ... }".

func (*FuncLit) Pos

func (x *FuncLit) Pos() token.Pos

type GlobalDecl

type GlobalDecl struct {
	Global token.Pos
	Vars   []*VarSpec
}

GlobalDecl is "global a, b = 1;".

func (*GlobalDecl) Pos

func (d *GlobalDecl) Pos() token.Pos

type Ident

type Ident struct {
	NamePos token.Pos
	Name    string
}

Ident is a name.

func (*Ident) Pos

func (x *Ident) Pos() token.Pos

type IfStmt

type IfStmt struct {
	If   token.Pos
	Cond Expr
	Then Stmt
	Else Stmt // nil without else
}

IfStmt is "if (cond) then [else else]". In "else if (c) s", Else is another *IfStmt.

func (*IfStmt) Pos

func (s *IfStmt) Pos() token.Pos

type IncDecExpr

type IncDecExpr struct {
	Op     token.Kind // token.INC or token.DEC
	OpPos  token.Pos
	Prefix bool
	X      Expr
}

IncDecExpr is "++x", "--x", "x++" or "x--".

func (*IncDecExpr) Pos

func (x *IncDecExpr) Pos() token.Pos

type IndexExpr

type IndexExpr struct {
	X      Expr
	Lbrack token.Pos
	Index  Expr
}

IndexExpr is "x[index]".

func (*IndexExpr) Pos

func (x *IndexExpr) Pos() token.Pos

type IntLit

type IntLit struct {
	ValuePos token.Pos
	Text     string
	Value    int64
}

IntLit is an integer literal. A "-" directly before a literal is folded into it, so Value may be negative; Text is then "-" plus the literal.

func (*IntLit) Pos

func (x *IntLit) Pos() token.Pos

type LabeledStmt

type LabeledStmt struct {
	Label *Ident
	Stmt  Stmt
}

LabeledStmt is "label: stmt".

func (*LabeledStmt) Pos

func (s *LabeledStmt) Pos() token.Pos

type LocalDecl

type LocalDecl struct {
	Local token.Pos
	Vars  []*VarSpec
}

LocalDecl is "local a, b = 1;".

func (*LocalDecl) Pos

func (s *LocalDecl) Pos() token.Pos

type MapEntry

type MapEntry struct {
	Key   Expr
	Value Expr
}

MapEntry is "key: value" in a map literal.

func (*MapEntry) Pos

func (x *MapEntry) Pos() token.Pos

type MapLit

type MapLit struct {
	Lbrace  token.Pos
	Entries []*MapEntry
}

MapLit is "{k: v, ...}".

func (*MapLit) Pos

func (x *MapLit) Pos() token.Pos

type Member

type Member interface {
	Node
	// contains filtered or unexported methods
}

Member is a member declaration inside a class: *MethodDecl or *FieldDecl.

type MemberExpr

type MemberExpr struct {
	X    Expr
	Name *Ident
}

MemberExpr is "x->name" without a call: a data member or bound method.

func (*MemberExpr) Pos

func (x *MemberExpr) Pos() token.Pos

type MethodCall

type MethodCall struct {
	X    Expr
	Name *Ident
	Args []Expr // may contain *Spread
}

MethodCall is "x->name(args)".

func (*MethodCall) Pos

func (x *MethodCall) Pos() token.Pos

type MethodDecl

type MethodDecl struct {
	Start  token.Pos
	Static bool
	Name   *Ident
	Params []*Ident
	Rest   bool // the last parameter is a rest parameter
}

MethodDecl declares a (static) member function: "[static] name(a, b..);". The parameter names are documentation only.

func (*MethodDecl) Pos

func (m *MethodDecl) Pos() token.Pos

type NewExpr

type NewExpr struct {
	New    token.Pos
	Module *Ident // nil without a module
	Class  *Ident
	Args   []Expr // may contain *Spread
}

NewExpr is "new [module::]Class(args)".

func (*NewExpr) Pos

func (x *NewExpr) Pos() token.Pos

type NilLit

type NilLit struct {
	ValuePos token.Pos
}

NilLit is "nil".

func (*NilLit) Pos

func (x *NilLit) Pos() token.Pos

type Node

type Node interface {
	Pos() token.Pos
}

Node is any node of the tree. Pos is the position of its first token.

type Param

type Param struct {
	Name    *Ident
	Default Expr // nil for a required or rest parameter
	Rest    bool
}

Param is a parameter: "a", "a = default" or "a..".

func (*Param) Pos

func (p *Param) Pos() token.Pos

type ParamList

type ParamList struct {
	Params []*Param
}

ParamList is the parameter list of a function definition or function literal.

type ReturnStmt

type ReturnStmt struct {
	Return token.Pos
	Value  Expr // nil: return nil
}

ReturnStmt is "return [value];".

func (*ReturnStmt) Pos

func (s *ReturnStmt) Pos() token.Pos

type Spread

type Spread struct {
	X Expr
}

Spread is "x.." in an argument list or vector literal.

func (*Spread) Pos

func (x *Spread) Pos() token.Pos

type StaticRef

type StaticRef struct {
	Module *Ident // set only for module::Class::name
	Class  *Ident
	Name   *Ident
}

StaticRef is "Class::name" (a static member or static function), "module::name" (a top-level name of a module; the compiler tells the two apart) or "module::Class::name".

func (*StaticRef) Pos

func (x *StaticRef) Pos() token.Pos

type Stmt

type Stmt interface {
	Node
	// contains filtered or unexported methods
}

Stmt is a statement.

type StringLit

type StringLit struct {
	ValuePos token.Pos
	Text     string
	Value    string
}

StringLit is a string literal; Value holds the decoded bytes.

func (*StringLit) Pos

func (x *StringLit) Pos() token.Pos

type SwitchStmt

type SwitchStmt struct {
	Switch token.Pos
	Tag    Expr
	Body   *Block
}

SwitchStmt is "switch (tag) body". Case and default labels are statements somewhere inside Body.

func (*SwitchStmt) Pos

func (s *SwitchStmt) Pos() token.Pos

type ThisExpr

type ThisExpr struct {
	This token.Pos
}

ThisExpr is "this".

func (*ThisExpr) Pos

func (x *ThisExpr) Pos() token.Pos

type ThrowStmt

type ThrowStmt struct {
	Throw token.Pos
	Value Expr
}

ThrowStmt is "throw value;".

func (*ThrowStmt) Pos

func (s *ThrowStmt) Pos() token.Pos

type TryStmt

type TryStmt struct {
	Try      token.Pos
	Body     *Block
	CatchVar *Ident
	Catch    *Block
	Finally  *Block
}

TryStmt is "try body [catch (v) catch] [finally finally]"; at least one of Catch and Finally is set.

func (*TryStmt) Pos

func (s *TryStmt) Pos() token.Pos

type UnaryExpr

type UnaryExpr struct {
	Op    token.Kind
	OpPos token.Pos
	X     Expr
}

UnaryExpr is "op x" with op one of - ! ~.

func (*UnaryExpr) Pos

func (x *UnaryExpr) Pos() token.Pos

type UseDecl added in v0.2.0

type UseDecl struct {
	Use  token.Pos
	Name *Ident
}

UseDecl is "use name;": the file uses module name.

func (*UseDecl) Pos added in v0.2.0

func (d *UseDecl) Pos() token.Pos

type VarSpec

type VarSpec struct {
	Name  *Ident
	Value Expr // nil without an initializer
}

VarSpec is "name [= value]" in global, const and local declarations.

func (*VarSpec) Pos

func (v *VarSpec) Pos() token.Pos

type VectorLit

type VectorLit struct {
	Lbrack token.Pos
	Elems  []Expr // may contain *Spread
}

VectorLit is "[a, b, c..]".

func (*VectorLit) Pos

func (x *VectorLit) Pos() token.Pos

type WhileStmt

type WhileStmt struct {
	While token.Pos
	Cond  Expr
	Body  Stmt
}

WhileStmt is "while (cond) body".

func (*WhileStmt) Pos

func (s *WhileStmt) Pos() token.Pos

type WithStmt added in v0.7.0

type WithStmt struct {
	With token.Pos
	Vars []*VarSpec // each with a value
	Body Stmt
}

WithStmt is "with (local a = e, ...) body": at the end of body, however it is left, a->close() is called for each variable that is not nil, the last one first.

func (*WithStmt) Pos added in v0.7.0

func (s *WithStmt) Pos() token.Pos

Jump to

Keyboard shortcuts

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