From 8cdc7d321073c4c1584364c42fbdc5ba3accbb29 Mon Sep 17 00:00:00 2001 From: "Mr.UNIX" Date: Tue, 26 Mar 2024 21:28:50 +0100 Subject: [PATCH] feat: print variable values when entered in REPL shell --- src/utils/break_lines.cpp | 24 +++++++++++++++++------- tests/variables_test.cpp | 22 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/utils/break_lines.cpp b/src/utils/break_lines.cpp index 0ba64ba..ce3d4ab 100644 --- a/src/utils/break_lines.cpp +++ b/src/utils/break_lines.cpp @@ -7,16 +7,26 @@ std::vector break_lines(const std::string &input) { std::stack brackets_stack; for (auto c: input) { - current_line.push_back(c); - if (c == '(') { - brackets_stack.push(c); - } else if (c == ')') { - brackets_stack.pop(); - if (brackets_stack.empty()) { + if (c == ' ' && brackets_stack.empty()) { + if (!current_line.empty()) { result.push_back(current_line); - current_line = ""; + current_line.clear(); + } + } else { + current_line.push_back(c); + if (c == '(') { + brackets_stack.push(c); + } else if (c == ')') { + brackets_stack.pop(); + if (brackets_stack.empty()) { + result.push_back(current_line); + current_line.clear(); + } } } } + if (!current_line.empty()) { + result.push_back(current_line); + } return result; } \ No newline at end of file diff --git a/tests/variables_test.cpp b/tests/variables_test.cpp index 59d89f8..522223e 100644 --- a/tests/variables_test.cpp +++ b/tests/variables_test.cpp @@ -35,6 +35,26 @@ TEST(variables_test, ShouldDefineAVariableWithAnExpression) { EXPECT_EQ(actual_result, expected_result); } +TEST(variables_test, ShouldDisplayAVariableValueWhenCalled) { + const auto program = + "(define x 10)" + "x"; + + const auto expected_result = StackObject((double) 10); + const auto actual_result = run_program(program); + + EXPECT_EQ(actual_result, expected_result); +} + +TEST(variables_test, ShouldDisplayALiteralWhenItsCalled) { + const auto program = "10"; + + const auto expected_result = StackObject((double) 10); + const auto actual_result = run_program(program); + + EXPECT_EQ(actual_result, expected_result); +} + TEST(variables_test, ShouldThrowAnErrorOnUndefinedVariable) { const auto program = "(+ x 10)"; @@ -43,7 +63,7 @@ TEST(variables_test, ShouldThrowAnErrorOnUndefinedVariable) { try { run_program(program); - } catch (const SyntaxError& e) { + } catch (const SyntaxError &e) { errorIsThrown = true; errorMessage = e.message; }