implemented simple evaluator for program,
statements and integer literals, together with tests
This commit is contained in:
41
src/evaluator/evaluator.cpp
Normal file
41
src/evaluator/evaluator.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
#include "evaluator.hpp"
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "ast/expressions/boolean.hpp"
|
||||
#include "ast/expressions/integer.hpp"
|
||||
#include "ast/program.hpp"
|
||||
#include "ast/statements/expression.hpp"
|
||||
#include "object/boolean.hpp"
|
||||
#include "object/integers.hpp"
|
||||
#include "object/null.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace eval {
|
||||
static object::null* null = new object::null;
|
||||
|
||||
object::object* eval(std::vector<ast::statement*> statements) {
|
||||
object::object* ret;
|
||||
for (auto& stmt : statements)
|
||||
ret = eval(stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
object::object* eval(ast::node* node) {
|
||||
if (ast::integer_literal* integer =
|
||||
dynamic_cast<ast::integer_literal*>(node)) {
|
||||
return new object::integer(integer->value);
|
||||
} else if (ast::boolean_literal* boolean =
|
||||
dynamic_cast<ast::boolean_literal*>(node)) {
|
||||
return new object::boolean(boolean->value);
|
||||
} else if (ast::program* program = dynamic_cast<ast::program*>(node)) {
|
||||
return eval(program->statements);
|
||||
} else if (ast::expression_stmt* expression_stmt =
|
||||
dynamic_cast<ast::expression_stmt*>(node)) {
|
||||
return eval(expression_stmt->expression);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
} // namespace eval
|
8
src/evaluator/evaluator.hpp
Normal file
8
src/evaluator/evaluator.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "object/object.hpp"
|
||||
|
||||
namespace eval {
|
||||
object::object* eval(ast::node*);
|
||||
} // namespace eval
|
@@ -6,6 +6,8 @@ namespace object {
|
||||
bool value;
|
||||
::object::type type = type::BOOLEAN_OBJ;
|
||||
|
||||
explicit boolean(bool value): value(value) {}
|
||||
|
||||
std::string inspect();
|
||||
};
|
||||
} // namespace object
|
||||
|
@@ -6,6 +6,8 @@ namespace object {
|
||||
int value;
|
||||
::object::type type = type::INTEGER_OBJ;
|
||||
|
||||
explicit integer(int value): value(value) {}
|
||||
|
||||
std::string inspect();
|
||||
};
|
||||
} // namespace object
|
||||
|
20
test/evaluator/integer.cpp
Normal file
20
test/evaluator/integer.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "doctest.h"
|
||||
#include "utils.hpp"
|
||||
|
||||
TEST_SUITE("Eval: integers") {
|
||||
TEST_CASE_FIXTURE(
|
||||
test::utils::EvalFixture,
|
||||
"Simple integer expression statements"
|
||||
) {
|
||||
struct test_struct {
|
||||
std::string input;
|
||||
int expected;
|
||||
};
|
||||
struct test_struct tests[]{{"5", 5}, {"10", 10}};
|
||||
|
||||
for (auto& t : tests) {
|
||||
setup(t.input);
|
||||
test::utils::test_integer_object(result.get(), t.expected);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,3 +1,5 @@
|
||||
#include "evaluator/evaluator.hpp"
|
||||
#include "object/object.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
#include <doctest.h>
|
||||
@@ -30,4 +32,9 @@ namespace test::utils {
|
||||
"parse_program() returned a null pointer"
|
||||
);
|
||||
}
|
||||
|
||||
void EvalFixture::setup(std::string source) {
|
||||
ParserFixture::setup(source);
|
||||
result = std::unique_ptr<object::object>(eval::eval(program.get()));
|
||||
}
|
||||
} // namespace test::utils
|
||||
|
10
test/utils/test_eval.cpp
Normal file
10
test/utils/test_eval.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "object/integers.hpp"
|
||||
#include "object/object.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace test::utils {
|
||||
void test_integer_object(object::object* obj, int expected) {
|
||||
object::integer* i = cast<object::integer>(obj);
|
||||
CHECK(i->value == expected);
|
||||
}
|
||||
} // namespace test::utils
|
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "lexer/lexer.hpp"
|
||||
#include "object/object.hpp"
|
||||
#include "parser/parser.hpp"
|
||||
|
||||
#include <any>
|
||||
@@ -21,9 +22,15 @@ namespace test::utils {
|
||||
|
||||
ParserFixture() = default;
|
||||
|
||||
virtual void setup(std::string);
|
||||
};
|
||||
|
||||
struct EvalFixture : ParserFixture {
|
||||
std::unique_ptr<object::object> result;
|
||||
void setup(std::string);
|
||||
};
|
||||
|
||||
// parser tests
|
||||
void test_identifier(ast::expression*, std::string);
|
||||
void test_integer_literal(ast::expression*, int);
|
||||
void test_boolean_literal(ast::expression*, bool);
|
||||
@@ -32,6 +39,9 @@ namespace test::utils {
|
||||
test_infix_expression(ast::expression*, std::any, std::string, std::any);
|
||||
void test_failing_parsing(std::string, std::vector<token::type>, int = 0);
|
||||
|
||||
// eval tests
|
||||
void test_integer_object(object::object*, int);
|
||||
|
||||
namespace {
|
||||
|
||||
std::string demangle(const char* name) {
|
||||
@@ -73,4 +83,9 @@ namespace test::utils {
|
||||
return cast_impl<T, ast::error::error>(err);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* cast(object::object* err) {
|
||||
return cast_impl<T, object::object>(err);
|
||||
}
|
||||
|
||||
} // namespace test::utils
|
||||
|
Reference in New Issue
Block a user