Skip to content

Instantly share code, notes, and snippets.

@viega
viega / defer.h
Created March 20, 2025 01:58
A C implementation of defer using `goto`
// defer.h
// [email protected]
// © 2025 Crash Override, Inc.
// Licensed under the BSD 3-Clause license
#pragma once
#include <stdint.h>
typedef struct n00b_defer_ll_t n00b_defer_ll_t;
@pizlonator
pizlonator / pizlossafull.md
Last active April 24, 2025 17:05
How I implement SSA form

This document explains how I would implement an SSA-based compiler if I was writing one today.

This document is intentionally opinionated. It just tells you how I would do it. This document is intended for anyone who has read about SSA and understands the concept, but is confused about how exactly to put it into practice. If you're that person, then I'm here to show you a way to do it that works well for me. If you're looking for a review of other ways to do it, I recommend this post.

My approach works well when implementing the compiler in any language that easily permits cyclic mutable data structures. I know from experience that it'll work great in C++, C#, or Java. The memory management of this approach is simple (and I'll explain it), so you won't have to stress about use after frees.

I like my approach because it leads to an ergonomic API by minimizing the amount of special cases you have to worry about. Most of the compiler is analyses and transformations ov

@pizlonator
pizlonator / pizlossa.md
Last active February 27, 2025 05:26
Pizlo SSA Form (short version)

Here's a much more complete description of how I do SSA, beyond just how I do Phis.

This describes how I do SSA form, which avoids the need to have any coupling between CFG data structures and SSA data structures.

Let's first define a syntax for SSA and some terminology. Here's an example SSA node:

A = Add(B, C)

In reality, this will be a single object in your in-memory representation, and the names are really addresses of those objects. So, this node has an "implicit variable" called A; it's the variable that is implicitly assigned to when you execute the node. If you then do:

@skeeto
skeeto / example.c
Last active April 7, 2025 16:13
Code for "Examples of quick hash tables and dynamic arrays in C"
// Examples of quick hash tables and dynamic arrays in C
// https://nullprogram.com/blog/2025/01/19/
// This is free and unencumbered software released into the public domain.
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@Joker-vD
Joker-vD / cps_demo.py
Created December 27, 2024 00:09
First-order CPS transform
def cps(expr, tail_cont=None):
'''
When called without `tail_cont`, CPS-transforms `expr` in "general position" and \
returns `(cps_builder, tmp_name)` pair: `cps_builder` is a partially-built CPS expression
that has the computed value of `expr` bound to `tmp_name` inside of it.
When called with `tail_cont`, CPS-transforms `expr` in "tail position" with `tail_cont` being
the continuation that is immediately applied to the value of `expr`, and returns
a fully completed CPS expression.
'''
#include <stdio.h>
#include <stdlib.h>
#define MEM_SIZE 1000000000 //10^9
struct mem_array {
size_t buff_size;
char * addr;
};
@VictorTaelin
VictorTaelin / icc.hvml
Last active February 6, 2024 13:45
Interaction Calculus of Constructions
// Interaction Calculus of Constructions
// =====================================
// Type
// ----
data Term
= (Lam bod)
| (App fun arg)
| (Val bod)
@VictorTaelin
VictorTaelin / sic.hvml
Created January 20, 2024 21:33
Symmetric Interaction Calculus in HVM2
// COMPUTATION RULES
// ===================================================
// APP | ((λx body) arg)
// X | ---------------------------------------------
// LAM | x ~ arg; body
// ===================================================
// APP | ({fst snd} arg)
// X | ---------------------------------------------
// SUP | dup #L{a,b} = arg; {(fst a) (snd b)}
// ===================================================
@adtac
adtac / Dockerfile
Last active April 7, 2025 15:29
#!/usr/bin/env docker run
#!/usr/bin/env -S bash -c "docker run -p 8080:8080 -it --rm \$(docker build --progress plain -f \$0 . 2>&1 | tee /dev/stderr | grep -oP 'sha256:[0-9a-f]*')"
# syntax = docker/dockerfile:1.4.0
FROM node:20
WORKDIR /root
RUN npm install sqlite3
@VictorTaelin
VictorTaelin / sat.md
Last active December 7, 2024 20:59
Simple SAT Solver via superpositions

Solving SAT via interaction net superpositions

I've recently been amazed, if not mind-blown, by how a very simple, "one-line" SAT solver on Interaction Nets can outperform brute-force by orders of magnitude by exploiting "superposed booleans" and optimal evaluation of λ-expressions. In this brief note, I'll provide some background for you to understand how this works, and then I'll present a simple code you can run in your own computer to observe and replicate this effect. Note this is a new observation, so I know little about how this algorithm behaves asymptotically, but I find it quite