Skip to content

Commit

Permalink
feat: format the stack frame' labels in a nicer way
Browse files Browse the repository at this point in the history
  • Loading branch information
Dich0tomy authored and PoetaKodu committed Sep 17, 2022
1 parent cf011c2 commit 54aecf2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
10 changes: 10 additions & 0 deletions VM/include/RigCVM/DevServer/Utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include RIGCVM_PCH

namespace rigc::vm
{

auto formatStackFrameLabel(ParserNode const&) -> std::string;

}
72 changes: 72 additions & 0 deletions VM/src/DevServer/Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include RIGCVM_PCH

#include <RigCVM/VM.hpp>
#include <RigCVM/DevServer/Utils.hpp>

#include <numeric>
#include <functional>

namespace {

auto formatFunctionSignature(rigc::ParserNode const& node) {
using namespace rigc;
using namespace rigc::vm;

auto const* const name = findElem<rigc::Name>(node);
auto const* const params = findElem<rigc::FunctionParams>(node);
auto const* const returnType = findElem<rigc::ExplicitReturnType>(node);

// name(arg1: type, arg2: type): return_type
return name->string()
+ (params ? params->string() : "()")
+ (returnType ? ": " : "")
+ (returnType ? returnType->string() : "");
}

auto stripWhitespace(std::string& str) {
auto const prefixIt = rg::find_if(str, [](char c) { return std::isalpha(c); });

auto const begin = rg::begin(str);
auto const end = begin + (prefixIt - begin);

str.erase(begin, end);
}

auto formatCodeBlock(rigc::ParserNode const& node) {
using namespace std::string_view_literals;

auto static constexpr maxLabelNameLen = 20;
auto static constexpr overflowIndicator = "..."sv;
auto static constexpr overflowIndicatorLen = overflowIndicator.length();
auto static constexpr maxAllowedLen = maxLabelNameLen - overflowIndicatorLen;

auto nodeString = node.string();

if (node.is_type<rigc::CodeBlock>()) {
stripWhitespace(nodeString);
}

if(nodeString.length() > maxLabelNameLen) {
return
nodeString.substr(0, maxAllowedLen)
+ std::string(overflowIndicator);
} else {
return nodeString;
}
}

}

namespace rigc::vm {

auto formatStackFrameLabel(ParserNode const& node) -> std::string {
auto const isFunctionDefinition = node.is_type<rigc::FunctionDefinition>();

if(isFunctionDefinition) {
return formatFunctionSignature(node);
} else {
return formatCodeBlock(node);
}
}

}
3 changes: 2 additions & 1 deletion VM/src/StackFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <RigCVM/StackFrame.hpp>
#include <RigCVM/VM.hpp>
#include <RigCVM/DevServer/Utils.hpp>

namespace rigc::vm
{
Expand All @@ -11,7 +12,7 @@ StackFramePusher::StackFramePusher(Instance& vm_, ParserNode const& stmt_)
: vm(vm_)
{
#if DEBUG
vm.pushStackFrameOf(&stmt_, fmt::format("{} -> {}", stmt_.type, stmt_.string()));
vm.pushStackFrameOf(&stmt_, formatStackFrameLabel(stmt_));
#else
vm.pushStackFrameOf(&stmt_);
#endif
Expand Down
3 changes: 2 additions & 1 deletion VM/src/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <RigCVM/TypeSystem/ArrayType.hpp>
#include <RigCVM/TypeSystem/FuncType.hpp>
#include <RigCVM/DevServer/Messaging.hpp>
#include <RigCVM/DevServer/Utils.hpp>

#include <RigCVM/ErrorHandling/Exceptions.hpp>

Expand Down Expand Up @@ -289,7 +290,7 @@ R"msg(


#if DEBUG
auto& fnScope = this->pushStackFrameOf(func_.addr(), func_.runtimeImpl().node->string());
auto& fnScope = this->pushStackFrameOf(func_.addr(), formatStackFrameLabel(*func_.runtimeImpl().node));
#else
auto& fnScope = this->pushStackFrameOf(func_.addr());
#endif
Expand Down

0 comments on commit 54aecf2

Please sign in to comment.