Parse switch expressions
This commit is contained in:
parent
e27fb9e79b
commit
d8f82d3646
|
@ -117,6 +117,7 @@ func (this *treeParser) parseExpressionRootIdent () (entity.Expression, error) {
|
|||
case "nil": return this.parseLiteralNil()
|
||||
case "if": return this.parseIfElse()
|
||||
case "match": return this.parseMatch()
|
||||
case "switch": return this.parseSwitch()
|
||||
case "loop": return this.parseLoop()
|
||||
case "for": return this.parseFor()
|
||||
default:
|
||||
|
@ -610,6 +611,49 @@ func (this *treeParser) parseMatchCase () (*entity.MatchCase, error) {
|
|||
return cas, nil
|
||||
}
|
||||
|
||||
func (this *treeParser) parseSwitch () (*entity.Switch, error) {
|
||||
err := this.ExpectValue(lexer.Ident, "switch")
|
||||
if err != nil { return nil, err }
|
||||
switc := &entity.Switch {
|
||||
Pos: this.Pos(),
|
||||
}
|
||||
|
||||
err = this.ExpectNextDesc(descriptionExpression, startTokensExpression...)
|
||||
if err != nil { return nil, err }
|
||||
switc.Value, err = this.parseExpression()
|
||||
if err != nil { return nil, err }
|
||||
|
||||
for this.Is(lexer.Symbol) && this.ValueIs("|") {
|
||||
cas, err := this.parseSwitchCase()
|
||||
if err != nil { return nil, err }
|
||||
switc.Cases = append(switc.Cases, cas)
|
||||
}
|
||||
|
||||
if this.Is(lexer.Star) {
|
||||
defaul, err := this.parseDefaultCase()
|
||||
if err != nil { return nil, err }
|
||||
switc.Default = defaul
|
||||
}
|
||||
|
||||
return switc, nil
|
||||
}
|
||||
|
||||
func (this *treeParser) parseSwitchCase () (*entity.SwitchCase, error) {
|
||||
err := this.ExpectValue(lexer.Symbol, "|")
|
||||
if err != nil { return nil, err }
|
||||
cas := &entity.SwitchCase {
|
||||
Pos: this.Pos(),
|
||||
}
|
||||
|
||||
this.Next()
|
||||
cas.Key, err = this.parseExpression()
|
||||
if err != nil { return nil, err }
|
||||
cas.Expression, err = this.parseExpression()
|
||||
if err != nil { return nil, err }
|
||||
|
||||
return cas, nil
|
||||
}
|
||||
|
||||
func (this *treeParser) parseDefaultCase () (*entity.DefaultCase, error) {
|
||||
err := this.Expect(lexer.Star)
|
||||
if err != nil { return nil, err }
|
||||
|
|
|
@ -74,7 +74,8 @@ 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] * 0`,
|
||||
- [matchToInt u:(| Int F64)]:Int = match u | u:Int u | u:F64 [~ Int u] * 0
|
||||
- [switch x:Int] = switch x | 0 5 | 1 4 | 2 3 * 0`,
|
||||
// input
|
||||
`
|
||||
[var] = sdfdf
|
||||
|
@ -128,6 +129,11 @@ testString (test,
|
|||
| u:Int u
|
||||
| u:F64 [~Int u]
|
||||
* 0
|
||||
[switch x:Int] = switch x
|
||||
| 0 5
|
||||
| 1 4
|
||||
| 2 3
|
||||
* 0
|
||||
`)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue