Skip to content

Jedsek/lisp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lisp

Intro

My toy lisp language.

Note:
I am so busy now in my life!
I will continue this project next year(maybe).

Type cargo r -p repl to start REPL.
Type cargo r -p lang examples/plug.lisp to run example code.

Examples:

  • fib.lisp:
(define (fib n) (cond
  ((= n 0) 0)
  ((= n 1) 1)
  (else (+ (fib (- n 1)) (fib (- n 2))))
))

(displayln (fib 10))
  • big-sum.lisp:
(define (min x y)
  (if (< x y) x y))

(define (big-sum x y z)
    (- (+ x y z)
       (min (min x y) z)))
  
(displayln (big-sum 2 3 4))
  • sqrt.lisp:
(define (square x)
  (* x x))

(define (abs x)
  (if (> x 0) x (- x)))

(define (average x y)
  (/ (+ x y) 2))

(define (improve guess x)
  (average guess (/ x guess)))

(define (good-enough? old-guess guess)
  (> 0.01
    (/ (abs (- guess old-guess))
        old-guess)))

(define (sqrt-iter guess x)
  (if (good-enough? guess (improve guess x))
    (improve guess x)
    (sqrt-iter (improve guess x) x)))

(define (sqrt x)
  (sqrt-iter 1.0 x))

(displayln (sqrt 2))
  • plus.lisp:
(define (inc n)
  (+ n 1))

(define (dec n)
  (- n 1))

(define (plus1 a b)
  (if (= a 0)
    b
    (inc (plus1 (dec a) b))))

(define (plus2 a b)
  (if (= a 0)
    b
    (plus2 (dec a) (inc b))))

(displayln (plus1 4 5))
(displayln (plus2 4 5))

Roadmap

Note again:
I am so busy now in my life!
I will continue this project next year(maybe).

  • better error handling
  • improve symbol-table/nested-scope
  • more std functions/procedures
  • gc, jit, bytecode
  • llvm/codegen
  • module system
  • macro system
  • write a blog to teach how to implement it
  • REPL: syntax highlight && complete
  • tail-recursion optimization
  • Improve my shit code
  • Improve my shit code

Showcase

show