阶段 B 步骤 5:类型元数据配置化(TypeInfo)。

- TypeInfo.h/cpp:TypeMeta(配置别名 + 内建基元解析),查询大小写不敏感;
  提供 width/is_signed/is_float/tag/has_range/value_in_range
- Codegen:layout_data 初值按 type_meta 的基元宽度/浮点写(删除手写宽度分支);
  const_id 的 tag 改查 type_meta
- machine_test:+10 断言(BOOL 1B/range[0,1]/tag0、INT 2B 有符号、TIME 8B、大小写不敏感、未定义空)
- 验收:line1 224 字节 hash 0xc3fe4ae74ad45900 不变、ctest 12/12
This commit is contained in:
2026-08-21 22:16:48 +08:00
parent 447a8d0119
commit e187ed7b03
5 changed files with 127 additions and 11 deletions
+33
View File
@@ -10,6 +10,7 @@
#include <string>
#include "compiler/MachineConfig.h"
#include "compiler/TypeInfo.h"
#ifndef REPO_ROOT
#define REPO_ROOT "."
@@ -252,10 +253,42 @@ static bool test_positive_min() {
return true;
}
// ---- 4. 类型元数据(TypeInfo----
static bool test_type_meta() {
compiler::MachineConfig cfg;
std::string err;
CHECK(cfg.load(std::string(REPO_ROOT) + "/compiler/machine.toml", &err));
// BOOLbase=uint8 → 1 字节、无符号、非浮点、tag 0、range [0,1]
const compiler::TypeMeta b = compiler::type_meta(cfg, "BOOL");
CHECK(b && b.width() == 1 && !b.is_signed() && !b.is_float() && b.tag() == 0);
CHECK(b.has_range() && b.value_in_range(0) && b.value_in_range(1));
CHECK(!b.value_in_range(2) && !b.value_in_range(-1));
// 大小写不敏感(Linker type_name 是小写)
const compiler::TypeMeta b2 = compiler::type_meta(cfg, "bool");
CHECK(b2 && b2.tag() == 0 && b2.width() == 1);
// INTint16 → 2 字节、有符号、tag 1、无 range
const compiler::TypeMeta i = compiler::type_meta(cfg, "int");
CHECK(i && i.width() == 2 && i.is_signed() && !i.is_float() && i.tag() == 1);
CHECK(!i.has_range() || i.value_in_range(30000));
// TIMEint64 → 8 字节、有符号、tag 2
const compiler::TypeMeta t = compiler::type_meta(cfg, "TIME");
CHECK(t && t.width() == 8 && t.is_signed() && !t.is_float() && t.tag() == 2);
// 未定义类型 → 空
CHECK(!compiler::type_meta(cfg, "REAL"));
CHECK(!compiler::type_meta(cfg, ""));
return true;
}
int main() {
if (!test_positive()) return 1;
if (!test_negative()) return 1;
if (!test_positive_min()) return 1;
if (!test_type_meta()) return 1;
std::printf("machine_test: %d checks passed\n", g_checks);
return 0;
}