1 Star 0 Fork 0

yanggeorge / minilisp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

MiniLisp背景

写一个主流语言的编译器非常困难,仅仅前端Parser的工作量就很大。直到看到 这个项目rui314/minilisp

用Java实现一个这样的Lisp解释器应该很有趣。

功能

目前的版本包含了如下特性,

  • 整数
(+ 1 2) ;; -> 3
  • cell的操作,如:cons,car,cdr

cons组成一个cell

(define pair (cons 'a 'b))
(println pair) ;; -> (a . b)
(println (car pair)) ;; -> a
(println (cdr pair)) ;; -> b
  • 基本的函数,如:+,-,if, while, setq

define关键字定义一个变量,println打印结果。

(if (= 1 1) 
   (println 'yes)
   (println 'no)) ;; -> yes
   
(define i 0)
(define array ())
(while (<= i 3)
   (setq array (cons i array))
   (setq i (+ i 1)))
(println array)  ;; -> (3 2 1 0)
  • 自定义函数

用defun关键字定义函数

(defun add (x y)
   (+ x y)) 
(println (add 1 2)) ;; -> 3 

可以进行参数的匹配,因为 (1 2) <=> (1 . (2)), 所以当形参是 (x . y) 的时候, x 为 1,y 为 (2)。

(defun list (x . y)
    (println x)
    (println y)
    (cons x y)) 

(define arr (list 1 2))
;;x -> 1
;;y -> (2)
(println arr) ;; -> (1 2)

再看一个例子,形参是(x . (y z)) 匹配 (1 2 3)的话,结果是 x -> 1,y -> 2, z -> 3

(defun match ( x . ( y z ))
   (println x)
   (println y)
   (println z))
(match 1 2 3)
;; x -> 1
;; y -> 2
;; z -> 3
  • 闭包

每次执行都有结果返回,但是默认不打印。所以需要调用println来对返回的结果进行打印。

;; A countup function. We use lambda to introduce local variables because we
;; do not have "let" and the like.
(define counter
  ((lambda (count)
     (lambda ()
       (setq count (+ count 1))
       count))  ;;
   0))

(println (counter))  ; -> 1
(println (counter))  ; -> 2

;; This will not return 12345 but 3. Variable "count" in counter function
;; is resolved based on its lexical context rather than dynamic context.
(println ((lambda (count) (counter)) 12345))  ; -> 3

defmacro关键字定义一个宏。unless是一个宏。list是一个工具函数,会生成一个list expression。macroexpand是一个函数,实现对宏的扩展。

(defun list (x . y)
  (cons x y))
  
(defmacro unless (condition expr)
  (list 'if condition () expr))
  
(define x 0)
(println (unless (= x 0) '(x is not 0)))  ; -> ()
(println (unless (= x 1) '(x is not 1)))  ; -> (x is not 1)

(println (macroexpand '(unless (= x 1) '(x is not 1))))
;; -> (if (= x 1) () '(x is not 1))

运行

该项目是基于Java8和maven的,所以环境要首先支持。

编译打包

进入项目根目录,运行如下命令

 ~/work/minilisp/$ ./bin/package.sh 
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< com.threelambda:minilisp >----------------------
[INFO] Building minilisp 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
......
......

执行hello.sh

 ~/work/minilisp/$ ./bin/hello.sh                     
(hello word)

REPL方式运行

 ~/work/minilisp/$ ./bin/run.sh 
  (println 'hello)
 hello

执行文件

nqueens.lisp来自于rui314/minilisp 。 但是我修改了几个地方,因为实现的有点不同。以下是4皇后的解法。

 ~/work/minilisp/$ ./bin/run.sh < examples/nqeens.lisp
start
(x @ x x)
(x x x @)
(@ x x x)
(x x @ x)
$
(x x @ x)
(@ x x x)
(x x x @)
(x @ x x)
$
done

演示

  • 运行 hello.sh
  • REPL运行
(define name 'ym)
(define put println)  
(put name) ;; -> ym 
  • 运行examples/life.lisp和examples/nqueens.lisp

show_minilisp

MIT License Copyright (c) 2018 alenym@qq.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

mini-lisp 是一个精简的Lisp语言解释器,实现了闭包,宏等特性。 展开 收起
Java 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/yanggeorge/minilisp.git
git@gitee.com:yanggeorge/minilisp.git
yanggeorge
minilisp
minilisp
master

搜索帮助