Skip to content

Instantly share code, notes, and snippets.

View fowlmouth's full-sized avatar
🦃

Tim E fowlmouth

🦃
  • United States of America
View GitHub Profile
import tables,hashes,streams
type
WordPair = (string,string)
MarkovChain* = object
words*: Table[WordPair, seq[string]]
proc hash (wp: WordPair): Hash =
import cmodel4, vm4
# not used in the vm
defPrimitiveComponent(Int, int)
defPrimitiveComponent(String, string)
## this is an component composition framework, it is a basic object model
## founded on composition rather than inheritance.
##
## changes since entoody:
## * removed unicast/multicast messages
## send() sends a message to the first component that applies,
## multicast() is an iterator that sends a message to all components that apply
##
## * removed static component ordering.
## order matters! higher components can intercept messages before
## test that storing Objects in slots of components is gc safe
import cmodel, std_test, gctester
gcTest:
let my_comp = dynaComponent("my_comp", "child1", "child2")
let aggx = aggregate(my_comp)
let my1 = aggx.instantiate()
let my2 = aggx.instantiate()
import cmodel, stdobj
type
Instr* {.pure.} = enum
NOP, PushNIL, PushPOD, Pop, Dup, Send
iseq = seq[byte]
discard """
a generic gc test?
what operations are required
- an expression that saves a ref must increment its reference
- an expression that replaces a ref must decrement the ref it held previously
- a gc-safe type decrements held references upon destruction (manual or automatic)
"""
template gcTest* (body: untyped): stmt {. dirty, immediate } =
proc initT (some: var string) = some = newStringOfCap(64)
proc initT [T] (some: var seq[T]) = newSeq[T](some, 0)
template filterIt*(seq1, pred: expr): expr {.immediate.} =
## Returns a new sequence with all the items that fulfilled the predicate.
##
## Unlike the `proc` version, the predicate needs to be an expression using
## the ``it`` variable for testing, like: ``filterIt("abcxyz", it == 'x')``.
## Example:
##
import macros
macro echoCode (x:varargs[untyped]): stmt =
var call = newCall("echo")
let high = len(x)-1
for i in 0 .. high:
let item = x[i]
let code = repr(item)
call.add(
quote do: `code`,
newLit(": "),
{.emit:"""
#include <stdarg.h>
void foobar(int i, ...) {
va_list args;
int j;
va_start(args, i);
for (j = 0; j < i; j++) {
int x = va_arg(args, int);
printf("%i\n", x);
}
lib X
fun itoa = itoa(value : Int32, str : UInt8*, radix : Int32) : Int32
end
z = Pointer(UInt8).new(9)
X.itoa(1, z, 1)
p z