parser

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package parser parses B++ source code into an AST tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchTypes

func MatchTypes(data []Statement, pos *Pos, types []DataType) error

MatchTypes compares 2 signatures, and is used in type-checking for function and block parsing. It supports variadic arguments.

func SetupArrays

func SetupArrays()

SetupArrays adds the ARRAY and INDEX functions

func SetupBlocks

func SetupBlocks()

SetupBlocks adds the IFB and WHILE functions

func SetupComparisons

func SetupComparisons()

SetupComparisons adds the IF and COMPARE functions

func SetupFunctions

func SetupFunctions()

SetupFunctions adds the PARAM statement and the FUNCTION block

func SetupMath

func SetupMath()

SetupMath adds the MATH, ROUND, FLOOR, and CEIL statements

func SetupOthers

func SetupOthers()

SetupOthers adds the ARGS, CONCAT, and REPEAT statements

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

func SetupVariables

func SetupVariables()

SetupVariables adds the DEFINE and VAR statements

Types

type ArgsStmt

type ArgsStmt struct {
	*BasicStatement
	Index Statement
}

ArgsStmt is the equivalent of [ARGS stmt.Index]

func (*ArgsStmt) Type

func (a *ArgsStmt) Type() DataType

Type returns the return type of an ARGS statement (STRING)

type ArrayStmt

type ArrayStmt struct {
	*BasicStatement
	Values []Statement
}

ArrayStmt is the equivalent of [ARRAY stmt.Values[0] stmt.Values[1] etc...]

func (*ArrayStmt) Type

func (a *ArrayStmt) Type() DataType

Type gets the type of an ARRAY statement (ARRAY)

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]

func (*CeilStmt) Type

func (c *CeilStmt) Type() DataType

Type gives the return type of a CEIL statement (INT)

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.

func ParseData

func ParseData(src string, pos *Pos) *Data

ParseData parses a literal and converts it to a Data statement with the corresponding type

func (*Data) Type

func (d *Data) Type() DataType

Type returns the type of a piece of data

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

const (
	STRING     DataType                       = 1 << iota // string
	INT                                                   // int
	FLOAT                                                 // float64
	ARRAY                                                 // []Data
	IDENTIFIER                                            // string
	NULL                                                  // nil
	VARIADIC                                              // Multiple args
	PARAMETER                                             // For PARAM statements
	NUMBER     = INT | FLOAT                              // interface{}
	ANY        = STRING | INT | FLOAT | ARRAY             // interface{}
)

func (DataType) IsEqual

func (a DataType) IsEqual(b DataType) bool

IsEqual compares data types using bit masks

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]

func (*FloorStmt) Type

func (f *FloorStmt) Type() DataType

Type gives the return type of a FLOOR statement (INT)

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

type FunctionType struct {
	Signature  []DataType
	Names      []string
	ReturnType DataType
}

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) End

func (i *IfBlock) End(kind string, _ []Statement, statements []Statement) bool

End parses the end of an IFB statement

func (*IfBlock) EndSignature

func (i *IfBlock) EndSignature() []DataType

EndSignature gets the ending signature of an IFB statement (blank)

func (*IfBlock) Keywords

func (i *IfBlock) Keywords() []string

Keywords gets the keywords of an IFB statement (ELSE and ENDIF)

func (*IfBlock) Type

func (i *IfBlock) Type() DataType

Type gets the type of an IFB statement (NULL)

type IfStmt

type IfStmt struct {
	*BasicStatement
	Condition Statement
	Body      Statement
	Else      Statement
}

IfStmt is the equivalent of [IF stmt.Condition stmt.Body stmt.Else]

func (*IfStmt) Type

func (i *IfStmt) Type() DataType

Type gives the return type of an IF statement (ANY or NULL)

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]

func (*IndexStmt) Type

func (i *IndexStmt) Type() DataType

Type gets the type of an INDEX statement (ANY)

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]

func (*MathStmt) Type

func (m *MathStmt) Type() DataType

Type gives the return type of a MATH statement (INT or FLOAT)

type Operator

type Operator int

Operator is an enum for all the B++ math and comparison operators

const (
	EQUAL          Operator = iota // =
	NOTEQUAL                       // !=
	GREATER                        // >
	LESS                           // <
	GREATEREQUAL                   // >=
	LESSEQUAL                      // <=
	ADDITION                       // +
	SUBTRACTION                    // -
	MULTIPLICATION                 // *
	DIVISION                       // /
	POWER                          // ^
)

func (Operator) String

func (o Operator) String() string

type ParamStmt

type ParamStmt struct {
	*BasicStatement
	Name string
	Kind DataType
}

ParamStmt is the equivalent of [PARAM stmt.Name stmt.Type]

func (*ParamStmt) Type

func (p *ParamStmt) Type() DataType

Type returns the type of a PARAM statement (PARAMETER)

type Pos

type Pos struct {
	Filename string
	Line     int
}

Pos defines a position in a B++ program, commonly used in debug prints

func NewPos

func NewPos(filename string, line int) *Pos

NewPos creates a new initialized Pos with the values supplied.

func (*Pos) String

func (p *Pos) String() string

String allows Pos to implement the Stringer interface

type Program

type Program struct {
	Statements []Statement
}

Program is the main program, containing the source AST

func Parse

func Parse(filename, code string) (*Program, error)

Parse parses B++ source code and returns a parsed program

func ParseFiles

func ParseFiles(mainname string, files map[string]string) (*Program, error)

ParseFiles parses multiple B++ source code files, accepting the name of the main file and a map of filename to source code.

func (*Program) End

func (p *Program) End(_ string, _ []Statement, stmts []Statement) bool

End is there to make a Program implement the Block interface

func (*Program) EndSignature

func (p *Program) EndSignature() []DataType

EndSignature is there to make a Program implement the Block interface

func (*Program) Keywords

func (p *Program) Keywords() []string

Keywords is there to make a Program implement the Block interface

func (*Program) Pos

func (p *Program) Pos() *Pos

Pos is there to make a Program implement the Statement interface

func (*Program) Type

func (p *Program) Type() DataType

Type is there to make a Program implement the Statement 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]

func (*RoundStmt) Type

func (r *RoundStmt) Type() DataType

Type gives the return type of a round statement (INT)

type Scope

type Scope struct {
	Block      Block
	Statements []Statement
	// contains filtered or unexported fields
}

Scope represents the data of a B++ scope

func NewScope

func NewScope(block Block) *Scope

NewScope creates a new initialized scope from a block

func (*Scope) HasKeyword

func (s *Scope) HasKeyword(keyword string) bool

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

type Statement interface {
	Pos() *Pos
	Type() DataType
}

Statement stores the data for everything in B++

func ParseArgs

func ParseArgs(args []string, pos *Pos) ([]Statement, error)

ParseArgs parses the source code of arguments to a function

func ParseStmt

func ParseStmt(line string, pos *Pos, scope ...*ScopeStack) (Statement, error)

ParseStmt parses a B++ statement's source code and returns the parsed statement

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]

func (*VarStmt) Type

func (v *VarStmt) Type() DataType

Type gives the return type of a VAR statement (ANY)

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)

Jump to

Keyboard shortcuts

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