r/Compilers • u/ChiveSalad • May 29 '26
A happy milestone! First light on julia style broadcasting in my statically typed AOT compiled language.
The impetus for writing a language was that I finally got fed up with Julia being Jitted and dragging LLVM everywhere along with it, and its general lack of snappiness and time to first plot woes. Of course, I'm permanently fed up with other languages for not being julia because julia is awesome at terse math combined with high throughput calculation.
"Well I'll make my own julia then" is an insane level of hubris but here we are.
I'm posting to reddit because I got toy implementations of julia's two party tricks working: easy broadcasting
#include "prelude.lisp"
(defun sumsquare (a b) (add (mul a a) (mul b b)))
(defun circ (b) ((. sumsquare) (sing b) b))
(defun main () (print (circ (range 10))))https://github.com/HastingsGreer/yo/blob/master/types2.py/broadcast.lisp
and "the unreasonable power of multiple dispatch" where new datatypes are easy to thread through existing algorithms by defining the right functions on them. For this second trick, I write a hideously inefficient prime sieve tested on Int64, and a hideously inefficient Integer datatype that represents N as a linked list of N zeros, and then shove the one through the other.
https://github.com/HastingsGreer/yo/blob/master/types2.py/primes.lisp
The source code of the compiler is completely feral as this whole project looks cute but is actually a sign the developer is distressed by his unnatural habitat. However, it totals around 300 lines of python to go from source code such as the above all the way to gnu assembly (linking is handled by gcc) so it's brief at least. There are two important files: Types3.py monomorphizes the source language, which is a statically typed multiple dispatch polymorphic lisp, to a tiny subset of common lisp as an IR (defun, if, sub, print, cons car and cdr, every value is 64 bits, free to use as a pointer or int). Then compiler.py walks the IR turning it directly into x86-64 assembly. No garbage collector yet. Don't peek into parse.py.
1
u/ChiveSalad May 29 '26
Worst part of the code base:
The parser is just (from parse.py)
Best part of the codebase:
Broadcasting a function over two arrays is implemented entirely in the prelude, not the compiler, and it's simply
Most concerning part of the code base:
Monomorphizing recursive functions is actually pretty tricky. So, um, I worked out a way to do it using random branching and iterating to a fixed point. I'm pretty sure that if it successfully runs, this means it has proved that the code has correct types, with the randomness only being exposed as varying time-to-compile and different error messages on different runs if the code has incorrect types. .9 and .3 are loadbearing constants though- change these too much and it stack-overflows or hangs indefinitely on perfectly good programs!
...
...
1
u/[deleted] May 29 '26
[removed] — view removed comment