Skip to content

Instantly share code, notes, and snippets.

@darkf
darkf / possumparens.md
Created July 3, 2013 01:56
Proposal for optional parentheses in Possum

If we extend the Possum language to include optional parentheses, which group together tokens (atoms and constants), and when evaluated act as function applications, and when quoted act as homoiconic representations of a program.

Consider the following pseudo-Possum program:

defun compose f g is
  -> x is
    f g x
  end
end
@darkf
darkf / eval.hs
Last active December 18, 2015 07:00
Haskell 3-stage Brainfuck parser/reducer/evaluator
module Eval (bf_eval) where
import qualified Data.Vector.Mutable as MV
import Control.Monad.ST (runST)
import Data.Char (chr)
import Reducer
bf_eval :: [ISC] -> String
bf_eval instrs =
runST $ bf_eval' instrs
where
@darkf
darkf / midsum.py
Last active December 16, 2015 21:28
Find the indices where the sum on both sides of the medians is equal, or something
import math
def sumPoint(nums, n):
acc = 0
for i,x in enumerate(nums):
acc += x
if acc == n:
return i
return None
@darkf
darkf / calc.py
Created April 19, 2013 09:51
ugly non-non-precendence-enabled infix calculator
import operator
def evaluate(input):
ops = {'+': operator.add, '-': operator.sub, '*': operator.mul}
def e(toks, s_num, s_ops):
if toks == []:
return s_num.pop()
try:
@darkf
darkf / proxy.py
Created April 11, 2013 05:31
Twisted TCP/IP Proxy
# Twisted TCP/IP proxy
# You would set REAL_IP to the IP of the server you want to proxy
# then override the domain name in your hosts file. Run this,
# and then it should proxy through.
# I don't think this works with more than one concurrent client, and in that case you should refactor this code a bit. (Sorry.)
# Copyright (c) 2013 darkf; licensed under CC-0 (or in the public domain if applicable)
from twisted.internet.protocol import Factory, ClientFactory, Protocol
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor, defer
@darkf
darkf / http_proxy.py
Created April 11, 2013 05:27
Twisted HTTP proxy
# Twisted HTTP proxy
# You would set REAL_IP to the IP of the server you want to proxy
# then override the domain name in your hosts file. Run this,
# and then it should proxy through.
# Copyright (c) 2013 darkf; licensed under CC-0 (or in the public domain if applicable)
from twisted.web import proxy, http
from twisted.internet import reactor
import urlparse
from urllib import quote as urlquote
@darkf
darkf / map.hs
Created March 21, 2013 02:35
More ugly Haskell reimplementation!
type Map k v = [(k, v)]
newMap :: (Eq k) => [(k, v)] -> Map k v
newMap = id
mapLookup :: (Eq k) => Map k v -> k -> Maybe v
mapLookup [] k = Nothing
mapLookup (x:xs) k
| fst x == k = Just $ snd x
| otherwise = mapLookup xs k
@darkf
darkf / cipher.c
Created March 5, 2013 03:19
Cesar cipher in C
/* 2013/3/4 by darkf - licensed under WTFPL - cesar cipher program */
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
int key, i, len;
char output[128];
if(argc != 3) {
fprintf(stderr, "usage: %s CIPHER KEY\n", argv[0]);
@darkf
darkf / vm.ml
Created January 27, 2013 14:29
Stack-based VM in OCaml with comparison x86 program
(* Ugly imperative code, meant to be the start of something else - ideally `iter ()` would become `iter ip`. *)
type value = Int of int
| Str of string
type op = Nop
| Push of value
| Dup
| Mul
| Add
@darkf
darkf / macros.md
Last active December 10, 2015 16:09
Possum macro proposal
loop for i to 10
  print i
end

In the parser we encounter loop. It's not a function or a special form, but it is a macro. We pass it off to the macro transformer function, which spits out an AST node, and we add that, instead of the original (illegal) code.

The loop macro can be defined like: