Micro Lisp - 用不到200行的C代码实现一个非常小的Lisp编程语言

网友投稿 747 2022-10-30

Micro Lisp - 用不到200行的C代码实现一个非常小的Lisp编程语言

Micro Lisp - 用不到200行的C代码实现一个非常小的Lisp编程语言

Micro Lisp

Objective: implement a small Lisp/Scheme language in as little C code as possible.

This is a hobby project for educational purposes, it has bugs and may fail without warning. Pull requests and improvements welcome

The interpreter supports lambda, e.g.

((lambda (x) (cons x (quote 1))) (quote 7)) (7 . 1)

Note that lambda does not capture free variables (variables that are not passed as arguments and refer to an outer scope). Free variables will resolve to their assigned values in the environment when the body of the lambda is evaluated.

The special forms if and quote behave in a typical way:

(if (quote t) (quote 7) (quote 0)) 7

The only types are symbols and pairs.

Non-quoted symbols are looked up in the environment. If they have no associated value the result is null; in fact, zero. Because there is no numeric type a number e.g. 7 will be treated like any other symbol and looked up in the environment. Note in the examples above how numbers are quoted to prevent that.

The built-in primitives in the environment are: car, cdr, cons, eq?, pair?, read, write.

Also provided is apply which takes a function and a single list argument:

(apply write (quote ((hello world)))) (hello world) (quote t)

Lists can be built up by consing:

(apply write (cons (cons (quote hello) (cons (quote world) null)) null)) (hello world) (quote t)

Read Eval Print Loop

A REPL is implemented in micro-lisp itself. To try it out in a terminal:

cat repl.lisp - | ./micro-lisp

To exit, press 'control c' to terminate the process.

Note the - argument to cat to pipe stdin through, otherwise micro-lisp will receive end-of-file.

The source code for the REPL is in repl.lisp. It implements eval and provides an environment which resolves symbols to the primitive functions in the underlying micro-lisp interpreter.

Debugging with GDB

A .gdbinit file sets the target, breakpoints and runs the executable. Simply run gdb.

Pull requests welcome.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:一个示例程序显示汽车在地图上的移动像Uber一样
下一篇:SpringBoot集成ElasticSearch的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~