Documentation
¶
Index ¶
- Constants
- Variables
- func ErrCountMismatch(leftCount, rightCount int) error
- func ErrIndirection(varName, varType string) error
- func ErrInvalidFunc(name string, typ string) error
- func ErrUndefined(varName string) error
- func GetPrefix(place string) string
- func GetType(kind symkind) string
- func InsertSymbol(key string, kind symkind, vals ...interface{})
- func NewLabel() string
- func NewScope()
- func NewTmp() string
- func RealName(s string) string
- func RenameVariable(v string) string
- func StripPrefix(place string) string
- type ArrayType
- type AstNode
- type BasicLit
- type BasicType
- type DeferStackItem
- type DeferStmt
- type Expr
- type Field
- type ForStmt
- type FuncType
- type LabeledStmt
- type Node
- func AppendExpr(expr, exprlist *Node) (*Node, error)
- func AppendExprCaseClause(expr, exprList *Node) (*Node, error)
- func AppendFieldDecl(decl, declList *Node) (*Node, error)
- func AppendIdent(ident string, identList *Node) (*Node, error)
- func AppendKeyedElement(key, keyList *Node) (*Node, error)
- func AppendParam(decl, declList *Node) (*Node, error)
- func InitNode(place string, code []string) (*Node, error)
- func NewArithExpr(op string, leftexpr, rightexpr *Node) (*Node, error)
- func NewArrayType(arrLen, arrType string) (*Node, error)
- func NewAssignStmt(typ int, op string, leftExpr, rightExpr *Node) (*Node, error)
- func NewBlock(stmt *Node) (*Node, error)
- func NewBlockMarker() (*Node, error)
- func NewBoolExpr(op string, leftexpr, rightexpr *Node) (*Node, error)
- func NewConstSpec(typ int, args ...*Node) (*Node, error)
- func NewElementList(key, keyList *Node) (*Node, error)
- func NewExprCaseClause(expr, stmtList *Node) (*Node, error)
- func NewFieldDecl(identList, typ *Node) (*Node, error)
- func NewForClause(typ int, args ...*Node) (*Node, error)
- func NewFuncMarker(name, signature *Node) (*Node, error)
- func NewIOStmt(typ string, expr *Node) (*Node, error)
- func NewIdentifier(varName string) (*Node, error)
- func NewIfStmt(typ int, args ...*Node) (*Node, error)
- func NewIncDecStmt(op string, expr *Node) (*Node, error)
- func NewParamList(decl, declList *Node) (*Node, error)
- func NewPkgDecl(pkgName []byte) (*Node, error)
- func NewPrimaryExprArgs(expr, args *Node) (*Node, error)
- func NewPrimaryExprIndex(expr, index *Node) (*Node, error)
- func NewPrimaryExprSel(expr, selector *Node) (*Node, error)
- func NewRelExpr(op, leftexpr, rightexpr *Node) (*Node, error)
- func NewResult(params *Node) (*Node, error)
- func NewShortDecl(identList *Node, exprList AstNode) (*Node, error)
- func NewSignature(typ int, args ...*Node) (*Node, error)
- func NewStmtList(stmt, stmtList *Node) (*Node, error)
- func NewTopLevelDecl(topDecl, repeatTopDecl *Node) (*Node, error)
- func NewTypeDecl(typespec AstNode) (*Node, error)
- func NewUnaryExpr(op, expr *Node) (*Node, error)
- func NewVarSpec(typ int, args ...*Node) (*Node, error)
- func PrintIR(src *Node) (*Node, error)
- type PointerType
- type ReturnStmt
- type StructType
- type SwitchStmt
- type SymInfo
- type SymTabEntry
- type VarType
Constants ¶
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 = "&" )
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.
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 ¶
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 :=") )
var (
PkgName string
)
Functions ¶
func ErrCountMismatch ¶
ErrCountMismatch returns an assignment count mismatch error.
func ErrIndirection ¶
ErrIndirection returns an invalid indirection error.
func ErrInvalidFunc ¶
ErrInvalidFunc returns an non-function call error.
func ErrUndefined ¶
ErrUndefined returns an undefined variable error.
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 RenameVariable ¶
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 ¶
StripPrefix strips the prefix from a place value.
Types ¶
type ArrayType ¶
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 ¶
NewCompositeLit returns a composite literal.
func NewFuncDecl ¶
NewFuncDecl returns a function declaration.
func NewLabelStmt ¶
NewLabelStmt returns a labeled statement.
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 ¶
NewDeferStmt returns a defer statement.
type ForStmt ¶
type ForStmt struct {
Node
}
ForStmt represents an AST node for a for statement.
func NewForStmt ¶
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 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 ¶
AppendExpr appends a list of expressions to a given expression.
func AppendExprCaseClause ¶
AppendExprCaseClause appends an expression case clause to a list of same.
func AppendFieldDecl ¶
AppendFieldDecl appends a field declaration to a list of field declarations.
func AppendIdent ¶
AppendIdent appends an identifier to a list of identifiers.
func AppendKeyedElement ¶
AppendKeyedElement appends a keyed element to a list of keyed elements.
func AppendParam ¶
AppendParam appends a parameter to a list of parameters.
func NewArithExpr ¶
NewArithExpr returns an arithmetic expression.
func NewArrayType ¶
NewArrayType returns an array.
func NewAssignStmt ¶
NewAssignStmt returns an assignment statement.
func NewBlockMarker ¶
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 ¶
NewBoolExpr returns a new logical expression.
func NewConstSpec ¶
NewConstSpec returns a constant declaration.
func NewElementList ¶
NewElementList returns a keyed element list.
func NewExprCaseClause ¶
NewExprCaseClause returns an expression case clause.
func NewFieldDecl ¶
NewFieldDecl returns a field declaration.
func NewForClause ¶
NewForClause returns a for clause.
func NewFuncMarker ¶
NewFuncMarker returns a marker non-terminal used in the production rule for function declaration.
func NewIdentifier ¶
NewIdentifier returns a new identifier.
func NewIncDecStmt ¶
NewIncDecStmt returns an increment or a decrement statement.
func NewParamList ¶
NewParamList returns a list of parameters.
func NewPkgDecl ¶
NewPkgDecl initializes package relevant data structures.
func NewPrimaryExprArgs ¶
NewPrimaryExprArgs returns an AST node for PrimaryExpr Arguments. NOTE: This is the production rule for a function call.
func NewPrimaryExprIndex ¶
NewPrimaryExprIndex returns an AST node for PrimaryExpr Index.
func NewPrimaryExprSel ¶
NewPrimaryExprSel returns an AST node for PrimaryExpr Selector.
func NewRelExpr ¶
NewRelExpr returns a new relational expression.
func NewShortDecl ¶
NewShortDecl returns a short variable declaration.
func NewSignature ¶
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 ¶
NewStmtList returns a statement list.
func NewTopLevelDecl ¶
NewTopLevelDecl returns a top level declaration.
func NewTypeDecl ¶
NewTypeDecl returns a type declaration.
func NewUnaryExpr ¶
NewUnaryExpr returns a unary expression.
func NewVarSpec ¶
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.
type PointerType ¶
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 ¶
StructType represents an AST node of 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 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.