Implemented switch statements
This commit is contained in:
37
src/build/ast/parseSwitch.go
Normal file
37
src/build/ast/parseSwitch.go
Normal file
@ -0,0 +1,37 @@
|
||||
package ast
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/build/expression"
|
||||
"git.akyoto.dev/cli/q/src/build/token"
|
||||
)
|
||||
|
||||
// parseSwitch generates the cases inside a switch statement.
|
||||
func parseSwitch(tokens token.List, source []byte) ([]Case, error) {
|
||||
var cases []Case
|
||||
|
||||
err := EachInstruction(tokens, func(caseTokens token.List) error {
|
||||
blockStart, _, body, err := block(caseTokens, source)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
conditionTokens := caseTokens[:blockStart]
|
||||
var condition *expression.Expression
|
||||
|
||||
if len(conditionTokens) == 1 && conditionTokens[0].Kind == token.Identifier && conditionTokens[0].Text(source) == "_" {
|
||||
condition = nil
|
||||
} else {
|
||||
condition = expression.Parse(conditionTokens)
|
||||
}
|
||||
|
||||
cases = append(cases, Case{
|
||||
Condition: condition,
|
||||
Body: body,
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return cases, err
|
||||
}
|
Reference in New Issue
Block a user