练习单纯使用 JS 编写简单的 玩具 LISP 解析器。
注:本项目是阅读和学习《Building an Interpreter from scratch》时的随手练习,并无实际用途。程序的原理、讲解和代码的原始出处请移步 http://dmitrysoshnikov.com/courses/essentials-of-interpretation/ (这个教程的作者非常用心,有能力的请多多支持作者)
$ npm test
或者
$ node test/test.js
程序的运行方法可以参考测试文件 test/test-evaluator.js
。
计算斐波那契数:
(defn fib (i)
(if (native.i64.eq i 1)
1
(if (native.i64.eq i 2)
2
(native.i64.add (fib (native.i64.sub i 1)) (fib (native.i64.sub i 2)))
)
)
)
(fib 8)
(defn accumulate (count)
(do
(let internalLoop
(fn (i result)
(if (native.i64.eq i 0)
result
(internalLoop (native.i64.sub i 1) (native.i64.add i result))
)
)
)
(internalLoop count 0)
)
)
(accumulate 100)
(defnr sumToOneHundred (i accu)
(if (native.i64.gt_s i 100)
(break accu)
(do
(let next (native.i64.add i 1))
(let sum (native.i64.add accu i))
(recur next sum)
)
)
)
(sumToOneHundred 1 0)
::TODO