diff --git a/src/Script.cpp b/src/Script.cpp index f3d2af5..67a7fd3 100644 --- a/src/Script.cpp +++ b/src/Script.cpp @@ -115,6 +115,15 @@ Porygon::Script::Script(shared_ptr boundScript, _evaluator = new Evaluator(_scriptVariables, this -> GetScriptOptions()); } +std::string Porygon::Script::GetTreeString() { + if (_treeString.empty()){ + std::stringstream stream; + _boundScript->GetTreeString(stream, 0); + _treeString = stream.str(); + } + return _treeString; +} + extern "C" { Porygon::Script* CreateScript(char16_t * s, Porygon::ScriptOptions* options){ return Porygon::Script::Create(s, options); @@ -160,4 +169,16 @@ extern "C" { const char * GetResultError(Porygon::EvaluateResult * result){ return result->GetError(); } + + const size_t GetTreeStringLength(Porygon::Script* script){ + return script->GetTreeString().size(); + } + + void GetTreeString(Porygon::Script* script, char* buffer){ + auto s = script->GetTreeString(); + for (size_t i = 0; i < s.size(); i++){ + buffer[i] = s[i]; + } + } + } \ No newline at end of file diff --git a/src/Script.hpp b/src/Script.hpp index 402d0fd..f29a3ca 100644 --- a/src/Script.hpp +++ b/src/Script.hpp @@ -23,6 +23,7 @@ namespace Porygon{ shared_ptr _boundScript; shared_ptr _returnType = nullptr; ScriptOptions* _scriptOptions; + std::string _treeString; explicit Script(const u16string&, ScriptOptions*); Script(shared_ptr boundScript, shared_ptr diagnostics); @@ -65,6 +66,8 @@ namespace Porygon{ const map* GetScriptVariables(){ return _scriptVariables; } + + std::string GetTreeString(); }; } diff --git a/tests/integration/BoundTreeStringTests.cpp b/tests/integration/BoundTreeStringTests.cpp new file mode 100644 index 0000000..60f464d --- /dev/null +++ b/tests/integration/BoundTreeStringTests.cpp @@ -0,0 +1,20 @@ +#ifdef TESTS_BUILD +#include +#include "../src/Script.hpp" + +using namespace Porygon; + +TEST_CASE( "Basic script to bound tree string", "[integration]" ) { + Script* script = Script::Create("return 10 + 500"); + REQUIRE(!script->Diagnostics -> HasErrors()); + auto boundTreeString = script->GetTreeString(); + REQUIRE(boundTreeString == "BlockStatement\n" + "\tReturnStatement\n" + "\t\tBinaryExpression: addition (number)\n" + "\t\t\tLiteralInteger: 10 (number)\n" + "\t\t\tLiteralInteger: 500 (number)"); + delete script; +} + +#endif +