1
5
mirror of https://github.com/vinc/moros.git synced 2024-06-19 07:27:06 +00:00
moros/dsk/lib/lisp/core.lsp
Vincent Ollivier 1bafa2271c
Add apply to Lisp (#410)
* Replace mapcar by apply

* Add map and reduce to core lib

* Add pi.lsp example

* Fix tests

* Refactor pi-digits

* Move builtin join to core lib as string-join

* Rename decode-* and encode-* to *-decode and *-encode

* Update doc
2022-09-15 20:50:23 +02:00

115 lines
2.0 KiB
Common Lisp

(defn eq? (x y)
(eq x y))
(defn atom? (x)
(atom x))
(defn string? (x)
(eq? (type x) "string"))
(defn boolean? (x)
(eq? (type x) "boolean"))
(defn symbol? (x)
(eq? (type x) "symbol"))
(defn number? (x)
(eq? (type x) "number"))
(defn list? (x)
(eq? (type x) "list"))
(defn function? (x)
(eq? (type x) "function"))
(defn lambda? (x)
(eq? (type x) "lambda"))
(def null '())
(defn null? (x)
(eq? x null))
(defn and (x y)
(cond
(x (cond (y true) (true false)))
(true false)))
(defn not (x)
(cond (x false) (true true)))
(defn or (x y)
(cond (x true) (y true) (true false)))
(defn rest (x)
(cdr x))
(defn first (x)
(car x))
(defn second (x)
(first (rest x)))
(defn third (x)
(second (rest x)))
(defn reduce (f ls)
(cond
((null? (rest ls)) (first ls))
(true (f (first ls) (reduce f (rest ls))))))
(defn string-join (ls s)
(reduce (fn (x y) (string x s y)) ls))
(defn map (f ls)
(cond
((null? ls) null)
(true (cons
(f (first ls))
(map f (rest ls))))))
(defn append (x y)
(cond
((null? x) y)
(true (cons (first x) (append (rest x) y)))))
(defn reverse (x)
(cond
((null? x) x)
(true (append (reverse (rest x)) (cons (first x) '())))))
(defn range (i n)
(cond
((= i n) null)
(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)))
(defn print (exp)
(do (append-file-bytes "/dev/console" (string-encode (string exp))) '()))
(defn println (exp)
(do (print exp) (print "\n")))
(def pr print)
(def prn println)
(defn uptime ()
(number-decode (read-file-bytes "/dev/clk/uptime" 8)))
(defn realtime ()
(number-decode (read-file-bytes "realtime" 8)))
(defn write-file (path str)
(write-file-bytes path (string-encode str)))
(defn append-file (path str)
(append-file-bytes path (string-encode str)))
(defn regex-match? (pattern str)
(not (null? (regex-find pattern str))))