Imposter-zx/Dim

GitHub: Imposter-zx/Dim

一门用 Python 从零构建的静态编译语言,融合类 Python 语法、Rust 风格所有权系统和 AI 原生支持,旨在探索系统级安全编程与智能应用的交汇点。

Stars: 4 | Forks: 0

# Dim 编程语言 **Dim** 是一门下一代静态编译编程语言,旨在将符合人体工程学的开发者体验与系统级控制桥接起来——并将 **AI/ML**、**内存安全**和**安全性**作为一等公民的语言特性。 ## 功能 | 功能 | 状态 | | --------------------------- | ---------------------------------------------- | | Tree-Walking 解释器 | ✅ 生产可用 (`dim_interpreter.py`) | | REPL | ✅ 生产可用 (`dim_repl.py`) | | AI `prompt` 类型 | ✅ 生产可用 (`dim_ai.py`) | | 字符串插值 | ✅ 生产可用 (`dim_lexer.py`, `dim_interpreter.py`) | | `?` 操作符 (Result) | ✅ 生产可用 (`dim_parser.py`, `dim_interpreter.py`) | | 基本类型方法 | ✅ 生产可用 (`dim_interpreter.py`) | | VS Code 扩展 | ✅ (`editors/vscode/`) | | 带有 INDENT/DEDENT 的词法分析器 | ✅ 生产可用 (`dim_lexer.py`) | | EBNF 语法 | ✅ 正式规范 (`dim_grammar.ebnf`) | | 带有 Span 标注的 AST | ✅ v0.5 (`dim_ast.py`) | | 代数类型系统 | ✅ (`dim_types.py`) | | Hindley-Milner 类型检查器 | ✅ (`dim_type_checker.py`) | | 结构化诊断 | ✅ (`dim_diagnostic.py`) | | 中间表示层 (MIR / CFG) | ✅ (`dim_mir.py`) | | AST → MIR 降低 | ✅ (`dim_mir_lowering.py`) | | 借用检查器 (MIR 级别) | ✅ 受 Polonius 启发 (`dim_borrow_checker.py`) | | LLVM IR 代码生成 | ✅ (`dim_mir_to_llvm.py`) | | 模块导入解析 | ✅ (`dim_module_resolver.py`) | | 一等公民 `prompt` 类型 | ✅ AST + 类型系统 | | `actor` / 消息传递 | ✅ AST + 解析器 | | `@tool` 装饰器 | ✅ 解析器 + 语义分析 | | 错误处理 (try/catch) | ✅ 解析器 + 类型检查器 | | 闭包与 lambda | ✅ `|x, y| -> expr` 语法 | | 泛型与特征 | ✅ (`dim_type_checker.py`, `dim_types.py`) | | FFI (外部函数接口/use) | ✅ 解析器 + 类型检查器 | | 标准库 | ✅ 完整可用的标准库 | | WASM 编译 | ✅ (`dim_wasm_codegen.py`) | | 包管理器 | ✅ (`dim_pkg.py`) | | LSP 服务器 | ✅ (`dim_lsp.py`) | | REPL | ✅ (`dim_repl.py`) | | 调试器 | ✅ (`dim_debugger.py`) | | 测试框架 | ✅ (`dim_test.py`) | | 构建系统 | ✅ (`dim_build.py`) | | 宏系统 | ✅ (`dim_macro.py`) | | 内存管理 (RC + GC)| ✅ 引用计数 + GC | | 并发 (线程/Future)| ✅ 线程池 + async/futures | | 测试套件 | ✅ 79 个测试 (`dim_tests.py`) | ## 交互式演示 无需安装即可体验 Dim!下载并在浏览器中打开 [`dim_language_demo.html`](dim_language_demo.html): - **Hello World** - 运行 Dim 程序 - **REPL** - 交互式控制台 - **AI 提示词** - 真正的 AI 集成 - **VS Code** - 语法高亮演示 或者在本地运行: ``` # 运行 Dim 文件 python dim_cli.py run hello.dim # 启动 REPL python dim_repl.py # 类型检查 python dim_cli.py check hello.dim ``` ## 语法预览 ``` # 带有返回类型的函数 fn add(x: i32, y: i32) -> i32: return x + y # 所有权 — 默认不可变,移动语义 fn process(data: Buffer) -> Result[str, Error]: let view = &data # shared borrow return Ok(view.to_string()) # Async / Await async fn fetch(url: String) -> Result[Data, Error]: let resp = await http::get(url) return parse(resp.body) # Actor — 通过消息传递实现状态隔离 actor Counter: state: i32 = 0 receive Increment(): self.state += 1 receive GetCount() -> i32: return self.state # AI 作为一等类型 prompt Classify: role system: "You are a text classifier." role user: "Classify: {input}" output: ClassLabel # 模式匹配 fn describe(x: i32) -> str: match x: 0: return "zero" n if n > 0: return "positive" _: return "negative" # 泛型 + Traits trait Summable[T]: fn add(a: T, b: T) -> T fn generic_sum[T: Summable](a: T, b: T) -> T: return T.add(a, b) # 错误处理 fn safe_div(a: i32, b: i32): try: if b == 0: throw Error() return a / b catch e: print("Error!") finally: cleanup() # 闭包和元组 fn use_closure(): let add = |x, y| -> x + y let point = (1, 2, 3) let len = point.len # 标准库 fn demo_std(): let x = abs(-5) let m = min(1, 2) let mx = max(1, 2) let s = sin(3.14159) let root = sqrt(144.0) # 模块导入 import std.io import std.vec import std.math import std.str fn demo_io(): print("Hello, World!") let content = read_file("test.txt") if file_exists("test.txt"): println("File exists!") fn demo_vec(): let arr = [1, 2, 3] push(arr, 4) let x = pop(arr) # FFI - 调用 C 函数 foreign "libc.so" [ fn puts(msg: str) -> i32 fn rand() -> i32 ] ``` ## 编译器管线 ``` Source (.dim) ↓ dim_lexer.py — Tokenisation + INDENT/DEDENT ↓ dim_parser.py — AST construction (Pratt + recursive descent) ↓ dim_module_resolver.py — Module import resolution ↓ dim_type_checker.py — HM type inference, scope resolution ↓ dim_mir_lowering.py — AST → MIR (SSA Control Flow Graph) ↓ dim_borrow_checker.py — Ownership & borrow validation (Polonius) ↓ dim_mir_to_llvm.py — MIR → LLVM IR (x86_64, wasm32, wasm64) ↓ clang — Native binary / WASM ``` ## 使用方法 ``` # 初始化新项目 dim new myproject cd myproject # 构建并运行 dim run main.dim dim run main.dim arg1 arg2 # 仅进行类型检查 dim check hello.dim # 打印 AST dim parse hello.dim # 打印 MIR (SSA/CFG) dim mir hello.dim # 运行借用检查器 dim borrow hello.dim # 运行完整构建流水线 dim build hello.dim # 运行测试套件 dim test dim test --tag lexer # filter by category # 包管理器 dim pkg init mypkg dim pkg add http 1.0.0 dim pkg remove http dim pkg install dim pkg list # 格式化代码 dim fmt hello.dim # LSP 服务器 (用于 IDE) python dim_lsp.py # REPL python dim_repl.py # 调试器 python dim_debugger.py hello.dim ``` ## 项目结构 ``` dim/ — Dim source root ├── Compiler Core │ ├── dim_token.py — Token and Span definitions │ ├── dim_lexer.py — Lexer with INDENT/DEDENT │ ├── dim_parser.py — Recursive descent parser │ ├── dim_ast.py — AST node definitions │ ├── dim_types.py — Type system │ ├── dim_type_checker.py — Hindley-Milner type inference │ ├── dim_semantic.py — Semantic analysis │ ├── dim_borrow_checker.py — Borrow checking │ └── dim_module_resolver.py — Module resolution ├── IR & Codegen │ ├── dim_mir.py — Mid-Level IR │ ├── dim_mir_lowering.py — AST → MIR lowering │ ├── dim_mir_to_llvm.py — MIR → LLVM IR │ ├── dim_native_codegen.py — Native binary generation │ └── dim_wasm_codegen.py — WASM compilation ├── Tools │ ├── dim_cli.py — CLI interface │ ├── dim_diagnostic.py — Error/warning system │ ├── dim_lsp.py — LSP server (IDE support) │ ├── dim_repl.py — Interactive REPL │ ├── dim_debugger.py — Debugger │ ├── dim_test.py — Test framework │ ├── dim_pkg.py — Package manager │ ├── dim_build.py — Build system │ ├── dim_formatter.py — Code formatter │ └── dim_macro.py — Macro system ├── Runtime │ ├── runtime/dim_runtime.c — C runtime (native) │ └── runtime/dim_runtime_wasm.c — C runtime (WASM) ├── Stdlib │ ├── std/io.dim — I/O utilities │ ├── std/vec.dim — Vector/array utilities │ ├── std/math.dim — Math functions │ ├── std/str.dim — String utilities │ ├── std/file.dim — File I/O │ └── std/json.dim — JSON utilities ├── Tests & Docs │ ├── dim_tests.py — 70 test cases │ └── examples/ — Example .dim files │ ├── functions.dim │ ├── control_flow.dim │ ├── ai_tools.dim │ ├── types.dim │ ├── ownership.dim │ ├── traits.dim │ ├── ffi.dim │ └── test_example.dim └── Documentation ├── README.md — This file ├── CHANGELOG.md — Version history ├── CONTRIBUTING.md — Contribution guide ├── LICENSE.md — MIT License ├── COMPILER_INTERNALS.md — Compiler internals ├── QUICK_REFERENCE.md — Language quick reference └── dim_specification.md — Language specification ``` ## 路线图 | 阶段 | 里程碑 | 状态 | | ----- | -------------------------------------------------------- | ------ | | 0 | 词法分析器、解析器、AST 原型 | ✅ 完成 | | 1 | 类型、MIR、借用检查器、诊断 | ✅ 完成 | | 2 | LLVM IR 代码生成、函数调用、@tool 解析 | ✅ 完成 | | 3 | 原生二进制文件、WASM、完整的 LLVM 后端 | ✅ 完成 | | 4 | 包管理器、LSP、REPL、调试器 | ✅ 完成 | | 5 | AI/LLM 引擎 (类型化提示词、模型适配器) | ✅ 完成 | | 6 | 安全性:污点分析、能力模型、契约 | ✅ 完成 | | 7 | 工具:Linter、基准测试、文档生成器 | ✅ 完成 | | 8 | 自举 (bootstrap/)、v1.0 | ✅ 完成 | ## 设计目标 - **内存安全**且无需垃圾回收 — 所有权 + 借用检查器 - **原生 AI 支持** — `prompt` 和 `model` 是语言关键字,而非库调用 - **默认安全** — 基于能力的访问控制、静态污点分析 - **符合人体工程学** — 类 Python 语法、基于缩进的代码块、HM 类型推断 - **高性能** — 通过 LLVM 编译为原生代码 - **面向 Web** — 支持 WASM 编译,适用于浏览器/无服务器环境 _Dim — 系统编程与智能的交汇之处。_
标签:Actor模型, CFG, DLL 劫持, Hindley-Milner, LLVM, Pratt解析器, Python语法, SOC Prime, SSA, 人工智能, 借用检查器, 内存安全, 大语言模型, 开发工具, 用户模式Hook绕过, 类型推断, 系统编程, 编程语言, 编程语言实现, 编译器, 词法分析器, 逆向工具, 静态编译语言