Skip to content

Instantly share code, notes, and snippets.

View forestbelton's full-sized avatar

Forest Belton forestbelton

View GitHub Profile
@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 / 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 / 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 / snake.coffee
Created April 6, 2013 03:41
A simple snake game
# Copyright (C) 2011 Forest Belton
#
# 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 restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
@forestbelton
forestbelton / fplot.py
Created April 6, 2013 03:34
Basic function plotter
import pygame, sys
from pygame.locals import *
from math import floor
import math
WIDTH, HEIGHT = 320, 200
pygame.init()
pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Function plotter')
@forestbelton
forestbelton / bandcamp.py
Created April 6, 2013 03:34
Bandcamp album ripper
from ID3 import *
import os, re, string, urllib2
embed_data = re.compile(r'var EmbedData = {.*?album_title : "([^"]+?)".*?artist : "([^"]+?)".*?};', re.DOTALL)
track_data = re.compile(r'trackinfo : \[([^\]]+?)\]', re.DOTALL)
def grab(url):
f = urllib2.urlopen(url)
data = f.read()
@forestbelton
forestbelton / RPN.hs
Created April 6, 2013 03:33
RPN calculator in Haskell
import Control.Monad
import Data.Char
import Data.List
op f (y:x:xs) = (f x y):xs -- Pop two, evaluate, push.
parse [value] [] = value -- Done parsing, evaluate.
parse _any [] = error "wrong stack size" -- This shouldn't happen!
parse stack (' ':xs) = parse stack xs -- Ignore spaces.
parse stack ('+':xs) = parse (op (+) stack) xs -- Addition.
@forestbelton
forestbelton / strsplit.c
Created April 6, 2013 03:32
string splitting in C (a la PHP's explode())
char **strsplit(const char *s, int delim) {
int state;
char *sout;
char **out;
size_t i, count, word;
/* Skip any leading delimiters. */
while(*s == delim)
++s;
@forestbelton
forestbelton / qmalloc.c
Created April 6, 2013 03:32
malloc wrapper that tracks size of allocated data
#define find_chunk(ptr) (struct mchunk*)((char*)(ptr) - offsetof(struct mchunk, data))
struct mchunk {
size_t sz;
char data[];
};
void *qmalloc(size_t sz) {
struct mchunk *chunk = malloc(sizeof *chunk + sz);
@forestbelton
forestbelton / gswap.c
Created April 6, 2013 03:31
generic swapping function in C
void gswap(void *v1, void *v2, size_t sz) {
char *c1 = v1;
char *c2 = v2;
char tmp;
while(sz--) {
tmp = *c1;
*c1++ = *c2;
*c2++ = tmp;
}