Skip to content

Commit

Permalink
feat: print variable values when entered in REPL shell
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Mar 26, 2024
1 parent 6597eb7 commit 8cdc7d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/utils/break_lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ std::vector<std::string> break_lines(const std::string &input) {
std::stack<char> 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;
}
22 changes: 21 additions & 1 deletion tests/variables_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)";

Expand All @@ -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;
}
Expand Down

0 comments on commit 8cdc7d3

Please sign in to comment.