Skip to content

Commit

Permalink
Extend and refactor Lisp implementation (#412)
Browse files Browse the repository at this point in the history
* Adopt a syntax closer to scheme

* Add parse and eval

* Replace Exp::Func with Exp::Primitive

* Refactor built in autocompletion

* Replace null by nil

* Fix test

* Update doc

* Bump version
  • Loading branch information
vinc authored Sep 20, 2022
1 parent 46137e6 commit ec57ff1
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 168 deletions.
26 changes: 13 additions & 13 deletions doc/lisp.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ of strings to the language and reading from the filesystem.
- `cond`

## Two Special Forms
- `label` (aliased to `def`)
- `lambda` (aliased to `fn`)
- `label` (aliased to `define` and `def`)
- `lambda` (aliased to `function`, `fun`, and `fn`)

## Additional Builtins
- `defun` (aliased to `defn`)
- `apply`
- `type`
- `string`
- `string-encode` and `string-decode`
- `number-encode` and `number-decode`
- `string->number`
- `string->bytes` and `bytes->string`
- `number->bytes` and `bytes->number`
- `regex-find`
- `parse`
- `system`
- `load`

Expand All @@ -47,7 +47,7 @@ of strings to the language and reading from the filesystem.
- File IO: `read-file`, `read-file-bytes`, `write-file-bytes`, `append-file-bytes`

## Core Library
- `null`, `null?`, `eq?`
- `nil`, `nil?`, `eq?`
- `atom?`, `string?`, `boolean?`, `symbol?`, `number?`, `list?`, `function?`, `lambda?`
- `first`, `second`, `third`, `rest`
- `map`, `reduce`, `append`, `reverse`
Expand All @@ -64,10 +64,10 @@ The interpreter can be invoked from the shell:

```
> lisp
MOROS Lisp v0.1.0
MOROS Lisp v0.4.0
> (+ 1 2)
3
> (+ 1 2 3)
6
> (quit)
```
Expand All @@ -78,15 +78,15 @@ with the following content:
```lisp
(load "/lib/lisp/core.lsp")
(defn fib (n)
(define (fibonacci n)
(cond
((< n 2) n)
(true (+ (fib (- n 1)) (fib (- n 2))))))
(true (+ (fibonacci (- n 1)) (fibonacci (- n 2))))))
(println
(cond
((null? args) "Usage: fibonacci <num>")
(true (fib(parse (car args))))))
((nil? args) "Usage: fibonacci <num>")
(true (fibonacci (string->number (car args))))))
```

Would produce the following output:
Expand Down
104 changes: 51 additions & 53 deletions dsk/lib/lisp/core.lsp
Original file line number Diff line number Diff line change
@@ -1,114 +1,112 @@
(defn eq? (x y)
(define (eq? x y)
(eq x y))

(defn atom? (x)
(define (atom? x)
(atom x))

(defn string? (x)
(define (string? x)
(eq? (type x) "string"))

(defn boolean? (x)
(define (boolean? x)
(eq? (type x) "boolean"))

(defn symbol? (x)
(define (symbol? x)
(eq? (type x) "symbol"))

(defn number? (x)
(define (number? x)
(eq? (type x) "number"))

(defn list? (x)
(define (list? x)
(eq? (type x) "list"))

(defn function? (x)
(define (function? x)
(eq? (type x) "function"))

(defn lambda? (x)
(eq? (type x) "lambda"))
(define nil '())

(def null '())
(define (nil? x)
(eq? x nil))

(defn null? (x)
(eq? x null))

(defn and (x y)
(define (and x y)
(cond
(x (cond (y true) (true false)))
(true false)))

(defn not (x)
(define (not x)
(cond (x false) (true true)))

(defn or (x y)
(define (or x y)
(cond (x true) (y true) (true false)))

(defn rest (x)
(define (rest x)
(cdr x))

(defn first (x)
(define (first x)
(car x))

(defn second (x)
(define (second x)
(first (rest x)))

(defn third (x)
(define (third x)
(second (rest x)))

(defn reduce (f ls)
(define (reduce f ls)
(cond
((null? (rest ls)) (first ls))
((nil? (rest ls)) (first ls))
(true (f (first ls) (reduce f (rest ls))))))

(defn string-join (ls s)
(define (string-join ls s)
(reduce (fn (x y) (string x s y)) ls))

(defn map (f ls)
(define (map f ls)
(cond
((null? ls) null)
((nil? ls) nil)
(true (cons
(f (first ls))
(map f (rest ls))))))

(defn append (x y)
(define (append x y)
(cond
((null? x) y)
((nil? x) y)
(true (cons (first x) (append (rest x) y)))))

(defn reverse (x)
(define (reverse x)
(cond
((null? x) x)
((nil? x) x)
(true (append (reverse (rest x)) (cons (first x) '())))))

(defn range (i n)
(define (range i n)
(cond
((= i n) null)
((= i n) nil)
(true (append (list i) (range (+ i 1) n)))))

(defn read-line ()
(string-decode (reverse (rest (reverse (read-file-bytes "/dev/console" 256))))))

(defn read-char ()
(string-decode (read-file-bytes "/dev/console" 4)))
(define (read-line)
(bytes->string (reverse (rest (reverse (read-file-bytes "/dev/console" 256))))))

(defn print (exp)
(do (append-file-bytes "/dev/console" (string-encode (string exp))) '()))
(define (read-char)
(bytes->string (read-file-bytes "/dev/console" 4)))

(defn println (exp)
(do (print exp) (print "\n")))
(define (print exp)
(do
(append-file-bytes "/dev/console" (string->bytes (string exp)))
'()))

(def pr print)
(def prn println)
(define (println exp)
(do
(print exp)
(print "\n")))

(defn uptime ()
(number-decode (read-file-bytes "/dev/clk/uptime" 8)))
(define (uptime)
(bytes->number (read-file-bytes "/dev/clk/uptime" 8)))

(defn realtime ()
(number-decode (read-file-bytes "realtime" 8)))
(define (realtime)
(bytes->number (read-file-bytes "realtime" 8)))

(defn write-file (path str)
(write-file-bytes path (string-encode str)))
(define (write-file path str)
(write-file-bytes path (string->bytes str)))

(defn append-file (path str)
(append-file-bytes path (string-encode str)))
(define (append-file path str)
(append-file-bytes path (string->bytes str)))

(defn regex-match? (pattern str)
(not (null? (regex-find pattern str))))
(define (regex-match? pattern str)
(not (nil? (regex-find pattern str))))
12 changes: 6 additions & 6 deletions dsk/tmp/lisp/factorial.lsp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(load "/lib/lisp/core.lsp")

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

(defn fact (n)
(fact-acc n 1))
(define (factorial n)
(factorial-helper n 1))

(println
(cond
((null? args) "Usage: factorial <num>")
(true (fact (parse (car args))))))
((nil? args) "Usage: factorial <num>")
(true (factorial (string->number (car args))))))
8 changes: 4 additions & 4 deletions dsk/tmp/lisp/fibonacci.lsp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(load "/lib/lisp/core.lsp")

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

(println
(cond
((null? args) "Usage: fibonacci <num>")
(true (fib (parse (car args))))))
((nil? args) "Usage: fibonacci <num>")
(true (fibonacci (string->number (car args))))))
8 changes: 4 additions & 4 deletions dsk/tmp/lisp/pi.lsp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
(load "/lib/lisp/core.lsp")

(defn pi-nth (n)
(define (pi-nth n)
(* (^ 16 (- n)) (-
(/ 4 (+ 1 (* 8 n)))
(/ 2 (+ 4 (* 8 n)))
(/ 1 (+ 5 (* 8 n)))
(/ 1 (+ 6 (* 8 n))))))

(defn pi-digits (n)
(define (pi-digits n)
(apply + (map pi-nth (range 0 n))))

(println
(cond
((null? args) "Usage: pi <precision>")
(true (pi-digits (parse (car args))))))
((nil? args) "Usage: pi <precision>")
(true (pi-digits (string->number (car args))))))
Loading

0 comments on commit ec57ff1

Please sign in to comment.