Skip to content

Instantly share code, notes, and snippets.

@Skrylar
Skrylar / bin.rs
Created February 3, 2014 06:47
Just some research in to doing Morphic-like GUIs in Rust.
#[crate_id = "mu1"];
#[crate_type = "bin"];
use std::cast::transmute;
use std::cell::RefCell;
use std::libc::size_t;
use std::rc::{Rc, Weak};
static ErrAlreadyParented: &'static str = "Morph already has a parent.";
@Skrylar
Skrylar / test.nim
Created February 27, 2014 19:50
Using 'check' prevents propagation of changes to variable parameters.
import unittest
proc doThings(spuds: var int): int =
spuds = 24
return 99
suite "broken":
test "count the spuds":
var spuds = 0
check doThings(spuds) == 99
@Skrylar
Skrylar / maxrectpack.nim
Created March 16, 2014 10:18
Code to demonstrate a crash in the Nimrod compiler. Its important that both files are separate, combining the two makes the crash go away.
import
rectangle
type
MaxRectPacker* [T] = object
initialWidth, initialHeight: T
freeGeometry: seq[Rectangle[T]]
proc TryGet* [T](self: var MaxRectPacker[T];
@Skrylar
Skrylar / cyclic.nim
Created March 22, 2014 09:12
Demonstrating how cyclic references which are both made weak will result in a GC crash.
type
RPizza = ref Pizza
Pizza = object
Backref: RPizza
proc NoPizza(self: RPizza) =
debugEcho "Pizza eaten by ghosts."
proc main() =
# Something kind of like this; replicating wxWidgets style event mapping:
ImplHandleEvent(MyEventHandler):
On(CommandTriggeredEvent, self.OnCommand)
On MorphClosedEvent, self.OnClosed
On SomethingEvent do:
debugEcho "Something else entirely..."
# This works
macro MakeIntoEvent* (etype: typedesc): stmt =
let uid = NextEventTypeUID
Inc(NextEventTypeUID)
debugEcho "DUMP ID #", uid, ": ", repr(etype)
result = quote do:
proc GetEventTypeUid* (
self: typedesc[`etype`]): EventTypeUid {.inline.} =
let x = `uid`
return x
@Skrylar
Skrylar / eventmacro.nim
Created March 26, 2014 20:41
An example of my attempts at event map macros.
type
CommandEvent = object
Actions = enum
AMeep,
APacake
MakeIntoEvent CommandEvent
when isMainModule:
@Skrylar
Skrylar / weirdtypes.nim
Created March 29, 2014 01:28
Produces an "invalid type: Receivers" compile error, instead of warning that proc(x) should be proc(x:type).
type
FHandler = proc(x) {.closure.}
Receivers = seq[FHandler]
Beans = object
floor: bool
proc Emit(self: Receivers; x: int) =
for e in items(self):
e(x)
@Skrylar
Skrylar / JSON.red
Last active October 20, 2015 16:19 — forked from dockimbel/JSON.red
JSON toy parser for Red
Red []
digit-nz: charset "123456789"
digit: append copy digit-nz #"0"
hex: append copy digit "abcdefABCDEF"
sign: charset "+-"
exponent-e: charset "eE"
dquot: #"^""
exponent: [ exponent-e opt sign some digit ]
Red []
random/seed 1337
; produces a series of chromosomes within [0, dna] of each supplied maximum
make-dna-strand: func[dna
/local dna-length result][
dna-length: length? dna
result: make block! dna-length
foreach chromosome dna [