mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 22:46:30 +00:00
add to_json.cpp
This commit is contained in:
parent
9c4ad4929c
commit
06a2a84117
14
runtime/example.slg
Normal file
14
runtime/example.slg
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
fn add(a, b) {
|
||||||
|
+ a b
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = 0;
|
||||||
|
let i = 0;
|
||||||
|
loop {
|
||||||
|
if >= i 10 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
result = add(result, 5);
|
||||||
|
i = + i 1;
|
||||||
|
}
|
||||||
|
|
41
runtime/to_json.cpp
Normal file
41
runtime/to_json.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include "vm.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace sliger;
|
||||||
|
|
||||||
|
void FlameGraphBuilder::to_json(json::Writer& writer) const
|
||||||
|
{
|
||||||
|
fg_node_to_json(writer, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FlameGraphBuilder::fg_node_to_json(
|
||||||
|
json::Writer& writer, size_t node_index) const
|
||||||
|
{
|
||||||
|
const auto& node = this->nodes[node_index];
|
||||||
|
writer << "{\"fn\":" << std::to_string(node.fn)
|
||||||
|
<< ",\"acc\":" << std::to_string(node.acc)
|
||||||
|
<< ",\"parent\":" << std::to_string(node.parent)
|
||||||
|
<< ",\"children:[\"";
|
||||||
|
auto first = true;
|
||||||
|
for (auto child_index : node.children) {
|
||||||
|
if (!first) {
|
||||||
|
writer << ",";
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
fg_node_to_json(writer, child_index);
|
||||||
|
}
|
||||||
|
writer << "]}";
|
||||||
|
}
|
||||||
|
|
||||||
|
void CodeCoverageBuilder::to_json(json::Writer& writer) const
|
||||||
|
{
|
||||||
|
writer << "[";
|
||||||
|
writer.add_comma_seperated(
|
||||||
|
this->entries, [&](json::Writer& writer, CCPosEntry entry) {
|
||||||
|
writer << "{\"index\":" << std::to_string(entry.pos.index)
|
||||||
|
<< ",\"line\":" << std::to_string(entry.pos.line)
|
||||||
|
<< ",\"col\":" << std::to_string(entry.pos.col)
|
||||||
|
<< ",\"covers\":" << std::to_string(entry.covers) << "}";
|
||||||
|
});
|
||||||
|
writer << "]";
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user