Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / interp.hs
Last active December 25, 2015 17:49
Barebones interpreter in Haskell showing `State s a`
import Control.Monad.State (State, runState, evalState, get, put)
import qualified Data.Map as M
data AST = Add AST AST
| Def String AST
| Var String
| StrConst String
| IntConst Int
deriving (Show, Eq)
@darkf
darkf / interp_pattern.hs
Created October 18, 2013 10:15
Interpreter for a simple dynamically typed pattern matching language
-- Interpreter for a simple dynamically typed pattern matching language
-- Copyright (c) 2013 darkf
-- Written on 10/18/2013
import Control.Monad.State (State, runState, evalState, get, put)
import qualified Data.Map as M
data AST = Add AST AST
| Def String AST
| Var String
@darkf
darkf / sdl_test.nim
Last active December 31, 2015 05:18
SDL test in Nimrod
import math
import sdl
const
WIDTH = 1024
HEIGHT = 768
TIMESTEP = 1000 div 30 # 30 fps
THING_WIDTH = 32
@darkf
darkf / event.cpp
Created February 8, 2014 11:57
Simple event system in C++
#include <functional>
#include <map>
#include <typeinfo>
#include <iostream>
struct Event {
virtual ~Event() {}
};
struct TestEvent : Event {
std::string msg;
@darkf
darkf / lc.hs
Created March 5, 2014 01:58
Simply typed lambda calculus interpreter in Haskell
import Data.Maybe (fromJust)
data Type = IntTy
| ArrowTy Type Type
deriving (Show, Eq)
data Term = Const Int
| Var String
| Abs String Type Term
| App Term Term
@darkf
darkf / closureobjs.rkt
Last active August 29, 2015 14:01
Closures as Stateful Objects
#lang racket
(define (obj state msg)
(match msg
['init (cons state (curry obj state))]
['get-x (cons state (curry obj state))]
[(list 'set-x x) (cons x (curry obj x))]
['ping (cons "pong" (curry obj state))]))
(define-syntax-rule (send! obj msg)
@darkf
darkf / Main.hs
Created July 7, 2014 02:19
Simple RISC-y VM in suboptimal Haskell
module Main where
import Test.Hspec
import VM
($=) = shouldBe
main :: IO ()
main = hspec $ do
describe "runVM" $ do
@darkf
darkf / concurrent.hs
Created September 9, 2014 12:50
A Haskell Concurrency pattern for servers
{-# LANGUAGE LambdaCase #-}
import Control.Concurrent
import Control.Concurrent.Chan
import Control.Monad
import Network.Socket
import System.IO
data MainMessage = Accept Socket SockAddr
| MsgReceived String