-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend and refactor Lisp implementation (#412)
* 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
Showing
6 changed files
with
183 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |
Oops, something went wrong.