Skip to content

Instantly share code, notes, and snippets.

@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:

@darkf
darkf / rpn.hs
Created December 29, 2012 03:01
My Haskell is horrible
data Tok = Op Char | NumTok Int
op '+' = (+)
op '*' = (*)
op _ = \_ _ -> 666
eval toks =
eval' toks []
where
eval' [] s = head s
@darkf
darkf / overkill-even.scm
Created December 21, 2012 03:52
Overkill version of even?
(define (compose f g)
(lambda (x)
(f (g x))))
(define (composeN acc f n)
(if (zero? n)
acc
(composeN (compose acc f) f (- n 1))))
(define (even? n)
@darkf
darkf / postfix.ml
Created October 4, 2012 00:12
AST to Postfix string representation
open Printf
type node =
| Call of string * node list
| Int of int
let rec toString = function
| Call (fn, args) -> sprintf "(%s %s)" fn (String.concat " " (List.map toString args))
| Int i -> sprintf "%d" i