33 class WriteStreamJSON :
public WriteStream {
42 if (
err.length() > 0) {
43 fatal(
"cannot get internal param names: %s",
err);
52 std::vector<Json> result;
59 for (
size_t col = 0; col <
arity; ++col) {
65 destination <<
params[
"relation"][
"params"][col].
dump() <<
": ";
79 using ValueTuple = std::pair<const std::string, const RamDomain>;
80 std::stack<std::variant<ValueTuple, std::string>> worklist;
81 worklist.push(std::make_pair(name, value));
85 while (!worklist.empty()) {
86 std::variant<ValueTuple, std::string> curr = worklist.top();
89 if (std::holds_alternative<std::string>(curr)) {
90 destination << std::get<std::string>(curr);
94 const std::string& currType = std::get<ValueTuple>(curr).first;
95 const RamDomain currValue = std::get<ValueTuple>(curr).second;
96 assert(currType.length() > 2 &&
"Invalid type length");
97 switch (currType[0]) {
100 case 'i': destination << currValue;
break;
101 case 'u': destination << (int)ramBitCast<RamUnsigned>(currValue);
break;
102 case 'f': destination << ramBitCast<RamFloat>(currValue);
break;
104 auto&& recordInfo =
types[
"records"][currType];
105 assert(!recordInfo.is_null() &&
"Missing record type information");
106 if (currValue == 0) {
107 destination <<
"null";
111 auto&& recordTypes = recordInfo[
"types"];
112 const size_t recordArity = recordInfo[
"arity"].long_value();
115 for (
auto i = (
long long)(recordArity - 1);
i >= 0; --
i) {
116 if (
i != (
long long)(recordArity - 1)) {
119 const std::string& recordType = recordTypes[
i].string_value();
121 worklist.push(std::make_pair(recordType, recordValue));
127 default:
fatal(
"unsupported type attribute: `%c`", currType[0]);
133 using ValueTuple = std::pair<const std::string, const RamDomain>;
134 std::stack<std::variant<ValueTuple, std::string>> worklist;
135 worklist.push(std::make_pair(name, value));
139 while (!worklist.empty()) {
140 std::variant<ValueTuple, std::string> curr = worklist.top();
143 if (std::holds_alternative<std::string>(curr)) {
144 destination << std::get<std::string>(curr);
148 const std::string& currType = std::get<ValueTuple>(curr).first;
149 const RamDomain currValue = std::get<ValueTuple>(curr).second;
150 const std::string& typeName = currType.substr(2);
151 assert(currType.length() > 2 &&
"Invalid type length");
152 switch (currType[0]) {
155 case 'i': destination << currValue;
break;
156 case 'u': destination << (int)ramBitCast<RamUnsigned>(currValue);
break;
157 case 'f': destination << ramBitCast<RamFloat>(currValue);
break;
159 auto&& recordInfo =
types[
"records"][currType];
160 assert(!recordInfo.is_null() &&
"Missing record type information");
161 if (currValue == 0) {
162 destination <<
"null";
166 auto&& recordTypes = recordInfo[
"types"];
167 const size_t recordArity = recordInfo[
"arity"].long_value();
170 for (
auto i = (
long long)(recordArity - 1);
i >= 0; --
i) {
171 if (
i != (
long long)(recordArity - 1)) {
174 const std::string& recordType = recordTypes[
i].string_value();
176 worklist.push(std::make_pair(recordType, recordValue));
179 auto&& recordParam =
params[
"records"][typeName][
"params"][
i];
180 assert(recordParam.is_string());
181 worklist.push(recordParam.dump());
187 default:
fatal(
"unsupported type attribute: `%c`", currType[0]);
193 class WriteFileJSON :
public WriteStreamJSON {
231 static std::string
getFileName(
const std::map<std::string, std::string>& rwOperation) {
232 auto name =
getOr(rwOperation,
"filename", rwOperation.at(
"name") +
".json");
233 if (name.front() !=
'/') {
234 name =
getOr(rwOperation,
"output-dir",
".") +
"/" + name;
240 class WriteCoutJSON :
public WriteStreamJSON {
256 std::cout <<
"null\n";
269 class WriteFileJSONFactory :
public WriteStreamFactory {
271 Own<WriteStream>
getWriter(
const std::map<std::string, std::string>& rwOperation,
272 const SymbolTable& symbolTable,
const RecordTable& recordTable)
override {
273 return mk<WriteFileJSON>(rwOperation, symbolTable, recordTable);
276 const std::string&
getName()
const override {
277 static const std::string name =
"jsonfile";
288 return mk<WriteCoutJSON>(rwOperation, symbolTable, recordTable);
291 const std::string&
getName()
const override {
292 static const std::string name =
"json";