Skip to content

Instantly share code, notes, and snippets.

View forestbelton's full-sized avatar

Forest Belton forestbelton

View GitHub Profile
@forestbelton
forestbelton / lispy.c
Created April 6, 2013 03:41
LISP-like list macros for C
#include <assert.h>
#include <stdlib.h>
#define LIST(a) struct { \
a v; \
void *n; \
}
#define CAR(xs) xs->v
#define CDR(xs) xs->n
@forestbelton
forestbelton / sym.py
Created April 6, 2013 03:46
Symmetric group arithmetic
# Computes ab in the symmetric group S_n, where a and b are given in cyclic
# notation.
def compose(a, b, n):
i = 1
out = []
while not i in out:
out.append(i)
i = apply(a, apply(b, i))
@forestbelton
forestbelton / bf.l
Created April 6, 2013 04:01
Brainfuck compiler
%option noyywrap
%{
/*
* Copyright (c) 2010 Forest Belton (apples)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
@forestbelton
forestbelton / tenyr.js
Created April 6, 2013 04:41
Tenyr simulator in Javascript
var tenyr = (function() {
"use strict";
var regbuf = new ArrayBuffer(4 * 16);
var regs = new Int32Array(regbuf);
var mem = {};
var hooks = [];
var add_hook = function(hook) {
hooks.push(hook);
@forestbelton
forestbelton / vvtb.coffee
Created April 6, 2013 04:46
VVTB interpreter
intro = [
'=======================================',
'VVTB (Very Very Tiny Basic) interpreter',
'(c) 2012 Forest Belton (case)',
'======================================='
]
prgm = {}
vars = {}
@forestbelton
forestbelton / leqn.hs
Last active December 16, 2015 04:29
slightly better equation solver with support for conditionals, expression parsing, and pretty-printing. unfortunately very ugly
{-# LANGUAGE DeriveFunctor #-}
import Text.ParserCombinators.Parsec
import Data.Functor.Foldable
import qualified Data.Set as S
data FormF a = Var Char
| Not a
| And a a
| Or a a
@forestbelton
forestbelton / zipr.erl
Last active December 25, 2015 13:59
everyday zipper type
-module(zipr).
-export([new/1, cur/1, to_list/1, next/1, prev/1, addl/2, addr/2]).
new(X) -> {[], X, []}.
cur({_L, X, _R}) -> X.
to_list({L, X, R}) -> lists:reverse(L) ++ [X] ++ R.
next({L, X, [H|R]}) -> {[X]++L, H, R}.
prev({[H|L], X, R}) -> {L, X, [H]++R}.
addl({L, X, R}, X1) -> {L, X1, [X]++R}.
addr({L, X, R}, X1) -> {[X]++L, X1, R}.
@forestbelton
forestbelton / lambda.js
Created February 12, 2014 20:42
curried lambdas with sweet.js
macro lam {
rule { $x:ident $y:expr -> $body:expr } => {
(function($x) { return lam $y -> $body; })
}
rule { $x:ident -> $body:expr } => {
(function($x) { return $body; })
}
}
@forestbelton
forestbelton / rm_lr.js
Created February 19, 2014 06:05
remove left recursion
var rm_lr = function(name, grammar) {
var p_with = grammar[name].filter(function(x) { return x[0] == name; }),
p_wo = grammar[name].filter(function(x) { return x[0] != name; });
grammar[name] = p_wo.concat(p_wo.map(function(wo) {
return wo.concat([name + "'"]);
}));
grammar[name + "'"] = [['e']].concat(p_with.map(function(x) {
return x.slice(1).concat([name + "'"]);
@forestbelton
forestbelton / gist:b9f40a91f4f18510e317
Last active August 29, 2015 14:01
lazy evaluation with sweet.js
macro lazy {
rule { $e:expr } => {
new function() {
this.done = false;
this.valueOf = function() {
if(!this.done) {
this.done = true;
this.value = $e;
}
return this.value;