← 返回专栏列表

Crafting Interpreters:手写解释器

共 35 篇文章

21. Types of Values:tagged union 与动态值表示

本章把 clox 的单一 number Value 扩展为 tagged value system,支持 Boolean、nil 和未来的 heap-allocated objects。

22. Strings:clox 的字符串对象与内存管理

本章把 string 从简单的 C 字符数组提升为 clox 的 heap object,并实现 concatenation、比较、打印和对象释放。

23. Hash Tables:open addressing、tombstone 与 interning

本章实现 clox 的 hash table:用 open addressing 管理 key/value entries,并为字符串 interning 提供高效查找。

24. Global Variables:声明、读取与 assignment

本章把 clox 从只能编译 expression 扩展为能够编译 statements、声明 global variables,并在 VM 中读取和更新它们。

25. Local Variables:stack slot 与局部作用域

本章把 global variables 之外的局部变量放入 VM stack,通过 compiler 在编译期分配 local slot,实现高效的 lexical scope。

26. Jumping Back and Forth:bytecode 控制流与 backpatching

本章在 clox bytecode 中实现控制流:使用 jump instruction、backpatching 和 loop back edges 编译 if、logical operators、while 与 for。

27. Calls and Functions:call frame 与函数调用栈

本章把 Lox function 编译成独立 function object,并用 call frame 管理参数、局部变量、返回地址和嵌套调用。

28. Closures:upvalue 捕获与生命周期

本章实现 clox 的 closure:让 function 捕获外层局部变量,并在原始 stack frame 消失后继续访问这些变量。

29. Garbage Collection:mark-sweep 与对象可达性

本章为 clox 实现 mark-sweep garbage collector:从根集合追踪可达对象,清扫不可达对象,并在合适时机触发回收。

30. Classes and Instances:clox 对象与属性访问

本章把前面 tree-walk interpreter 的 object model 搬到 clox:实现 class、instance、fields 和属性读写。

第 3 / 4 页 · 共 35 篇