ast

package
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeID         = "ID"
	TypeNumber     = "Number"
	TypeString     = "String"
	TypeKeyword    = "Keyword"
	TypeSymbol     = "Symbol"
	TypeNewline    = "Newline"
	TypeEOF        = "EOF"
	TypeComment    = "Comment"
	TypeWhitespace = "Whitespace"
	TypeUnknown    = "Unknown"
)

Defines the different types a token can be

View Source
const (
	// This visit represents the beginning of the node
	PreVisit = -1
	// This visit represents the end of the node
	PostVisit = -2
	// This visit is the only one for this node
	SingleVisit = -3
	// This visit is the first intermediate visit of the node
	InterVisit1 = -4
	// This visit is the second intermediate visit of the node
	InterVisit2 = -5
	// This visit is the third intermediate visit of the node
	InterVisit3 = -6
)

Visit() is called multiple times per node. The visitType tells the handler which one of the multiple calls the current one is

Variables

View Source
var UnknownPosition = Position{}

UnknownPosition is used, when a position is expected, but a real one can not be provided Usually used by ast-elements that rely on their children to determin their position, if the children are nil

Functions

This section is empty.

Types

type Acceptor

type Acceptor interface {
	Accept(v Visitor) error
}

Acceptor MUST be implemented by EVERY AST-Node. The node must do the following things: - call Visit(node,Previsit) on the visitor - call Accept() on every one of it's children - if it has a list of children it MUST call Visit(node,i) before calling accept() on the i-th element of the list - if the node has multiple children (e.g. if.statement(condition,ifblock,elseblock)) it must call Visit(node,InterVisitX) between the different kinds of children - it must call Visit(node,PostVisit) after accept() has been called on all children if the node has no children is MUST call Visit(node,SingleVisit) and NOTHING ELSE

type Assignment

type Assignment struct {
	Position Position
	// The name of the variable that is assigned to
	Variable string
	// The value to be assigned
	Value Expression
	// Operator to use (=,+=,-=, etc.)
	Operator string
}

Assignment represents the assignment to a variable

func (*Assignment) Accept

func (a *Assignment) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*Assignment) End

func (n *Assignment) End() Position

End is needed to implement Node

func (*Assignment) Start

func (n *Assignment) Start() Position

Start is needed to implement Node

func (*Assignment) Stmt added in v0.0.23

func (n *Assignment) Stmt()

Stmt implements type-checking dummy-func

type BinaryOperation

type BinaryOperation struct {
	Operator string
	Exp1     Expression
	Exp2     Expression
}

BinaryOperation is a binary operation

func (*BinaryOperation) Accept

func (o *BinaryOperation) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*BinaryOperation) End

func (n *BinaryOperation) End() Position

End is needed to implement Node

func (*BinaryOperation) Expr added in v0.0.23

func (n *BinaryOperation) Expr()

Expr implements type-checking dummy-func

func (*BinaryOperation) Start

func (n *BinaryOperation) Start() Position

Start is needed to implement Node

type Dereference

type Dereference struct {
	Position Position
	// The name of the dereferenced variable
	Variable string
	// Additional operator (++ or --)
	Operator string
	// Wheter to use the Operator as Pre- or Postoperator
	PrePost string
	// True if this is used as a statement instead of expression
	IsStatement bool
}

Dereference represents the dereferencing of a variable

func (*Dereference) Accept

func (d *Dereference) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*Dereference) End

func (n *Dereference) End() Position

End is needed to implement Node

func (*Dereference) Expr added in v0.0.23

func (n *Dereference) Expr()

Expr implements type-checking dummy-func

func (*Dereference) Start

func (n *Dereference) Start() Position

Start is needed to implement Node

func (*Dereference) Stmt added in v0.0.23

func (n *Dereference) Stmt()

Stmt implements type-checking dummy-func Dereferences can be used as statement when combined with a pre/post-op

type Expression

type Expression interface {
	Node
	// dummy function for type-safety
	Expr()
}

Expression is the interface for all expressions

func MustExpression added in v0.0.23

func MustExpression(n Node, err error) (Expression, error)

MustExpression casts a (node,err)-pair to an (expression,err)

type GoToStatement

type GoToStatement struct {
	Position Position
	// The Line to go to
	Line Expression
}

GoToStatement represents a goto

func (*GoToStatement) Accept

func (g *GoToStatement) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*GoToStatement) End

func (n *GoToStatement) End() Position

End is needed to implement Node

func (*GoToStatement) Start

func (n *GoToStatement) Start() Position

Start is needed to implement Node

func (*GoToStatement) Stmt added in v0.0.23

func (n *GoToStatement) Stmt()

Stmt implements type-checking dummy-func

type IfStatement

type IfStatement struct {
	Position Position
	// Condition for the if
	Condition Expression
	// Statements to execute if true
	IfBlock []Statement
	// Statements to execute if false
	ElseBlock []Statement
}

IfStatement represents an if-statement

func (*IfStatement) Accept

func (s *IfStatement) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*IfStatement) End

func (n *IfStatement) End() Position

End is needed to implement Node

func (*IfStatement) Start

func (n *IfStatement) Start() Position

Start is needed to implement Node

func (*IfStatement) Stmt added in v0.0.23

func (n *IfStatement) Stmt()

Stmt implements type-checking dummy-func

type Line

type Line struct {
	Position   Position
	Statements []Statement
	// A Line can have a comment at the end or be just a comment without statements
	Comment string
}

Line represents a line in the yolol programm

func AcceptChildLines added in v0.0.10

func AcceptChildLines(parent Node, v Visitor, old []*Line) ([]*Line, error)

AcceptChildLines calles Accept for ever element of old and handles node-replacements

func (*Line) Accept

func (l *Line) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*Line) End

func (n *Line) End() Position

End is needed to implement Node

func (*Line) Start

func (n *Line) Start() Position

Start is needed to implement Node

type Node

type Node interface {
	Acceptor
	// Start returns the start-position of the node in the source-code
	Start() Position
	// End returns the end-position of the node in the source-code
	End() Position
}

Node is the base interface

func AcceptChild added in v0.0.10

func AcceptChild(v Visitor, node Node) (Node, error)

AcceptChild calls node.Accept(v) and processes any returned NodeReplacements The returned node is either the input node or an appropriate replacement

type NodeReplacement

type NodeReplacement struct {
	Replacement []Node
	// if skip is true, do not re-visit the replaced node
	Skip bool
}

NodeReplacement special error type. If this type is returned by an AST-child during Accept() the parent-node MUST react to this by replacing the child with the nodes in Replacement[] and then discard the error (and NOT relay it to its parent) Also, the new replacement node must be visited again, if Skip==false. If this new visit also reslults in a replacement, the new replacement must be visited again and so on, until no replacement is received anymore.

func NewNodeReplacement

func NewNodeReplacement(replacement ...Node) NodeReplacement

NewNodeReplacement is used to replace the current node

func NewNodeReplacementSkip added in v0.0.10

func NewNodeReplacementSkip(replacement ...Node) NodeReplacement

NewNodeReplacementSkip is used to replace the current node and signals not to re-visit the new node

func (NodeReplacement) Error

func (e NodeReplacement) Error() string

Error() must be implemented for the error-interface Should NEVER be called

type NumberConstant

type NumberConstant struct {
	Position Position
	// Value of the constant
	Value string
}

NumberConstant represents a constant of type number

func (*NumberConstant) Accept

func (c *NumberConstant) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*NumberConstant) End

func (n *NumberConstant) End() Position

End is needed to implement Node

func (*NumberConstant) Expr added in v0.0.23

func (n *NumberConstant) Expr()

Expr implements type-checking dummy-func

func (*NumberConstant) Start

func (n *NumberConstant) Start() Position

Start is needed to implement Node

type Position

type Position struct {
	File    string
	Line    int
	Coloumn int
}

Position represents the starting-position of a token in the source-code

func NewPosition

func NewPosition(file string, line int, coloumn int) Position

NewPosition creates a new position from a given line and coloumn

func (Position) Add

func (p Position) Add(col int) Position

Add creates a new position from the old one and adds the given amount of coloumns

func (Position) Before

func (p Position) Before(other Position) bool

Before returns true if p represents a position in the file before the position of other

func (Position) String

func (p Position) String() string

type Program

type Program struct {
	Lines []*Line
}

Program represents the whole yolol-programm

func (*Program) Accept

func (p *Program) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*Program) End

func (n *Program) End() Position

End is needed to implement Node

func (*Program) Start

func (n *Program) Start() Position

Start is needed to implement Node

type Statement

type Statement interface {
	Node
	Stmt()
}

Statement is the interface for all statements

func AcceptChildStatements added in v0.0.10

func AcceptChildStatements(parent Node, v Visitor, old []Statement) ([]Statement, error)

AcceptChildStatements calles Accept for ever element of old and handles node-replacements

type StringConstant

type StringConstant struct {
	Position Position
	// Value of the constant
	Value string
}

StringConstant represents a constant of type string

func (*StringConstant) Accept

func (c *StringConstant) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*StringConstant) End

func (n *StringConstant) End() Position

End is needed to implement Node

func (*StringConstant) Expr added in v0.0.23

func (n *StringConstant) Expr()

Expr implements type-checking dummy-func

func (*StringConstant) Start

func (n *StringConstant) Start() Position

Start is needed to implement Node

type Token

type Token struct {
	Type     string
	Value    string
	Position Position
}

Token represents a token fount in the source-code

func (Token) String

func (t Token) String() string

type Tokenizer

type Tokenizer struct {
	Symbols []string
	// KeywordRegex is used to parse keywords
	KeywordRegex *regexp.Regexp
	// IdentifierRegex is used to parse identifiers
	IdentifierRegex *regexp.Regexp
	// NumberRegex is used to parse numbers
	NumberRegex *regexp.Regexp
	// CommentRegex is used to parse comments
	CommentRegex *regexp.Regexp
	// contains filtered or unexported fields
}

Tokenizer splits the input source-code into tokens

func NewTokenizer

func NewTokenizer() *Tokenizer

NewTokenizer creates a new tokenizer

func (*Tokenizer) Load

func (t *Tokenizer) Load(input string)

Load loads programm code as input

func (*Tokenizer) Next

func (t *Tokenizer) Next() *Token

Next returns the next token from the source document

func (*Tokenizer) SetFilename added in v0.0.10

func (t *Tokenizer) SetFilename(name string)

SetFilename sets the filename that is set in the position if all returned tokens

type UnaryOperation

type UnaryOperation struct {
	Position Position
	Operator string
	Exp      Expression
}

UnaryOperation represents a unary operation (-, not)

func (*UnaryOperation) Accept

func (u *UnaryOperation) Accept(v Visitor) error

Accept is used to implement Acceptor

func (*UnaryOperation) End

func (n *UnaryOperation) End() Position

End is needed to implement Node

func (*UnaryOperation) Expr added in v0.0.23

func (n *UnaryOperation) Expr()

Expr implements type-checking dummy-func

func (*UnaryOperation) Start

func (n *UnaryOperation) Start() Position

Start is needed to implement Node

type Visitor

type Visitor interface {
	Visit(node Node, visitType int) error
}

Visitor defines an interface that allows easy node-agnostic traversal of the AST

type VisitorFunc

type VisitorFunc func(node Node, visitType int) error

VisitorFunc allows simple functions to be used as Visitor

func (VisitorFunc) Visit

func (f VisitorFunc) Visit(node Node, visitType int) error

Visit is called by the currently visited AST-Node The Visitor has to use a type-assertion to find out what kind of node it is visitType tells the visitor which one of the multiple-visits for this node the current one is. If the Visitor is used as argument to Acceptor.Accept() visit is called (multiple times) for every AST-Node in the sub-tree starting at the acceptor. It can then modify these nodes. By return the NodeReplacement type as the 'error' the currently visited node can be replaced by another node.

Jump to

Keyboard shortcuts

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