ast

package
v0.0.0-...-209b98f Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// arithmetic operators
	ADD = "+"
	SUB = "-"
	// An asterisk can be to define a multiplication and dereference
	// operator, hence its name.
	AST = "*"
	DIV = "/"
	REM = "%"

	// boolean operators
	NOT = "!"
	OR  = "||"
	AND = "&&"

	// comparison operators
	EQ  = "=="
	NEQ = "!="
	LEQ = "<="
	LT  = "<"
	GEQ = ">="
	GT  = ">"

	// increment/decrement operators
	INC = "++"
	DEC = "--"

	AMP = "&"
)
View Source
const (
	FNC    = "func"
	DRF    = "deref"
	PTR    = "pointer"
	ARR    = "arr"
	ARRINT = "arrint"
	ARRSTR = "arrstr"
	INT    = "int"
	STR    = "string"
	STRCT  = "struct"
)

Prefix declarations used as symbol table metadata. The use of these constants is to act as meta information stored in the place attribute of a node. Currently these are simply prepended to the place attribute, and checked for presence when required.

View Source
const (
	NIL symkind = iota
	FUNCTION
	DEREF
	POINTER
	ARRAYINT
	ARRAYSTR
	INTEGER
	STRING
	STRUCT
)

The following declarations determine the values which can be taken by symkind.

Variables

View Source
var (
	ErrDeclArr    = errors.New("use short declaration for declaring arrays")
	ErrDeclStruct = errors.New("use short declaration for declaring structs")
	ErrShortDecl  = errors.New("no new variables on left side of :=")
)
View Source
var (
	PkgName string
)

Functions

func ErrCountMismatch

func ErrCountMismatch(leftCount, rightCount int) error

ErrCountMismatch returns an assignment count mismatch error.

func ErrIndirection

func ErrIndirection(varName, varType string) error

ErrIndirection returns an invalid indirection error.

func ErrInvalidFunc

func ErrInvalidFunc(name string, typ string) error

ErrInvalidFunc returns an non-function call error.

func ErrUndefined

func ErrUndefined(varName string) error

ErrUndefined returns an undefined variable error.

func GetPrefix

func GetPrefix(place string) string

GetPrefix returns the prefix from a place value.

func GetType

func GetType(kind symkind) string

GetType returns the type information from a symkind variable.

func InsertSymbol

func InsertSymbol(key string, kind symkind, vals ...interface{})

InsertSymbol creates a symbol table entry corresponding to a key in the symbol table.

func NewLabel

func NewLabel() string

NewLabel generates a unique label name.

func NewScope

func NewScope()

NewScope modifies the symbol table hierarchy, creating a new scope.

func NewTmp

func NewTmp() string

NewTmp generates a unique temporary variable name.

func RealName

func RealName(s string) string

RealName extracts the original name of a variable from its renamed version.

func RenameVariable

func RenameVariable(v string) string

NewVar generates a unique variable name used for renaming. A variable named var will be renamed to 'var.int_lit' where int_lit is an integer. Since variable names cannot contain a '.', this will not result in a naming conflict with an existing variable. The renamed variable will only occur in the IR (there is no constraint on variable names in IR as of now).

func StripPrefix

func StripPrefix(place string) string

StripPrefix strips the prefix from a place value.

Types

type ArrayType

type ArrayType struct {
	Node
	Type Expr // type of elements
	Len  int  // array size
}

ArrayType represents an AST node of an array. TODO: ArrayType hasn't been used yet.

type AstNode

type AstNode interface {
	// contains filtered or unexported methods
}

AstNode defines a node in the AST of a given program.

func NewCompositeLit

func NewCompositeLit(typ, val *Node) (AstNode, error)

NewCompositeLit returns a composite literal.

func NewFuncDecl

func NewFuncDecl(marker, body *Node) (AstNode, error)

NewFuncDecl returns a function declaration.

func NewLabelStmt

func NewLabelStmt(label, stmt *Node) (AstNode, error)

NewLabelStmt returns a labeled statement.

func NewTypeDef

func NewTypeDef(ident string, typ AstNode) (AstNode, error)

NewTypeDef returns a type definition.

type BasicLit

type BasicLit struct {
	*Node
	Type *token.Token
}

BasicLit represents an AST node of a literal of basic type.

type BasicType

type BasicType int

BasicType defines the base types.

const (
	Invalid BasicType = iota
	Int
	String
)

type DeferStackItem

type DeferStackItem []string

DeferStackItem is an individual item stored when a call to defer is made. It contains the code for the corresponding function call which is placed at the end of the function body.

type DeferStmt

type DeferStmt struct {
	Node
}

DeferStmt represents an AST node for a defer statement.

func NewDeferStmt

func NewDeferStmt(expr, args *Node) (*DeferStmt, error)

NewDeferStmt returns a defer statement.

type Expr

type Expr interface {
	AstNode
}

Expr represents an AST node of an expression.

type Field

type Field struct {
	Name string
	Type Expr
}

Field represents an AST node of a field declaration.

type ForStmt

type ForStmt struct {
	Node
}

ForStmt represents an AST node for a for statement.

func NewForStmt

func NewForStmt(typ int, args ...*Node) (*ForStmt, error)

NewForStmt returns a for statement. The accepted variants of the variadic arguments (args) are -

  • Block
  • Condition, Block
  • ForClause, Block

The cardinal argument `typ` determines the index of the production rule invoked starting from top.

type FuncType

type FuncType struct {
	Node
	Params  []*Field
	Results []*Field
}

FuncType represents an AST node of a function.

type LabeledStmt

type LabeledStmt struct {
	Node
}

LabeledStmt represents an AST node of a label statement.

type Node

type Node struct {
	// If the AST node represents an expression, then place stores the name
	// of the variable storing the value of the expression.
	Place string
	Code  []string // IR instructions
}

Node implements the common parts of AstNode.

func AppendExpr

func AppendExpr(expr, exprlist *Node) (*Node, error)

AppendExpr appends a list of expressions to a given expression.

func AppendExprCaseClause

func AppendExprCaseClause(expr, exprList *Node) (*Node, error)

AppendExprCaseClause appends an expression case clause to a list of same.

func AppendFieldDecl

func AppendFieldDecl(decl, declList *Node) (*Node, error)

AppendFieldDecl appends a field declaration to a list of field declarations.

func AppendIdent

func AppendIdent(ident string, identList *Node) (*Node, error)

AppendIdent appends an identifier to a list of identifiers.

func AppendKeyedElement

func AppendKeyedElement(key, keyList *Node) (*Node, error)

AppendKeyedElement appends a keyed element to a list of keyed elements.

func AppendParam

func AppendParam(decl, declList *Node) (*Node, error)

AppendParam appends a parameter to a list of parameters.

func InitNode

func InitNode(place string, code []string) (*Node, error)

InitNode initializes an AST node with the given "Place" and "Code" attributes.

func NewArithExpr

func NewArithExpr(op string, leftexpr, rightexpr *Node) (*Node, error)

NewArithExpr returns an arithmetic expression.

func NewArrayType

func NewArrayType(arrLen, arrType string) (*Node, error)

NewArrayType returns an array.

func NewAssignStmt

func NewAssignStmt(typ int, op string, leftExpr, rightExpr *Node) (*Node, error)

NewAssignStmt returns an assignment statement.

func NewBlock

func NewBlock(stmt *Node) (*Node, error)

NewBlock ends the scope of the previous block and returns a new block.

func NewBlockMarker

func NewBlockMarker() (*Node, error)

NewBlockMarker returns a marker non-terminal used in the production rule for block declaration. This marker demarcates the beginning of a new block and the corresponding symbol table is instantiated here.

func NewBoolExpr

func NewBoolExpr(op string, leftexpr, rightexpr *Node) (*Node, error)

NewBoolExpr returns a new logical expression.

func NewConstSpec

func NewConstSpec(typ int, args ...*Node) (*Node, error)

NewConstSpec returns a constant declaration.

func NewElementList

func NewElementList(key, keyList *Node) (*Node, error)

NewElementList returns a keyed element list.

func NewExprCaseClause

func NewExprCaseClause(expr, stmtList *Node) (*Node, error)

NewExprCaseClause returns an expression case clause.

func NewFieldDecl

func NewFieldDecl(identList, typ *Node) (*Node, error)

NewFieldDecl returns a field declaration.

func NewForClause

func NewForClause(typ int, args ...*Node) (*Node, error)

NewForClause returns a for clause.

func NewFuncMarker

func NewFuncMarker(name, signature *Node) (*Node, error)

NewFuncMarker returns a marker non-terminal used in the production rule for function declaration.

func NewIOStmt

func NewIOStmt(typ string, expr *Node) (*Node, error)

NewIOStmt returns an I/O statement.

func NewIdentifier

func NewIdentifier(varName string) (*Node, error)

NewIdentifier returns a new identifier.

func NewIfStmt

func NewIfStmt(typ int, args ...*Node) (*Node, error)

NewIfStmt returns an if statement.

func NewIncDecStmt

func NewIncDecStmt(op string, expr *Node) (*Node, error)

NewIncDecStmt returns an increment or a decrement statement.

func NewParamList

func NewParamList(decl, declList *Node) (*Node, error)

NewParamList returns a list of parameters.

func NewPkgDecl

func NewPkgDecl(pkgName []byte) (*Node, error)

NewPkgDecl initializes package relevant data structures.

func NewPrimaryExprArgs

func NewPrimaryExprArgs(expr, args *Node) (*Node, error)

NewPrimaryExprArgs returns an AST node for PrimaryExpr Arguments. NOTE: This is the production rule for a function call.

func NewPrimaryExprIndex

func NewPrimaryExprIndex(expr, index *Node) (*Node, error)

NewPrimaryExprIndex returns an AST node for PrimaryExpr Index.

func NewPrimaryExprSel

func NewPrimaryExprSel(expr, selector *Node) (*Node, error)

NewPrimaryExprSel returns an AST node for PrimaryExpr Selector.

func NewRelExpr

func NewRelExpr(op, leftexpr, rightexpr *Node) (*Node, error)

NewRelExpr returns a new relational expression.

func NewResult

func NewResult(params *Node) (*Node, error)

NewResult defines the return type of a function.

func NewShortDecl

func NewShortDecl(identList *Node, exprList AstNode) (*Node, error)

NewShortDecl returns a short variable declaration.

func NewSignature

func NewSignature(typ int, args ...*Node) (*Node, error)

NewSignature returns a function signature. The accepted variadic arguments (args) in their order are -

  • parameters
  • result

The cardinal argument `typ` determines the index of the production rule invoked starting from top.

func NewStmtList

func NewStmtList(stmt, stmtList *Node) (*Node, error)

NewStmtList returns a statement list.

func NewTopLevelDecl

func NewTopLevelDecl(topDecl, repeatTopDecl *Node) (*Node, error)

NewTopLevelDecl returns a top level declaration.

func NewTypeDecl

func NewTypeDecl(typespec AstNode) (*Node, error)

NewTypeDecl returns a type declaration.

func NewUnaryExpr

func NewUnaryExpr(op, expr *Node) (*Node, error)

NewUnaryExpr returns a unary expression.

func NewVarSpec

func NewVarSpec(typ int, args ...*Node) (*Node, error)

NewVarSpec creates a new variable specification. The accepted variadic arguments (args) in their order are -

  • IdentifierList
  • Type
  • ExpressionList

The cardinal argument `typ` determines the index of the production rule invoked starting from top.

func PrintIR

func PrintIR(src *Node) (*Node, error)

PrintIR generates the IR instructions accumulated in the "Code" attribute of the "SourceFile" non-terminal.

type PointerType

type PointerType struct {
	Node
	Type Expr
}

PointerType represents an AST node of a pointer.

type ReturnStmt

type ReturnStmt struct {
	Node
}

ReturnStmt represents an AST node for a return statement.

func NewReturnStmt

func NewReturnStmt(expr ...*Node) (*ReturnStmt, error)

NewReturnStmt returns a return statement. A return statement can be of the following types -

  • an empty return: In this case the argument expr is empty.
  • non-empty return: In this case the argument expr contains the return expression.

type StructType

type StructType struct {
	Node
	Name string // name of the struct
	Len  int
}

StructType represents an AST node of a struct.

func NewStruct

func NewStruct(node *Node) (*StructType, error)

NewStruct returns a struct.

type SwitchStmt

type SwitchStmt struct {
	Node
}

SwitchStmt represents an AST node for a switch statement.

func NewSwitchStmt

func NewSwitchStmt(expr, caseClause *Node) (*SwitchStmt, error)

NewSwitchStmt returns a switch statement.

type SymInfo

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

type SymTabEntry

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

func GetSymbol

func GetSymbol(key string) (SymTabEntry, bool)

GetSymbol returns the symbol table entry in current scope for a key.

func Lookup

func Lookup(v string) (*SymTabEntry, bool)

Lookup returns the symbol table entry for a given variable in the current scope. If not found, the parent symbol table is looked up until the topmost symbol table is reached. If not found in all these symbol tables, then the global symbol table is looked up which contains the entries corresponding to structs and functions.

type VarType

type VarType struct {
	Node
	Name string
	Type BasicType
}

VarType represents an AST node of a variable declaration.

Jump to

Keyboard shortcuts

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