Documentation
¶
Overview ¶
Package parser parses B++ source code into an AST tree.
Index ¶
- func MatchTypes(data []Statement, pos *Pos, types []DataType) error
- func SetupArrays()
- func SetupBlocks()
- func SetupComparisons()
- func SetupFunctions()
- func SetupMath()
- func SetupOthers()
- func SetupRandoms()
- func SetupTypes()
- func SetupVariables()
- type ArgsStmt
- type ArrayStmt
- type BasicStatement
- type Block
- type BlockParser
- type CeilStmt
- type ChooseStmt
- type ComparisonStmt
- type ConcatStmt
- type Data
- type DataType
- type DefineStmt
- type FloorStmt
- type FunctionBlock
- type FunctionCallStmt
- type FunctionType
- type IfBlock
- type IfStmt
- type ImportStmt
- type IndexStmt
- type LengthStmt
- type MathStmt
- type Operator
- type ParamStmt
- type Pos
- type Program
- type RandintStmt
- type RandomStmt
- type RoundStmt
- type Scope
- type ScopeStack
- type Statement
- type StatementParser
- type TypeCastStmt
- type VarStmt
- type WhileBlock
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MatchTypes ¶
MatchTypes compares 2 signatures, and is used in type-checking for function and block parsing. It supports variadic arguments.
func SetupFunctions ¶
func SetupFunctions()
SetupFunctions adds the PARAM statement and the FUNCTION block
func SetupRandoms ¶
func SetupRandoms()
SetupRandoms adds the RANDINT, RANDOM, and CHOOSE statements
func SetupTypes ¶
func SetupTypes()
SetupTypes adds the type-cast parsers for STRING, INT, FLOAT, and LIST
Types ¶
type ArgsStmt ¶
type ArgsStmt struct {
*BasicStatement
Index Statement
}
ArgsStmt is the equivalent of [ARGS stmt.Index]
type ArrayStmt ¶
type ArrayStmt struct {
*BasicStatement
Values []Statement
}
ArrayStmt is the equivalent of [ARRAY stmt.Values[0] stmt.Values[1] etc...]
type BasicStatement ¶
type BasicStatement struct {
// contains filtered or unexported fields
}
BasicStatement allows other statements to implement the Statement interface
func (*BasicStatement) Pos ¶
func (b *BasicStatement) Pos() *Pos
Pos gives the pos of a basic statement
func (*BasicStatement) Type ¶
func (b *BasicStatement) Type() DataType
Type gives NULL for a basic statement, this method is usually overwritten by the statement embedding this struct
type Block ¶
type Block interface {
Pos() *Pos
Type() DataType
Keywords() []string
EndSignature() []DataType
End(keyword string, arguments []Statement, statements []Statement) bool // Returns whether closed or not
}
Block is a statement that supports being multiple types
type BlockParser ¶
type BlockParser struct {
Parse func(args []Statement, pos *Pos) (Block, error)
Signature []DataType
}
BlockParser defines the type of a block parser - a function to parse the first statement of a block and return a block object based on that, and the signature of the first statement in the block
type CeilStmt ¶
type CeilStmt struct {
*BasicStatement
Val Statement
}
CeilStmt is the equivalent of [CEIL stmt.Val]
type ChooseStmt ¶
type ChooseStmt struct {
*BasicStatement
Data Statement
}
ChooseStmt is the equivalent of [CHOOSE stmt.Data]
func (*ChooseStmt) Type ¶
func (c *ChooseStmt) Type() DataType
Type gives the return type of a CHOOSE statement (STRING if the data is a STRING, otherwise ANY)
type ComparisonStmt ¶
type ComparisonStmt struct {
*BasicStatement
Operation Operator
Left Statement
Right Statement
}
ComparisonStmt is the equivalent of [COMPARE stmt.Left stmt.Operation stmt.Right]
func (*ComparisonStmt) Type ¶
func (c *ComparisonStmt) Type() DataType
Type gives the return type of an COMPARE statement (an INT, equal to 0 or 1)
type ConcatStmt ¶
type ConcatStmt struct {
*BasicStatement
Strings []Statement
}
ConcatStmt is the equivalent of [CONCAT stmt.Strings]
func (*ConcatStmt) Type ¶
func (c *ConcatStmt) Type() DataType
Type returns the return type of a CONCAT statement (STRING)
type Data ¶
type Data struct {
*BasicStatement
Data interface{}
// contains filtered or unexported fields
}
Data represents a piece of Data in B++, most often a literal. It implements the Statement interface.
type DataType ¶
type DataType int
DataType is an enum for all B++ data types. It also supports combining multiple data types through bit masks. The data type for the B++ Data struct is commented next to the enum value
type DefineStmt ¶
type DefineStmt struct {
*BasicStatement
Label Statement
Value Statement
}
DefineStmt is the equivalent of [DEFINE stmt.Label stmt.Value]
func (*DefineStmt) Type ¶
func (d *DefineStmt) Type() DataType
Type gives the return type of a DEFINE statement (NULL)
type FloorStmt ¶
type FloorStmt struct {
*BasicStatement
Val Statement
}
FloorStmt is the equivalent of [FLOOR stmt.Val]
type FunctionBlock ¶
type FunctionBlock struct {
*BasicStatement
Name string
Signature FunctionType
Return Statement
Body []Statement
}
FunctionBlock is the equivalent of [IFB stmt.Condition] stmt.Body [ELSE] stmt.Else [ENDIF], the stmt.Else may be nil
func (*FunctionBlock) End ¶
func (f *FunctionBlock) End(_ string, args []Statement, statements []Statement) bool
End parses a function end (which would be a RETURN)
func (*FunctionBlock) EndSignature ¶
func (f *FunctionBlock) EndSignature() []DataType
EndSignature gets the ending signature of a FUNCTION (ANY or NULL)
func (*FunctionBlock) Keywords ¶
func (f *FunctionBlock) Keywords() []string
Keywords return the keywords for a FUNCTION block (RETURN)
func (*FunctionBlock) Type ¶
func (f *FunctionBlock) Type() DataType
Type returns the return type of a FUNCTION block
type FunctionCallStmt ¶
type FunctionCallStmt struct {
*BasicStatement
Name string
Args []Statement
ReturnType DataType
}
FunctionCallStmt is the equivalent of [stmt.Name stmt.Args...]
func (*FunctionCallStmt) Type ¶
func (f *FunctionCallStmt) Type() DataType
Type returns the return type of a function call
type FunctionType ¶
FunctionType stores the types of a function signature
type IfBlock ¶
type IfBlock struct {
*BasicStatement
Condition Statement
Body []Statement
Else []Statement
}
IfBlock is the equivalent of [IFB stmt.Condition] stmt.Body [ELSE] stmt.Else [ENDIF], the stmt.Else may be nil
func (*IfBlock) EndSignature ¶
EndSignature gets the ending signature of an IFB statement (blank)
type IfStmt ¶
type IfStmt struct {
*BasicStatement
Condition Statement
Body Statement
Else Statement
}
IfStmt is the equivalent of [IF stmt.Condition stmt.Body stmt.Else]
type ImportStmt ¶
type ImportStmt struct {
*BasicStatement
Statements []Statement
Filename string
}
ImportStmt is the AST tree resulted of [IMPORT stmt.Filename]
func (*ImportStmt) Type ¶
func (i *ImportStmt) Type() DataType
Type returns the return type of a IMPORT statement (NULL)
type IndexStmt ¶
type IndexStmt struct {
*BasicStatement
Value Statement
Index Statement
}
IndexStmt is the equivalent of [VAR stmt.Label stmt.Index]
type LengthStmt ¶
type LengthStmt struct {
*BasicStatement
Value Statement
}
LengthStmt is the equivalent of [LENGTH stmt.Value]
func (*LengthStmt) Type ¶
func (l *LengthStmt) Type() DataType
Type gets the type of a LENGTH statement (INT)
type MathStmt ¶
type MathStmt struct {
*BasicStatement
Operation Operator
Left Statement
Right Statement
}
MathStmt is the equivalent of [MATH stmt.Left stmt.Operation stmt.Right]
type ParamStmt ¶
type ParamStmt struct {
*BasicStatement
Name string
Kind DataType
}
ParamStmt is the equivalent of [PARAM stmt.Name stmt.Type]
type Pos ¶
Pos defines a position in a B++ program, commonly used in debug prints
type Program ¶
type Program struct {
Statements []Statement
}
Program is the main program, containing the source AST
func ParseFiles ¶
ParseFiles parses multiple B++ source code files, accepting the name of the main file and a map of filename to source code.
func (*Program) EndSignature ¶
EndSignature is there to make a Program implement the Block interface
type RandintStmt ¶
type RandintStmt struct {
*BasicStatement
Lower Statement
Upper Statement
}
RandintStmt is the equivalent of [RANDINT stmt.Lower stmt.Upper]
func (*RandintStmt) Type ¶
func (r *RandintStmt) Type() DataType
Type gives the return type of a RANDINT statement (INT)
type RandomStmt ¶
type RandomStmt struct {
*BasicStatement
Lower Statement
Upper Statement
}
RandomStmt is the equivalent of [RANDOM stmt.Lower stmt.Upper]
func (*RandomStmt) Type ¶
func (r *RandomStmt) Type() DataType
Type gives the return type of a RANDOM statement (FLOAT)
type RoundStmt ¶
type RoundStmt struct {
*BasicStatement
Val Statement
}
RoundStmt is the equivalent of [ROUND stmt.Val]
type Scope ¶
Scope represents the data of a B++ scope
func (*Scope) HasKeyword ¶
HasKeyword checks if a B++ scope contains the keyword for a statement efficiently
type ScopeStack ¶
type ScopeStack struct {
// contains filtered or unexported fields
}
ScopeStack represents the data for a program's scopes - a Stack of scopes
func NewScopeStack ¶
func NewScopeStack() *ScopeStack
NewScopeStack creates a new initialized stack of scopes
func (*ScopeStack) AddScope ¶
func (s *ScopeStack) AddScope(scope *Scope)
AddScope adds a scope to the stack
func (*ScopeStack) AddStatement ¶
func (s *ScopeStack) AddStatement(stmt Statement)
AddStatement adds a statement to the scope's statements
func (*ScopeStack) FinishScope ¶
func (s *ScopeStack) FinishScope(keyword string, arguments []Statement)
FinishScope pops a scope off of the end of the stack, after processing the ending of the block
func (*ScopeStack) GetScope ¶
func (s *ScopeStack) GetScope() *Scope
GetScope gets the scope at the top of the stack
type Statement ¶
Statement stores the data for everything in B++
type StatementParser ¶
type StatementParser struct {
Parse func(args []Statement, pos *Pos) (Statement, error)
Signature []DataType
}
StatementParser defines the type for a statement parser - a cusotm function that can parse the statement and a signature of its parameters
type TypeCastStmt ¶
type TypeCastStmt struct {
*BasicStatement
Value Statement
NewType DataType
}
TypeCastStmt is the equivalent of [stmt.NewType stmt.Val]
func (*TypeCastStmt) Type ¶
func (t *TypeCastStmt) Type() DataType
Type gives the return type of a type-cast statement (stmt.NewType)
type VarStmt ¶
type VarStmt struct {
*BasicStatement
Label Statement
}
VarStmt is the equivalent of [VAR stmt.Label]
type WhileBlock ¶
type WhileBlock struct {
*BasicStatement
Condition Statement
Body []Statement
}
WhileBlock is the equivalent of [WHILE stmt.Condition] [ENDWHILE]
func (*WhileBlock) End ¶
func (w *WhileBlock) End(kind string, _ []Statement, statements []Statement) bool
End parses the ending of a WHILE statement
func (*WhileBlock) EndSignature ¶
func (w *WhileBlock) EndSignature() []DataType
EndSignature gets the end signature of a WHILE statement (blank)
func (*WhileBlock) Keywords ¶
func (w *WhileBlock) Keywords() []string
Keywords give the keywords of a WHILE statement (just ENDWHILE)
func (*WhileBlock) Type ¶
func (w *WhileBlock) Type() DataType
Type gives the type of a WHILE statement (nothing)