Skip to content

Instantly share code, notes, and snippets.

@mbillingr
mbillingr / bimpy_experiment.py
Last active January 30, 2019 16:03
Playing with Dear ImGui in Python
import numpy as np
import bimpy
class Axis:
def __init__(self, size=None, xlim=(0, 1), ylim=(0, 1), bg_color=0x55FFFFFF):
self.xlim = xlim
self.ylim = ylim
if size is None:
@mbillingr
mbillingr / forth.rs
Last active February 13, 2019 16:09
(WIP) A simple but powerful Forth dialect written in Rust.
const INLINE_THRESHOLD: usize = 32; // inline words up to a length of 32 commands
type Stack = Vec<Data>;
#[derive(Debug, Copy, Clone, PartialEq)]
struct WordId(usize);
type Result<T> = std::result::Result<T, ForthError>;
#[derive(Debug)]
@mbillingr
mbillingr / stack_effects.py
Last active February 28, 2019 16:49
Deriving Stack Effects
from functools import cmp_to_key
class StackItem: pass
class Row(StackItem):
def __init__(self, name=''):
self.name = name
def __repr__(self):
return '..' + self.name
@mbillingr
mbillingr / object_protocol.rs
Created March 6, 2019 07:09
Experimental Object Protocol
use std::any::Any;
type State = Vec<Object>;
trait ObjectProtocol: std::fmt::Debug {
fn as_any(&self) -> &dyn Any;
fn as_number(&self) -> Option<&dyn NumberProtocol>;
fn repr(&self, _state: &mut State);
fn is_number(&self) -> bool { self.as_number().is_some() }
@mbillingr
mbillingr / dyn_closure.rs
Created March 7, 2019 15:39
Runtime composition of closures
use std::collections::HashMap;
use std::rc::Rc;
type State = Vec<Object>;
type Closure = Rc<Fn(&mut State)>;
#[derive(Clone)]
enum Object {
Int(i32),
@mbillingr
mbillingr / stack_effects.rs
Last active March 21, 2019 22:35
Stack Effects 2.0
use std::cell::RefCell;
use std::collections::HashSet;
use std::rc::Rc;
type Result<T> = std::result::Result<T, AlignError>;
#[derive(Debug, Clone, PartialEq)]
struct StackEffect {
inputs: StackSequence,
outputs: StackSequence,
@mbillingr
mbillingr / tailcall.rs
Created June 3, 2019 08:25
Demonstration of implementing tail calls in Rust through trampolining.
//! Demonstration of implementing tail calls in Rust through trampolining.
//! Although the core types are generic, there are a few restrictions with
//! this approach:
//! 1. Functions take exactly one argument and return one value. This can be
//! worked around by taking and returning compound types.
//! 2. Functions must have the same type signature to be able to tail-call
//! each other. I.e. a function that takes an i32 cannot call a another
//! function that takes an i64, even if they return the same type. To
//! work around this restriction one has to use dynamic typing, which
//! comes at a runtime cost.
@mbillingr
mbillingr / register_machine.rs
Created June 3, 2019 09:21
Simple register machine experiment (how to do jumps)
use std::collections::HashMap;
type T = i64;
enum Label {
FactIterativeLoop,
Done,
}
pub fn main() {
@mbillingr
mbillingr / bfjit-llvm.py
Last active June 12, 2019 09:25
Conceptual Brainfuck JIT using llvm-lite
from ctypes import CFUNCTYPE, c_int32, c_voidp, c_char
import llvmlite.binding as llvm
from llvmlite import ir
hello_world_source = """
++++++++ Set Cell #0 to 8
[
>++++ Add 4 to Cell #1; this will always set Cell #1 to 4
[ as the cell will be cleared by the loop
>++ Add 2 to Cell #2
@mbillingr
mbillingr / lispish.c
Created June 12, 2019 14:34
LISP-ish low level experiments in C
#include <stdbool.h>
#include <memory.h>
#define NIL 0
#define POINTER 1
#define NUMBER 2
typedef struct TaggedCell TaggedCell;
struct TaggedCell {