Parse namespaced function calls

This commit is contained in:
Sasha Koshka 2024-02-08 03:28:09 -05:00
parent 823cb44406
commit 8ec3649637

View File

@ -13,6 +13,7 @@ import "git.tebibyte.media/sashakoshka/fspl/entity"
// | | 'if' =IfElse // | | 'if' =IfElse
// | | 'loop' =Loop // | | 'loop' =Loop
// | | +Colon =Declaration // | | +Colon =Declaration
// | | +DoubleColon =Call
// | // |
// | +LParen X // | +LParen X
// | | +Star =LiteralArray // | | +Star =LiteralArray
@ -145,13 +146,33 @@ func (this *Parser) parseExpressionRootIdent () (entity.Expression, error) {
err := this.expect(lexer.Ident) err := this.expect(lexer.Ident)
if err != nil { return nil, err } if err != nil { return nil, err }
name := this.value() name := this.value()
pos := this.pos()
switch name { switch name {
case "true", "false": return this.parseLiteralBoolean () case "true", "false": return this.parseLiteralBoolean ()
case "nil": return this.parseLiteralNil() case "nil": return this.parseLiteralNil()
case "if": return this.parseIfElse() case "if": return this.parseIfElse()
case "loop": return this.parseLoop() case "loop": return this.parseLoop()
default: return this.parseVariableOrDeclaration() default:
println(this.token.String())
this.next()
switch this.kind() {
case lexer.Colon:
// Colon: declaration
return this.parseDeclarationCore(pos, name)
case lexer.DoubleColon:
// DoubleColon: call
err := this.expectNext(lexer.LBracket)
if err != nil { return nil, err }
this.next()
return this.parseCallCore(pos, name)
default:
// *: variable
return &entity.Variable {
Position: pos,
Name: name,
}, nil
}
} }
panic(this.bug()) panic(this.bug())
} }
@ -192,7 +213,7 @@ func (this *Parser) parseExpressionRootLBracketIdent (pos errors.Position) (enti
switch name { switch name {
case "break", "return": return this.parseReturnOrBreakCore(pos) case "break", "return": return this.parseReturnOrBreakCore(pos)
default: return this.parseCallCore(pos) default: return this.parseCallCore(pos, "")
} }
panic(this.bug()) panic(this.bug())
} }
@ -254,11 +275,12 @@ func (this *Parser) parseExpressionRootLBracketSymbol (pos errors.Position) (ent
panic(this.bug()) panic(this.bug())
} }
func (this *Parser) parseCallCore (pos errors.Position) (*entity.Call, error) { func (this *Parser) parseCallCore (pos errors.Position, module string) (*entity.Call, error) {
err := this.expect(lexer.Ident) err := this.expect(lexer.Ident)
if err != nil { return nil, err } if err != nil { return nil, err }
call := &entity.Call { call := &entity.Call {
Position: pos, Position: pos,
Module: module,
Name: this.value(), Name: this.value(),
} }
this.next() this.next()
@ -541,23 +563,6 @@ func (this *Parser) parseDeclarationCore (pos errors.Position, name string) (*en
}, nil }, nil
} }
func (this *Parser) parseVariableOrDeclaration () (entity.Expression, error) {
err := this.expectDesc("variable or declaration", lexer.Ident)
if err != nil { return nil, err }
variable := &entity.Variable {
Position: this.pos(),
Name: this.value(),
}
this.next()
if this.token.Is(lexer.Colon) {
return this.parseDeclarationCore(variable.Position, variable.Name)
} else {
return variable, nil
}
}
func (this *Parser) parseIfElse () (*entity.IfElse, error) { func (this *Parser) parseIfElse () (*entity.IfElse, error) {
err := this.expectValue(lexer.Ident, "if") err := this.expectValue(lexer.Ident, "if")
if err != nil { return nil, err } if err != nil { return nil, err }