Added match statements to the parser

Added match statements to the parser
This commit is contained in:
Sasha Koshka 2024-03-01 21:36:40 -05:00
parent fb7558576e
commit f94c562896
2 changed files with 44 additions and 1 deletions

View File

@ -76,7 +76,8 @@ func (this *Member) String () string {
// Case represents a match case.
type Case struct {
Declaration Declaration
Position errors.Position
Declaration *Declaration
Expression
}
func (this *Case) String () string {

View File

@ -116,6 +116,7 @@ func (this *treeParser) parseExpressionRootIdent () (entity.Expression, error) {
case "true", "false": return this.parseLiteralBoolean ()
case "nil": return this.parseLiteralNil()
case "if": return this.parseIfElse()
case "match": return this.parseMatch()
case "loop": return this.parseLoop()
default:
this.Next()
@ -563,6 +564,47 @@ func (this *treeParser) parseIfElse () (*entity.IfElse, error) {
return ifElse, nil
}
func (this *treeParser) parseMatch () (*entity.Match, error) {
err := this.ExpectValue(lexer.Ident, "match")
if err != nil { return nil, err }
match := &entity.Match {
Position: this.Pos(),
}
err = this.ExpectNextDesc(descriptionExpression, startTokensExpression...)
if err != nil { return nil, err }
match.Value, err = this.parseExpression()
if err != nil { return nil, err }
err = this.ExpectValue(lexer.Ident, "in")
if err != nil { return nil, err }
for this.Is(lexer.Symbol) && this.ValueIs("|") {
cas, err := this.parseCase()
if err != nil { return nil, err }
match.Cases = append(match.Cases, cas)
}
return match, nil
}
func (this *treeParser) parseCase () (*entity.Case, error) {
err := this.ExpectValue(lexer.Symbol, "|")
if err != nil { return nil, err }
cas := &entity.Case {
Position: this.Pos(),
}
this.Next()
cas.Declaration, err = this.parseDeclaration()
if err != nil { return nil, err }
this.Next()
cas.Expression, err = this.parseExpression()
if err != nil { return nil, err }
return cas, nil
}
func (this *treeParser) parseLoop () (*entity.Loop, error) {
err := this.ExpectValue(lexer.Ident, "loop")
if err != nil { return nil, err }