Files
dl_old/dl/misc/dl_code_writer.cpp
2025-12-21 22:57:06 +08:00

341 lines
8.1 KiB
C++

#include "dl_code_writer.h"
#include "base/dl_range_select.h"
#include "math/dl_math.h"
#include "io/dl_file.h"
namespace dl::CodeWriter
{
void Struct::FormatVariable()
{
auto fn_format = [&](int type) {
// 计算类型最大宽度
std::size_t w = Select::Max(_allVariable, [&](const Variable& v) {
const std::string& str = type ? v._name : v._type;
return str.size();
});
if (w % 4 == 0)
w += 4;
else
w = (std::size_t)Math::AlignPow2((unsigned)w, 4u);
for (Variable& v : _allVariable)
{
//------------------- 对齐类型宽度
std::string& str = type ? v._name : v._type;
assert(w >= str.size());
std::size_t num_space = w - str.size();
std::size_t num_t = num_space / 4;
if (num_space % 4)
++num_t;
for (std::size_t i = 0; i < num_t; ++i)
str.push_back('\t');
}
};
fn_format(0);
fn_format(1);
}
void Segment::Output(std::string_view path_name)
{
std::string str;
std::size_t tab_num = 0;// 当前tab数
// 写入换行
auto endl = [&]() {
str.push_back('\n');
};
// 写入tab
auto tab = [&]() {
for (std::size_t i = 0; i < tab_num; ++i)
str += '\t';
};
// 头部
//str += "{"; endl();
//++tab_num;
for (const Struct& s : _allStruct)
{
if (s._name == "//")
{// 注释行
tab(); str += s._value; endl();
continue;
}
tab(); str += s._name; endl();
tab(); str += "{"; endl();
++tab_num;
Struct s_copy = s;
s_copy.FormatVariable();
for (const Variable& v : s_copy._allVariable)
{
tab();
//
if (v._typeVariable == VariableType::BASE)
{
if (v._value.empty())
str += fmt::format("{}{};", v._type, v._name);
else
str += fmt::format("{}{}= {}; // {}", v._type, v._name, v._value, v._comment);
}
else if (v._typeVariable == VariableType::USER
|| v._typeVariable == VariableType::ARRAY
|| v._typeVariable == VariableType::SINGLE)
{
std::string init_list;
if (v._value.empty())
init_list = "{}";
else
init_list = fmt::format("{{ {} }}", v._value);
str += fmt::format("{}{}= {}; // {}", v._type, v._name, init_list, v._comment);
}
else if (v._typeVariable == VariableType::PAIR)
{
// 以空格分隔
std::vector<std::string> vec_str;
split(v._value, ' ', vec_str);
std::string init_list = "{";
for (auto& iter : vec_str)
{
init_list += "{ ";
init_list += iter;
init_list += "}, ";
}
init_list += "};";
str += fmt::format("{}{}= {}; // {}", v._type, v._name, init_list, v._comment);
}
else
assert(0);
endl();
}
--tab_num;
tab(); str += "}"; str += s._value; str += ";"; endl();
}
if (false)
{
// 成员函数
tab(); str += "void Save(StreamOutput& s)"; endl();
tab(); str += "{"; endl();
++tab_num;
for (Struct& s : _allStruct)
{
for (Variable& v : s._allVariable)
{
tab(); str += fmt::format("s.Write(&{}.{});", s._value, v._name);
endl();
}
}
--tab_num;
tab(); str += "}"; endl();
//
tab(); str += "void Load(StreamInput& s)"; endl();
tab(); str += "{"; endl();
++tab_num;
for (Struct& s : _allStruct)
{
for (Variable& v : s._allVariable)
{
tab(); str += fmt::format("s.Read(&{}.{});", s._value, v._name);
endl();
}
}
--tab_num;
tab(); str += "}"; endl();
}
//Segment* seg;
// 从Segment(自己)加载
{
tab(); str += "void Load(const Segment* seg)"; endl();
tab(); str += "{"; endl();
++tab_num;
tab(); str += "Variable* v;"; endl();
// 逐变量生成解析代码
for (Struct& s : _allStruct)
{
tab(); str += fmt::format("//{:-^64}", s._comment); endl();
if (s._name == "//")
{// 注释行
tab(); str += s._value; endl();
continue;
}
for (Variable& v : s._allVariable)
{
std::string name = fmt::format("{}.{}", s._value, v._name);
tab(); str += fmt::format("//{}", v._comment); endl();
tab(); str += fmt::format("v = seg->GetVariable(\"{}\", \"{}\");", s._value, v._name); endl();
tab(); str += "if (!v)"; endl();
tab(); str += "{"; endl();
++tab_num;
tab(); str += fmt::format("log_err(\"变量获取失败:{}\");", name); endl();
--tab_num;
tab(); str += "}"; endl();
tab(); str += fmt::format("if (!String::ToValue(v->_value, {}))", name); endl();
tab(); str += "{"; endl();
++tab_num;
tab(); str += fmt::format("log_err(\"变量转换失败:{}\");", name); endl();
--tab_num;
tab(); str += "}"; endl();
}
}
/*tab(); str += "Vector2<std::string> vec2;"; endl();
tab(); str += "if (!FileXlsx::Read(path_name, vec2))"; endl();
tab(); str += "{"; endl();
++tab_num;
tab(); str += "return;"; endl();
--tab_num;
tab(); str += "}"; endl();*/
/*std::vector<std::string> vec_str = String::FormatString2(vec2);
std::string str;
for (auto& iter : vec_str)
{
str += iter;
str += '\n';
}*/
/*s = CodeWriter::Create(vec2);
s->Output("out_code.h");*/
--tab_num;
tab(); str += "}"; endl();
}
// 结束
/*--tab_num;
tab(); str += "}"; endl();*/
File::SaveBuffer(CreateBufferView(str), path_name);
}
Segment* Create(const Vector2<std::string, size_t>& vec2)
{
Segment* ret = new Segment;
enum Index : std::size_t
{
E_COMMENT,
E_TYPE,
E_NAME,
E_VALUE,
E_MAX
};
// 单值类型
constexpr std::array<std::string_view, 6> arr_type_single = {
"bool", "int", "unsigned", "float", "double", "string"
};
//constexpr std::array<std::string_view, 3> arr_type_multi = {
// "array", "vector", "map"
//};
assert(vec2.GetWidth() >= E_VALUE);// value列可以没有
bool no_value = (vec2.GetWidth() - E_VALUE) == 0;// 值数量
bool is_struct = false;
Struct s;
using namespace std::literals;
for (unsigned j = 0; j < vec2.GetHeight(); ++j)
{
std::span<std::string> vec = vec2[j];
std::string_view comment = vec[E_COMMENT];
if (Sub::IsBegin(comment, "//"sv))
{// 注释行写入注释 并跳过
Struct s;
s._name = "//";
s._value = comment;
ret->_allStruct.push_back(s);
continue;
}
std::string_view type = vec[E_TYPE];
std::string_view name = vec[E_NAME];
// 类型解析
if (Sub::IsBegin(type, "struct"sv))
{
if (is_struct)
{// 上一个结构体结束
ret->_allStruct.push_back(s);
}
else
is_struct = true;
// 新结构体
s._comment = comment;
s._name = type;
s._value = name;
s._allVariable.clear();
continue;
}
//----------------------------计算值写法-----------------------
// 单个值
bool is_single = true;
// 包含逗号
if (Sub::IsSub(type, ","sv))
is_single = false;
// 普通已知类型
bool is_normal = true;
is_normal = Any::Yes(arr_type_single, [&](std::string_view str) {
return Sub::IsBegin<std::string_view>(type, str);
});
Variable v; // 值
std::string_view value = vec[E_VALUE];
v._comment = comment;
v._name = name;
v._value = value;
if (type == "size_t")
int i = 3;
// 单个值
if (is_single)
{
v._type = type;
AddStdNamespace(v._type);
ReplaceType(v._type);
v._typeVariable = VariableType::BASE;
#ifndef NDEBUG
// 检查是否有额外配置
for (std::size_t i = E_VALUE; i < vec2.GetWidth(); ++i)
{
if (!vec[E_VALUE + i].empty())
{
log_warn("此类型 {}只需要配一个值!在第{}行,第{}列,请检查(下标从0开始)。",
type, j, i);
break;
}
}
#endif
}
else
{
// 如果包含逗号则转换为array
std::vector<std::string> vec_str;
split(std::string{ type }, ',', vec_str);
if (vec_str.size() >= 2)
{
AddStdNamespace(vec_str[0]);
ReplaceType(vec_str[0]);
if (vec_str[1] == "n")
v._type = fmt::format("std::vector<{}>", vec_str[0]);
else
v._type = fmt::format("std::array<{}, {}>",
vec_str[0], vec_str[1]);
v._typeVariable = VariableType::ARRAY;
}
else
{// 自定义类型(包括size_t等非基础类型)
v._type = type;
AddStdNamespace(v._type);
ReplaceType(v._type);
v._typeVariable = VariableType::USER;
}
}
// 写入行
s._allVariable.push_back(v);
}
return ret;
}
}