Skip to content

🖨 A Lisp-to-JavaScript optimizing compiler.

Notifications You must be signed in to change notification settings

healeycodes/lisp-to-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lisp-to-js

My blog posts:


This compiler optimizes Lisp code and turns it into JavaScript.

Here's an example:

/*
(let ((fib (lambda (n)
    (if (< n 2)
        n
        (+ (fib (- n 1)) (fib (- n 2)))))))
(print (fib 10)))
*/

let print = console.log;

let fib = (n) => n < 2 ? n : (fib(n - 1) + fib(n - 2));
print(fib(10));

The implemented optimizations are constant folding and propagation, and dead code elimination. This stage runs before code generation.

; before optimization
(let ((b 2) (c 3))
  (print
    (+
      (+ b 4 c)
      (- b c 7)
  )))
 
; after optimization
(let () (print 1))

The Lisp variant is very similar to Little Lisp.

; atoms
1 ; f64 numbers
a ; symbols

; arithmetic expressions
(+ 1 2) ; 3
(- 1 2) ; -1

; control flow expressions
(< 1 2) ; true
(> 1 2) ; false
(if (< 1 2) (+ 10 10) (+ 10 5)) ; 20

; lambda expressions
(lambda (x) (+ x x)) ; function that doubles

; variable definition
(let ((a 1)) (print a)) ; prints 1
(let ((double (lambda (x) (+ x x)))) (double 2)) ; 4

Tests

There are tests for parsing, optimization, and code generation.

Run:

cargo test

Running the Compiler

To compile Lisp code to JavaScript, run the following command:

$ cargo run -- -o < fib10.lisp > fib10.js
$ node fib10.js
55

Flags:

  • -o for optimization

Releases

No releases published

Packages

No packages published

Languages