Parse pointers/slices

This commit is contained in:
Sasha Koshka 2024-02-07 00:22:58 -05:00
parent 6ad56ed4ee
commit 59e3185500

View File

@ -1,5 +1,6 @@
package parser package parser
import "fmt"
import "git.tebibyte.media/sashakoshka/fspl/lexer" import "git.tebibyte.media/sashakoshka/fspl/lexer"
import "git.tebibyte.media/sashakoshka/fspl/entity" import "git.tebibyte.media/sashakoshka/fspl/entity"
@ -13,11 +14,10 @@ var startTokensType = []lexer.TokenKind {
} }
func (this *Parser) parseType () (entity.Type, error) { func (this *Parser) parseType () (entity.Type, error) {
println(this.token.String())
err := this.expectDesc(descriptionType, startTokensType...) err := this.expectDesc(descriptionType, startTokensType...)
if err != nil { return nil, err } if err != nil { return nil, err }
switch this.token.Kind { switch this.kind() {
case lexer.Ident: case lexer.Ident:
ident := this.token.Value ident := this.token.Value
err := this.expectNext(lexer.DoubleColon) err := this.expectNext(lexer.DoubleColon)
@ -30,6 +30,8 @@ func (this *Parser) parseType () (entity.Type, error) {
return this.parseTypeNamedCore("") return this.parseTypeNamedCore("")
case lexer.Star: case lexer.Star:
return this.parseTypePointerOrSlice()
case lexer.Int: case lexer.Int:
case lexer.LParen: case lexer.LParen:
} }
@ -39,22 +41,42 @@ func (this *Parser) parseType () (entity.Type, error) {
func (this *Parser) parseTypeNamedCore (module string) (entity.Type, error) { func (this *Parser) parseTypeNamedCore (module string) (entity.Type, error) {
err := this.expect(lexer.TypeIdent) err := this.expect(lexer.TypeIdent)
if err != nil { return nil, err } if err != nil { return nil, err }
typeName := this.token.Value
this.next() defer this.next()
return &entity.TypeNamed { return &entity.TypeNamed {
Name: typeName, Position: this.pos(),
Module: module, Name: this.value(),
Module: module,
}, nil }, nil
} }
func (this *Parser) parseTypePointer () (entity.Type, error) { func (this *Parser) parseTypePointerOrSlice () (entity.Type, error) {
// TODO err := this.expectDesc("Pointer type or Slice type", lexer.Star)
return nil, nil if err != nil { return nil, err }
} start := this.pos()
func (this *Parser) parseTypeSlice () (entity.Type, error) { xpc := appendCopy(startTokensType, lexer.Colon)
// TODO fmt.Println(xpc)
return nil, nil err = this.expectNextDesc("Colon or Type", xpc...)
if err != nil { return nil, err }
if this.token.Is(lexer.Colon) {
this.next()
element, err := this.parseType()
if err != nil { return nil, err }
return &entity.TypeSlice {
Position: start.Union(this.pos()),
Element: element,
}, nil
} else {
referenced, err := this.parseType()
if err != nil { return nil, err }
return &entity.TypePointer {
Position: start.Union(this.pos()),
Referenced: referenced,
}, nil
}
} }
func (this *Parser) parseTypeArray () (entity.Type, error) { func (this *Parser) parseTypeArray () (entity.Type, error) {
@ -86,3 +108,4 @@ func (this *Parser) parseTypeFloat () (entity.Type, error) {
// TODO // TODO
return nil, nil return nil, nil
} }