/
vit1251
/
cmm
Обзор
Документация
Войти
/
vit1251
/
cmm
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/header_gen.cpp
90 строк
3 KB
Vitold S
Commits added a large set of example and demo programs,
26 июл 2026, 14:31
26 июл 2026, 14:31
8e0776e
Код
Авторство
О чём код?
#include "header_gen.h" #include <sstream> #include <cassert> static std::string cmm_to_c_type(const std::string& tn) { if (tn == "i32") return "int32_t"; if (tn == "u32") return "uint32_t"; if (tn == "i64") return "int64_t"; if (tn == "u64") return "uint64_t"; if (tn == "i8") return "int8_t"; if (tn == "u8") return "uint8_t"; if (tn == "i16") return "int16_t"; if (tn == "u16") return "uint16_t"; if (tn == "f32") return "float"; if (tn == "f64") return "double"; if (tn == "bool") return "uint8_t"; if (tn == "void") return "void"; if (tn == "*void") return "void*"; if (tn == "*u8") return "const char*"; if (tn == "*i8") return "const char*"; // Pointer type: *Name → Name* if (tn.size() > 0 && tn[0] == '*') return cmm_to_c_type(tn.substr(1)) + "*"; return tn; // hope it's a struct name } std::string generate_c_header(const Program& prog) { std::ostringstream h; h << "// Auto-generated by cmmc\n"; h << "#include <stdint.h>\n"; h << "#include <stddef.h>\n\n"; // Collect all structs referenced as pointers for forward decls std::vector<std::string> forward_decls; // Pub structs: emit bodies for (auto& sd : prog.structs) { if (!sd.is_pub) continue; // Check if any fields reference structs from other modules for (auto& f : sd.fields) { if (f.type_name.size() > 0 && f.type_name[0] != '*' && f.type_name != "i32" && f.type_name != "u32" && f.type_name != "i64" && f.type_name != "u64" && f.type_name != "i8" && f.type_name != "u8" && f.type_name != "f32" && f.type_name != "f64" && f.type_name != "bool" && f.type_name != "void") { // It's a named struct type — might need forward decl bool found = false; for (auto& sd2 : prog.structs) if (sd2.name == f.type_name) { found = true; break; } if (!found) { bool dup = false; for (auto& fd : forward_decls) if (fd == f.type_name) { dup = true; break; } if (!dup) forward_decls.push_back(f.type_name); } } } } for (auto& fd : forward_decls) h << "struct " << fd << ";\n"; if (!forward_decls.empty()) h << "\n"; // Pub structs for (auto& sd : prog.structs) { if (!sd.is_pub) continue; h << "struct " << sd.name << " {\n"; for (auto& f : sd.fields) { h << " " << cmm_to_c_type(f.type_name) << " " << f.name << ";\n"; } h << "};\n\n"; } // Pub function declarations (non-extern) for (auto& fn : prog.functions) { if (!fn->is_pub || fn->is_extern) continue; h << cmm_to_c_type(fn->ret_type_name) << " " << fn->name << "("; for (size_t i = 0; i < fn->params.size(); i++) { if (i > 0) h << ", "; // Convert self: *WavWriter → WavWriter* std::string ctype = cmm_to_c_type(fn->params[i].type_name); h << ctype << " " << fn->params[i].name; } if (fn->params.empty()) h << "void"; h << ");\n"; } return h.str(); }