Skip to content

Commit

Permalink
Add initial interpreting logic for return statements
Browse files Browse the repository at this point in the history
  • Loading branch information
asynchroza committed Nov 15, 2023
1 parent ed4785d commit 7c75a90
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
12 changes: 11 additions & 1 deletion nulascript/eval/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ Storage* evaluateIf(Conditional* expression) {
return nilStorage;
}

bool checkBase(Node* passed, const std::type_info& expected) {
template <typename T>
bool checkBase(T* passed, const std::type_info& expected) {
return typeid(*passed) == expected;
}

Expand Down Expand Up @@ -143,6 +144,10 @@ Storage* evaluate(Node* node) {
} else if (checkBase(node, typeid(Conditional))) {
auto conditional = dynamic_cast<Conditional*>(node);
return evaluateIf(conditional);
} else if (checkBase(node, typeid(ReturnStatement))) {
auto statement = dynamic_cast<ReturnStatement*>(node);
auto result = evaluate(statement->returnValue);
return new ReturnStorage(result);
}

return nilStorage;
Expand All @@ -153,6 +158,11 @@ Storage* evaluateSequence(std::vector<Statement*> statements) {

for (auto statement : statements) {
result = evaluate(statement);

if (checkBase(result, typeid(ReturnStorage))) {
auto returnVal = dynamic_cast<ReturnStorage*>(result);
return returnVal->value;
}
}

return result;
Expand Down
6 changes: 6 additions & 0 deletions nulascript/storage/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ NilStorage::NilStorage() {}
StorageType NilStorage::getType() const { return StorageType::NIL; }

std::string NilStorage::evaluate() const { return "nil"; }

ReturnStorage::ReturnStorage(Storage* value) : value(value){};

StorageType ReturnStorage::getType() const { return StorageType::RETURN; }

std::string ReturnStorage::evaluate() const { return value->evaluate(); }
19 changes: 12 additions & 7 deletions nulascript/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <iostream>
#include <string>

enum class StorageType { INTEGER, BOOLEAN, NIL };
enum class StorageType { INTEGER, BOOLEAN, NIL, RETURN };

class Storage {
public:
Expand All @@ -18,10 +18,9 @@ class IntegerStorage : public Storage {
public:
int64_t value;

// Constructor
public:
IntegerStorage(int64_t value);

// Overrides
StorageType getType() const override;
std::string evaluate() const override;
};
Expand All @@ -30,20 +29,26 @@ class BooleanStorage : public Storage {
public:
bool value;

// Constructor
public:
BooleanStorage(bool value);

// Overrides
StorageType getType() const override;
std::string evaluate() const override;
};

class NilStorage : public Storage {
public:
// Constructor
NilStorage();
StorageType getType() const override;
std::string evaluate() const override;
};

class ReturnStorage : public Storage {
public:
Storage* value;

// Overrides
public:
ReturnStorage(Storage* value);
StorageType getType() const override;
std::string evaluate() const override;
};
Expand Down
14 changes: 14 additions & 0 deletions nulascript/tests/eval_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ TEST(EvalSuite, TestIfStatement) {
std::vector<Test> tests = {{"if (true) { 69 }", "69"},
{"if (false) { 69 }", "nil"}};

for (auto test : tests) {
auto result = getEvaluatedStorage(test.input);
ASSERT_EQ(result->evaluate(), test.expected);
}
}

TEST(EvalSuite, TestReturn) {
struct Test {
std::string input;
std::string expected;
};

std::vector<Test> tests = {{"return 69", "69"}};

for (auto test : tests) {
auto result = getEvaluatedStorage(test.input);
ASSERT_EQ(result->evaluate(), test.expected);
Expand Down

0 comments on commit 7c75a90

Please sign in to comment.