From b06e1da52950070ce41c28d1c8d93307274f060b Mon Sep 17 00:00:00 2001 From: IlushaShurupov Date: Tue, 1 Aug 2023 18:57:36 +0300 Subject: [PATCH] Add Interpreter tests and improve Parser error logging A set of tests for the InterpreterObject has been added to enhance its reliability and maintainability. The tests check the essential functionalities such as its creation, execution, saving, loading, and destruction. Furthermore, the error logging for the Parser has been improved, now providing more precise location of detected errors. The changes improve the robustness and debugability of the code. --- Objects/private/compiler/function.cpp | 3 +- Objects/tests/Tests.cpp | 2 + Objects/tests/interpreter/TestInterpreter.cpp | 40 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 Objects/tests/interpreter/TestInterpreter.cpp diff --git a/Objects/private/compiler/function.cpp b/Objects/private/compiler/function.cpp index 6342b90..ce7e815 100644 --- a/Objects/private/compiler/function.cpp +++ b/Objects/private/compiler/function.cpp @@ -541,8 +541,9 @@ bool obj::BCgen::Compile(obj::MethodObject* method) { auto res = sParger->parse(script); if (res.err) { - auto loc = res.err->get_err_location(script.read()); // TODO : print parse error + auto loc = res.err->get_err_location(script.read()); + printf("Parser Error (%i,%i): \n", loc.head, loc.tail); // tp::gLogeer->write(tp::sfmt("Parser Error (%i,%i): \n", loc.head, loc.tail), true, tp::Logger::LogEntry::ERR); // tp::GLog->write(res.err->mDescr, true, tp::Logger::LogEntry::ERR); return false; diff --git a/Objects/tests/Tests.cpp b/Objects/tests/Tests.cpp index f3a24b4..640bf29 100644 --- a/Objects/tests/Tests.cpp +++ b/Objects/tests/Tests.cpp @@ -10,6 +10,7 @@ using namespace obj; void testCore(); void testPrimitives(); +void testInterpreter(); int main() { @@ -20,6 +21,7 @@ int main() { testCore(); testPrimitives(); + testInterpreter(); module.deinitialize(); } diff --git a/Objects/tests/interpreter/TestInterpreter.cpp b/Objects/tests/interpreter/TestInterpreter.cpp new file mode 100644 index 0000000..8cb70ca --- /dev/null +++ b/Objects/tests/interpreter/TestInterpreter.cpp @@ -0,0 +1,40 @@ + +#include "NewPlacement.hpp" + +#include "Testing.hpp" + +#include "core/object.h" +#include "primitives/methodobject.h" +#include "primitives/interpreterobject.h" +#include "primitives/linkobject.h" +#include "compiler/function.h" +#include "interpreter/interpreter.h" + +using namespace tp; +using namespace obj; + +TEST_DEF_STATIC(Simple) { + + auto method = NDO_CAST(MethodObject, NDO->create("method")); + auto interpreter = NDO_CAST(InterpreterObject, NDO->create("interpreter")); + + interpreter->getMember("target method")->setLink(method); + + method->mScript->mReadable->val = "<< 3;"; + method->compile(); + + interpreter->exec(); + + NDO->save(interpreter, "interp.o"); + + auto interpreterLoaded = NDO_CAST(InterpreterObject, NDO->load("interp.o")); + + interpreterLoaded->exec(); + + NDO->destroy(interpreter); + NDO->destroy(interpreterLoaded); +} + +TEST_DEF(Interpreter) { + testSimple(); +} \ No newline at end of file