9个版本

使用旧Rust 2015

0.1.9 2017年7月2日
0.1.8 2017年6月25日

#968编程语言

每月 23 次下载
用于 teko-rs

GPL-3.0 许可证

200KB
2K SLoC

Rust实现的Teko编程语言

此库实现了运行Teko的虚拟机。如果您正在寻找命令行界面,请参阅teko-rs

文档

通过运行cargo doc --open生成文档。

构建

您需要cargo,可以从rustup下载。

cargo build --release

构建库应该足够了。


lib.rs:

Rust实现的Teko编程语言。

关于

此实现提供了Teko编程语言的解析和评估工具。Teko属于Lisp-1语言家族,具有动态作用域、一级宏和尾递归优化。

Teko是为了用作日常脚本语言而设计的。该语言旨在易于实现且有用。将Teko与其他Lisp进行比较揭示了核心动机:实现一个超级最小化的Lisp,使其成为一个完整的编程语言。

Teko具有严格评估的特性,但缺乏内部可变性。这使得实现可以选择使用引用计数垃圾回收 - 因为无法创建循环 - 这对于实时应用来说很理想,因为它不会导致执行过程中的意外暂停。

为什么是Lisp?

以下是来自Let Over Lambda,第1章的最喜欢的摘录,完美地表达了这一点

Macros are the single greatest advantage that lisp has as a programming language and the single
greatest advantage of any programming language. With them you can do things that you simply
cannot do in other languages. Because macros can be used to transform lisp into other
programming languages and back, programmers who gain experience with them discover that all
other languages are just skins on top of lisp. This is the big deal. Lisp is special because
programming with it is actually programing at a higher level. Where most languages invent and
enforce syntactic and semantic rules, lisp is general and malleable.
With lisp, you make the rules.

代码示例

以下是Teko中的标志性hello world

(" Hello world!)

没有类似函数式的语言是完整的,没有尾递归阶乘函数的定义。

(def factorial (fn (n accum)
                   (if (= n 1)
                       accum
                       (factorial (- n 1) (* n accum)))))
(factorial 5 1)

用法

示例:使用此库来解析Teko

extern crate teko;
extern crate num_traits;
use num_traits::cast::ToPrimitive;
fn main() {
	let program = teko::parse::parse_string("
	(def factorial (fn (n accum)
	                   (if (= n 1)
	                       accum
	                       (factorial (- n 1) (* n accum)))))
	(write (factorial 5 1))").ok().unwrap();
	let env = teko::interpret::interpret(program);

	match env.result.1 {
		teko::data_structures::Coredata::Integer(ref value) => {
			assert_eq![value.to_i32().unwrap(), 120];
		}
		_ => {
			panic!["Expected Integer but got a different data type"];
		}
	}
}

注意,上面的示例中write没有产生结果,因此来自factorial的先前结果留在了环境中。

依赖关系

~0.3–1.1MB
~28K SLoC