valeri/src/writer.cpp

168 lines
4 KiB
C++
Raw Normal View History

2024-07-28 16:54:04 +00:00
#include "writer.hpp"
Result<String> Writer::write_one(Value& obj) {
switch (obj.tag()) {
case Tag::Nil:
return write_nil(*obj.to<Nil>());
case Tag::Int64:
return write_int64(*obj.to<Int64>());
case Tag::Float:
return write_float(*obj.to<Float>());
case Tag::Bool:
return write_bool(*obj.to<Bool>());
case Tag::ByteArray:
return write_bytearray(*obj.to<ByteArray>());
case Tag::String:
return write_string(*obj.to<String>());
case Tag::Symbol:
return write_symbol(*obj.to<Symbol>());
case Tag::Syntax:
return write_syntax(*obj.to<Syntax>());
case Tag::Pair:
return write_pair(*obj.to<Pair>());
};
return String();
}
Result<String> Writer::write_int64(Int64& val) {
char tmp[32];
2024-07-28 23:14:30 +00:00
sprintf(tmp, "%lu", val.value());
2024-07-28 16:54:04 +00:00
size_t len = strlen(tmp);
return String::create(_arena, tmp);
}
bool needs_dot(char* str) {
bool has_e = 0;
bool has_dot = 0;
for (char* c = str; *c != 0; c++) {
if (*c == 'e' || *c == 'E') has_e = true;
if (*c == '.') has_dot = true;
}
return !has_e && !has_dot;
}
Result<String> Writer::write_float(Float& val) {
char tmp[32];
sprintf(tmp, "%g", val.value());
size_t len = strlen(tmp);
if (needs_dot(tmp)) {
tmp[len] = '.';
tmp[len + 1] = '0';
tmp[len + 2] = 0;
len += 2;
}
return String::create(_arena, tmp);
}
Result<String> Writer::write_nil(Nil& val) {
return String::create(_arena, "nil");
}
Result<String> Writer::write_bool(Bool& val) {
if (val.value()) return String::create(_arena, "true");
return String::create(_arena, "false");
}
Result<String> Writer::write_bytearray(ByteArray& val) {
return ErrorCode::NotImplemented;
}
Result<String> Writer::write_string(String& val) {
String res = TRY(String::create(_arena, "\""));
// TODO: optimize this
for (uint64_t i = 0; i < val.size(); i++) {
char32_t c = TRY(val[i]);
const char* replace = 0;
switch (c) {
case '\r':
replace = "\\r";
break;
case '\n':
replace = "\\n";
break;
case '\t':
replace = "\\t";
break;
case '\\':
replace = "\\\\";
break;
case '"':
replace = "\\\"";
break;
default:
break;
};
if (replace != 0) {
res = TRY(res.concat(_arena, replace));
} else {
res = TRY(res.concat(_arena, &c, 1));
}
}
res = TRY(res.concat(_arena, "\""));
return res;
}
int is_valid_symbol_char(char32_t codepoint) {
return ('a' <= codepoint && codepoint <= 'z') ||
('A' <= codepoint && codepoint <= 'Z') ||
('0' <= codepoint && codepoint <= '9') || codepoint == '!' ||
codepoint == '$' || codepoint == '%' || codepoint == '&' ||
codepoint == '*' || codepoint == '+' || codepoint == '-' ||
codepoint == '.' || codepoint == '/' || codepoint == ':' ||
codepoint == '<' || codepoint == '=' || codepoint == '>' ||
codepoint == '?' || codepoint == '@' || codepoint == '^' ||
codepoint == '_' || codepoint == '~';
}
Result<String> Writer::write_symbol(Symbol& val) {
String res = TRY(String::create(_arena, ""));
// TODO: optimize this
for (uint64_t i = 0; i < val.size(); i++) {
char32_t c = TRY(val[i]);
if (!is_valid_symbol_char(c)) return ErrorCode::InvalidSymbol;
res = TRY(res.concat(_arena, &c, 1));
}
return res;
}
Result<String> Writer::write_syntax(Syntax& val) {
return ErrorCode::NotImplemented;
}
Result<String> Writer::write_pair(Pair& val) {
String res = TRY(String::create(_arena, "("));
Value cur = TRY(val.copy(_arena));
bool is_first = true;
while (!cur.is<Nil>()) {
if (!cur.is<Pair>()) return ErrorCode::MalformedList;
Pair& pair = *cur.to<Pair>();
Value first = TRY(pair.first(_arena));
if (!is_first) res = TRY(res.concat(_arena, " "));
String first_str = TRY(write_one(first));
res = TRY(res.concat(_arena, first_str));
cur = TRY(pair.rest(_arena));
2024-07-28 23:14:30 +00:00
is_first = false;
2024-07-28 16:54:04 +00:00
}
res = TRY(res.concat(_arena, ")"));
return res;
}