AST building #2
11
include/utils/json.hh
Normal file
11
include/utils/json.hh
Normal 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);
|
||||
}
|
@ -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) {
|
||||
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);
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include <cstdio>
|
||||
#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);
|
||||
|
47
src/utils/json.cc
Normal file
47
src/utils/json.cc
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user