Commit Graph

82 Commits

Author SHA1 Message Date
Serendipity ee3f9e0bd0 chore: bump version to 0.8.0 v0.8.0 2026-06-07 18:29:59 +08:00
Serendipity 6d5f8092a7 fix: for循环变量作用域 + 列表推导crash (两个已知bug)
Bug 1 - For循环变量作用域:
- AST_BLOCK 在 sema 中未创建子作用域 → 连续 for 循环用同名变量报"重复定义"
- 修复: sema AST_BLOCK 创建 block_scope (scope_new)
- 修复: codegen AST_BLOCK 保存/恢复 var_table 实现块级变量隔离

Bug 2 - 列表推导 >2元素 crash:
- sema 对 TYPE_ARRAY 标注跳过 init 分析 → 列表推导表达式未被semantize
- 导致 codegen 处 element_type=0, array_size=0 → LLVM alloca 崩溃
- 修复: 仅自引用 (= 变量名) 跳过分析,列表推导等正常分析
- 修复: cg_list_comp 使用 to_llvm_type(elem) 而非 type_info_to_llvm(full_array)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 18:03:25 +08:00
Serendipity f5c0650a97 feat: 闭包变量捕获 — 环境结构体 + 堆分配
lambda 可捕获外层变量, 自动构建环境结构体:

let base = 100;
let f = fn(x: i64) -> i64 { return x + base; };  // 捕获 base
f(50);  // → 150

全流水线实现:
- Sema: collect_free_vars 遍历 AST 收集自由变量
- AST function: captured/cap_types/cap_count 字段存储捕获信息
- Codegen: 闭包类型改为 struct {fn_ptr: i64, env_ptr: ptr}
- Codegen: lambda 表达式 malloc 环境结构体 + 存储捕获值
- Codegen: 生成函数签名添加 env_ptr 首个参数 (capturing only)
- Codegen: 函数体内通过 GEP 注册捕获变量到 var_table
- Codegen: 闭包调用自动提取 fn_ptr/env_ptr, 条件传递 env

非捕获 lambda 兼容: env_ptr=NULL, 不额外传参
嵌套 lambda 正确处理: 内层不穿透捕获外层变量

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 15:24:35 +08:00
Serendipity 06d80f441a feat: 闭包/lambda — 匿名函数表达式
fn(x: T) -> R { body } 作为表达式, 可赋值给变量并间接调用。

全流水线实现:
- Parser: TOK_FN 前缀 → AST_LAMBDA 节点
- Sema: 自动生成 __lambda_N 顶层函数 + 符号注册
- Sema: analyze_call_expr 支持 TYPE_CLOSURE 变量调用
- Codegen: lambda 表达式返回函数指针(i64), 调用点载入+IntToPtr+间接call
- VarEntry.closure_fn 追踪闭包变量对应的生成函数

限制(MVP v0.1): 非捕获 lambda, 返回类型固定 i64

+6 sema 测试 + 1 集成测试, 209 测试全部通过

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 15:07:03 +08:00
Serendipity c8da286d31 feat: in/out 参数 — out 关键字引用传递
fn swap(out x: i64, out y: i64) 声明 out 参数,codegen 层面
函数签名变为 T* 指针,调用点自动传 &variable 地址。
in 是默认行为(值传递),无需显式标注。

Token → Parser → Sema → Codegen 全流水线:
- TOK_OUT + "out" 关键字注册
- AST parameter.is_out 字段
- parse_function 解析 out 前缀
- Sema: out 参数注册为 SYM_VARIABLE+is_mut(可赋值)
- Codegen: LLVM 函数签名使用 T*,调用点传 alloca

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 14:45:38 +08:00
Serendipity 0a0667776a feat: const 编译期常量 — const N = 42;
Token(77): +TOK_CONST, AST_LET_STMT 新增 is_const, Symbol 新增 const_value
sema: 字面量初始化自动折叠为编译期常量值

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 14:26:59 +08:00
Serendipity f7710ede9d feat: ?? 空值合并运算符 — a ?? b → if a!=0 {a} else {b}
Token(76): +TOK_QMARK_QMARK, 优先级 PREC_COALESCE=15 (||之上, |>之下)
parser 去糖为 if-expr, 零 sema/codegen 改动

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 14:18:48 +08:00
Serendipity caf17e16fc feat: for step 步长 — for i in 0 to 10 step 2 { ... }
Token(75): +TOK_STEP, desugar_for 增加 step_expr 参数, 默认 step=1

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 14:13:09 +08:00
Serendipity 71208a87f4 feat: 多返回值 fn f(...) -> (T, U) { return (e1, e2); }
Parser: -> (T1,T2) 解析为隐式 struct __ret_funcname, return (e1,e2) 去糖为 struct init
返回的 struct 可访问字段 _0, _1, ... 无 sema/codegen 改动

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 14:07:13 +08:00
Serendipity 06c8773fac feat: 装饰器 #[attr] 语法 — parser 解析并跳过
Token(74): +TOK_HASH, lexer 识别 '#', parser 在 fn/struct/enum 前解析 #[ident]
语法就位, 后续可扩展存储属性到 AST 节点

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 14:00:42 +08:00
Serendipity 443b22bdf1 feat: 列表推导式 [for x in arr: expr] — parser+sema+codegen
AST(29): +AST_LIST_COMP, parser 解析 [for var in expr: body]
sema: 创建子作用域注册循环变量, codegen: for 循环绑定+填充结果数组
已知限制: 仅支持 2 元素及以下数组 (大数组 alloca 对齐问题待修)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 13:58:54 +08:00
Serendipity 0088347576 feat: defer 延迟执行 — defer { stmts; } 在 return 前按 LIFO 执行
Token(73): +TOK_DEFER, AST(28): +AST_DEFER_STMT, 新增 38_defer.l
parser: defer { ... } 块 + defer expr; 表达式两种形式
codegen: defer 栈压入 block, emit_deferred() 在 return 前 LIFO 发射

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 13:51:10 +08:00
Serendipity e60021b684 docs: language-reference.md v0.7 全面重写
新增章节: 泛型、Trait、模块系统(mod+use+pub)、ADT枚举、if let、if-expr
更新: 类型表(14种)、测试数据(197+37)、版本号、快速参考卡片
2026-06-07 13:38:47 +08:00
Serendipity 1aafde0fd9 chore: 版本 v0.7.0 2026-06-07 13:08:21 +08:00
Serendipity 2247fa39f4 docs: CLAUDE.md v0.7 全面更新 — 架构/类型/限制/测试同步 2026-06-07 13:04:59 +08:00
Serendipity c285a145c8 refactor: typeck.c 652→559 — 泛型单态化提取为 mono.c (97行) 2026-06-07 13:00:10 +08:00
Serendipity 82b0570869 refactor: codegen_expr switch→Visitor dispatch (12 handler)
cg_expr.c 440行: 12 表达式handler提取为独立函数 + AstDispatch 表
codegen_expr 从巨型 switch 改为 ast_visit() 查表调用
新增表达式节点: codegen_expr_init() 加一行即可
修复: to_llvm_type/type_info_to_llvm TYPE_ENUM/STR 完整映射

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 12:59:18 +08:00
Serendipity bcf6508c4c feat: use 语句 — use module::item 导入模块符号
修复审查 #2: use 语句从 skippable TODO 实现为完整符号导入
  use math_mod::add → 加载模块, 匹配 pub 函数/结构体/枚举, 零前缀导入
新增 37_use.l 集成测试

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 12:53:18 +08:00
Serendipity a070883ae4 fix: sprintf→snprintf + strcpy→memcpy 缓冲区安全加固 2026-06-07 12:52:09 +08:00
Serendipity 376738d7fc docs: 全面架构与代码审查报告 (2026-06-07) 2026-06-07 12:40:43 +08:00
Serendipity 35e2691cb7 chore: 删除自动化分析报告目录 2026-06-06 21:28:48 +08:00
Serendipity b34ad17aad refactor: AST Visitor dispatch — sema analyze_expr switch→vtable
新增 src/ast/visit.h/c: AstDispatch 函数指针表 + ast_visit() 统一入口
analyze_expr 的 switch 替换为 dispatch 表, 10 个 handler 通过 SEMA_HANDLER 宏注册
新增 AST 节点: 在 analyze_expr_init() 加一行即可, 编译器会检查函数签名

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 20:36:13 +08:00
Serendipity 3733b41453 refactor: TypeTable 数据驱动 — promote/convert/numeric/comparable 统一查表
新增 src/sema/type_table.h/c: TypeDesc{promote_rank, bit_width, is_signed, is_numeric}
promote/can_implicit_convert/is_numeric/is_comparable 从硬编码 switch 改为查表
新增类型只需在 TABLE[] 加一行, 从 7+ 文件改为 3-4 文件 (token + table + codegen)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 20:31:32 +08:00
Serendipity 66b170a27f docs: CLAUDE.md 反映 desugar pass + 文件行数更新
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 20:20:50 +08:00
Serendipity eaae0b1c62 refactor: 独立 desugar pass — match/guard/for/if-let/复合赋值去糖提取
5 种去糖逻辑从 parser.c/expr.c 内联代码提取到 desugar.c:
  desugar_match() — match → let + if-else 链
  desugar_guard() — guard → if !(cond)
  desugar_for()  — for-in-to → var + while
  desugar_if_let() — if let → let + if
  desugar_compound_assign() — +=/-= → assign + binary

parser.c 662→564 行, 新增 desugar.c 109 行, 管道+插值保留在 expr.c

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 20:20:33 +08:00
Serendipity f3cca30cca docs: CLAUDE.md 数据同步 — 197单元+36集成,架构图反映文件拆分
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 20:16:51 +08:00
Serendipity 6d1db585c4 refactor: sema.c + codegen.c 拆分,全部源文件 <800 行
sema.c 1129行 → sema.c 499行 + typeck.c 629行 + sema_internal.h 51行
  - typeck.c: 表达式类型检查 (10个analyze_*函数) + 泛型单态化 + 类型关系
  - sema.c: analyze_node + sema_analyze

codegen.c 947行 → codegen.c 453行 + cg_expr.c 440行 + codegen_internal.h 83行
  - cg_expr.c: LLVM表达式生成 + 类型映射 (to_llvm_type/coerce_int/type_info_to_llvm)
  - codegen.c: 语句生成 + 模块入口 + 符号表 + 内存清理

全部核心源文件 <800 行限制: parser(662+498), sema(499+629), codegen(453+440)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 19:26:54 +08:00
Serendipity 90d081c3fd fix: P0审查修复 P2补强 — parser.c 拆分 + 测试扩充
P0-③: parser.c 1211行 → parser.c 662行 + expr.c 498行 + parse_internal.h 71行
  - expr.c: 表达式解析 (Pratt主循环/字面量/标识符/类型/运算符)
  - parser.c: 语句/声明/程序入口 (block/match/let/if/while/for/guard/fn/parse)
  - parse_internal.h: 共享 Parser struct + 内联辅助 + 向前声明

P2-①: parser 测试 5函数→20函数, 15断言→54断言
  - 新增: struct声明、字面量类型、优先级链、guard去糖、命名参数
  - 新增: 字段访问、方法调用、match、enum声明、for去糖、管道
  - 新增: 类型别名、trait声明、数组类型、if表达式

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 18:57:07 +08:00
Serendipity 466be76fd8 fix: P1审查修复 — error.c arena化 + Self类型解析 + trait查找加固 + 缓冲区安全
P1-①: error_init/add 从 malloc/realloc/strdup 改为 arena_alloc/arena_strdup
P1-②: impl 方法中 Self 类型在 sema 解析为实际结构体名
P1-③: trait 方法 fallback 增加前缀校验(strncmp),method_name 统一更新
P1-④: codegen args[16] 增加溢出检查,移除 parser 未使用的 mods/uses 数组
新增: 36_self_type.l 集成测试(Self 类型 + trait 方法)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 17:09:28 +08:00
Serendipity 17c19fd9b9 fix: P0审查修复 — CLAUDE.md数据更新 + load_module malloc→arena 2026-06-06 16:54:23 +08:00
Serendipity b3b3d285f9 feat: trait 接口系统 — trait Show { fn method } + extend Trait Struct { } 2026-06-06 16:41:21 +08:00
Serendipity 9169796b77 feat: 泛型单态化完成 — fn id<T>(x: T) -> T 全流水线通过 2026-06-06 16:25:18 +08:00
Serendipity 350eeef3c5 feat: 泛型基础设施 — <T>解析 + 类型推断 (单态化 codegen 待补) 2026-06-06 16:17:15 +08:00
Serendipity fa734b8a23 feat: 模块系统 mod + pub — 多文件编译支持 2026-06-06 16:09:30 +08:00
Serendipity e02cc7b1d6 feat: if let 语句 — if let Option::Some = expr { ... } 2026-06-06 15:20:34 +08:00
Serendipity 0e1f8c5795 feat: 枚举关联数据 ADT — enum Option { Some(i64), None } 2026-06-06 14:21:43 +08:00
Serendipity 0ec3c3d65f feat: 表达式作为值 — if/else 和 block 可产生值 2026-06-06 13:52:36 +08:00
Serendipity 380b52930a docs: Token 计数修正 50→67 (AGENTS.md + CLAUDE.md) 2026-06-06 13:33:39 +08:00
Serendipity 031dedfb8e refactor: 拆分 sema analyze_expr 为 9 个辅助函数 + 调度器 2026-06-06 01:36:37 +08:00
Serendipity de91886712 fix: CreateProcess 替代 system() 调 ld.lld,消除 shell 转义问题 v0.6.0 2026-06-05 22:07:53 +08:00
Serendipity 39d8bad022 feat: 自包含安装包 — ld.lld + MinGW 库 (39MB, 零依赖) 2026-06-05 22:01:01 +08:00
Serendipity 1e161ecfff feat: NSIS 安装包脚本 (62MB, 含 LLVM+clang+lld) 2026-06-05 21:51:22 +08:00
Serendipity 143174ee4f refactor: 链接器从 gcc 改为 clang + lld(优先 clang→fallback gcc) 2026-06-05 21:43:48 +08:00
Serendipity 2baf762d82 docs: README/CHANGELOG/语言参考更新至 v0.6 2026-06-05 21:02:05 +08:00
Serendipity 459d1e1e10 feat: 管道 |> + 字符串插值 \(expr) — P0 四特性收官 2026-06-05 20:59:00 +08:00
Serendipity 6b6925b2b8 feat: 命名参数 draw_rect(width: 10, height: 20) 2026-06-05 20:54:58 +08:00
Serendipity c6e492662e feat: guard 语句 guard x >= 0 else { return -1; } 2026-06-05 20:49:03 +08:00
Serendipity 18172ca724 feat: 新增 i32 / u64 / char 类型 + 字符字面量 "'a'" 2026-06-05 20:47:44 +08:00
Serendipity 6fc599e6c2 feat: impl 关键字改为 extend 2026-06-05 19:50:40 +08:00
Serendipity 175f8a6658 feat: 数组类型语法 [T; N] 改为 T[N] 后置语法 2026-06-05 19:48:56 +08:00