Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 65 additions & 8 deletions devel/0074.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
# [0074] 在 (scheme base) 中导出 call/cc,撰写对应的单元测试(包含文档)
# [0074] 完善 call/cc 支持:测试与底层代码拆分

## 1. 相关文档
- [dddd.md](dddd.md) - 任务文档模板

## 2. 任务相关的代码文件
- goldfish/scheme/base.scm
- tests/scheme/base/call-slash-cc-test.scm
- `goldfish/scheme/base.scm`
- `tests/scheme/base/call-slash-cc-test.scm`
- `tests/scheme/base/call-with-current-continuation-test.scm`
- `src/s7.c`
- `src/s7_continuation.c`

## 3. 如何测试

### 3.1 确定性测试(单元测试)
### 3.1 编译测试
```bash
xmake b goldfish
```
bin/gf test tests/scheme/base/call-slash-cc-test.scm

### 3.2 确定性测试(单元测试)
```bash
bin/gf test --changed-since=main
```

### 3.2 非确定性测试(文档验证)
### 3.3 continuation 专项测试
```bash
bin/gf test tests/scheme/base/call-slash-cc-test.scm
bin/gf test tests/scheme/base/call-with-current-continuation-test.scm
bin/gf test tests/scheme/base/dynamic-wind-test.scm
```

### 3.4 非确定性测试(文档验证)
```bash
bin/gf doc "call/cc"
bin/gf doc "call-with-current-continuation"
```
Expand All @@ -29,11 +44,13 @@ bin/gf test --changed-since=main
```

## 5. 2026-05-31 在 (scheme base) 中导出 call/cc,撰写对应的单元测试(包含文档)

### 5.1 What
确认 `(scheme base)` 已导出 `call/cc`(`call-with-current-continuation` 的简写),并为其撰写包含文档和丰富测试用例的单元测试文件。

1. `goldfish/scheme/base.scm` 中已导出 `call/cc` 和 `call-with-current-continuation`
1. `goldfish/scheme/base.scm` 第 214-215 行已导出 `call-with-current-continuation` 和 `call/cc`
2. 更新 `tests/scheme/base/call-slash-cc-test.scm`,补充文档注释和更多测试用例
3. 新建 `tests/scheme/base/call-with-current-continuation-test.scm`,提供完整名称的独立测试文件

### 5.2 Why
`call/cc` 是 Scheme 语言最核心的特性之一(first-class continuations),需要:
Expand All @@ -42,7 +59,7 @@ bin/gf test --changed-since=main
- 通过丰富的测试用例覆盖常见用法,包括基本返回、续延保存、多值返回、非局部退出等

### 5.3 How
1. **确认导出**:`goldfish/scheme/base.scm` 第 214-215 行已导出 `call-with-current-continuation` 和 `call/cc`
1. **确认导出**:`goldfish/scheme/base.scm` 中已导出 `call-with-current-continuation` 和 `call/cc`
2. **撰写测试**:在 `tests/scheme/base/call-slash-cc-test.scm` 中编写包含以下内容的测试:
- 基本用法:过程正常返回
- 调用续延立即返回
Expand All @@ -51,6 +68,8 @@ bin/gf test --changed-since=main
- 在嵌套表达式中使用
- 实现非局部退出(类似 break)
- 验证续延只影响调用栈、不恢复变量绑定
- 用 call/cc 实现简单的生成器(yield 模式)
- 用 call/cc 实现异常风格的抛出/捕获
3. **验证文档**:运行 `bin/gf doc "call/cc"` 确认能正确显示文档和测试用例

### 5.4 测试用例说明
Expand All @@ -64,3 +83,41 @@ bin/gf test --changed-since=main
| 嵌套表达式中使用 | 验证续延返回值正确参与外层表达式求值 |
| 非局部退出 | 使用 `call/cc` 实现类似 break 的控制流 |
| 变量绑定不恢复 | 验证调用续延只重置调用栈,不恢复变量状态 |
| 生成器(yield 模式) | 用 call/cc 实现简单的协程风格生成器 |
| 异常抛出/捕获 | 用 call/cc 实现 throw/catch 控制流 |

## 6. 2026-05-31 将 s7.c 中的 continuation 相关代码迁移到 s7_continuation.c

### 6.1 What
将 `src/s7.c` 中与 continuation/goto 相关的类型宏、GC 处理、标记函数、I/O 显示、核心实现等代码迁移到独立的 `src/s7_continuation.c` 中,降低 `s7.c` 的复杂度。

1. 创建 `src/s7_continuation.c`,实现 continuation 相关逻辑
2. 在 `src/s7.c` 中 `#include "s7_continuation.c"`,保持与现有拆分文件(如 `s7_scheme_predicate.c`)一致的编译方式
3. 从 `s7.c` 中移除约 760 行 continuation 相关代码

### 6.2 Why
- `s7.c` 目前超过 8.9 万行,模块化程度低,维护困难
- continuation 是 Scheme 核心特性,逻辑相对独立,适合拆分为单独模块
- 与项目中已有的 `s7_scheme_predicate.c`、`s7_scheme_write.c` 等拆分文件保持一致风格

### 6.3 How
1. **提取 continuation 宏定义**:将 `is_continuation`、`continuation_block`、`continuation_stack`、`call_exit_goto_loc`、`is_goto` 等宏迁移到 `s7_continuation.c`
2. **提取 GC 相关函数**:
- `process_continuation`(sweep 阶段)
- `mark_continuation`(mark 阶段)
3. **提取 I/O 函数**:
- `continuation_to_port`(显示/打印)
4. **提取核心实现**:
- `is_continuation_b_p`
- `s7_make_continuation`
- `call_with_current_continuation`
- `call_with_exit`
- `op_implicit_continuation_a`、`op_implicit_goto` 等
5. **提取 setter 函数**:
- `b_is_continuation_setter`
6. **更新注册点**:在 `s7.c` 中保留 `mark_function[T_CONTINUATION]`、`display_functions[T_CONTINUATION]` 等函数指针注册

### 6.4 已知问题与注意事项
- `s7.c` 中大量内部宏(如 `T_Con`、`type`、`block_t`、`push_stack`、`begin_temp` 等)在新文件中需要可用,因此采用 `#include "s7_continuation.c"` 方式而非独立编译单元
- `call_with_current_continuation` 依赖栈操作、op_stack、dynamic-wind 等内部机制,迁移时需确保所有依赖符号可见
- 迁移后必须完整运行测试套件,确保 continuation 行为未改变
Loading
Loading