90分钟实现一门编程语言——极简解释器教程

开发 开发工具 后端
与我两年之前实现的Scheme方言Lucida相比,iScheme除了没有字符串类型,其它功能和Lucida相同,而代码量只是前者的八分之一,编写时间是前者的十分之一(Lucida用了两天,iScheme用了一个半小时),可扩展性和易读性均秒杀前者。

关键字

解释器, C#, Scheme, 函数式编程

关于

本文介绍了如何使用C#实现一个简化但全功能的Scheme方言——iScheme及其解释器,通过从零开始逐步构建,展示了编程语言/解释器的工作原理。

作者

Lucida a.k.a Luc

如果你是通过移动设备阅读本教程,或者认为本文的代码字体太小的,请使用该链接以获得更好的可读性(博客园的markdown解析器实在诡异,这里就不多吐槽了)。

提示

如果你对下面的内容感兴趣:

  • 实现基本的词法分析,语法分析并生成抽象语法树。
  • 实现嵌套作用域和函数调用。
  • 解释器的基本原理。
  • 以及一些C#编程技巧。

那么请继续阅读。

如果你对以下内容感兴趣:

  • 高级的词法/语法分析技术。
  • 类型推导/分析。
  • 目标代码优化。

本文则过于初级,你可以跳过本文,但欢迎指出本文的错误 :-)

代码样例

代码示例

  1. public static int Add(int a, int b) {  
  2.     return a + b;  
  3. }  
  4.  
  5. >> Add(3, 4)  
  6. >> 7  
  7.  
  8. >> Add(5, 5)  
  9. >> 10 

这段代码定义了Add函数,接下来的>>符号表示对Add(3, 4)进行求值,再下一行的>> 7表示上一行的求值结果,不同的求值用换行分开。可以把这里的>>理解成控制台提示符(即Terminal中的PS)。

什么是解释器

解释器图示

解释器(Interpreter)是一种程序,能够读入程序并直接输出结果,如上图。相对于编译器(Compiler),解释器并不会生成目标机器代码,而是直接运行源程序,简单来说:

 解释器是运行程序的程序。

计算器就是一个典型的解释器,我们把数学公式(源程序)给它,它通过运行它内部的"解释器"给我们答案。

CASIO 计算器

iScheme编程语言

iScheme是什么?

  • Scheme语言的一个极简子集。
  • 虽然小,但变量,算术|比较|逻辑运算,列表,函数和递归这些编程语言元素一应俱全。
  • 非常非常慢——可以说它只是为演示本文的概念而存在。

OK,那么Scheme是什么?

计算机程序的构造与解释

以计算阶乘为例:

C#版阶乘

  1. public static int Factorial(int n) {  
  2.     if (n == 1) {  
  3.         return 1;  
  4.     } else {  
  5.         return n * Factorial(n - 1);  
  6.     }  

iScheme版阶乘

  1. (def factorial (lambda (n) (  
  2.     if (= n 1)  
  3.        1  
  4.        (* n (factorial (- n 1)))))) 

数值类型

由于iScheme只是一个用于演示的语言,所以目前只提供对整数的支持。iScheme使用C#的Int64类型作为其内部的数值表示方法。

定义变量

iScheme使用def关键字定义变量

  1. >> (def a 3)  
  2. >> 3  
  3.  
  4. >> a  
  5. >> 3 

算术|逻辑|比较操作

与常见的编程语言(C#, Java, C++, C)不同,Scheme使用波兰表达式,即前缀表示法。例如:

C#中的算术|逻辑|比较操作

  1. // Arithmetic ops  
  2. a + b * c  
  3. a / (b + c + d)  
  4. // Logical ops  
  5. (cond1 && cond2) || cond3  
  6. // Comparing ops  
  7. a == b  
  8. 1 < a && a < 3 

对应的iScheme代码

  1. ; Arithmetic ops  
  2. (+ a (* b c))  
  3. (/ a (+ b c d))  
  4. ; Logical ops  
  5. (or (and cond1 cond2) cond3)  
  6. ; Comparing ops  
  7. (= a b)  
  8. (< 1 a 3) 

需要注意的几点:

  1. iScheme中的操作符可以接受不止两个参数——这在一定程度上控制了括号的数量。
  2. iScheme逻辑操作使用andornot代替了常见的&&||!——这在一定程度上增强了程序的可读性。

顺序语句

iScheme使用begin关键字标识顺序语句,并以最后一条语句的值作为返回结果。以求两个数的平均值为例:

C#的顺序语句

  1. int a = 3;  
  2. int b = 5;  
  3. int c = (a + b) / 2; 

iScheme的顺序语句

  1. (def c (begin  
  2.     (def a 3)  
  3.     (def b 5)  
  4.     (/ (+ a b) 2))) 

控制流操作

iScheme中的控制流操作只包含if

if语句示例

  1. >> (define a (if (> 3 2) 1 2))  
  2. >> 1  
  3.  
  4. >> a  
  5. >> 1 

列表类型

iScheme使用list关键字定义列表,并提供first关键字获取列表的第一个元素;提供rest关键字获取列表除第一个元素外的元素。

iScheme的列表示例

  1. >> (define alist (list 1 2 3 4))  
  2. >> (list 1 2 3 4)  
  3.  
  4. >> (first alist)  
  5. >> 1  
  6.  
  7. >> (rest alist)  
  8. >> (2 3 4) 

定义函数

iScheme使用func关键字定义函数:

iScheme的函数定义

  1. (def square (func (x) (* x x)))  
  2.  
  3. (def sum_square (func (a b) (+ (square a) (square b)))) 

对应的C#代码

  1. public static int Square (int x) {  
  2.     return x * x;  
  3. }  
  4.  
  5. public static int SumSquare(int a, int b) {  
  6.     return Square(a) + Square(b);  

递归

由于iScheme中没有forwhile这种命令式语言(Imperative Programming Language)的循环结构,递归成了重复操作的唯一选择。

以计算最大公约数为例:

iScheme计算最大公约数

  1. (def gcd (func (a b)  
  2.     (if (= b 0)  
  3.         a  
  4.         (func (b (% a b)))))) 

对应的C#代码

  1. public static int GCD (int a, int b) {  
  2.     if (b == 0) {  
  3.         return a;  
  4.     } else {  
  5.         return GCD(b, a % b);  
  6.     }  

#p#

高阶函数

和Scheme一样,函数在iScheme中是头等对象,这意味着:

  • 可以定义一个变量为函数。
  • 函数可以接受一个函数作为参数。
  • 函数返回一个函数。

iScheme的高阶函数示例

  1. ; Defines a multiply function.  
  2. (def mul (func (a b) (* a b)))  
  3. ; Defines a list map function.  
  4. (def map (func (f alist)  
  5.     (if (empty? alist)  
  6.         (list )  
  7.         (append (list (f (first alist))) (map f (rest alist)))  
  8.         )))  
  9. ; Doubles a list using map and mul.  
  10. >> (map (mul 2) (list 1 2 3))  
  11. >> (list 2 4 6) 

小结

对iScheme的介绍就到这里——事实上这就是iScheme的所有元素,会不会太简单了? -_-

接下来进入正题——从头开始构造iScheme的解释程序。

解释器构造

iScheme解释器主要分为两部分,解析(Parse)和求值(Evaluation):

  • 解析(Parse):解析源程序,并生成解释器可以理解的中间(Intermediate)结构。这部分包含词法分析,语法分析,语义分析,生成语法树。
  • 求值(Evaluation):执行解析阶段得到的中介结构然后得到运行结果。这部分包含作用域,类型系统设计和语法树遍历。

词法分析

词法分析负责把源程序解析成一个个词法单元(Lex),以便之后的处理。

iScheme的词法分析极其简单——由于iScheme的词法元素只包含括号,空白,数字和变量名,因此C#自带的String#Split就足够。

iScheme的词法分析及测试

  1. public static String[] Tokenize(String text) {  
  2.     String[] tokens = text.Replace("("" ( ").Replace(")"" ) ").Split(" \t\r\n".ToArray(), StringSplitOptions.RemoveEmptyEntries);  
  3.     return tokens;  
  4. }  
  5.  
  6. // Extends String.Join for a smooth API.  
  7. public static String Join(this String separator, IEnumerable<Object> values) {  
  8.     return String.Join(separator, values);  
  9. }  
  10.  
  11. // Displays the lexes in a readable form.  
  12. public static String PrettyPrint(String[] lexes) {  
  13.     return "[" + ", ".Join(lexes.Select(s => "'" + s + "'") + "]";  
  14. }  
  15.  
  16. // Some tests  
  17. >> PrettyPrint(Tokenize("a"))  
  18. >> ['a']  
  19.  
  20. >> PrettyPrint(Tokenize("(def a 3)"))  
  21. >> ['(''def''a''3'')']  
  22.  
  23. >> PrettyPrint(Tokenize("(begin (def a 3) (* a a))"))  
  24. >> ['begin''(''def''a''3'')''(''*''a''a'')'')'

注意

  • 个人不喜欢String.Join这个静态方法,所以这里使用C#的扩展方法(Extension Methods)对String类型做了一个扩展。
  • 相对于LINQ Syntax,我个人更喜欢LINQ Extension Methods,接下来的代码也都会是这种风格。
  • 不要以为词法分析都是这么离谱般简单!vczh的词法分析教程给出了一个完整编程语言的词法分析教程。

语法树生成

得到了词素之后,接下来就是进行语法分析。不过由于Lisp类语言的程序即是语法树,所以语法分析可以直接跳过。

以下面的程序为例:

程序即语法树

  1. ;  
  2. (def x (if (> a 1) a 1))  
  3. ; 换一个角度看的话:  
  4. (  
  5.     def  
  6.     x  
  7.     (  
  8.         if 
  9.         (  
  10.             >  
  11.             a  
  12.             1  
  13.         )  
  14.         a  
  15.         1  
  16.     )  

更加直观的图片:

抽象语法树

这使得抽象语法树(Abstract Syntax Tree)的构建变得极其简单(无需考虑操作符优先级等问题),我们使用SExpression类型定义iScheme的语法树(事实上S Expression也是Lisp表达式的名字)。

抽象语法树的定义

  1. public class SExpression {  
  2.     public String Value { get; private set; }  
  3.     public List<SExpression> Children { get; private set; }  
  4.     public SExpression Parent { get; private set; }  
  5.  
  6.     public SExpression(String value, SExpression parent) {  
  7.         this.Value = value;  
  8.         this.Children = new List<SExpression>();  
  9.         this.Parent = parent;  
  10.     }  
  11.  
  12.     public override String ToString() {  
  13.         if (this.Value == "(") {  
  14.             return "(" + " ".Join(Children) + ")";  
  15.         } else {  
  16.             return this.Value;  
  17.         }  
  18.     }  

然后用下面的步骤构建语法树:

  1. 碰到左括号,创建一个新的节点到当前节点(current),然后重设当前节点。
  2. 碰到右括号,回退到当前节点的父节点。
  3. 否则把为当前词素创建节点,添加到当前节点中。

抽象语法树的构建过程

  1. public static SExpression ParseAsIScheme(this String code) {  
  2.     SExpression program = new SExpression(value: "", parent: null);  
  3.     SExpression current = program;  
  4.     foreach (var lex in Tokenize(code)) {  
  5.         if (lex == "(") {  
  6.             SExpression newNode = new SExpression(value: "(", parent: current);  
  7.             current.Children.Add(newNode);  
  8.             current = newNode;  
  9.         } else if (lex == ")") {  
  10.             current = current.Parent;  
  11.         } else {  
  12.             current.Children.Add(new SExpression(value: lex, parent: current));  
  13.         }  
  14.     }  
  15.     return program.Children[0];  

注意

  • 使用自动属性(Auto Property),从而避免重复编写样版代码(Boilerplate Code)。
  • 使用命名参数(Named Parameters)提高代码可读性:new SExpression(value: "", parent: null)new SExpression("", null)可读。
  • 使用扩展方法提高代码流畅性:code.Tokenize().ParseAsISchemeParseAsIScheme(Tokenize(code))流畅。
  • 大多数编程语言的语法分析不会这么简单!如果打算实现一个类似C#的编程语言,你需要更强大的语法分析技术:
    • 如果打算手写语法分析器,可以参考LL(k), Precedence Climbing和Top Down Operator Precedence。
    • 如果打算生成语法分析器,可以参考ANTLR或Bison。

作用域

作用域决定程序的运行环境。iScheme使用嵌套作用域。

以下面的程序为例

  1. >> (def x 1)  
  2. >> 1  
  3.  
  4. >> (def y (begin (def x 2) (* x x)))  
  5. >> 4  
  6.  
  7. >> x  
  8. >> 1 

作用域示例

利用C#提供的Dictionary<TKey, TValue>类型,我们可以很容易的实现iScheme的作用域SScope

iScheme的作用域实现

  1. public class SScope {  
  2.     public SScope Parent { get; private set; }  
  3.     private Dictionary<String, SObject> variableTable;  
  4.  
  5.     public SScope(SScope parent) {  
  6.         this.Parent = parent;  
  7.         this.variableTable = new Dictionary<String, SObject>();  
  8.     }  
  9.  
  10.     public SObject Find(String name) {  
  11.         SScope current = this;  
  12.         while (current != null) {  
  13.             if (current.variableTable.ContainsKey(name)) {  
  14.                 return current.variableTable[name];  
  15.             }  
  16.             current = current.Parent;  
  17.         }  
  18.         throw new Exception(name + " is not defined.");  
  19.     }  
  20.  
  21.     public SObject Define(String name, SObject value) {  
  22.         this.variableTable.Add(name, value);  
  23.         return value;  
  24.     }  

类型实现

iScheme的类型系统极其简单——只有数值,Bool,列表和函数,考虑到他们都是iScheme里面的值对象(Value Object),为了便于对它们进行统一处理,这里为它们设置一个统一的父类型SObject

  1. public class SObject { } 

数值类型

iScheme的数值类型只是对.Net中Int64(即C#里的long)的简单封装:

  1. public class SNumber : SObject {  
  2.     private readonly Int64 value;  
  3.     public SNumber(Int64 value) {  
  4.         this.value = value;  
  5.     }  
  6.     public override String ToString() {  
  7.         return this.value.ToString();  
  8.     }  
  9.     public static implicit operator Int64(SNumber number) {  
  10.         return number.value;  
  11.     }  
  12.     public static implicit operator SNumber(Int64 value) {  
  13.         return new SNumber(value);  
  14.     }  

注意这里使用了C#的隐式操作符重载,这使得我们可以:

  1. SNumber foo = 30;  
  2. SNumber bar = 40;  
  3. SNumber foobar = foo * bar; 

而不必:

  1. SNumber foo = new SNumber(value: 30);  
  2. SNumber bar = new SNumber(value: 40);  
  3. SNumber foobar = new SNumber(value: foo.Value * bar.Value); 

为了方便,这里也为SObject增加了隐式操作符重载(尽管Int64可以被转换为SNumberSNumber继承自SObject,但.Net无法直接把Int64转化为SObject):

  1. public class SObject {  
  2.     ...  
  3.     public static implicit operator SObject(Int64 value) {  
  4.         return (SNumber)value;  
  5.     }  

Bool类型

由于Bool类型只有True和False,所以使用静态对象就足矣。

  1. public class SBool : SObject {  
  2.     public static readonly SBool False = new SBool();  
  3.     public static readonly SBool True = new SBool();  
  4.     public override String ToString() {  
  5.         return ((Boolean)this).ToString();  
  6.     }  
  7.     public static implicit operator Boolean(SBool value) {  
  8.         return value == SBool.True;  
  9.     }  
  10.     public static implicit operator SBool(Boolean value) {  
  11.         return value ? True : False;  
  12.     }  

这里同样使用了C#的隐式操作符重载,这使得我们可以:

  1. SBool foo = a > 1;  
  2. if (foo) {  
  3.     // Do something...  

而不用

  1. SBool foo = a > 1 ? SBool.True: SBool.False;  
  2. if (foo == SBool.True) {  
  3.     // Do something...  

同样,为SObject增加隐式操作符重载

  1. public class SObject {  
  2.     ...  
  3.     public static implicit operator SObject(Boolean value) {  
  4.         return (SBool)value;  
  5.     }  

#p#

列表类型

iScheme使用.Net中的IEnumberable<T>实现列表类型SList

  1. public class SList : SObject, IEnumerable<SObject> {  
  2.     private readonly IEnumerable<SObject> values;  
  3.     public SList(IEnumerable<SObject> values) {  
  4.         this.values = values;  
  5.     }  
  6.     public override String ToString() {  
  7.         return "(list " + " ".Join(this.values) + ")";  
  8.     }  
  9.     public IEnumerator<SObject> GetEnumerator() {  
  10.         return this.values.GetEnumerator();  
  11.     }  
  12.     IEnumerator IEnumerable.GetEnumerator() {  
  13.         return this.values.GetEnumerator();  
  14.     }  

实现IEnumerable<SObject>后,就可以直接使用LINQ的一系列扩展方法,十分方便。

函数类型

iScheme的函数类型(SFunction)由三部分组成:

  • 函数体:即对应的SExpression
  • 参数列表。
  • 作用域:函数拥有自己的作用域

SFunction的实现

  1. public class SFunction : SObject {  
  2.     public SExpression Body { get; private set; }  
  3.     public String[] Parameters { get; private set; }  
  4.     public SScope Scope { get; private set; }  
  5.     public Boolean IsPartial {  
  6.         get {  
  7.             return this.ComputeFilledParameters().Length.InBetween(1, this.Parameters.Length);  
  8.         }  
  9.     }  
  10.  
  11.     public SFunction(SExpression body, String[] parameters, SScope scope) {  
  12.         this.Body = body;  
  13.         this.Parameters = parameters;  
  14.         this.Scope = scope;  
  15.     }  
  16.  
  17.     public SObject Evaluate() {  
  18.         String[] filledParameters = this.ComputeFilledParameters();  
  19.         if (filledParameters.Length < Parameters.Length) {  
  20.             return this;  
  21.         } else {  
  22.             return this.Body.Evaluate(this.Scope);  
  23.         }  
  24.     }  
  25.  
  26.     public override String ToString() {  
  27.         return String.Format("(func ({0}) {1})",  
  28.             " ".Join(this.Parameters.Select(p => {  
  29.                 SObject value = null;  
  30.                 if ((value = this.Scope.FindInTop(p)) != null) {  
  31.                     return p + ":" + value;  
  32.                 }  
  33.                 return p;  
  34.             })), this.Body);  
  35.     }  
  36.  
  37.     private String[] ComputeFilledParameters() {  
  38.         return this.Parameters.Where(p => Scope.FindInTop(p) != null).ToArray();  
  39.     }  
需要注意的几点
  • iScheme支持部分求值(Partial Evaluation),这意味着:

部分求值

  1. >> (def mul (func (a b) (* a b)))  
  2. >> (func (a b) (* a b))  
  3.  
  4. >> (mul 3 4)  
  5. >> 12  
  6.  
  7. >> (mul 3)  
  8. >> (func (a:3 b) (* a b))  
  9.  
  10. >> ((mul 3) 4)  
  11. >> 12 

也就是说,当SFunction的实际参数(Argument)数量小于其形式参数(Parameter)的数量时,它依然是一个函数,无法被求值。

这个功能有什么用呢?生成高阶函数。有了部分求值,我们就可以使用

  1. (def mul (func (a b) (* a b)))  
  2. (def mul3 (mul 3))  
  3.  
  4. >> (mul3 3)  
  5. >> 9 

而不用专门定义一个生成函数:

  1. (def times (func (n) (func (n x) (* n x)) ) )  
  2. (def mul3 (times 3))  
  3.  
  4. >> (mul3 3)  
  5. >> 9 
  • SFunction#ToString可以将其自身还原为源代码——从而大大简化了iScheme的理解和测试。

内置操作

iScheme的内置操作有四种:算术|逻辑|比较|列表操作。

我选择了表达力(Expressiveness)强的lambda方法表来定义内置操作:

首先在SScope中添加静态字段builtinFunctions,以及对应的访问属性BuiltinFunctions和操作方法BuildIn

  1. public class SScope {  
  2.     private static Dictionary<String, Func<SExpression[], SScope, SObject>> builtinFunctions =  
  3.         new Dictionary<String, Func<SExpression[], SScope, SObject>>();  
  4.     public static Dictionary<String, Func<SExpression[], SScope, SObject>> BuiltinFunctions {  
  5.         get { return builtinFunctions; }  
  6.     }  
  7.     // Dirty HACK for fluent API.  
  8.     public SScope BuildIn(String name, Func<SExpression[], SScope, SObject> builtinFuntion) {  
  9.         SScope.builtinFunctions.Add(name, builtinFuntion);  
  10.         return this;  
  11.     }  

注意:

  1. Func<T1, T2, TRESULT>是C#提供的委托类型,表示一个接受T1T2,返回TRESULT
  2. 这里有一个小HACK,使用实例方法(Instance Method)修改静态成员(Static Member),从而实现一套流畅的API(参见Fluent Interface)。

接下来就可以这样定义内置操作:

  1. new SScope(parent: null)  
  2.     .BuildIn("+", addMethod)  
  3.     .BuildIn("-", subMethod)  
  4.     .BuildIn("*", mulMethod)  
  5.     .BuildIn("/", divMethod); 

一目了然。

断言(Assertion)扩展

为了便于进行断言,我对Boolean类型做了一点点扩展。

  1. public static void OrThrows(this Boolean condition, String message = null) {  
  2.     if (!condition) { throw new Exception(message ?? "WTF"); }  

从而可以写出流畅的断言:

  1. (a < 3).OrThrows("Value must be less than 3."); 

而不用

  1. if (a < 3) {  
  2.         throw new Exception("Value must be less than 3.");  

算术操作

iScheme算术操作包含+ - * / %五个操作,它们仅应用于数值类型(也就是SNumber)。

从加减法开始:

  1. .BuildIn("+", (args, scope) => {  
  2.     var numbers = args.Select(obj => obj.Evaluate(scope)).Cast<SNumber>();  
  3.     return numbers.Sum(n => n);  
  4. })  
  5. .BuildIn("-", (args, scope) => {  
  6.     var numbers = args.Select(obj => obj.Evaluate(scope)).Cast<SNumber>().ToArray();  
  7.     Int64 firstValue = numbers[0];  
  8.     if (numbers.Length == 1) {  
  9.         return -firstValue;  
  10.     }  
  11.     return firstValue - numbers.Skip(1).Sum(s => s);  
  12. }) 

注意到这里有一段重复逻辑负责转型求值(Cast then Evaluation),考虑到接下来还有不少地方要用这个逻辑,我把这段逻辑抽象成扩展方法:

  1. public static IEnumerable<T> Evaluate<T>(this IEnumerable<SExpression> expressions, SScope scope)  
  2. where T : SObject {  
  3.     return expressions.Evaluate(scope).Cast<T>();  
  4. }  
  5. public static IEnumerable<SObject> Evaluate(this IEnumerable<SExpression> expressions, SScope scope) {  
  6.     return expressions.Select(exp => exp.Evaluate(scope));  

然后加减法就可以如此定义:

  1. .BuildIn("+", (args, scope) => (args.Evaluate<SNumber>(scope).Sum(s => s)))  
  2. .BuildIn("-", (args, scope) => {  
  3.     var numbers = args.Evaluate<SNumber>(scope).ToArray();  
  4.     Int64 firstValue = numbers[0];  
  5.     if (numbers.Length == 1) {  
  6.         return -firstValue;  
  7.     }  
  8.     return firstValue - numbers.Skip(1).Sum(s => s);  
  9. }) 

乘法,除法和求模定义如下:

  1. .BuildIn("*", (args, scope) => args.Evaluate<SNumber>(scope).Aggregate((a, b) => a * b))  
  2. .BuildIn("/", (args, scope) => {  
  3.     var numbers = args.Evaluate<SNumber>(scope).ToArray();  
  4.     Int64 firstValue = numbers[0];  
  5.     return firstValue / numbers.Skip(1).Aggregate((a, b) => a * b);  
  6. })  
  7. .BuildIn("%", (args, scope) => {  
  8.     (args.Length == 2).OrThrows("Parameters count in mod should be 2");  
  9.     var numbers = args.Evaluate<SNumber>(scope).ToArray();  
  10.     return numbers[0] % numbers[1];  
  11. }) 

逻辑操作

iScheme逻辑操作包括andornot,即与,或和非。

需要注意的是iScheme逻辑操作是短路求值(Short-circuit evaluation),也就是说:

  • (and condA condB),如果condA为假,那么整个表达式为假,无需对condB求值。
  • (or condA condB),如果condA为真,那么整个表达式为真,无需对condB求值。

此外和+ - * /一样,andor也可以接收任意数量的参数。

需求明确了接下来就是实现,iScheme的逻辑操作实现如下:

  1. .BuildIn("and", (args, scope) => {  
  2.     (args.Length > 0).OrThrows();  
  3.     return !args.Any(arg => !(SBool)arg.Evaluate(scope));  
  4. })  
  5. .BuildIn("or", (args, scope) => {  
  6.     (args.Length > 0).OrThrows();  
  7.     return args.Any(arg => (SBool)arg.Evaluate(scope));  
  8. })  
  9. .BuildIn("not", (args, scope) => {  
  10.     (args.Length == 1).OrThrows();  
  11.     return args[0].Evaluate(scope);  
  12. }) 

比较操作

iScheme的比较操作包括= < > >= <=,需要注意下面几点:

  • =是比较操作而非赋值操作。
  • 同算术操作一样,它们应用于数值类型,并支持任意数量的参数。

=的实现如下:

  1. .BuildIn("=", (args, scope) => {  
  2.     (args.Length > 1).OrThrows("Must have more than 1 argument in relation operation.");  
  3.     SNumber current = (SNumber)args[0].Evaluate(scope);  
  4.     foreach (var arg in args.Skip(1)) {  
  5.         SNumber next = (SNumber)arg.Evaluate(scope);  
  6.         if (current == next) {  
  7.             current = next;  
  8.         } else {  
  9.             return false;  
  10.         }  
  11.     }  
  12.     return true;  
  13. }) 

可以预见所有的比较操作都将使用这段逻辑,因此把这段比较逻辑抽象成一个扩展方法:

  1. public static SBool ChainRelation(this SExpression[] expressions, SScope scope, Func<SNumber, SNumber, Boolean> relation) {  
  2.     (expressions.Length > 1).OrThrows("Must have more than 1 parameter in relation operation.");  
  3.     SNumber current = (SNumber)expressions[0].Evaluate(scope);  
  4.     foreach (var obj in expressions.Skip(1)) {  
  5.         SNumber next = (SNumber)obj.Evaluate(scope);  
  6.         if (relation(current, next)) {  
  7.             current = next;  
  8.         } else {  
  9.             return SBool.False;  
  10.         }  
  11.     }  
  12.     return SBool.True;  

接下来就可以很方便的定义比较操作:

  1. .BuildIn("=", (args, scope) => args.ChainRelation(scope, (s1, s2) => (Int64)s1 == (Int64)s2))  
  2. .BuildIn(">", (args, scope) => args.ChainRelation(scope, (s1, s2) => s1 > s2))  
  3. .BuildIn("<", (args, scope) => args.ChainRelation(scope, (s1, s2) => s1 < s2))  
  4. .BuildIn(">=", (args, scope) => args.ChainRelation(scope, (s1, s2) => s1 >= s2))  
  5. .BuildIn("<=", (args, scope) => args.ChainRelation(scope, (s1, s2) => s1 <= s2)) 

注意=操作的实现里面有Int64强制转型——因为我们没有重载SNumber#Equals,所以无法直接通过==来比较两个SNumber

列表操作

iScheme的列表操作包括firstrestempty?append,分别用来取列表的第一个元素,除第一个以外的部分,判断列表是否为空和拼接列表。

firstrest操作如下:

  1. .BuildIn("first", (args, scope) => {  
  2.     SList list = null;  
  3.     (args.Length == 1 && (list = (args[0].Evaluate(scope) as SList)) != null).OrThrows("<first> must apply to a list.");  
  4.     return list.First();  
  5. })  
  6. .BuildIn("rest", (args, scope) => {  
  7.     SList list = null;  
  8.     (args.Length == 1 && (list = (args[0].Evaluate(scope) as SList)) != null).OrThrows("<rest> must apply to a list.");  
  9.     return new SList(list.Skip(1));  
  10. }) 

又发现相当的重复逻辑——判断参数是否是一个合法的列表,重复代码很邪恶,所以这里把这段逻辑抽象为扩展方法:

  1. public static SList RetrieveSList(this SExpression[] expressions, SScope scope, String operationName) {  
  2.     SList list = null;  
  3.     (expressions.Length == 1 && (list = (expressions[0].Evaluate(scope) as SList)) != null)  
  4.         .OrThrows("<" + operationName + "> must apply to a list");  
  5.     return list;  

有了这个扩展方法,接下来的列表操作就很容易实现:

  1. .BuildIn("first", (args, scope) => args.RetrieveSList(scope, "first").First())  
  2. .BuildIn("rest", (args, scope) => new SList(args.RetrieveSList(scope, "rest").Skip(1)))  
  3. .BuildIn("append", (args, scope) => {  
  4.     SList list0 = null, list1 = null;  
  5.     (args.Length == 2  
  6.         && (list0 = (args[0].Evaluate(scope) as SList)) != null  
  7.         && (list1 = (args[1].Evaluate(scope) as SList)) != null).OrThrows("Input must be two lists");  
  8.     return new SList(list0.Concat(list1));  
  9. })  
  10. .BuildIn("empty?", (args, scope) => args.RetrieveSList(scope, "empty?").Count() == 0) 

测试

iScheme的内置操作完成之后,就可以测试下初步成果了。

首先添加基于控制台的分析/求值(Parse/Evaluation)循环:

  1. public static void KeepInterpretingInConsole(this SScope scope, Func<String, SScope, SObject> evaluate) {  
  2.     while (true) {  
  3.         try {  
  4.             Console.ForegroundColor = ConsoleColor.Gray;  
  5.             Console.Write(">> ");  
  6.             String code;  
  7.             if (!String.IsNullOrWhiteSpace(code = Console.ReadLine())) {  
  8.                 Console.ForegroundColor = ConsoleColor.Green;  
  9.                 Console.WriteLine(">> " + evaluate(code, scope));  
  10.             }  
  11.         } catch (Exception ex) {  
  12.             Console.ForegroundColor = ConsoleColor.Red;  
  13.             Console.WriteLine(">> " + ex.Message);  
  14.         }  
  15.     }  

然后在SExpression#Evaluate中补充调用代码:

  1. public static void KeepInterpretingInConsole(this SScope scope, Func<String, SScope, SObject> evaluate) {  
  2.     while (true) {  
  3.         try {  
  4.             Console.ForegroundColor = ConsoleColor.Gray;  
  5.             Console.Write(">> ");  
  6.             String code;  
  7.             if (!String.IsNullOrWhiteSpace(code = Console.ReadLine())) {  
  8.                 Console.ForegroundColor = ConsoleColor.Green;  
  9.                 Console.WriteLine(">> " + evaluate(code, scope));  
  10.             }  
  11.         } catch (Exception ex) {  
  12.             Console.ForegroundColor = ConsoleColor.Red;  
  13.             Console.WriteLine(">> " + ex.Message);  
  14.         }  
  15.     }  

最后在Main中调用该解释/求值循环:

  1. static void Main(String[] cmdArgs) {  
  2.     new SScope(parent: null)  
  3.         .BuildIn("+", (args, scope) => (args.Evaluate<SNumber>(scope).Sum(s => s)))  
  4.         // 省略若干内置函数  
  5.         .BuildIn("empty?", (args, scope) => args.RetrieveSList("empty?").Count() == 0)  
  6.         .KeepInterpretingInConsole((code, scope) => code.ParseAsScheme().Evaluate(scope));  

运行程序,输入一些简单的表达式:

运行结果

看样子还不错 :-)

接下来开始实现iScheme的执行(Evaluation)逻辑。

#p#

执行逻辑

iScheme的执行就是把语句(SExpression)在作用域(SScope)转化成对象(SObject)并对作用域(SScope)产生作用的过程,如下图所示。

编程语言的实质

iScheme的执行逻辑就在SExpression#Evaluate里面:

  1. public class SExpression {  
  2.     // ...  
  3.     public override SObject Evaluate(SScope scope) {  
  4.         // TODO: Todo your ass.  
  5.     }  

首先明确输入和输出:

  1. 处理字面量(Literals):3;和具名量(Named Values):x
  2. 处理if(if (< a 3) 3 a)
  3. 处理def(def pi 3.14)
  4. 处理begin(begin (def a 3) (* a a))
  5. 处理func(func (x) (* x x))
  6. 处理内置函数调用:(+ 1 2 3 (first (list 1 2)))
  7. 处理自定义函数调用:(map (func (x) (* x x)) (list 1 2 3))

此外,情况1和2中的SExpression没有子节点,可以直接读取其Value进行求值,余下的情况需要读取其Children进行求值。

首先处理没有子节点的情况:

处理字面量和具名量

  1. if (this.Children.Count == 0) {  
  2.     Int64 number;  
  3.     if (Int64.TryParse(this.Value, out number)) {  
  4.         return number;  
  5.     } else {  
  6.         return scope.Find(this.Value);  
  7.     }  

接下来处理带有子节点的情况:

首先获得当前节点的第一个节点:

  1. SExpression first = this.Children[0]; 

然后根据该节点的Value决定下一步操作:

处理if

if语句的处理方法很直接——根据判断条件(condition)的值判断执行哪条语句即可:

  1. if (first.Value == "if") {  
  2.     SBool condition = (SBool)(this.Children[1].Evaluate(scope));  
  3.     return condition ? this.Children[2].Evaluate(scope) : this.Children[3].Evaluate(scope);  

处理def

直接定义即可:

  1. else if (first.Value == "def") {  
  2.     return scope.Define(this.Children[1].Value, this.Children[2].Evaluate(new SScope(scope)));  

处理begin

遍历语句,然后返回最后一条语句的值:

  1. else if (first.Value == "begin") {  
  2.     SObject result = null;  
  3.     foreach (SExpression statement in this.Children.Skip(1)) {  
  4.         result = statement.Evaluate(scope);  
  5.     }  
  6.     return result;  

处理func

利用SExpression构建SFunction,然后返回:

  1. else if (first.Value == "func") {  
  2.     SExpression body = this.Children[2];  
  3.     String[] parameters = this.Children[1].Children.Select(exp => exp.Value).ToArray();  
  4.     SScope newScope = new SScope(scope);  
  5.     return new SFunction(body, parameters, newScope);  

处理list

首先把获得list里元素的值,然后创建SList

  1. else if (first.Value == "list") {  
  2.     return new SList(this.Children.Skip(1).Select(exp => exp.Evaluate(scope)));  

处理内置操作

首先对参数求值,然后调用对应的内置函数:

  1. else if (SScope.BuiltinFunctions.ContainsKey(first.Value)) {  
  2.     var arguments = this.Children.Skip(1).Select(node => node.Evaluate(scope)).ToArray();  
  3.     return SScope.BuiltinFunctions[first.Value](arguments, scope);  

处理自定义函数调用

自定义函数调用有两种情况:

  1. 非具名函数调用:((func (x) (* x x)) 3)
  2. 具名函数调用:(square 3)

调用自定义函数时应使用新的作用域,所以为SFunction增加Update方法:

  1. public SFunction Update(SObject[] arguments) {  
  2.     var existingArguments = this.Parameters.Select(p => this.Scope.FindInTop(p)).Where(obj => obj != null);  
  3.     var newArguments = existingArguments.Concat(arguments).ToArray();  
  4.     SScope newScope = this.Scope.Parent.SpawnScopeWith(this.Parameters, newArguments);  
  5.     return new SFunction(this.Body, this.Parameters, newScope);  

为了便于创建自定义作用域,并判断函数的参数是否被赋值,为SScope增加SpawnScopeWithFindInTop方法:

  1. public SScope SpawnScopeWith(String[] names, SObject[] values) {  
  2.     (names.Length >= values.Length).OrThrows("Too many arguments.");  
  3.     SScope scope = new SScope(this);  
  4.     for (Int32 i = 0; i < values.Length; i++) {  
  5.         scope.variableTable.Add(names[i], values[i]);  
  6.     }  
  7.     return scope;  
  8. }  
  9. public SObject FindInTop(String name) {  
  10.     if (variableTable.ContainsKey(name)) {  
  11.         return variableTable[name];  
  12.     }  
  13.     return null;  

下面是函数调用的实现:

  1. else {  
  2.     SFunction function = first.Value == "(" ? (SFunction)first.Evaluate(scope) : (SFunction)scope.Find(first.Value);  
  3.     var arguments = this.Children.Skip(1).Select(s => s.Evaluate(scope)).ToArray();  
  4.     return function.Update(arguments).Evaluate();  

完整的求值代码

综上所述,求值代码如下

  1. public SObject Evaluate(SScope scope) {  
  2.     if (this.Children.Count == 0) {  
  3.         Int64 number;  
  4.         if (Int64.TryParse(this.Value, out number)) {  
  5.             return number;  
  6.         } else {  
  7.             return scope.Find(this.Value);  
  8.         }  
  9.     } else {  
  10.         SExpression first = this.Children[0];  
  11.         if (first.Value == "if") {  
  12.             SBool condition = (SBool)(this.Children[1].Evaluate(scope));  
  13.             return condition ? this.Children[2].Evaluate(scope) : this.Children[3].Evaluate(scope);  
  14.         } else if (first.Value == "def") {  
  15.             return scope.Define(this.Children[1].Value, this.Children[2].Evaluate(new SScope(scope)));  
  16.         } else if (first.Value == "begin") {  
  17.             SObject result = null;  
  18.             foreach (SExpression statement in this.Children.Skip(1)) {  
  19.                 result = statement.Evaluate(scope);  
  20.             }  
  21.             return result;  
  22.         } else if (first.Value == "func") {  
  23.             SExpression body = this.Children[2];  
  24.             String[] parameters = this.Children[1].Children.Select(exp => exp.Value).ToArray();  
  25.             SScope newScope = new SScope(scope);  
  26.             return new SFunction(body, parameters, newScope);  
  27.         } else if (first.Value == "list") {  
  28.             return new SList(this.Children.Skip(1).Select(exp => exp.Evaluate(scope)));  
  29.         } else if (SScope.BuiltinFunctions.ContainsKey(first.Value)) {  
  30.             var arguments = this.Children.Skip(1).Select(node => node.Evaluate(scope)).ToArray();  
  31.             return SScope.BuiltinFunctions[first.Value](arguments, scope);  
  32.         } else {  
  33.             SFunction function = first.Value == "(" ? (SFunction)first.Evaluate(scope) : (SFunction)scope.Find(first.Value);  
  34.             var arguments = this.Children.Skip(1).Select(s => s.Evaluate(scope)).ToArray();  
  35.             return function.Update(arguments).Evaluate();  
  36.         }  
  37.     }  

去除尾递归

到了这里iScheme解释器就算完成了。但仔细观察求值过程还是有一个很大的问题,尾递归调用:

  • 处理if的尾递归调用。
  • 处理函数调用中的尾递归调用。

Alex Stepanov曾在Elements of Programming中介绍了一种将严格尾递归调用(Strict tail-recursive call)转化为迭代的方法,细节恕不赘述,以阶乘为例:

  1. // Recursive factorial.  
  2. int fact (int n) {  
  3.     if (n == 1)  
  4.         return result;  
  5.     return n * fact(n - 1);  
  6. }  
  7. // First tranform to tail recursive version.  
  8. int fact (int n, int result) {  
  9.     if (n == 1)  
  10.         return result;  
  11.     else {  
  12.         n -= 1;  
  13.         result *= 1;  
  14.         return fact(n, result);// This is a strict tail-recursive call which can be omitted  
  15.     }  
  16. }  
  17. // Then transform to iterative version.  
  18. int fact (int n, int result) {  
  19.     while (true) {  
  20.         if (n == 1)  
  21.             return result;  
  22.         else {  
  23.             n -= 1;  
  24.             result *= 1;  
  25.         }  
  26.     }  

应用这种方法到SExpression#Evaluate,得到转换后的版本:

  1. public SObject Evaluate(SScope scope) {  
  2.     SExpression current = this;  
  3.     while (true) {  
  4.         if (current.Children.Count == 0) {  
  5.             Int64 number;  
  6.             if (Int64.TryParse(current.Value, out number)) {  
  7.                 return number;  
  8.             } else {  
  9.                 return scope.Find(current.Value);  
  10.             }  
  11.         } else {  
  12.             SExpression first = current.Children[0];  
  13.             if (first.Value == "if") {  
  14.                 SBool condition = (SBool)(current.Children[1].Evaluate(scope));  
  15.                 current = condition ? current.Children[2] : current.Children[3];  
  16.             } else if (first.Value == "def") {  
  17.                 return scope.Define(current.Children[1].Value, current.Children[2].Evaluate(new SScope(scope)));  
  18.             } else if (first.Value == "begin") {  
  19.                 SObject result = null;  
  20.                 foreach (SExpression statement in current.Children.Skip(1)) {  
  21.                     result = statement.Evaluate(scope);  
  22.                 }  
  23.                 return result;  
  24.             } else if (first.Value == "func") {  
  25.                 SExpression body = current.Children[2];  
  26.                 String[] parameters = current.Children[1].Children.Select(exp => exp.Value).ToArray();  
  27.                 SScope newScope = new SScope(scope);  
  28.                 return new SFunction(body, parameters, newScope);  
  29.             } else if (first.Value == "list") {  
  30.                 return new SList(current.Children.Skip(1).Select(exp => exp.Evaluate(scope)));  
  31.             } else if (SScope.BuiltinFunctions.ContainsKey(first.Value)) {  
  32.                 var arguments = current.Children.Skip(1).Select(node => node.Evaluate(scope)).ToArray();  
  33.                 return SScope.BuiltinFunctions[first.Value](arguments, scope);  
  34.             } else {  
  35.                 SFunction function = first.Value == "(" ? (SFunction)first.Evaluate(scope) : (SFunction)scope.Find(first.Value);  
  36.                 var arguments = current.Children.Skip(1).Select(s => s.Evaluate(scope)).ToArray();  
  37.                 SFunction newFunction = function.Update(arguments);  
  38.                 if (newFunction.IsPartial) {  
  39.                     return newFunction.Evaluate();  
  40.                 } else {  
  41.                     current = newFunction.Body;  
  42.                     scope = newFunction.Scope;  
  43.                 }  
  44.             }  
  45.         }  
  46.     }  

#p#

一些演示

基本的运算

基本的运算

高阶函数

高阶函数

回顾

小结

除去注释(貌似没有注释-_-),iScheme的解释器的实现代码一共333行——包括空行,括号等元素。

在这300余行代码里,实现了函数式编程语言的大部分功能:算术|逻辑|运算,嵌套作用域,顺序语句,控制语句,递归,高阶函数部分求值

与我两年之前实现的Scheme方言Lucida相比,iScheme除了没有字符串类型,其它功能和Lucida相同,而代码量只是前者的八分之一,编写时间是前者的十分之一(Lucida用了两天,iScheme用了一个半小时),可扩展性和易读性均秒杀前者。这说明了:

  1. 代码量不能说明问题。
  2. 不同开发者生产效率的差别会非常巨大。
  3. 这两年我还是学到了一点东西的。-_-

一些设计决策

使用扩展方法提高可读性

例如,通过定义OrThrows

  1. public static void OrThrows(this Boolean condition, String message = null) {  
  2.     if (!condition) { throw new Exception(message ?? "WTF"); }  

写出流畅的断言:

  1. (a < 3).OrThrows("Value must be less than 3."); 

声明式编程风格

Main函数为例:

  1. static void Main(String[] cmdArgs) {  
  2.     new SScope(parent: null)  
  3.         .BuildIn("+", (args, scope) => (args.Evaluate<SNumber>(scope).Sum(s => s)))  
  4.         // Other build  
  5.         .BuildIn("empty?", (args, scope) => args.RetrieveSList("empty?").Count() == 0)  
  6.         .KeepInterpretingInConsole((code, scope) => code.ParseAsIScheme().Evaluate(scope));  

非常直观,而且

  • 如果需要添加新的操作,添加写一行BuildIn即可。
  • 如果需要使用其它语法,替换解析函数ParseAsIScheme即可。
  • 如果需要从文件读取代码,替换执行函数KeepInterpretingInConsole即可。

不足

当然iScheme还是有很多不足:

语言特性方面:

  1. 缺乏实用类型:没有DoubleString这两个关键类型,更不用说复合类型(Compound Type)。
  2. 没有IO操作,更不要说网络通信。
  3. 效率低下:尽管去除尾递归挽回了一点效率,但iScheme的执行效率依然惨不忍睹。
  4. 错误信息:错误信息基本不可读,往往出错了都不知道从哪里找起。
  5. 不支持延续调用(Call with current continuation,即call/cc)。
  6. 没有并发。
  7. 各种bug:比如可以定义文本量,无法重载默认操作,空括号被识别等等。

设计实现方面:

  1. 使用了可变(Mutable)类型。
  2. 没有任何注释(因为觉得没有必要 -_-)。
  3. 糟糕的类型系统:Lisp类语言中的数据和程序可以不分彼此,而iScheme的实现中确把数据和程序分成了SObjectSExpression,现在我依然没有找到一个融合他们的好办法。

这些就留到以后慢慢处理了 -_-(TODO YOUR ASS)

延伸阅读

书籍

  1. Compilers: Priciples, Techniques and Tools Principles: http://www.amazon.co.uk/Compilers-Principles-Techniques-V-Aho/dp/1292024348/
  2. Language Implementation Patterns: http://www.amazon.co.uk/Language-Implementation-Patterns-Domain-Specific-Programming/dp/193435645X/
  3. *The Definitive ANTLR4 Reference: http://www.amazon.co.uk/Definitive-ANTLR-4-Reference/dp/1934356999/
  4. Engineering a compiler: http://www.amazon.co.uk/Engineering-Compiler-Keith-Cooper/dp/012088478X/
  5. Flex & Bison: http://www.amazon.co.uk/flex-bison-John-Levine/dp/0596155972/
  6. *Writing Compilers and Interpreters: http://www.amazon.co.uk/Writing-Compilers-Interpreters-Software-Engineering/dp/0470177071/
  7. Elements of Programming: http://www.amazon.co.uk/Elements-Programming-Alexander-Stepanov/dp/032163537X/

注:带*号的没有中译本。

文章

大多和编译前端相关,自己没时间也没能力研究后端。-_-

为什么编译技术很重要?看看Steve Yegge(没错,就是被王垠黑过的Google高级技术工程师)是怎么说的(需要翻墙)。

http://steve-yegge.blogspot.co.uk/2007/06/rich-programmer-food.html

本文重点参考的Peter Norvig的两篇文章:

  1. How to write a lisp interpreter in Python: http://norvig.com/lispy.html
  2. An even better lisp interpreter in Python: http://norvig.com/lispy2.html

几种简单实用的语法分析技术:

  1. LL(k) Parsing:
  2. Top Down Operator Precendence:http://javascript.crockford.com/tdop/tdop.html
  3. Precendence Climbing Parsing:http://en.wikipedia.org/wiki/Operator-precedence_parser

关于本文作者

曾经的Windows/.Net/C#程序员,研究生毕业后糊里糊涂变成Linux/Java开发者。所谓一入Java深似海,现在无比怀念使用C#的岁月。

对解释器/编译器感兴趣,现在正在自学Coursera的Compiler课程

欢迎来信交流技术:lunageek#gmail#com

原文链接:http://www.cnblogs.com/figure9/p/3620079.html

责任编辑:林师授 来源: 博客园
相关推荐

2012-09-04 11:20:31

2022-02-27 14:45:16

编程语言JavaC#

2019-08-15 07:13:54

负载平衡服务器迁移IIS

2022-09-07 08:05:32

GScript​编程语言

2021-05-31 07:22:46

ORM框架程序

2022-02-21 11:15:59

编程语言后端开发

2020-09-27 15:52:02

编程语言C 语言Python

2014-12-03 09:48:36

编程语言

2017-04-07 10:45:43

编程语言

2013-07-26 10:23:04

2018-07-16 12:36:48

编程语言PythonJava

2017-04-07 16:49:00

语言程序编程

2020-08-17 10:50:29

Python代码get

2011-07-14 17:58:11

编程语言

2015-07-28 15:35:48

学习语言

2013-09-13 14:08:01

2016-05-19 13:55:19

Linux运维编程语言

2020-11-12 07:00:50

JavaScript前端编程语言

2013-08-06 09:31:42

IT技术周刊

2019-11-18 11:00:58

程序员编程语言
点赞
收藏

51CTO技术栈公众号