Documentation
¶
Index ¶
- Constants
- type Acceptor
- type Assignment
- type BinaryOperation
- type Dereference
- type Expression
- type FuncCall
- type GoToStatement
- type IfStatement
- type Line
- type Node
- type NodeReplacement
- type NumberConstant
- type Position
- type Program
- type Statement
- type StringConstant
- type Token
- type Tokenizer
- type UnaryOperation
- type Visitor
- type VisitorFunc
Constants ¶
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
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 )
Visit() is called multiple times per node. The visitType tells the handler which one of the multiple calls the current one is
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Acceptor ¶
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 variable to assign 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
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) Start ¶
func (n *BinaryOperation) Start() Position
Start is needed to implement Node
type Dereference ¶
type Dereference struct {
Position Position
// The variable to dereference
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) Start ¶
func (n *Dereference) Start() Position
Start is needed to implement Node
type Expression ¶
type Expression interface {
Node
}
Expression is the interface for all expressions
type FuncCall ¶
type FuncCall struct {
Position Position
Function string
Argument Expression
}
FuncCall represents a func-call
type GoToStatement ¶
GoToStatement represents a goto
func (*GoToStatement) Accept ¶
func (g *GoToStatement) Accept(v Visitor) error
Accept is used to implement Acceptor
func (*GoToStatement) Start ¶
func (n *GoToStatement) Start() Position
Start is needed to implement Node
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) Start ¶
func (n *IfStatement) Start() Position
Start is needed to implement Node
type Line ¶
Line represents a line in the yolol programm
func PatchLines ¶
func PatchLines(old []*Line, position int, repl NodeReplacement) []*Line
PatchLines is used to replace the element at position in old with the elements in repl
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
type NodeReplacement ¶
type NodeReplacement struct {
Replacement []Node
}
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)
func NewNodeReplacement ¶
func NewNodeReplacement(replacement ...Node) NodeReplacement
NewNodeReplacement is used to replace the current node
func (NodeReplacement) Error ¶
func (e NodeReplacement) Error() string
Error() must be implemented for the error-interface Should NEVER be called
type NumberConstant ¶
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) Start ¶
func (n *NumberConstant) Start() Position
Start is needed to implement Node
type Position ¶
Position represents the starting-position of a token in the source-code
func NewPosition ¶
NewPosition creates a new position from a given line and coloumn
func (Position) Add ¶
Add creates a new position from the old one and adds the given amount of coloumns
type Program ¶
type Program struct {
Lines []*Line
// Contains all the comments that were found while parsing the program. MUST be ordered.
Comments []*Token
}
Program represents the whole yolol-programm
type Statement ¶
type Statement interface {
Node
}
Statement is the interface for all statements
func PatchStatements ¶
func PatchStatements(old []Statement, position int, repl NodeReplacement) []Statement
PatchStatements is used to replace the element at position in old with the elements in repl
type StringConstant ¶
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) Start ¶
func (n *StringConstant) Start() Position
Start is needed to implement Node
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
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) Start ¶
func (n *UnaryOperation) Start() Position
Start is needed to implement Node
type VisitorFunc ¶
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.