Skip to content

Commit

Permalink
Improve Lisp (#344)
Browse files Browse the repository at this point in the history
* Add println

* Rewrite fib

* Refactor parse

* Update lisp completer

* Store args in env

* Add 'or' and 'and' builtins

* Add factorial.lsp

* Update docs

* Add cat operation

* Add join operation

* Add system command

* Add time command (#346)

* Add time command

* Fix merge artefact

* Fix call to realtime

* Replace clock syscalls with device files (#345)

* Replace clock syscalls with device files

* Add missing newline to read

* Update time command

* Use Rc<RefCell<Env>>

* Add first TCO

* Remove Box

* Change result of env_for_lambda

* Run clippy

* Remove env clone

* Remove TCO

* Change return type of env_for_lambda
  • Loading branch information
vinc authored Jun 6, 2022
1 parent 13ca997 commit b2da751
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 110 deletions.
17 changes: 8 additions & 9 deletions doc/lisp.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
A minimalist Lisp interpreter is available in MOROS to extend the capabilities
of the Shell.

MOROS Lisp is a Lisp-1 dialect inspired by Scheme and Clojure.

It started from [Risp](https://github.com/stopachka/risp) and was extended to
include the seven primitive operators and the two special forms of John
McCarthy's paper "Recursive Functions of Symbolic Expressions and Their
Computation by Machine" (1960) and "The Roots of Lisp" (2002) by Paul Graham.

MOROS Lisp dialect is also inspired by Scheme and Clojure.

In version 0.2.0 the whole implementation was refactored and the parser was
rewritten to use [Nom](https://github.com/Geal/nom). This allowed the addition
of strings to the language and reading from the filesystem.
Expand Down Expand Up @@ -53,18 +53,17 @@ And it can execute a file. For example a file located in `/tmp/fibonacci.lsp`
with the following content:

```lisp
(label fib
(lambda (n)
(cond
((< n 2) n)
(true (+ (fib (- n 1)) (fib (- n 2)))))))
(defn fib (n)
(cond
((< n 2) n)
(true (+ (fib (- n 1)) (fib (- n 2))))))
(print (fib 6))
(println (fib 10))
```

Would produce the following output:

```
> lisp /tmp/fibonacci.lsp
8
55
```
3 changes: 3 additions & 0 deletions dsk/ini/lisp/core.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@

(defn third (lst)
(second (rest lst)))

(defn println (exp)
(do (print exp) (print "\n")))
7 changes: 0 additions & 7 deletions dsk/tmp/fibonacci.lsp

This file was deleted.

15 changes: 15 additions & 0 deletions dsk/tmp/lisp/factorial.lsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(load "/ini/lisp/core.lsp")

(defn fact-acc (n acc)
(cond
((< n 2) acc)
(true (fact-acc (- n 1) (* acc n)))))

(defn fact (n)
(fact-acc n 1))

(println
(fact
(cond
((null? args) 10)
(true (parse (car args))))))
12 changes: 12 additions & 0 deletions dsk/tmp/lisp/fibonacci.lsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(load "/ini/lisp/core.lsp")

(defn fib (n)
(cond
((< n 2) n)
(true (+ (fib (- n 1)) (fib (- n 2))))))

(println
(fib
(cond
((null? args) 10)
(true (parse (car args))))))
5 changes: 4 additions & 1 deletion src/usr/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ pub fn copy_files(verbose: bool) {
copy_file("/ini/fonts/zap-vga-8x16.psf", include_bytes!("../../dsk/ini/fonts/zap-vga-8x16.psf"), verbose);

copy_file("/tmp/alice.txt", include_bytes!("../../dsk/tmp/alice.txt"), verbose);
copy_file("/tmp/fibonacci.lsp", include_bytes!("../../dsk/tmp/fibonacci.lsp"), verbose);

create_dir("/tmp/lisp", verbose);
copy_file("/tmp/lisp/factorial.lsp", include_bytes!("../../dsk/tmp/lisp/factorial.lsp"), verbose);
copy_file("/tmp/lisp/fibonacci.lsp", include_bytes!("../../dsk/tmp/lisp/fibonacci.lsp"), verbose);

create_dir("/tmp/beep", verbose);
copy_file("/tmp/beep/tetris.sh", include_bytes!("../../dsk/tmp/beep/tetris.sh"), verbose);
Expand Down
Loading

0 comments on commit b2da751

Please sign in to comment.