From 9a35a881d1e0f494d92197a943910f4db335deff Mon Sep 17 00:00:00 2001 From: chentianya Date: Wed, 19 Aug 2026 10:09:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=BA=E7=AB=8B=E5=9B=9B=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=20CMake=20=E9=AA=A8=E6=9E=B6=E4=B8=8E=20line1=20=E6=A0=B7?= =?UTF-8?q?=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - compiler / vm 空静态库,host 可执行打印版本 - 顶层解开 add_subdirectory,tests 接入 CTest(host_version 通过) - examples/line1 落地 project.toml 与三个 .st - Types.h 注释补饱和规则,与 Doc/isa 一致 - .gitignore 忽略 Doc/会话记录.md --- .gitignore | 3 ++- CMakeLists.txt | 10 +++++++--- compiler/CMakeLists.txt | 13 +++++++++++++ compiler/src/Lexer.cpp | 8 ++++++++ examples/line1/globals.st | 7 +++++++ examples/line1/main.st | 8 ++++++++ examples/line1/motor.st | 13 +++++++++++++ examples/line1/project.toml | 28 ++++++++++++++++++++++++++++ host/CMakeLists.txt | 10 ++++++++++ host/src/main.cpp | 14 ++++++++++++++ isa/include/isa/Types.h | 2 ++ tests/CMakeLists.txt | 3 +++ vm/CMakeLists.txt | 12 ++++++++++++ vm/src/Machine.cpp | 8 ++++++++ 14 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 compiler/CMakeLists.txt create mode 100644 compiler/src/Lexer.cpp create mode 100644 examples/line1/globals.st create mode 100644 examples/line1/main.st create mode 100644 examples/line1/motor.st create mode 100644 examples/line1/project.toml create mode 100644 host/CMakeLists.txt create mode 100644 host/src/main.cpp create mode 100644 tests/CMakeLists.txt create mode 100644 vm/CMakeLists.txt create mode 100644 vm/src/Machine.cpp diff --git a/.gitignore b/.gitignore index c463767..a5a33aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/** -.vscode/** \ No newline at end of file +.vscode/** +Doc/会话记录.md \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index cfd9226..4d813b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,10 @@ project(Stator include("${CMAKE_SOURCE_DIR}/cmake/Standards.cmake") add_subdirectory(isa) -# add_subdirectory(compiler) -# add_subdirectory(vm) -# add_subdirectory(host) \ No newline at end of file +add_subdirectory(compiler) +add_subdirectory(vm) +add_subdirectory(host) + +# 自动化用例 +enable_testing() +add_subdirectory(tests) \ No newline at end of file diff --git a/compiler/CMakeLists.txt b/compiler/CMakeLists.txt new file mode 100644 index 0000000..fb16c4f --- /dev/null +++ b/compiler/CMakeLists.txt @@ -0,0 +1,13 @@ +# CMake最低版本 +cmake_minimum_required(VERSION 3.21) + +# 工程名、版本号、工程描述 +project(Compiler + VERSION 0.1 + DESCRIPTION "ST / toml 编译器") + +include_directories(./include) + +# 词法、语法、符号表、类型检查、codegen、链接都留在 compiler 一个库里 +add_library(compiler STATIC + ./src/Lexer.cpp) diff --git a/compiler/src/Lexer.cpp b/compiler/src/Lexer.cpp new file mode 100644 index 0000000..f73d4d0 --- /dev/null +++ b/compiler/src/Lexer.cpp @@ -0,0 +1,8 @@ +/** + * @file Lexer.cpp + * @brief 词法分析(12.4 实现) + * @date 2026-08-19 + */ + +namespace compiler { +} diff --git a/examples/line1/globals.st b/examples/line1/globals.st new file mode 100644 index 0000000..6baeaa6 --- /dev/null +++ b/examples/line1/globals.st @@ -0,0 +1,7 @@ +(* globals.st *) +VAR_GLOBAL + EmergencyStop : BOOL := FALSE; + I0_0 : BOOL; + I0_1 : BOOL; + Q0_0 : BOOL; +END_VAR diff --git a/examples/line1/main.st b/examples/line1/main.st new file mode 100644 index 0000000..fef1b92 --- /dev/null +++ b/examples/line1/main.st @@ -0,0 +1,8 @@ +(* main.st *) +PROGRAM MAIN +VAR + starter : MotorStarter; +END_VAR + starter(start := I0_0, stop := I0_1); + Q0_0 := starter.Q; +END_PROGRAM diff --git a/examples/line1/motor.st b/examples/line1/motor.st new file mode 100644 index 0000000..69de7df --- /dev/null +++ b/examples/line1/motor.st @@ -0,0 +1,13 @@ +(* motor.st *) +FUNCTION_BLOCK MotorStarter +VAR_EXTERNAL + EmergencyStop : BOOL; +END_VAR +VAR_INPUT + start, stop : BOOL; +END_VAR +VAR_OUTPUT + Q : BOOL; +END_VAR + Q := start AND NOT stop AND NOT EmergencyStop; +END_FUNCTION_BLOCK diff --git a/examples/line1/project.toml b/examples/line1/project.toml new file mode 100644 index 0000000..3f6658f --- /dev/null +++ b/examples/line1/project.toml @@ -0,0 +1,28 @@ +[project] +name = "line1" +entry = "program MAIN" +cycle_limit = 100000 +dt_ms = 10 + +[files] +st = ["globals.st", "motor.st", "main.st"] + +# 全工程只允许这一处出现 VAR_GLOBAL(第一版:单个文件) +[gvl] +file = "globals.st" + +# 可选:I/O 绑定。不创造变量,var 必须已在 GVL 里声明 +[[io.input]] +var = "EmergencyStop" +channel = 0 +bit = 2 + +[[io.input]] +var = "I0_0" +channel = 0 +bit = 0 + +[[io.output]] +var = "Q0_0" +channel = 0 +bit = 0 diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt new file mode 100644 index 0000000..c7f1fa0 --- /dev/null +++ b/host/CMakeLists.txt @@ -0,0 +1,10 @@ +# CMake最低版本 +cmake_minimum_required(VERSION 3.21) + +# 工程名、版本号、工程描述 +project(Host + VERSION 0.1 + DESCRIPTION "可执行入口:编译并跑扫描周期") + +add_executable(host + ./src/main.cpp) diff --git a/host/src/main.cpp b/host/src/main.cpp new file mode 100644 index 0000000..5a1330c --- /dev/null +++ b/host/src/main.cpp @@ -0,0 +1,14 @@ +/** + * @file main.cpp + * @brief host 可执行入口 + * @date 2026-08-19 + */ + +#include + +int main(int argc, char** argv) { + (void)argc; + (void)argv; + std::printf("Stator host 0.1\n"); + return 0; +} diff --git a/isa/include/isa/Types.h b/isa/include/isa/Types.h index 4ee3721..5a58f5a 100644 --- a/isa/include/isa/Types.h +++ b/isa/include/isa/Types.h @@ -11,6 +11,8 @@ namespace isa { namespace types { + // 定宽类型。整数溢出规则:饱和(夹到 INT 最小/最大), + // 由 compiler 与 vm 按同一规则实现,见 Doc/isa/指令与映像.md。 using BOOL = uint8_t; using INT = int16_t; using TIME = uint64_t; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..8129641 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,3 @@ +# 自动化用例:先放一个必过的空测试,验证 CTest 接线 +add_test(NAME host_version + COMMAND host) diff --git a/vm/CMakeLists.txt b/vm/CMakeLists.txt new file mode 100644 index 0000000..9ffb15f --- /dev/null +++ b/vm/CMakeLists.txt @@ -0,0 +1,12 @@ +# CMake最低版本 +cmake_minimum_required(VERSION 3.21) + +# 工程名、版本号、工程描述 +project(VM + VERSION 0.1 + DESCRIPTION "寄存器虚拟机") + +include_directories(./include) + +add_library(vm STATIC + ./src/Machine.cpp) diff --git a/vm/src/Machine.cpp b/vm/src/Machine.cpp new file mode 100644 index 0000000..c337c8e --- /dev/null +++ b/vm/src/Machine.cpp @@ -0,0 +1,8 @@ +/** + * @file Machine.cpp + * @brief 寄存器虚拟机(12.9 实现) + * @date 2026-08-19 + */ + +namespace vm { +}