Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SLisp2 - a simple self-hosting lisp compiler

This is a simple but self-hosting (bootstrapped) lisp compiler that generates X86-64 (SysV ABI) machine code directly.

Lisp dialect

SLisp2 is a lisp dialect, not Scheme and not Common Lisp; main differences:

  • Lists are implemented as arrays, not linked lists of cons-pairs cells (I know it's a big shock; start breathing normally, it doesn't make much of a difference from a lisp point of view, but IMO works better)
  • Numbers are either double-precision 64 IEEE754 floats (almost: only 52 bits of mantissa instead of 53) and 60-bit fixnums
  • Strings are immutable
  • Currently only an exception-like error system is implemented (no condition system)
  • Common Lisp progn is named seq, Common Lisp lambda is named func
  • Lisp-3 (kind of) implemented on top of a Lisp-1:
    • macros are simply globals named with a "m/" prefix
    • functions are simply globals named with a "f/" prefix
    • when the first element of a form is a symbol (and is not a special form), then it's first looked up prefixing "m/" and if that fails it's assumed to mean looking up the symbol with a prefix of "f/".
    • you can have a variable, a function and a macro with the same name, no define-compiler-macro machinery... just macros
    • example1: (setq m/first (func (x) (list 'aref x 0)))
    • example2: (setq f/first (func (x) (first x))) ; previous macro invoked
    • macroexpansion is done once only by patching the source form (both in interpreter and compiler)
  • My quasi-quoting implementation may be is not identical to CL in case of nested quasi-quoting (you don't really want to nest quasiquoting anyway, do you?)
  • Generated machine code sucks A LOT compared to say SBCL (no optimization of any kind)

Minimal lisp interpreter implemented in C+ (C++ but not using much of the ++)

Extended lisp interpreter implemented in lisp, including reader and repl

Compiler implemented in lisp

Prerequisites

SLisp2 needs node.js (or something similar) to transform extended lisp boot0.lisp into boot.bin

No javascript dependencies of any kind (NPM) and no other dependencies except a C++ compiler

A X86-64 system with SysV ABI (i.e. basically any 64 bit PC NOT running windows; sorry... their fault)

Suggested rlwrap (slisp2 just reads from stdin, no line editing implemented → not easy to use)

No build system: slisp2 kernel is a single .cpp file

How to get it running

node bootreader.js             # needed once and later only if you change boot0.lisp
g++ -O3 slisp2.cpp -o slisp2   # needed once and if you change slisp2.cpp
rlwrap -q\" ./slisp2           # (or just ./slisp2, but you won't have line edit or par match)
(load "compiler.lisp")         ; loads the compiler - replaces eval with compilation+call
(load "boot0.lisp")            ; compiles minimal lisp extensions to machine code
(load "compiler.lisp")         ; compiles the compiler to machine code

Video

A video that covers some details is available on YouTube

(project has been cleaned up a bit by removing half-backed stuff from what is shown in the video)

License

MIT License

Copyright (c) 2025 Andrea Griffini

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Status, bugs, PRs

Notifications about bugs are super welcome.

PRs (except for bugs) and extensions will be read and considered but most probably not implemented.

I wrote this compiler about 2 years ago just for me and I'm not working on it. I'm publishing because someone was interested.

Code has not been commented a lot; I wrote it for myself only.

About

A simple self-hosting lisp compiler targeting X86-64 SysV ABI

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages