2021-07-21 07:40:40 +00:00
|
|
|
# MOROS Lisp
|
|
|
|
|
|
|
|
A minimalist Lisp interpreter is available in MOROS to extend the capabilities
|
|
|
|
of the Shell.
|
|
|
|
|
2022-06-06 14:02:34 +00:00
|
|
|
MOROS Lisp is a Lisp-1 dialect inspired by Scheme and Clojure.
|
|
|
|
|
2021-08-12 21:15:28 +00:00
|
|
|
It started from [Risp](https://github.com/stopachka/risp) and was extended to
|
2021-07-21 07:40:40 +00:00
|
|
|
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.
|
|
|
|
|
2021-12-12 10:43:57 +00:00
|
|
|
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.
|
|
|
|
|
2021-07-21 07:40:40 +00:00
|
|
|
## Seven Primitive Operators
|
|
|
|
- `quote` (with the `'` syntax)
|
|
|
|
- `atom` (aliased to `atom?`)
|
|
|
|
- `eq` (aliased to `eq?`)
|
|
|
|
- `car` (aliased to `first`)
|
|
|
|
- `cdr` (aliased to `rest`)
|
|
|
|
- `cons`
|
|
|
|
- `cond`
|
|
|
|
|
|
|
|
## Two Special Forms
|
|
|
|
- `label` (aliased to `def`)
|
|
|
|
- `lambda` (aliased to `fn`)
|
|
|
|
|
2021-12-12 10:43:57 +00:00
|
|
|
## Additional Builtins
|
2021-07-21 07:40:40 +00:00
|
|
|
- `defun` (aliased to `defn`)
|
2022-09-15 18:50:23 +00:00
|
|
|
- `apply`
|
2022-07-02 13:24:30 +00:00
|
|
|
- `type`
|
2022-08-25 06:48:19 +00:00
|
|
|
- `string`
|
2022-09-15 18:50:23 +00:00
|
|
|
- `string-encode` and `string-decode`
|
|
|
|
- `number-encode` and `number-decode`
|
2022-08-25 06:48:19 +00:00
|
|
|
- `regex-find`
|
|
|
|
- `parse`
|
2022-07-02 13:24:30 +00:00
|
|
|
- `system`
|
2022-08-25 06:48:19 +00:00
|
|
|
- `load`
|
2022-07-02 13:24:30 +00:00
|
|
|
|
|
|
|
- Arithmetic operations: `+`, `-`, `*`, `/`, `%`, `^`
|
|
|
|
- Trigonometric functions: `acos`, `asin`, `atan`, `cos`, `sin`, `tan`
|
|
|
|
- Comparisons: `>`, `<`, `>=`, `<=`, `=`
|
2022-08-25 06:48:19 +00:00
|
|
|
- Boolean operations: `not`, `and`, `or`
|
2022-09-15 18:50:23 +00:00
|
|
|
- String operations: `lines`
|
2022-08-25 06:48:19 +00:00
|
|
|
- File IO: `read-file`, `read-file-bytes`, `write-file-bytes`, `append-file-bytes`
|
|
|
|
|
|
|
|
## Core Library
|
|
|
|
- `null`, `null?`, `eq?`
|
|
|
|
- `atom?`, `string?`, `boolean?`, `symbol?`, `number?`, `list?`, `function?`, `lambda?`
|
|
|
|
- `first`, `second`, `third`, `rest`
|
2022-09-15 18:50:23 +00:00
|
|
|
- `map`, `reduce`, `append`, `reverse`
|
|
|
|
- `string-join`
|
2022-08-25 06:48:19 +00:00
|
|
|
- `read-line`, `read-char`
|
|
|
|
- `print`, `println`
|
|
|
|
- `write-file`, `append-file`
|
|
|
|
- `uptime`, `realtime`
|
|
|
|
- `regex-match?`
|
2021-07-21 07:40:40 +00:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
The interpreter can be invoked from the shell:
|
|
|
|
|
|
|
|
```
|
|
|
|
> lisp
|
|
|
|
MOROS Lisp v0.1.0
|
|
|
|
|
|
|
|
> (+ 1 2)
|
|
|
|
3
|
|
|
|
|
2022-06-26 08:00:54 +00:00
|
|
|
> (quit)
|
2021-07-21 07:40:40 +00:00
|
|
|
```
|
|
|
|
|
2022-08-25 06:48:19 +00:00
|
|
|
And it can execute a file. For example a file located in `/tmp/lisp/fibonacci.lsp`
|
2021-12-12 10:43:57 +00:00
|
|
|
with the following content:
|
2021-07-21 07:40:40 +00:00
|
|
|
|
|
|
|
```lisp
|
2022-08-27 11:07:32 +00:00
|
|
|
(load "/lib/lisp/core.lsp")
|
2022-08-25 06:48:19 +00:00
|
|
|
|
2022-06-06 14:02:34 +00:00
|
|
|
(defn fib (n)
|
|
|
|
(cond
|
|
|
|
((< n 2) n)
|
|
|
|
(true (+ (fib (- n 1)) (fib (- n 2))))))
|
2021-07-21 07:40:40 +00:00
|
|
|
|
2022-08-25 06:48:19 +00:00
|
|
|
(println
|
|
|
|
(cond
|
|
|
|
((null? args) "Usage: fibonacci <num>")
|
|
|
|
(true (fib(parse (car args))))))
|
2021-07-21 07:40:40 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Would produce the following output:
|
|
|
|
|
|
|
|
```
|
2022-08-25 06:48:19 +00:00
|
|
|
> lisp /tmp/lisp/fibonacci.lsp 20
|
|
|
|
6755
|
2021-07-21 07:40:40 +00:00
|
|
|
```
|