-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.cpp
More file actions
166 lines (152 loc) · 5.11 KB
/
Copy pathbridge.cpp
File metadata and controls
166 lines (152 loc) · 5.11 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
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
#include "src/gals/Lexico.h"
#include "src/gals/Sintatico.h"
#include "src/gals/Semantico.h"
#include "src/gals/LexicalError.h"
#include "src/gals/SyntacticError.h"
#include "src/gals/SemanticError.h"
static std::string jsonEscape(const char* s) {
std::string out;
if (!s) return out;
for (const unsigned char* p = (const unsigned char*)s; *p; ++p) {
unsigned char c = *p;
switch (c) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[7];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
return out;
}
static std::string jsonEscape(const std::string& s) {
return jsonEscape(s.c_str());
}
static std::string boolToJson(bool value) {
return value ? "true" : "false";
}
static std::string symbolTableToJson(const std::vector<ExportedSymbol>& symbols) {
std::string json = "[";
bool first = true;
for (const auto& sym : symbols) {
if (!first) json += ",";
first = false;
json += "{";
json += "\"name\":\"" + jsonEscape(sym.name) + "\"";
json += ",\"type\":\"" + jsonEscape(sym.type) + "\"";
json += ",\"initialized\":" + boolToJson(sym.initialized);
json += ",\"used\":" + boolToJson(sym.used);
json += ",\"scope\":" + std::to_string(sym.scope);
json += ",\"isParameter\":" + boolToJson(sym.isParameter);
json += ",\"position\":" + std::to_string(sym.position);
json += ",\"line\":" + std::to_string(sym.line);
json += ",\"column\":" + std::to_string(sym.column);
json += ",\"isArray\":" + boolToJson(sym.isArray);
json += ",\"isFunction\":" + boolToJson(sym.isFunction);
json += ",\"isConstant\":" + boolToJson(sym.isConstant);
json += "}";
}
json += "]";
return json;
}
static std::string diagnosticsToJson(const std::vector<ExportedDiagnostic>& diagnostics) {
std::string json = "[";
bool first = true;
for (const auto& d : diagnostics) {
if (!first) json += ",";
first = false;
json += "{\"severity\":\"" + jsonEscape(d.severity) + "\",\"message\":\"" + jsonEscape(d.message) + "\",\"position\":" + std::to_string(d.position) + ",\"length\":" + std::to_string(d.length) + "}";
}
json += "]";
return json;
}
static std::string successResponse(const std::vector<ExportedSymbol>& symbols,
const std::vector<ExportedDiagnostic>& diagnostics,
const std::string& bipCode) {
std::string json = "{\"ok\":true";
json += ",\"symbolTable\":" + symbolTableToJson(symbols);
json += ",\"diagnostics\":" + diagnosticsToJson(diagnostics);
json += ",\"bipCode\":\"" + jsonEscape(bipCode) + "\"";
json += "}";
return json;
}
static std::string errorResponse(const char* kind, const char* message, int pos, int length) {
std::string json = "{\"ok\":false";
if (kind) {
json += ",\"kind\":\"";
json += jsonEscape(kind);
json += "\"";
}
if (message) {
json += ",\"message\":\"";
json += jsonEscape(message);
json += "\"";
}
json += ",\"pos\":" + std::to_string(pos);
json += ",\"length\":" + std::to_string(length <= 0 ? 1 : length);
json += ",\"symbolTable\":[]";
json += ",\"diagnostics\":[";
if (message) {
json += "{\"severity\":\"error\",\"message\":\"" + jsonEscape(message) + "\",\"position\":" + std::to_string(pos) + ",\"length\":" + std::to_string(length <= 0 ? 1 : length) + "}";
}
json += "]";
json += ",\"bipCode\":\"\"";
json += "}";
return json;
}
static char* duplicateString(const std::string& s) {
char* out = static_cast<char*>(std::malloc(s.size() + 1));
if (!out) {
return nullptr;
}
std::memcpy(out, s.c_str(), s.size() + 1);
return out;
}
extern "C" {
__attribute__((used))
char* uniscript_compile(const char* src) {
Lexico lex;
Sintatico sint;
Semantico sem;
sem.resetState();
sem.setSourceCode(src);
lex.setInput(src);
try {
sint.parse(&lex, &sem);
finalizeSemanticAnalysis();
auto symbols = snapshotSymbolTable();
auto diagnostics = snapshotDiagnostics();
auto bipCode = snapshotBipCode();
std::string json = successResponse(symbols, diagnostics, bipCode);
sem.resetState();
return duplicateString(json);
} catch (const LexicalError& e) {
sem.resetState();
return duplicateString(errorResponse("lexical", e.getMessage(), e.getPosition(), e.getLength()));
} catch (const SyntacticError& e) {
sem.resetState();
return duplicateString(errorResponse("syntactic", e.getMessage(), e.getPosition(), e.getLength()));
} catch (const SemanticError& e) {
sem.resetState();
return duplicateString(errorResponse("semantic", e.getMessage(), e.getPosition(), e.getLength()));
} catch (...) {
sem.resetState();
return duplicateString(errorResponse("unknown", "unknown error", -1, 1));
}
}
}