Added more tests
This commit is contained in:
parent
dbf416d45b
commit
57ee65abd1
@ -132,7 +132,7 @@ This is what generates expressions from tokens.
|
||||
- [x] `import`
|
||||
- [x] `loop`
|
||||
- [x] `return`
|
||||
- [ ] `switch`
|
||||
- [x] `switch`
|
||||
|
||||
### Optimizations
|
||||
|
||||
|
@ -54,5 +54,9 @@ func EachInstruction(body token.List, call func(token.List) error) error {
|
||||
}
|
||||
}
|
||||
|
||||
if start != len(body) {
|
||||
return call(body[start:])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -63,7 +63,13 @@ func parseKeyword(tokens token.List, source []byte, nodes AST) (Node, error) {
|
||||
return nil, errors.New(errors.MissingBlockEnd, nil, tokens[len(tokens)-1].End())
|
||||
}
|
||||
|
||||
cases, err := parseSwitch(tokens[blockStart+1:blockEnd], source)
|
||||
body := tokens[blockStart+1 : blockEnd]
|
||||
|
||||
if len(body) == 0 {
|
||||
return nil, errors.New(errors.EmptySwitch, nil, tokens[0].Position)
|
||||
}
|
||||
|
||||
cases, err := parseSwitch(body, source)
|
||||
return &Switch{Cases: cases}, err
|
||||
|
||||
default:
|
||||
|
@ -1,6 +1,7 @@
|
||||
package errors
|
||||
|
||||
var (
|
||||
EmptySwitch = &Base{"Empty switch"}
|
||||
ExpectedIfBeforeElse = &Base{"Expected an 'if' block before 'else'"}
|
||||
InvalidNumber = &Base{"Invalid number"}
|
||||
InvalidExpression = &Base{"Invalid expression"}
|
||||
|
@ -1,5 +1,9 @@
|
||||
package token
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// List is a slice of tokens.
|
||||
type List []Token
|
||||
|
||||
@ -24,3 +28,14 @@ func (list List) LastIndexKind(kind Kind) int {
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
// Text returns the concatenated token text.
|
||||
func (list List) Text(source []byte) string {
|
||||
tmp := strings.Builder{}
|
||||
|
||||
for _, t := range list {
|
||||
tmp.WriteString(t.Text(source))
|
||||
}
|
||||
|
||||
return tmp.String()
|
||||
}
|
||||
|
3
tests/errors/EmptySwitch.q
Normal file
3
tests/errors/EmptySwitch.q
Normal file
@ -0,0 +1,3 @@
|
||||
main() {
|
||||
switch {}
|
||||
}
|
@ -14,6 +14,7 @@ var errs = []struct {
|
||||
File string
|
||||
ExpectedError error
|
||||
}{
|
||||
{"EmptySwitch.q", errors.EmptySwitch},
|
||||
{"ExpectedFunctionDefinition.q", errors.ExpectedFunctionDefinition},
|
||||
{"ExpectedFunctionName.q", errors.ExpectedFunctionName},
|
||||
{"ExpectedFunctionParameters.q", errors.ExpectedFunctionParameters},
|
||||
|
44
tests/programs/switch.q
Normal file
44
tests/programs/switch.q
Normal file
@ -0,0 +1,44 @@
|
||||
import sys
|
||||
|
||||
main() {
|
||||
correct := 0
|
||||
|
||||
switch {
|
||||
1 == 1 { correct += 1 }
|
||||
}
|
||||
|
||||
switch {
|
||||
1 == 1 { correct += 1 }
|
||||
_ { correct -= 1 }
|
||||
}
|
||||
|
||||
switch {
|
||||
0 == 1 { correct -= 1 }
|
||||
_ { correct += 1 }
|
||||
}
|
||||
|
||||
switch {
|
||||
0 == 1 { correct -= 1 }
|
||||
0 == 2 { correct -= 1 }
|
||||
_ { correct += 1 }
|
||||
}
|
||||
|
||||
switch {
|
||||
0 == 1 { correct -= 1 }
|
||||
0 == 2 { correct -= 1 }
|
||||
2 == 2 { correct += 1 }
|
||||
_ { correct -= 1 }
|
||||
}
|
||||
|
||||
switch {
|
||||
0 == 1 { correct -= 1 }
|
||||
0 == 2 { correct -= 1 }
|
||||
0 == 3 { correct -= 1 }
|
||||
}
|
||||
|
||||
if correct == 5 {
|
||||
return
|
||||
}
|
||||
|
||||
sys.exit(1)
|
||||
}
|
@ -53,6 +53,7 @@ var programs = []struct {
|
||||
{"branch-or", "", "", 0},
|
||||
{"branch-both", "", "", 0},
|
||||
{"jump-near", "", "", 0},
|
||||
{"switch", "", "", 0},
|
||||
{"loop", "", "", 0},
|
||||
{"loop-lifetime", "", "", 0},
|
||||
{"out-of-memory", "", "", 0},
|
||||
|
Loading…
Reference in New Issue
Block a user