This is a small and simple but very powerful lisp written in JavaScript and Jison. Of course, it's not a serious language for solving production problems, but if you want to understand how to write your own programming language (or another formal language), you're always welcome to use or contribute to this language.
See a video and examples below to understand how much features already developed in this language.
(+ 1 2)
; 3
(* 2 2 2)
; 8
(/ 4 2)
; 2
(- 5 3)
; 2
(+ (* 2 2) (/ 30 5))
; 10
(+ "Hello, " "world" "!")
; Hello, world!
(def a 10)
(a)
; 10
(def (sum a b) (+ a b))
(sum 10 20)
; 30
(def a 10)
(set! a 20)
(a)
; 20
"do" function for sequential expressions execution. It always returns a result of the last expression
(do (+ 1 2)
(* 2 2)
(/ 15 5))
; 3
(if (< 10 20)
true
false)
; true
(def n 0)
; it'll print each state of the "n" variable
(while (< n 10)
(do (set! n (+ n 1))
(print (+ "N is " n " now"))))
(def l1 (list 1 2 3 4))
; and shor version of a list definition
(def l2 `(1 2 3 4))
; of course, lists can contain any type of a value
(def (sum a b) (+ a b))
(def l3 `(1, "hello", sum))
; returns a function
(fn (x) (* x x))
(def sqrt (fn (x) (* x x)))
(sqrt 4)
; 16
(def l `(1 2 3 4))
(map (fn (x) (* x x)) l)
; [1, 4, 9, 16]
(filter (fn (x) (> x 2)) l)
; [3, 4]
(reduce (fn (a b) (+ a b)) l)
; 10
(conj l 5)
; [1, 2, 3, 4, 5]
(nth l 2)
; 3
(let ((a 1) (b 1))
(let ((c (* a b)))
c))
; 1
(def (fib n)
(if (<= n 1)
n
(+ (fib (- n 1))
(fib (- n 2)))))
(def n 0)
(while (< n 15)
(do (set! n (+ n 1))
(print (+ n " Fibonacci number is " (fib n)))))
npm i -g tiny-lisp
Repl mode:
tiny-lisp
Execute file:
tiny-lisp prog.tl
- Develop modules system;
- Extend standard library;
- Improve performance;
- Develop interoperability with JS;