Bad Diode's Lisp
For some time I've been meaning to learn more about compilers and programming language theory. And so I found myself again delving into a rabbit hole of wheel-reinvention for the purpose of fun and learning.
The language, bdl
is inspired by Scheme
, but it doesn't aim to be compatible
with it grammatically or in terms of behaviour. Instead, the core language is
much smaller and we will grow it organically as needed. As such, it is heavily
in flux and not ready for any kind of production usage.
Currently bdl
conforms with the grammar described below. It is a dynamically
typed language, garbage collected and supports lambdas, lexical scopes and
closures.
Up to v0.7 we were building a fully functional tree-walking interpreter. A writeup for the different building blocks can be found in a series of articles that go into great detail about the implementation and basic concepts behind the language.
From v0.7 to v0.8, we created a second interpreter implementation. It was built by following along the second part of the Crafting Interpreters book. This reimagining of the project uses a single-pass compiler to generate bytecode for an custom abstract virtual machine. This interpreter is much faster than the tree-walking version and still supports closures, but it has no garbage collector and leaks memory. It was a good learning exercise but the VM is too abstracted for my taste and I don't wish to maintain a half-baked implementation.
Current development focuses on building from the fundamentals of previous
iterations to create a native compiler that could be used with multiple backends
for code generation (e.g. LLVM
, x86_64
/aarch64
assembly, uxn
bytecode,
etc.). One of the goals of this part is to have trivial C
interop when
compiled natively, meaning it should be possible to call bdl
code from C
and
C
functions from bdl
(though some wrappers may be necessary).
Grammar
program : <statement>* EOF
<statement> : <definition> | <expression>
<definition> : ( def <symbol> <expression> )
| ( fun <symbol> ( <symbol>* ) <body> )
<expression> : <constant>
| ( lambda ( <symbol>* ) <body> )
| ( if <expression> <expression> <expression> )
| ( if <expression> <expression> )
| ( set! <symbol> <expression> )
| ( <expression> <expression>* )
<body> : <statement>*
<constant> : <bool> | <number> | <string> | <symbol>
<bool> : true | false
<number> : -?<digit>+
<string> : " <character>+ "
<digit> : 0 | 1 | ... | 9
<symbol> : <character>+