diff --git a/include/utils/json.hh b/include/utils/json.hh new file mode 100644 index 0000000..a67ac1a --- /dev/null +++ b/include/utils/json.hh @@ -0,0 +1,11 @@ +#pragma once + +#include +#include +#include +#include +#include "utils/data.hh" + +namespace ppc::data::json { + std::string stringify(const data::value_t &map); +} \ No newline at end of file diff --git a/src/compiler/treeifier/ast.cc b/src/compiler/treeifier/ast.cc index fadbca8..2b1a552 100644 --- a/src/compiler/treeifier/ast.cc +++ b/src/compiler/treeifier/ast.cc @@ -27,15 +27,10 @@ namespace ppc::comp::tree::ast { bool ast_ctx_t::parse(msg_stack_t &messages, std::vector &tokens, data::map_t &out) { ast_ctx_t ctx(messages, tokens); ctx.init(); - data::map_t res; size_t i = 0; try { - if (glob_parser(ctx, i, out)) { - out = res; - return true; - } - else return false; + return glob_parser(ctx, i, out); } catch (const message_t &msg) { messages.push(msg); diff --git a/src/main/main.cc b/src/main/main.cc index f9917ad..2787e8a 100644 --- a/src/main/main.cc +++ b/src/main/main.cc @@ -22,6 +22,7 @@ #include #include "utils/threading.hh" #include "utils/strings.hh" +#include "utils/json.hh" #include "compiler/treeifier/lexer.hh" #include "compiler/treeifier/tokenizer.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)); data::map_t ast; if (!ast::ast_ctx_t::parse(msg_stack, tokens, ast)) throw msg_stack.peek(); + for (auto tok : tokens) { if (tok.is_identifier()) std::cout << "Identifier: \t" << tok.identifier(); 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() }; std::cout << std::endl; } + + std::cout << std::endl << data::json::stringify(ast); } catch (const messages::message_t &msg) { msg_stack.push(msg); diff --git a/src/utils/json.cc b/src/utils/json.cc new file mode 100644 index 0000000..1311ec8 --- /dev/null +++ b/src/utils/json.cc @@ -0,0 +1,47 @@ +#include "utils/json.hh" +#include + +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(); + } +} \ No newline at end of file