feat: add json stringification

This commit is contained in:
TopchetoEU 2022-10-09 14:43:28 +03:00
parent ee6c29bb7d
commit cca9bc2e07
4 changed files with 63 additions and 6 deletions

11
include/utils/json.hh Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <string>
#include <memory>
#include <vector>
#include <unordered_map>
#include "utils/data.hh"
namespace ppc::data::json {
std::string stringify(const data::value_t &map);
}

View File

@ -27,15 +27,10 @@ namespace ppc::comp::tree::ast {
bool ast_ctx_t::parse(msg_stack_t &messages, std::vector<token_t> &tokens, data::map_t &out) { bool ast_ctx_t::parse(msg_stack_t &messages, std::vector<token_t> &tokens, data::map_t &out) {
ast_ctx_t ctx(messages, tokens); ast_ctx_t ctx(messages, tokens);
ctx.init(); ctx.init();
data::map_t res;
size_t i = 0; size_t i = 0;
try { try {
if (glob_parser(ctx, i, out)) { return glob_parser(ctx, i, out);
out = res;
return true;
}
else return false;
} }
catch (const message_t &msg) { catch (const message_t &msg) {
messages.push(msg); messages.push(msg);

View File

@ -22,6 +22,7 @@
#include <cstdio> #include <cstdio>
#include "utils/threading.hh" #include "utils/threading.hh"
#include "utils/strings.hh" #include "utils/strings.hh"
#include "utils/json.hh"
#include "compiler/treeifier/lexer.hh" #include "compiler/treeifier/lexer.hh"
#include "compiler/treeifier/tokenizer.hh" #include "compiler/treeifier/tokenizer.hh"
#include "compiler/treeifier/ast.hh" #include "compiler/treeifier/ast.hh"
@ -156,6 +157,7 @@ int main(int argc, const char *argv[]) {
auto tokens = token_t::parse_many(msg_stack, lex::token_t::parse_file(msg_stack, file, f)); auto tokens = token_t::parse_many(msg_stack, lex::token_t::parse_file(msg_stack, file, f));
data::map_t ast; data::map_t ast;
if (!ast::ast_ctx_t::parse(msg_stack, tokens, ast)) throw msg_stack.peek(); if (!ast::ast_ctx_t::parse(msg_stack, tokens, ast)) throw msg_stack.peek();
for (auto tok : tokens) { for (auto tok : tokens) {
if (tok.is_identifier()) std::cout << "Identifier: \t" << tok.identifier(); if (tok.is_identifier()) std::cout << "Identifier: \t" << tok.identifier();
if (tok.is_operator()) std::cout << "Operator: \t" << operator_stringify(tok._operator()); if (tok.is_operator()) std::cout << "Operator: \t" << operator_stringify(tok._operator());
@ -165,6 +167,8 @@ int main(int argc, const char *argv[]) {
if (tok.is_string_lit()) std::cout << "String: \t" << std::string { tok.string_lit().begin(), tok.string_lit().end() }; if (tok.is_string_lit()) std::cout << "String: \t" << std::string { tok.string_lit().begin(), tok.string_lit().end() };
std::cout << std::endl; std::cout << std::endl;
} }
std::cout << std::endl << data::json::stringify(ast);
} }
catch (const messages::message_t &msg) { catch (const messages::message_t &msg) {
msg_stack.push(msg); msg_stack.push(msg);

47
src/utils/json.cc Normal file
View File

@ -0,0 +1,47 @@
#include "utils/json.hh"
#include <sstream>
namespace ppc::data::json {
std::string stringify(const data::value_t &val) {
std::stringstream out;
bool first = true;
if (val.is_array()) {
out << '[';
for (const auto &el : val.array()) {
if (!first) out << ',';
first = false;
out << stringify(el);
}
out << ']';
}
else if (val.is_map()) {
out << '{';
for (const auto &el : val.map()) {
if (!first) out << ',';
first = false;
out << '"' << el.first << '"' << ':' << stringify(el.second);
}
out << '}';
}
else if (val.is_bool()) {
if (val.boolean()) out << "true";
else out << "false";
}
else if (val.is_null()) {
out << "null";
}
else if (val.is_number()) {
out << val.number();
}
else if (val.is_string()) {
out << '"' << val.string() << '"';
}
return out.str();
}
}