init commit

This commit is contained in:
2025-12-10 23:44:52 +08:00
commit bb1fbae36f
15 changed files with 287 additions and 0 deletions

118
.clang-format Normal file
View File

@@ -0,0 +1,118 @@
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -4
# AlignAfterOpenBracket: BlockIndent
# 让多行的赋值语句和声明进行对齐
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
# AllowAllConstructorInitializersOnNextLine: false
# 禁止将多行语句写在一起
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty # 只有空函数允许单行
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
AlignConsecutiveMacros: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: true
SplitEmptyNamespace: false
AfterObjCDeclaration: false
AfterExternBlock: false
AfterStruct: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: false
BreakBeforeConceptDeclarations: true
BreakBeforeInheritanceComma: false
# BreakConstructorInitializers: AfterColon
BreakAfterJavaFieldAnnotations: true
BreakStringLiterals: true
BreakConstructorInitializersBeforeComma: false
# ColumnLimit: 110
# ConstructorInitializerAllOnOneLineOrOnePerLine: true
# SortIncludes: true
CompactNamespaces: true
ContinuationIndentWidth: 4
IndentCaseLabels: true
IndentPPDirectives: None
IndentExternBlock: NoIndent
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1
# NamespaceIndentation: None
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PointerAlignment: Left
ReflowComments: false
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
# SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never
IndentRequires: true
SpacesInConditionalStatement: false
SortUsingDeclarations: true
Standard: c++20
# 同一行长度限制
ColumnLimit: 110
# 排序include
SortIncludes: true
# 注释前面需要有1个空格
SpacesBeforeTrailingComments: 1
# 如果某行太长,则总是在左小括号后换行,同时右小括号也换行
AlignAfterOpenBracket: BlockIndent
# 构造函数的初始化列表前需要换行
BreakConstructorInitializers: AfterColon
# 构造函数的初始化列表必须换行
AllowAllConstructorInitializersOnNextLine: false
# 构造函数的初始化列表要么都在同一行,要么都各自一行
ConstructorInitializerAllOnOneLineOrOnePerLine: true
# 命名空间前面不空格
NamespaceIndentation: None

46
.gitignore vendored Normal file
View File

@@ -0,0 +1,46 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Linker files
*.ilk
# Debugger Files
*.pdb
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# debug information files
*.dwo
# IDE
.idea/
.vscode/
*.code-workspace

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "ext/flatbuffer"]
path = ext/flatbuffer
url = https://github.com/google/flatbuffers.git

63
CMakeLists.txt Normal file
View File

@@ -0,0 +1,63 @@
cmake_minimum_required(VERSION 4.0)
project(startra)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# 添加子目录
#add_subdirectory(src/common)
#add_subdirectory(src/compiler)
#add_subdirectory(src/vm)
#add_subdirectory(src/c_api)
#add_subdirectory(tools/cli)
#add_subdirectory(tests)
# 引入 Flatbuffer 库
set(FLATBUFFERS_DIR "ext/flatbuffer") # <--- 请根据实际文件夹名修改这里!!!
set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(FLATBUFFERS_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(${FLATBUFFERS_DIR})
include(${FLATBUFFERS_DIR}/CMake/BuildFlatBuffers.cmake)
# 主程序
add_executable(startra src/main.cpp)
# 自动编译 Schema 文件
set(SCHEMA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/schemas")
set(SCHEMA_FILE "${SCHEMA_DIR}/bytecode.fbs")
set(GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(GENERATED_HEADER "${GENERATED_DIR}/bytecode_generated.h")
# 添加自定义命令:调用 flatc 生成 .h
add_custom_command(
OUTPUT ${GENERATED_HEADER}
COMMAND ${CMAKE_COMMAND} -E make_directory ${GENERATED_DIR}
COMMAND $<TARGET_FILE:flatc> --cpp --scoped-enums -o ${GENERATED_DIR} ${SCHEMA_FILE}
DEPENDS flatc ${SCHEMA_FILE}
COMMENT "Running FlatBuffers compiler on ${SCHEMA_FILE}..."
VERBATIM
)
# 创建一个自定义 Target让 CMake 知道何时生成它
add_custom_target(generate_schema_headers DEPENDS ${GENERATED_HEADER})
# =========================================================
# 4. 链接与依赖 (Order Matters!)
# =========================================================
# 让 startra 依赖生成任务 (确保编译 C++ 前,头文件已经生成好了)
add_dependencies(startra generate_schema_headers)
# 包含路径
target_include_directories(startra PRIVATE
src
${FLATBUFFERS_DIR}/include # 引用 flatbuffers 库本身的头文件
${GENERATED_DIR} # 引用我们生成的 bytecode_generated.h
)
# 链接库
target_link_libraries(startra flatbuffers)

2
README.md Normal file
View File

@@ -0,0 +1,2 @@
# Startra: Story Tree Abstract Representation & Threaded Runtime Architecture

1
ext/flatbuffer Submodule

Submodule ext/flatbuffer added at 19b2300f93

12
schemas/bytecode.fbs Normal file
View File

@@ -0,0 +1,12 @@
namespace startra;
table Instruction {
opcode:uint;
args:[uint];
}
table Bytecode {
instructions:[Instruction];
}
root_type Bytecode;

0
src/c_api/.gitkeep Normal file
View File

0
src/common/.gitkeep Normal file
View File

1
src/compiler/.gitkeep Normal file
View File

@@ -0,0 +1 @@

37
src/main.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include <iostream>
#include "bytecode_generated.h"
void testBytecode() {
// 创建 FlatBufferBuilder
flatbuffers::FlatBufferBuilder builder;
// 构造指令数据
auto instr1 = startra::CreateInstruction(builder, 1, builder.CreateVector(std::vector<uint32_t>{10, 20}));
auto instr2 = startra::CreateInstruction(builder, 2, builder.CreateVector(std::vector<uint32_t>{30, 40}));
auto instructions = builder.CreateVector<flatbuffers::Offset<startra::Instruction> >({instr1, instr2});
// 构造 Bytecode 数据
auto bytecode = startra::CreateBytecode(builder, instructions);
builder.Finish(bytecode);
// 获取缓冲区指针
uint8_t* buf = builder.GetBufferPointer();
size_t size = builder.GetSize();
// 反序列化并验证数据
auto bytecodeData = flatbuffers::GetRoot<startra::Bytecode>(buf);
std::cout << "Instructions count: " << bytecodeData->instructions()->size() << "\n";
for (auto instr : *bytecodeData->instructions()) {
std::cout << "Opcode: " << instr->opcode() << ", Args: ";
for (auto arg : *instr->args()) {
std::cout << arg << " ";
}
std::cout << "\n";
}
}
int main() {
testBytecode();
return 0;
}

1
src/vm/.gitkeep Normal file
View File

@@ -0,0 +1 @@

1
tests/.gitkeep Normal file
View File

@@ -0,0 +1 @@

1
tools/cli/.gitkeep Normal file
View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@