Skip to content

Commit

Permalink
Fix issues with "-".
Browse files Browse the repository at this point in the history
Scan "-2" as two tokens: "-" and "2". This fixes issues with parsing "1 -2" etc.
  • Loading branch information
karnkaul committed Oct 6, 2023
1 parent 300c112 commit 7fd4457
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion kalcy/src/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ auto Scanner::next() -> Token {
if (at_end()) { return {}; }

auto out = Token{};
if (match_number(out)) { return out; }
if (match_single(out)) { return out; }
if (match_number(out)) { return out; }
if (match_identifier(out)) { return out; }

return {};
Expand Down
12 changes: 8 additions & 4 deletions tests/tests/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ADD_TEST(ParseUnary) {
}

ADD_TEST(ParseBinary) {
auto result = parse("3+2");
auto result = parse("3 -2");
ASSERT(result != nullptr);
ASSERT(std::holds_alternative<expr::Binary>(*result));
auto const& binary = std::get<expr::Binary>(*result);
Expand All @@ -45,7 +45,7 @@ ADD_TEST(ParseBinary) {
EXPECT(literal.token.type == Token::Type::eNumber);
EXPECT(literal.token.value == 3.0); // NOLINT

EXPECT(binary.op.type == Token::Type::ePlus);
EXPECT(binary.op.type == Token::Type::eMinus);

ASSERT(binary.right != nullptr);
ASSERT(std::holds_alternative<expr::Literal>(*binary.right));
Expand Down Expand Up @@ -77,8 +77,12 @@ ADD_TEST(ParseCall) {
ASSERT(std::holds_alternative<expr::Literal>(*pow_call.arguments[0]));
EXPECT(std::get<expr::Literal>(*pow_call.arguments[0]).token.value == 42); // NOLINT
ASSERT(pow_call.arguments[1] != nullptr);
ASSERT(std::holds_alternative<expr::Literal>(*pow_call.arguments[1]));
EXPECT(std::get<expr::Literal>(*pow_call.arguments[1]).token.value == -5); // NOLINT
ASSERT(std::holds_alternative<expr::Unary>(*pow_call.arguments[1]));
auto const& minus_5 = std::get<expr::Unary>(*pow_call.arguments[1]);
EXPECT(minus_5.op.type == Token::Type::eMinus);
ASSERT(minus_5.right != nullptr);
ASSERT(std::holds_alternative<expr::Literal>(*minus_5.right));
EXPECT(std::get<expr::Literal>(*minus_5.right).token.value == 5); // NOLINT
}

ADD_TEST(ParseErrorOnExtraneous) {
Expand Down
3 changes: 1 addition & 2 deletions tests/tests/test_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ ADD_TEST(ScanIdentifiers) {

ADD_TEST(ScanNumbers) {
EXPECT(match_number("3", 3));
EXPECT(match_number("-42", -42));
EXPECT(match_number("-4.2", -4.2)); // NOLINT
EXPECT(match_number("4.2", 4.2)); // NOLINT
}

ADD_TEST(ScanMultiple) {
Expand Down

0 comments on commit 7fd4457

Please sign in to comment.