Files
Interpreter/compiler/include/compiler/Project.h
T

81 lines
3.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file Project.h
* @brief 工程定义与 project.toml 解析(schema 校验)
* @details 本文件定义工程数据结构与解析接口(实现见 Project.cpp):
* - IoBinding / Projectproject.toml 的解析结果(I/O 绑定不创造变量,
* slot 由链接结果解析)
* - parse_project:解析并校验 project.toml,错误带稳定类别前缀 + 行号
* - compile_files:编译文件集合(files.st gvl.file,去重)
* - compute_project_hash:校验文件存在并计算工程哈希,缺文件前缀 "file missing"
* 字段约定与 Doc/初步计划.md 12.1 的 toml 字段表一一对应。
* @author
* @date 2026-08-21
*/
#pragma once
#include <cstdint>
#include <string>
#include <vector>
namespace compiler {
/**
* @brief I/O 绑定(project.toml [[io.*]];不创造变量,slot 由链接结果解析)。
*/
struct IoBinding {
std::string var; ///< 绑定的全局变量名(须已在 GVL 声明)
uint32_t slot = 0; ///< 全局槽号(12.6 链接阶段解析,此处填 0)
uint32_t channel = 0; ///< 通道号
uint32_t bit = 0; ///< 位号
bool is_input = false; ///< true = [[io.input]]false = [[io.output]]
};
/**
* @brief 工程定义。
* @details 与 Doc/初步计划.md 12.1 的 toml 字段表一一对应;
* 字段白名单 / 必填 / io 完整性在 parse_project 内校验。
*/
struct Project {
std::string name; ///< [project] 必填
std::string entry; ///< 必填,第一版必须 "program MAIN"
uint32_t cycle_limit = 0; ///< 必填 > 0
uint32_t dt_ms = 0; ///< 必填 > 0
std::vector<std::string> files_st; ///< [files] 必填非空
std::string gvl_file; ///< [gvl] 可选;无则空串
std::vector<IoBinding> io; ///< [[io.*]] 可选;slot 12.6 才解析,此处填 0
std::string base_dir; ///< toml 所在目录(解析相对路径用)
};
/**
* @brief 解析并校验 project.toml。
* @details 成功返回 true;失败返回 false 并写 err,err 带稳定类别前缀
* parse error / unknown key / missing field / invalid value / bad entry
* + 行号。
* @param toml_path project.toml 路径
* @param out 输出 Projectbase_dir 先填,其余字段由各段解析填充)
* @param err 错误输出;可为 nullptr(静默)
* @return true 成功;false 失败(err 已写)
*/
bool parse_project(const std::string& toml_path, Project* out, std::string* err);
/**
* @brief 编译文件集合:files.st gvl.file,去重(gvl 已在 files.st 则跳过)。
* @details 保持 files.st 顺序,gvl 追加在后;相对路径以 base_dir 为基准解析。
* @param p 工程定义
* @return 编译文件路径集合
*/
std::vector<std::string> compile_files(const Project& p);
/**
* @brief 校验全部文件存在并计算工程哈希。
* @details 集合按路径排序,对内容做 FNV-1a 64 增量;空集合 = basisStb::kFnvBasis)。
* 缺文件报错前缀 "file missing"。
* @param p 工程定义
* @param hash 输出工程哈希
* @param err 错误输出;可为 nullptr(静默)
* @return true 成功;false 文件缺失(err 已写)
*/
bool compute_project_hash(const Project& p, uint64_t* hash, std::string* err);
}