forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_to_json.cpp
More file actions
76 lines (66 loc) · 2.01 KB
/
ast_to_json.cpp
File metadata and controls
76 lines (66 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <lpython/ast_to_json.h>
using LFortran::AST::expr_t;
using LFortran::AST::Name_t;
using LFortran::AST::Num_t;
using LFortran::AST::BinOp_t;
using LFortran::AST::operatorType;
using LFortran::AST::BaseWalkVisitor;
namespace LFortran {
namespace {
std::string op2str(const operatorType type)
{
switch (type) {
case (operatorType::Add) : return "Add";
case (operatorType::Sub) : return "Sub";
case (operatorType::Mul) : return "Mul";
case (operatorType::Div) : return "Div";
case (operatorType::Pow) : return "Pow";
}
throw std::runtime_error("Unknown type");
}
}
class JSONVisitor : public BaseWalkVisitor<JSONVisitor>
{
rapidjson::Document d;
rapidjson::Value j;
rapidjson::Document::AllocatorType& al;
public:
JSONVisitor () : al{d.GetAllocator()} {}
void visit_BinOp(const BinOp_t &x) {
this->visit_expr(*x.m_left);
rapidjson::Value left = std::move(j);
this->visit_expr(*x.m_right);
rapidjson::Value right = std::move(j);
j.SetObject();
j.AddMember("type", "BinOp", al);
j.AddMember("op", op2str(x.m_op), al);
j.AddMember("left", left, al);
j.AddMember("right", right, al);
}
void visit_Name(const Name_t &x) {
j.SetObject();
j.AddMember("type", "Name", al);
j.AddMember("id", std::string(x.m_id, 1), al);
}
void visit_Num(const Num_t &x) {
j.SetObject();
j.AddMember("type", "Num", al);
j.AddMember("n", x.m_n, al);
}
std::string get_str() {
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
j.Accept(writer);
return strbuf.GetString();
}
};
std::string ast_to_json(LFortran::AST::ast_t &ast) {
JSONVisitor v;
v.visit_ast(ast);
return v.get_str();
}
}