Parse constant declarations

This commit is contained in:
Sasha Koshka 2024-04-06 13:40:03 -04:00
parent 7d3074ea2f
commit 386c39126d
2 changed files with 49 additions and 5 deletions

View File

@ -13,7 +13,7 @@ testString (test,
- StructArray: 31:24:340920:(. x:Int y:Int)
- String: *:U8
+ FileDescriptor: I32
| stdin 0
| stdin 0
| stdout 1
| stderr 2`,
// input

View File

@ -126,7 +126,14 @@ func (this *treeParser) parseFunctionCore (pos errors.Position, access entity.Ac
return function, nil
}
func (this *treeParser) parseMethodCore (pos errors.Position, access entity.Access, typeName string) (*entity.Method, error) {
func (this *treeParser) parseMethodCore (
pos errors.Position,
access entity.Access,
typeName string,
) (
*entity.Method,
error,
) {
function, err := this.parseFunctionCore(pos, access)
if err != nil { return nil, err }
return &entity.Method {
@ -139,14 +146,51 @@ func (this *treeParser) parseMethodCore (pos errors.Position, access entity.Acce
}, nil
}
func (this *treeParser) parseTypedefCore (pos errors.Position, access entity.Access, typeName string) (*entity.Typedef, error) {
func (this *treeParser) parseTypedefCore (
pos errors.Position,
access entity.Access,
typeName string,
) (
*entity.Typedef,
error,
) {
pos = pos.Union(this.Pos())
ty, err := this.parseType()
if err != nil { return nil, err }
return &entity.Typedef {
typedef := &entity.Typedef {
Pos: pos,
Acc: access,
Name: typeName,
Type: ty,
}, nil
}
for this.Is(lexer.Symbol) && this.ValueIs("|") {
constant, err := this.parseConstantDeclaration()
if err != nil { return nil, err }
typedef.Constants = append(typedef.Constants, constant)
}
return typedef, nil
}
func (this *treeParser) parseConstantDeclaration () (*entity.ConstantDeclaration, error) {
err := this.ExpectValue(lexer.Symbol, "|")
if err != nil { return nil, err }
declaration := &entity.ConstantDeclaration {
Pos: this.Pos(),
}
err = this.ExpectNext(lexer.Ident)
if err != nil { return nil, err }
declaration.Name = this.Value()
this.Next()
if this.Is(startTokensExpression...) {
value, err := this.parseExpression()
if err != nil { return nil, err }
declaration.Value = value
}
return declaration, nil
}