forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ast_to_json.cpp
More file actions
78 lines (69 loc) · 1.76 KB
/
test_ast_to_json.cpp
File metadata and controls
78 lines (69 loc) · 1.76 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
77
78
#include <tests/doctest.h>
#include <iostream>
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/document.h>
#include <lpython/ast_to_json.h>
#include <lpython/parser/parser.h>
using namespace rapidjson;
using LFortran::TRY;
using LFortran::parse;
TEST_CASE("Check ast_to_json()") {
Allocator al(4*1024);
std::string s, r;
LFortran::AST::ast_t* result;
rapidjson::Document d1, d2;
LFortran::diag::Diagnostics diagnostics;
result = TRY(parse(al, "2*x", diagnostics))->m_items[0];
s = LFortran::ast_to_json(*result);
std::cout << s << std::endl;
r = R"(
{
"left": {
"n": 2,
"type": "Num"
},
"op": "Mul",
"right": {
"id": "x",
"type": "Name"
},
"type": "BinOp"
}
)";
d1.Parse(s);
d2.Parse(r);
CHECK(d1 == d2);
result = TRY(parse(al, "(2*x)", diagnostics))->m_items[0];
s = LFortran::ast_to_json(*result);
std::cout << s << std::endl;
d1.Parse(s);
CHECK(d1 == d2);
result = TRY(parse(al, "2*x**y", diagnostics))->m_items[0];
s = LFortran::ast_to_json(*result);
std::cout << s << std::endl;
r = R"(
{
"left": {
"n": 2,
"type": "Num"
},
"op": "Mul",
"right": {
"left": {
"id": "x",
"type": "Name"
},
"op": "Pow",
"right": {
"id": "y",
"type": "Name"
},
"type": "BinOp"
},
"type": "BinOp"
}
)";
d1.Parse(s);
d2.Parse(r);
CHECK(d1 == d2);
}