Skip to content 跳到正文
JW.

Interpreter · course project 解释器 · 课程项目

Mini-Lisp Mini-Lisp 解释器

A small Lisp interpreter in C++ with a parser, lexical scope, closures, macros, a REPL, and script execution.

一个用 C++ 写的小型 Lisp 解释器,包含 parser、词法作用域、闭包、宏、REPL 和脚本执行。

Status 状态 Public course project · complete 公开课程项目 · 已完成
Built with 主要技术 C++ · CMake
Mini-Lisp interpreter REPL

语言很小,但该有的运行时骨架都在

这是北大《软件设计实践》的课程项目。解释器支持 REPL 和脚本执行,值类型包括数字、布尔、字符串、符号、pair / list、空表、过程和宏。

我实现了 tokenizer、parser、带父环境的链式作用域、闭包和特殊形式分派。列表直接由 PairValue 组成,没有借宿主语言的现成 list 偷懒。

definequasiquote

特殊形式包括 definelambdaifbeginletcond、短路 and/or、引用和简单宏。内建过程覆盖算术、比较、字符串、I/O 和 map/filter/reduce

(define-macro unless (cond body) (quasiquote (if (not (unquote cond)) (begin (unquote body)) ())))

它仍然只是一个教学用解释器

数字统一用 double,没有尾递归优化,错误处理和标准库也很小。把这些边界明确写出来,比假装它接近完整 Scheme 更诚实。

这个项目最有意思的地方,是把“求值”从一个课堂概念变成了一套自己能逐步跟进去的运行规则。

A small language with a real runtime skeleton

This was built for PKU's Software Design Practice course. The interpreter supports a REPL and scripts, with numbers, booleans, strings, symbols, pairs and lists, the empty list, procedures, and macros.

I implemented the tokenizer, parser, parent-linked lexical environments, closures, and special-form dispatch. Lists are built from PairValue rather than borrowed from a host-language list type.

From define to quasiquote

Special forms include define, lambda, if, begin, let, cond, short-circuiting and/or, quoting, and simple macros. Built-ins cover arithmetic, comparison, strings, I/O, and map/filter/reduce.

(define-macro unless (cond body) (quasiquote (if (not (unquote cond)) (begin (unquote body)) ())))

It is still a teaching interpreter

Numbers are all double, there is no tail-call optimization, and error handling and the standard library are deliberately small. Stating those limits is more honest than pretending this is a complete Scheme.

The satisfying part was turning “evaluation” from a lecture concept into a set of runtime rules I could step through myself.