The Basil Programming Language
The goal of Basil is to try to have it all - high-level language features like macros, dynamic code evaluation, minimal type annotations - without any of the runtime costs normally associated with high-level languages - large runtime systems, slow performance, high memory use.
To accomplish this, Basil makes use of a novel system to compile its code. The main innovation is that the Basil compiler is actually an interpreter, able to interpret any Basil expression. When it gets to an expression it wants to save for runtime, it wraps it up and saves it to the end of the program. These runtime values propagate and result in an expression tree of all code reachable at runtime - notably without unnecessary operations between constants or dead code. Basil also statically type-checks this tree, so none of the code it ultimately compiles incurs a dynamic typing or boxing overhead.
The primary goal of Basil as a language, however, isn't just speed. Basil exists to help programmers express complex problems in natural ways. While keeping Basil totally compiled and statically-typed, we've managed to implement the following features to enable uniquely customizable syntax:
Simple, homoiconic code! Basil is a Lisp dialect at its core, and has extremely simple base syntax with no reserved words and few special symbols.
Extremely simple macros! Only one primitive for code interpolation/evaluation, as opposed to the three or so usually present in Lisp dialects.
User-defined infix functions and macros! Can have any arity (unary, binary, or more!) and custom precedence.
Mixfix function syntax! Functions can be defined with keywords in their argument lists, to help break up high-arity function calls into natural-language phrases.
The Basil compiler was implemented entirely within the language jam, almost entirely from scratch in C++. Only two libraries were used (both created before the jam):
Jasmine, a runtime x86_64 assembler and object format.
Collections, an alternative C++ standard library.
Everything else is totally original, created since August 10th.
The Basil compiler has no runtime dependencies other than libc, and the entire compiler and runtime fit comfortably in under 400 kB.
The code is first tree-walk interpreted by the compiler, using dynamically-typed Basil values to represent code. This is then lowered for runtime values to typed nodes, which are then lowered to a simple IR and then to x86_64 machine code. This code is then linked, loaded and executed just-in-time by the Basil environment.
Try It Out!
The best way to give Basil a try is to work through the interactive tour! It walks you through every core Basil feature, from the basic syntax to infix macros.
You can also try running the Basil REPL. You can reach it by exiting the tour on the project page, and then running
./basil in command line. The REPL mode features a number of commands to view compiler intermediates or generated assembly, so if you're curious how that all fits together give it a shot!
If you're curious how some part of the project works, or are looking for a list of all the language's features, take a look at our README for a somewhat informal language specification.
Support the Project
If you'd like to get in touch (with a question, suggestion, etc), the best way to reach us is on Twitter or Discord (our handles are elucent#1839 and minchy#2474).
Thanks so much for taking a look at our project! It was a lot of fun making it, and we're going to be continuing work on it past the jam - so stay tuned! :)
@xxpertHacker Nothing complicated, it's mostly the same as in any Lisp. Code can be quoted (using the prefix
: operator) to skip evaluating it and treat it as data, and data can be spliced (using the
| ... | delimiters) into other code for evaluation. I think it might be bugged in the current REPL version unfortunately, but you could for example write the following function:
def (do stmt n :times) while n > 0 |stmt| n = n - 1 do :(display "hello world") 2 times
Basil also supports macros, for convenience, which automatically quote their arguments and splice their bodies into the containing code. They can be infix as well, so we could actually write the following:
infix-macro (stmt repeat n) def i |n| while i > 0 |stmt| i = i - 1 (display "hello world") repeat 2
It lets us skip the explicit quoting when we use the procedure.
It's important to note that all of this is still just operating on simple Basil values -
:(display "hello world") is just a list of a symbol and string, and could be equivalently written
[:display "hello world"]. Even
quote, beneath the syntactic sugar, are just built-in functions - not keywords.
This means that all of this can be manipulated and composed with the same mechanisms we'd use for any other value, whereas in most other languages a special, non-composable macro grammar would be necessary (like
#define or Rust's macros). The innovation in Basil over languages that have historically supported homoiconic metaprogramming is that Basil's compiler resolves all these operations ahead-of-time (erroring if it's impossible to do so), so there's no runtime cost, even though it's still possible to use arbitrary expressions.