All Projects → OlegTheCat → Unlisp Llvm

OlegTheCat / Unlisp Llvm

Licence: mit
Compiler for a toy Lisp language

Programming Languages

rust
11053 projects
lisp
113 projects

Projects that are alternatives of or similar to Unlisp Llvm

Bytecoder
Rich Domain Model for JVM Bytecode and Framework to interpret and transpile it.
Stars: ✭ 401 (+1115.15%)
Mutual labels:  compiler, llvm
Rhine Ml
🏞 an OCaml compiler for an untyped lisp
Stars: ✭ 621 (+1781.82%)
Mutual labels:  compiler, llvm
Scala Native
Your favorite language gets closer to bare metal.
Stars: ✭ 4,053 (+12181.82%)
Mutual labels:  compiler, llvm
Cfl
a Compileable statically typed Functional programming Language
Stars: ✭ 7 (-78.79%)
Mutual labels:  compiler, llvm
Lyca
programming language compiler w/ llvm
Stars: ✭ 9 (-72.73%)
Mutual labels:  compiler, llvm
Sericum
(Toy) Compiler Infrastructure influenced by LLVM written in Rust
Stars: ✭ 366 (+1009.09%)
Mutual labels:  compiler, llvm
Langcraft
Compiler from LLVM IR to Minecraft datapacks.
Stars: ✭ 495 (+1400%)
Mutual labels:  compiler, llvm
Speedy.js
Accelerate JavaScript Applications by Compiling to WebAssembly
Stars: ✭ 300 (+809.09%)
Mutual labels:  compiler, llvm
Numba
NumPy aware dynamic Python compiler using LLVM
Stars: ✭ 7,090 (+21384.85%)
Mutual labels:  compiler, llvm
Gocaml
🐫 Practical statically typed functional programming language implementation with Go and LLVM
Stars: ✭ 653 (+1878.79%)
Mutual labels:  compiler, llvm
Bfc
An industrial-grade brainfuck compiler
Stars: ✭ 340 (+930.3%)
Mutual labels:  compiler, llvm
Ldc
The LLVM-based D Compiler.
Stars: ✭ 937 (+2739.39%)
Mutual labels:  compiler, llvm
Staticscript
🎉🎉🎉 A new statically typed programming language, syntactically like TypeScript.
Stars: ✭ 337 (+921.21%)
Mutual labels:  compiler, llvm
Nlvm
LLVM-based compiler for the Nim language
Stars: ✭ 380 (+1051.52%)
Mutual labels:  compiler, llvm
Soll
SOLL is a new compiler for generate Ewasm from solidity and yul. See a demo here: https://asciinema.org/a/ezJqNLicn5fya02zwu4VXIo8a
Stars: ✭ 329 (+896.97%)
Mutual labels:  compiler, llvm
Enzyme
High-performance automatic differentiation of LLVM.
Stars: ✭ 418 (+1166.67%)
Mutual labels:  compiler, llvm
Ts Llvm
TypeScript to LLVM compiler (abandoned)
Stars: ✭ 230 (+596.97%)
Mutual labels:  compiler, llvm
Deepc
vendor independent deep learning library, compiler and inference framework microcomputers and micro-controllers
Stars: ✭ 260 (+687.88%)
Mutual labels:  compiler, llvm
Llvmswift
A Swift wrapper for the LLVM C API (version 9.0.1)
Stars: ✭ 641 (+1842.42%)
Mutual labels:  compiler, llvm
Grin
GRIN is a compiler back-end for lazy and strict functional languages with whole program optimization support.
Stars: ✭ 834 (+2427.27%)
Mutual labels:  compiler, llvm

unlisp-llvm

LLVM-based compiler for a toy Lisp language. It has interactive REPL, supports AOT compilation and expr-by-expr file execution.

Building

Prerequisites

  • rustup
  • cargo
  • clang
  • rlwrap

Build steps

  1. Switch to the latest nightly Rust: rustup update nightly && rustup default nightly
  2. Install LLVM-7 and make it's binaries available on PATH:
  3. cargo build && cargo build --manifest-path ./unlisp_rt_staticlib/Cargo.toml

Running

To launch REPL execute: rlwrap cargo run -p unlisp repl.

For more info on how to run the compiler, refer to cargo run -p unlisp -- --help.

Features

Literals

>>> 1
1
>>> nil
nil
>>> "foo"
"foo"

Lisp special forms

>>> (let ((x 1) (y x)) (+ y x))
2
>>> (if (equal 2 (+ 1 1)) "foo" "bar")
"foo"

Lists

>>> (cons 1 nil)
(1)
>>> (rest (list 1 2))
(2)
>>> (first (list 1 2))
1

Varargs

>>> (defun my-list (& args) args)
nil
>>> (my-list 1 2 3)
(1 2 3)

Apply

>>> (apply (symf +) (quote (1 2)))
3
>>> (apply (symf +) 1 2 (quote (3 4)))
10

Functions & closures

>>> (defun foo (x) (lambda (y) (+ x y)))
nil
>>> (funcall (foo 1) 2)
3

Mutability

>>> (let ((x 0))
  (defun next ()
    (set! x (+ x 1))))
nil
>>> (next)
1
>>> (next)
2
>>> (next)
3

Global variables

>>> (defvar x 100)
nil
>>> x
100


>>> (defonce y 200)
nil
>>> y
200
>>> (defonce y 300)
nil
>>> y
200

"Standard library"

It is located in file stdlib.unl.

Macros & quasiquote

Quasiquote is implemented using Unlisp's macro system. There are three macros, namely qquote which is quasiquote (like a backtick in other popular lisps), unq which stands for "unquote", and unqs which stands for "unquote-splicing".

>>> (defmacro strange-let (bindings & body)
  (reduce
   (lambda (acc binding)
     (let ((sym (first binding))
           (val (first (rest binding))))
       (qquote
        (funcall
         (lambda ((unq sym))
           (unq acc))
         (unq val)))))
   (qquote (let () (unqs body)))
   (reverse bindings)))
nil
>>> (strange-let ((x 1) (y 2) (z 3)) (+ x y z))
6
>>> (macroexpand-1 (quote (strange-let ((x 1) (y 2) (z 3)) (+ x y z))))
(funcall (lambda (x) (funcall (lambda (y) (funcall (lambda (z) (let nil (+ x y z))) 3)) 2)) 1)

Printing and writing to stdout

>>> (print 1)
11
>>> (println 1)
1
1
>>> (println "foo")
"foo"
"foo"
>>> (stdout-write "foo")
foonil

Error reporting

>>> x
compilation error: undefined symbol x

>>> (defun x (y))
nil

>>> (x 1 2)
runtime error: wrong number of arguments (2) passed to x

>>> (+ 1 (quote x))
runtime error: cannot cast symbol to int

>>> (undefined-fn 1 2 3)
runtime error: undefined function undefined-fn

AOT compilation

To compile a file into a binary, the function named -main needs to be defined, which designates an entrypoint. Also make sure to run cargo build --manifest-path ./unlisp_rt_staticlib/Cargo.toml command before AOT compilation to build static runtime library (needs to be only done once).

$ cargo build --manifest-path ./unlisp_rt_staticlib/Cargo.toml
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s

$ cat file.unl
(defun -main ()
  (println (fibo 10)))

$ cargo run -p unlisp -- compile -f file.unl -o binary
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/unlisp compile -f file.unl -o binary`
Compiling file: file.unl...
Linking with runtime library: ./unlisp_rt_staticlib/target/debug/libunlisp_rt.a...

$ ./binary
89
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].