Skip to content

Instantly share code, notes, and snippets.

Avatar
🚴

Max Bernstein tekknolagi

🚴
View GitHub Profile
@tekknolagi
tekknolagi / stack_machine.cpp
Created Feb 2, 2021 — forked from glampert/stack_machine.cpp
1-register stack caching test for stack-based interpreters.
View stack_machine.cpp
//
// Simple stack-based virtual machine tests.
//
// I use two slightly different strategies to manage the
// interpreter stack: First is using the "naive" approach
// of keeping a stack pointer that is bumped/decremented
// for every instruction that has stack operands. The second
// approach uses a "stack cache" register to cache the
// Top-of-Stack (TOS) value.
//
View icdemo.c
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// TODO(max): Consider writing this in Rust or Nim for some extra reader
// appeal.
typedef enum {
kInt,
@tekknolagi
tekknolagi / aoc8-compiler.py
Created Dec 13, 2020
Advent of Code Handheld Halting Compiler
View aoc8-compiler.py
import sys
def compile(prog):
result = (
"#include <stdbool.h>\n"
"#include <stdio.h>\n"
"#include <string.h>\n"
"int execute() {\n"
"int acc = 0;\n"
@tekknolagi
tekknolagi / Makefile
Last active Nov 10, 2020 — forked from pervognsen/asm_x64.c
x86-64 assembler in C
View Makefile
all:
gcc -Wall -Wextra -pedantic -g asm_x64.c -o asm_x64
@tekknolagi
tekknolagi / gartersnake.py
Created Oct 24, 2020
A small Python to C compiler...
View gartersnake.py
import ast
import sys
import json
NoSemicolon = True
def gather_imports(node):
names = []
View closure.py
def gen():
counter = 0
def get():
return counter
def inc():
nonlocal counter
counter += 1
return counter
return get, inc
View bnf.py
import inspect
import ast
def terminal(s):
return "<%s>" % s
def define_name(o):
return terminal(o.id) if isinstance(o, ast.Name) else o.s
@tekknolagi
tekknolagi / specification.md
Created Oct 24, 2018 — forked from rain-1/dcs.rkt
Dotted Canonical S-expressions - DCSexps
View specification.md

Dotted Canonical S-expressions - DCSexps

This is a specification for an s-expression interchange format that attempts to improve upon [2]rivest's canonical s-expressions.

It is an output format for a subset of s-expressions. Those containing only pairs and atoms.

It was designed with the following desirable properties in mind:

  • It has the canonicity property that (EQUAL? A B) implies the DCS output of A is byte equal to the DCS output of B.
  • It has the non-escaping property that arbitrary binary blobs can be contained as atoms without any processing. A consequence of this is that dcsexps can be nested easily.
View megan.py
import matplotlib.pyplot as plt
import numpy as np
from decimal import *
getcontext().prec = 1000
e = Decimal(np.e)
def y1(x):
View ex.py
import itertools
import math
import matplotlib.pyplot as plt
import numpy as np
import tufte
from collections import namedtuple
SNPoint = namedtuple('SNPoint', ['sn_name', 'z_cmb', 'mu_mag', 'err_mu',
'sample', 'flag'])