Documentation
¶
Index ¶
- Constants
- Variables
- type Acceptor
- type Assignment
- type BinaryOperation
- type Dereference
- type Expression
- 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 // 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 ¶
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 ¶
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) 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) 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) 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) 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
AcceptChildLines calles Accept for ever element of old and handles node-replacements
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
// 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 ¶
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 ¶
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
}
Program represents the whole yolol-programm
type Statement ¶
type Statement interface {
Node
Stmt()
}
Statement is the interface for all statements
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) 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 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 (*Tokenizer) SetFilename ¶ added in v0.0.10
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 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.