Added more tests

This commit is contained in:
Eduard Urbach 2024-08-03 23:05:09 +02:00
parent dbf416d45b
commit 57ee65abd1
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
9 changed files with 77 additions and 2 deletions

View File

@ -132,7 +132,7 @@ This is what generates expressions from tokens.
- [x] `import` - [x] `import`
- [x] `loop` - [x] `loop`
- [x] `return` - [x] `return`
- [ ] `switch` - [x] `switch`
### Optimizations ### Optimizations

View File

@ -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 return nil
} }

View File

@ -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()) 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 return &Switch{Cases: cases}, err
default: default:

View File

@ -1,6 +1,7 @@
package errors package errors
var ( var (
EmptySwitch = &Base{"Empty switch"}
ExpectedIfBeforeElse = &Base{"Expected an 'if' block before 'else'"} ExpectedIfBeforeElse = &Base{"Expected an 'if' block before 'else'"}
InvalidNumber = &Base{"Invalid number"} InvalidNumber = &Base{"Invalid number"}
InvalidExpression = &Base{"Invalid expression"} InvalidExpression = &Base{"Invalid expression"}

View File

@ -1,5 +1,9 @@
package token package token
import (
"strings"
)
// List is a slice of tokens. // List is a slice of tokens.
type List []Token type List []Token
@ -24,3 +28,14 @@ func (list List) LastIndexKind(kind Kind) int {
return -1 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()
}

View File

@ -0,0 +1,3 @@
main() {
switch {}
}

View File

@ -14,6 +14,7 @@ var errs = []struct {
File string File string
ExpectedError error ExpectedError error
}{ }{
{"EmptySwitch.q", errors.EmptySwitch},
{"ExpectedFunctionDefinition.q", errors.ExpectedFunctionDefinition}, {"ExpectedFunctionDefinition.q", errors.ExpectedFunctionDefinition},
{"ExpectedFunctionName.q", errors.ExpectedFunctionName}, {"ExpectedFunctionName.q", errors.ExpectedFunctionName},
{"ExpectedFunctionParameters.q", errors.ExpectedFunctionParameters}, {"ExpectedFunctionParameters.q", errors.ExpectedFunctionParameters},

44
tests/programs/switch.q Normal file
View 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)
}

View File

@ -53,6 +53,7 @@ var programs = []struct {
{"branch-or", "", "", 0}, {"branch-or", "", "", 0},
{"branch-both", "", "", 0}, {"branch-both", "", "", 0},
{"jump-near", "", "", 0}, {"jump-near", "", "", 0},
{"switch", "", "", 0},
{"loop", "", "", 0}, {"loop", "", "", 0},
{"loop-lifetime", "", "", 0}, {"loop-lifetime", "", "", 0},
{"out-of-memory", "", "", 0}, {"out-of-memory", "", "", 0},