Skip to content

Instantly share code, notes, and snippets.

@thedeemon
thedeemon / gist:b6dcfef0d74227c698b1
Last active August 29, 2015 14:12
micro interpreter in OCaml
type exp = IfGt of int * int * block * block
| Swap of int * int
| Copy of int * int
and block = exp list;;
let rec eval a = function
| IfGt(i, j, b1, b2) ->
let (r,n) = evalBlock a (if a.(i) > a.(j) then b1 else b2) in
(r, n+1)
| Swap(i, j) ->
@thedeemon
thedeemon / gist:b3969f49df0cbc6eb376
Created January 5, 2015 17:07
Micro interpreter in D
import std.stdio, std.algorithm;
struct Exp {
enum Tag { IfGt, Swap, Copy }
Tag tag;
int i, j;
Exp[] bl1, bl2;
}
int eval(int[] a, const ref Exp e) {
@thedeemon
thedeemon / gist:48ab320c3675916f3980
Created January 6, 2015 08:21
Micro interpreter in OCaml, simplified
type exp = IfGt of int * int * block * block
| Swap of int * int
| Copy of int * int
and block = exp list;;
let rec eval a = function
| IfGt(i, j, b1, b2) ->
1 + evalBlock a (if a.(i) > a.(j) then b1 else b2)
| Swap(i, j) ->
let ai = a.(i) and aj = a.(j) in
@thedeemon
thedeemon / gist:5afe7fe65a30165aae23
Created January 6, 2015 08:38
Micro interpreter in OCaml, take 3
type exp = IfGt of int * int * block * block
| Swap of int * int
| Copy of int * int
and block = exp list;;
let evalBlock a blk =
let rec eval = function
| IfGt(i, j, b1, b2) -> go 1 (if a.(i) > a.(j) then b1 else b2)
| Swap(i, j) ->
let ai = a.(i) and aj = a.(j) in
@thedeemon
thedeemon / gist:c2c3e436d78592dabf63
Created January 7, 2015 04:10
Micro interpreter, gds version
type exp = IfGt of int * int * block * block
| Swap of int * int
| Copy of int * int
and block = exp list;;
let rec eval (a : int array) = function
| IfGt(i, j, b1, b2) -> go a 1 (if a.(i) > a.(j) then b1 else b2)
| Swap(i, j) ->
let ai = a.(i) and aj = a.(j) in
a.(i) <- aj; a.(j) <- ai; 1
#![allow(unstable)]
enum Exp {
IfGt(usize, usize, Vec<Exp>, Vec<Exp>),
Swap(usize, usize),
Copy(usize, usize)
}
fn eval(a : &mut Vec<i32>, e : &Exp) -> i32 {
match *e {
If [0] > [1]
Swap [0] [1]
If [2] > [3]
Swap [2] [3]
If [4] > [5]
Swap [4] [5]
If [6] > [7]
Swap [6] [7]
If [0] > [2]
Swap [0] [2]
If [0] > [1]
Swap [0] [1]
If [2] > [3]
Swap [2] [3]
If [4] > [5]
Swap [4] [5]
If [6] > [7]
Swap [6] [7]
If [0] > [2]
Swap [0] [2]
(function doReplace() {
$("span").each(function(ind, span) {
var re = /\d+\.\d+\.\d+\.\d+/i;
var org = span.innerHTML;
var found = span.textContent.match(re);
if (found) {
span.textContent = span.textContent + " address search in progress ... ";
import std.stdio, std.digest.md, std.range, std.algorithm, std.ascii, std.conv,
std.concurrency, std.parallelism : totalCPUs;
char nextByte(char x) {
switch(x) { // this could be a one-liner: return x == '9' ? 'a' : ...
case '9' : return 'a';
case 'z' : return '0';
default : return cast(char)(x+1);
}
}