Lox解释器的Swift实现版本
Lox是一门迷你编程语言,来自于Bob Nystrom写的编译器教程Crafting Interpreters
UPDATE 2022/05/27 已经完成本书PART II
会尽量参照书中PART II所示的Java代码结构来实现
但是Swift本身有太多优秀和新奇的特点,比如enum
、Optional
、POP
等,我也会使用这些Swift独占特性,但整体的结构和逻辑应该和书本上的并无太大差异
-
4. Scanning
- Challenge 4: C-style /* ... */ block comments.
-
- Challenge 3: AST Printer In Reverse Polish Notation.
- GenerateAst tool
-
- Challenge 1: Add prefix and postfix ++ and -- operators.
- Challenge 2: Add support for the C-style conditional or “ternary” operator
?:
- Challenge 3: Add error productions to handle each binary operator appearing without a left-hand operand.
-
- Challenge 1: Allowing comparisons on types other than numbers could be useful.
- Challenge 2: Many languages define + such that if either operand is a string, the other is converted to a string and the results are then concatenated.
- Challenge 3: Change the implementation in visitBinary() to detect and report a runtime error when dividing by 0.
-
- Challenge 1: Add support to the REPL to let users type in both statements and expressions.
- Challenge 2: Make it a runtime error to access a variable that has not been initialized or assigned to
-
9. Control Flow
- Challenge 3: Add support for break statements.
-
10. Functions
- Challenge 2: Add anonymous function (lambdas) syntax.
-
- Challenge 3: Extend the resolver to report an error if a local variable is never used.
- Challenge 4: Store local variables in an array and look them up by index.
-
12. Classes
- Challenge 1: Add class methods.
- Challenge 2: Support getter methods.
-
13. Inheritance
- Challenge 1: Multiple inheritance. Nothing to implement...?
- Challenge 2: Reverse method lookup order in class hierarchy.
我尽量使用TDD的思想来开发每个阶段,所以Xcode工程中包含了使用XCTest
实现的单元测试文件。
使用 SPM 来管理frameworks、依赖文件
解释器的主体实现包含在一个framework中,通过一个简单的CLI程序使用该framework来运行解释器
- slox: 可执行文件,用于在CLI中直接运行解释器
- LoxCore: 包含Lox核心实现的Swift framework