Skip to content

Instantly share code, notes, and snippets.

View chrisosaurus's full-sized avatar

Chris Hall chrisosaurus

View GitHub Profile
@chrisosaurus
chrisosaurus / input
Created June 27, 2020 06:56
rewrite rules from 'imperative' to 'functional state-passing'
int foo()
{
int x = 0;
while (x < 10) x += 1;
return x;
}
@chrisosaurus
chrisosaurus / state-passing-style.hs
Last active October 20, 2020 07:42
Toy functional imperative language
-- This is a toy implementation of a purely functional 'imperative language'
-- using only pure Haskell functions.
-- I am trying to match the rough syntax
-- int x = 0;
-- while (x<10) x += 1;
-- return x;
type Env = [(String, Int)]
empty :: Env
;; taken from page 38 of 'the swine before perl' http://www.ai.mit.edu/projects/dynlangs/ll1/shriram-talk.pdf
(automaton init
(init : (c -> more))
(more : (a -> more)
(d -> more)
(r -> end))
(end : (r -> end))
@chrisosaurus
chrisosaurus / Makefile
Created June 15, 2018 01:56
using c extern to demonstrate malloc failure testing
.POSIX:
all: clean default overwrite
default:
@echo "using default malloc"
gcc main.c module.c
./a.out
@echo ""
@rm -rf *.out *.o
@chrisosaurus
chrisosaurus / Makefile
Created June 15, 2018 01:48
demonstrating c extern linkage usage to overwrite functions - useful for malloc failure testing
.POSIX:
all: clean default overwrite
default:
@echo "using bar_default implementation"
gcc main.c module.c
./a.out
@echo ""
@rm -rf *.out *.o
#include <stdio.h>
#include <unistd.h>
int main(void) {
/* FD for stdout is 1 */
if (isatty(1)) {
fputs("my stdout is a terminal", stderr);
} else {
fputs("my stdout is NOT a terminal", stderr);
}
int main(void) {
M : { // a region called M
int M results[] = generate_results<M>();
/* use results in some way... */
} /* results are destroyed here */
}
void generate_results<R>(void) {
int R *my_results = rmalloc(R, size-goes-here);
/* do some work on my_results */
int<?> max(int A *x, int B *y) {
C : {
int *m = x;
if (y > x) {
m = y;
}
return m;
}
}
@chrisosaurus
chrisosaurus / countZigZag.hs
Created July 10, 2017 01:33
zig zag counter based on
-- derived from https://gist.github.com/DomNomNom/060156de8472d9276e34262bfaef031f
data Direction = Up | Down
czz :: [Int] -> Int
czz (x:(y:rest)) | x < y = czz' (y:rest) 0 Up
czz (x:(y:rest)) | x > y = czz' (y:rest) 0 Down
czz (x:(y:rest)) | x == y = czz (y:rest)
czz _ = 0
(A (B (D) (E)) (C (F) (G)))
A
B C
D E F G