From 57ee65abd1ff21da000fc380af22c4c7f757b945 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 3 Aug 2024 23:05:09 +0200 Subject: [PATCH] Added more tests --- README.md | 2 +- src/build/ast/EachInstruction.go | 4 +++ src/build/ast/parseKeyword.go | 8 +++++- src/build/errors/CompileErrors.go | 1 + src/build/token/List.go | 15 +++++++++++ tests/errors/EmptySwitch.q | 3 +++ tests/errors_test.go | 1 + tests/programs/switch.q | 44 +++++++++++++++++++++++++++++++ tests/programs_test.go | 1 + 9 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 tests/errors/EmptySwitch.q create mode 100644 tests/programs/switch.q diff --git a/README.md b/README.md index 8dcacc6..8ba0c76 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ This is what generates expressions from tokens. - [x] `import` - [x] `loop` - [x] `return` -- [ ] `switch` +- [x] `switch` ### Optimizations diff --git a/src/build/ast/EachInstruction.go b/src/build/ast/EachInstruction.go index b4d9d8a..927cca6 100644 --- a/src/build/ast/EachInstruction.go +++ b/src/build/ast/EachInstruction.go @@ -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 } diff --git a/src/build/ast/parseKeyword.go b/src/build/ast/parseKeyword.go index a1363f9..0b6a423 100644 --- a/src/build/ast/parseKeyword.go +++ b/src/build/ast/parseKeyword.go @@ -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: diff --git a/src/build/errors/CompileErrors.go b/src/build/errors/CompileErrors.go index a214de3..c0e14cb 100644 --- a/src/build/errors/CompileErrors.go +++ b/src/build/errors/CompileErrors.go @@ -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"} diff --git a/src/build/token/List.go b/src/build/token/List.go index 7e57693..37ad0eb 100644 --- a/src/build/token/List.go +++ b/src/build/token/List.go @@ -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() +} diff --git a/tests/errors/EmptySwitch.q b/tests/errors/EmptySwitch.q new file mode 100644 index 0000000..866a186 --- /dev/null +++ b/tests/errors/EmptySwitch.q @@ -0,0 +1,3 @@ +main() { + switch {} +} \ No newline at end of file diff --git a/tests/errors_test.go b/tests/errors_test.go index 2b9ce40..2ec440e 100644 --- a/tests/errors_test.go +++ b/tests/errors_test.go @@ -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}, diff --git a/tests/programs/switch.q b/tests/programs/switch.q new file mode 100644 index 0000000..7c1e050 --- /dev/null +++ b/tests/programs/switch.q @@ -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) +} \ No newline at end of file diff --git a/tests/programs_test.go b/tests/programs_test.go index 4f2be2d..c381573 100644 --- a/tests/programs_test.go +++ b/tests/programs_test.go @@ -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},