implemented simple evaluator for program,

statements and integer literals, together with
tests
This commit is contained in:
Karma Riuk
2025-07-19 15:03:38 +02:00
parent 7b01840f4d
commit 47379c6635
9 changed files with 105 additions and 0 deletions

View File

@@ -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
View 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

View File

@@ -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