forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
287 lines (253 loc) · 8.76 KB
/
utils.cpp
File metadata and controls
287 lines (253 loc) · 8.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#include <windows.h>
#endif
#include <fstream>
#include <bin/tpl/whereami/whereami.h>
#include <libasr/exception.h>
#include <lpython/utils.h>
#include <libasr/string_utils.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef _WIN32
#define stat _stat
#if !defined S_ISDIR
#define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
#endif
#endif
namespace LCompilers::LPython {
void get_executable_path(std::string &executable_path, int &dirname_length)
{
#ifdef HAVE_WHEREAMI
int length;
length = wai_getExecutablePath(NULL, 0, &dirname_length);
if (length > 0) {
std::string path(length+1, '\0');
wai_getExecutablePath(&path[0], length, &dirname_length);
executable_path = path;
if (executable_path[executable_path.size()-1] == '\0') {
executable_path = executable_path.substr(0,executable_path.size()-1);
}
} else {
throw LCompilersException("Cannot determine executable path.");
}
#else
executable_path = "src/bin/lpython.js";
dirname_length = 7;
#endif
}
std::string get_runtime_library_dir()
{
char *env_p = std::getenv("LFORTRAN_RUNTIME_LIBRARY_DIR");
if (env_p) return env_p;
std::string path;
int dirname_length;
get_executable_path(path, dirname_length);
std::string dirname = path.substr(0,dirname_length);
if ( endswith(dirname, "src/bin")
|| endswith(dirname, "src\\bin")
|| endswith(dirname, "SRC\\BIN")) {
// Development version
return dirname + "/../runtime";
} else if (endswith(dirname, "src/lpython/tests") ||
endswith(to_lower(dirname), "src\\lpython\\tests")) {
// CTest Tests
return dirname + "/../../runtime";
} else {
// Installed version
return dirname + "/../share/lpython/lib";
}
}
std::string get_runtime_library_header_dir()
{
char *env_p = std::getenv("LFORTRAN_RUNTIME_LIBRARY_HEADER_DIR");
if (env_p) return env_p;
// The header file is in src/libasr/runtime for development, but in impure
// in installed version
std::string path;
int dirname_length;
get_executable_path(path, dirname_length);
std::string dirname = path.substr(0,dirname_length);
if ( endswith(dirname, "src/bin")
|| endswith(dirname, "src\\bin")
|| endswith(dirname, "SRC\\BIN")) {
// Development version
return dirname + "/../libasr/runtime";
} else if (endswith(dirname, "src/lpython/tests") ||
endswith(to_lower(dirname), "src\\lpython\\tests")) {
// CTest Tests
return dirname + "/../../libasr/runtime";
} else {
// Installed version
return dirname + "/../share/lpython/lib/impure";
}
return path;
}
bool is_directory(std::string path) {
struct stat buffer;
if (stat(path.c_str(), &buffer) == 0) {
if (S_ISDIR(buffer.st_mode)) {
return true;
} else {
return false;
}
}
return false;
}
bool path_exists(std::string path) {
struct stat buffer;
if (stat(path.c_str(), &buffer) == 0) {
return true;
} else {
return false;
}
}
// Decodes the exit status code of the process (in Unix)
// See `WEXITSTATUS` for more information.
// https://stackoverflow.com/a/27117435/15913193
// https://linux.die.net/man/3/system
int32_t get_exit_status(int32_t err) {
return (((err) >> 8) & 0x000000ff);
}
std::string generate_visualize_html(std::string &astr_data_json) {
std::hash<std::string> hasher;
std::ofstream out;
std::string file_name = "visualize" + std::to_string(hasher(astr_data_json)) + ".html";
out.open(file_name);
out << R"(<!DOCTYPE html>
<html>
<head>
<title>LCompilers AST/R Visualization</title>
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-flow-renderer/10.3.17/umd/index.js"></script>
<script src="https://dagrejs.github.io/project/dagre/latest/dagre.min.js"></script>
<script> )";
out << "var astr_data = " << astr_data_json << "; </script>\n";
out << R"(</head>
<body style="margin: 0px;">
<script type="text/babel" data-type="module">
function TreeNode({ node }) {
if (node.literals.length === 0) return <p><b>{node.node}</b></p>;
return (
<div>
<p><b>{node.node}</b></p>
<div style={{ backgroundColor: "#FBBD23", padding: "2px" }}>
{
node.literals.map((val, idx) => <p style={{ margin: "0px", padding: "1px" }} key={idx}>{val[0]}: {val[1]}</p>)
}
</div>
</div>
);
}
const getLayoutedElements = (nodes, edges, direction = 'TB') => {
const nodeWidth = 180;
const isHorizontal = direction === 'LR';
const dagreGraph = new dagre.graphlib.Graph();
dagreGraph.setDefaultEdgeLabel(() => ({}));
dagreGraph.setGraph({ rankdir: direction });
nodes.forEach(node => dagreGraph.setNode(node.id, { width: nodeWidth, height: node.nodeHeight }));
edges.forEach(edge => dagreGraph.setEdge(edge.source, edge.target));
dagre.layout(dagreGraph);
nodes.forEach((node) => {
const nodeWithPosition = dagreGraph.node(node.id);
node.targetPosition = isHorizontal ? 'left' : 'top';
node.sourcePosition = isHorizontal ? 'right' : 'bottom';
// Shifting the dagre node position (anchor=center center) to the top left
// so it matches the React Flow node anchor point (top left).
node.position = {
x: nodeWithPosition.x - nodeWidth / 2,
y: nodeWithPosition.y - node.nodeHeight / 2,
};
return node;
});
return [nodes, edges];
};
class Graph {
constructor() {
this.nodes = [];
this.edges = [];
this.idx = 1;
return this;
}
createNode(cur_node) {
cur_node.idx = this.idx++;
cur_node.literals = [];
let obj = cur_node.fields;
for (let prop in obj) {
let neigh = obj[prop];
if (typeof neigh === 'object') {
if (neigh.hasOwnProperty("node")) {
this.createEdge(cur_node.idx, neigh, prop);
} else {
if (neigh.length > 0) {
for (let i in neigh) {
let arrayElement = neigh[i];
if (typeof arrayElement === 'object') {
if (arrayElement.hasOwnProperty("node")) {
this.createEdge(cur_node.idx, arrayElement, `${prop}[${i}]`);
} else {
console.log("ERROR: Unexpected 2D Array found");
}
} else {
cur_node.literals.push([`${prop}[${i}]`, `${arrayElement}`]);
}
}
} else {
// 0 length array, show as literal
cur_node.literals.push([prop, "[]"]);
}
}
} else {
cur_node.literals.push([prop, `${neigh}`]);
}
}
this.nodes.push({ id: `${cur_node.idx}`, data: { label: <TreeNode node={cur_node} /> }, nodeHeight: 70 + 20 * (cur_node.literals.length) });
}
createEdge(parent_idx, cur_node, edge_label) {
this.edges.push({
id: `${parent_idx}-${this.idx}`,
source: `${parent_idx}`,
target: `${this.idx}`,
label: edge_label,
labelStyle: { fontWeight: 700 },
labelBgPadding: [8, 4],
labelBgStyle: { fill: '#FBBD23' },
});
this.createNode(cur_node);
}
}
function Flow({ nodes, edges }) {
return (
<div style={{ height: '100vh' }}>
<ReactFlow.default
defaultNodes={nodes}
defaultEdges={edges}
style={{ backgroundColor: '#e5e7eb' }}
>
<ReactFlow.Background />
<ReactFlow.Controls />
<ReactFlow.MiniMap />
</ReactFlow.default>
</div>
);
}
function MyApp() {
var g = new Graph();
g.createNode(astr_data);
var [layoutedNodes, layoutedEdges] = getLayoutedElements(g.nodes, g.edges);
return (<Flow nodes={layoutedNodes} edges={layoutedEdges} />);
}
ReactDOM.render(<MyApp />, document.body);
</script>
</body>
</html>)";
return file_name;
}
}