Parse default match cases

This commit is contained in:
Sasha Koshka 2024-03-25 01:53:40 -04:00
parent 1f04c3a593
commit e27fb9e79b
3 changed files with 26 additions and 4 deletions

View File

@ -70,6 +70,7 @@ expression, the parser follows this decision tree to determine what to parse:
| | 'nil' =LiteralNil
| | 'if' =IfElse
| | 'match' =Match
| | 'switch' =Switch
| | 'loop' =Loop
| | +Colon =Declaration
| | +DoubleColon =Call

View File

@ -580,18 +580,24 @@ func (this *treeParser) parseMatch () (*entity.Match, error) {
if err != nil { return nil, err }
for this.Is(lexer.Symbol) && this.ValueIs("|") {
cas, err := this.parseCase()
cas, err := this.parseMatchCase()
if err != nil { return nil, err }
match.Cases = append(match.Cases, cas)
}
if this.Is(lexer.Star) {
defaul, err := this.parseDefaultCase()
if err != nil { return nil, err }
match.Default = defaul
}
return match, nil
}
func (this *treeParser) parseCase () (*entity.Case, error) {
func (this *treeParser) parseMatchCase () (*entity.MatchCase, error) {
err := this.ExpectValue(lexer.Symbol, "|")
if err != nil { return nil, err }
cas := &entity.Case {
cas := &entity.MatchCase {
Pos: this.Pos(),
}
@ -604,6 +610,20 @@ func (this *treeParser) parseCase () (*entity.Case, error) {
return cas, nil
}
func (this *treeParser) parseDefaultCase () (*entity.DefaultCase, error) {
err := this.Expect(lexer.Star)
if err != nil { return nil, err }
cas := &entity.DefaultCase {
Pos: this.Pos(),
}
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 }

View File

@ -74,7 +74,7 @@ testString (test,
- [loop]:Int = {i:Int=0 if [< i 3] then return 1 loop {[print 3] if [> i 3] then break 0 i=[++ i]}}
- [for s:String] = for i:Index e:Byte in str if [> i 3] then [break e]
- [for s:String] = for e:Byte in s [break e]
- [matchToInt u:(| Int F64)]:Int = match u | u:Int u | u:F64 [~ Int u]`,
- [matchToInt u:(| Int F64)]:Int = match u | u:Int u | u:F64 [~ Int u] * 0`,
// input
`
[var] = sdfdf
@ -127,6 +127,7 @@ testString (test,
[matchToInt u:(| Int F64)]:Int = match u
| u:Int u
| u:F64 [~Int u]
* 0
`)
}