This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tables,hashes,streams | |
type | |
WordPair = (string,string) | |
MarkovChain* = object | |
words*: Table[WordPair, seq[string]] | |
proc hash (wp: WordPair): Hash = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cmodel4, vm4 | |
# not used in the vm | |
defPrimitiveComponent(Int, int) | |
defPrimitiveComponent(String, string) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cmodel, stdobj | |
type | |
Instr* {.pure.} = enum | |
NOP, PushNIL, PushPOD, Pop, Dup, Send | |
iseq = seq[byte] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
## |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(": "), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{.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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |