fix type-to-string conv. + fix binary type check + add modulo + rework import (now include)
Some checks failed
/ test (push) Failing after 41s
Some checks failed
/ test (push) Failing after 41s
This commit is contained in:
parent
91baf28fdf
commit
575fd8e37a
7 changed files with 131 additions and 91 deletions
|
|
@ -95,39 +95,21 @@ func NewParser(source string, trace []string, tokens []Token) *Parser {
|
|||
}
|
||||
|
||||
type Program struct {
|
||||
Imports []Import
|
||||
Block *BlockNode
|
||||
Path string
|
||||
}
|
||||
|
||||
type Import struct {
|
||||
path string
|
||||
start Pos
|
||||
end Pos
|
||||
}
|
||||
|
||||
func (i Import) Bounds() (Pos, Pos) {
|
||||
return i.start, i.end
|
||||
Block *BlockNode
|
||||
Path string
|
||||
}
|
||||
|
||||
func (p *Program) String() string {
|
||||
builder := strings.Builder{}
|
||||
sb := strings.Builder{}
|
||||
|
||||
builder.WriteString("=== Imports ===\n")
|
||||
for _, i := range p.Imports {
|
||||
builder.WriteString(i.path)
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
builder.WriteString("===============\n")
|
||||
sb.WriteString(fmt.Sprintf("=v= program %s =v=\n", p.Path))
|
||||
sb.WriteString(p.Block.String())
|
||||
sb.WriteString(fmt.Sprintf("=^= program %s =^=\n", p.Path))
|
||||
|
||||
builder.WriteString(p.Block.String())
|
||||
|
||||
return builder.String()
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (p *Parser) Parse(path string) (*Program, error) {
|
||||
imports := make([]Import, 0)
|
||||
|
||||
// top level statements
|
||||
statements := make([]Node, 0)
|
||||
|
||||
|
|
@ -135,20 +117,6 @@ func (p *Parser) Parse(path string) (*Program, error) {
|
|||
p.advance()
|
||||
|
||||
for int(p.pos) < len(p.tokens) && p.curr.Type != TokenEOF {
|
||||
if p.accept(TokenImport) {
|
||||
start := p.prev.Start
|
||||
if err := p.expect(TokenString, "import requires a path/name to import"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
imports = append(imports, Import{
|
||||
p.prev.Lexeme[1 : len(p.prev.Lexeme)-1],
|
||||
start,
|
||||
p.prev.End,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
for p.accept(TokenNewLine) {
|
||||
}
|
||||
|
||||
|
|
@ -168,7 +136,6 @@ func (p *Parser) Parse(path string) (*Program, error) {
|
|||
}
|
||||
|
||||
return &Program{
|
||||
imports,
|
||||
&BlockNode{
|
||||
statements,
|
||||
0,
|
||||
|
|
@ -415,6 +382,25 @@ func (p *Parser) expression(mustBeBlock bool) (Node, error) {
|
|||
p.prev.End,
|
||||
}, nil
|
||||
|
||||
case TokenInclude:
|
||||
p.advance()
|
||||
start := p.prev.Start
|
||||
|
||||
if err := p.expect(TokenString, "import requires a path/name to include"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &IncludeNode{
|
||||
&StringNode{
|
||||
p.prev.Lexeme[1 : len(p.prev.Lexeme)-1],
|
||||
p.prev.Lexeme,
|
||||
p.prev.Start,
|
||||
p.prev.End,
|
||||
},
|
||||
start,
|
||||
p.prev.End,
|
||||
}, nil
|
||||
|
||||
default:
|
||||
s, err := p.binary()
|
||||
if err != nil {
|
||||
|
|
@ -448,7 +434,7 @@ func (p *Parser) expression(mustBeBlock bool) (Node, error) {
|
|||
|
||||
func isBinaryOperator(tokenType TokenKind) bool {
|
||||
switch tokenType {
|
||||
case TokenPlus, TokenMinus, TokenStar, TokenSlash, TokenPipe, TokenDoubleAmpersand, TokenDoublePipe, TokenEquals, TokenBangEquals, TokenLessThan, TokenLessThanOrEqual, TokenGreaterThan, TokenGreaterThanOrEqual:
|
||||
case TokenPlus, TokenMinus, TokenStar, TokenSlash, TokenPercent, TokenPipe, TokenDoubleAmpersand, TokenDoublePipe, TokenEquals, TokenBangEquals, TokenLessThan, TokenLessThanOrEqual, TokenGreaterThan, TokenGreaterThanOrEqual:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
|
@ -461,10 +447,12 @@ func binaryPrecedence(op TokenKind) int {
|
|||
return 1
|
||||
case TokenEquals, TokenBangEquals, TokenLessThan, TokenGreaterThan, TokenLessThanOrEqual, TokenGreaterThanOrEqual:
|
||||
return 2
|
||||
case TokenPlus, TokenMinus, TokenPipe:
|
||||
case TokenPercent:
|
||||
return 3
|
||||
case TokenStar, TokenSlash:
|
||||
case TokenPlus, TokenMinus, TokenPipe:
|
||||
return 5
|
||||
case TokenStar, TokenSlash:
|
||||
return 10
|
||||
default:
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
|
@ -480,6 +468,8 @@ func tokenToBinaryOperation(tokenType TokenKind) BinaryOperation {
|
|||
return BinaryMultiplication
|
||||
case TokenSlash:
|
||||
return BinaryDivision
|
||||
case TokenPercent:
|
||||
return BinaryModulo
|
||||
case TokenPipe:
|
||||
panic("unimplemented bitwise ops")
|
||||
case TokenDoubleAmpersand:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue