Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
PhilipWitte / concept.nim
Last active August 29, 2015 14:21
GameObject Container Concept
import macros
# ---
type
GameObj[T] = ref object
props: T
alive: bool
import jsonstream
let
js = JsonStream() # Compile-time Error: empty params not allowed
js = new JsonStream # Compile-time Error: empty params not allowed
js = JsonStream("some/path.data") # Works: calls 'create' implicitly
something(js) # won't get here unless you properly construct the JsonStream (or go out of your way)
type
PBCEnv = ref object
PBCPattern = int
proc pbc_pattern_new(e:PBCEnv, msg, format: cstring, offsets: varargs[int]): int =
echo msg, ", ", format
for n in offsets:
result += n
macro wrapped_pbc_pattern_new(e: PBCEnv, msg, format: cstring, offsets: varargs[int]): expr =
import macros
{.emit:"""
void foobar(int i, ...) {
va_list args;
int j;
printf("Count: %i\n", i);
va_start(args, i);
for (j = 0; j < i; j++) {
macro doThis(s:static[string]): stmt =
let n = parseStmt(s)
quote do:
template actuallyDoIt: stmt =
`n`
doThis("echo 1")
static:
actuallyDoIt()
import math, threadPool
# ---
type
Person = object
age: int
friend: ref Person
import macros
# ---------- ---------- ---------- #
proc hasPragma(procedure:expr, name:string): bool {.compileTime.} =
# checks if a procedure has a specific pragma
let procPragma = procedure[4]#.pragma
for p in procPragma.children:
if p.repr == name:
import unsigned, macros
type
TokenType = enum
comment, qstr, phrase, local_dot_atom_pre_comment, local_dot_atom, local_quoted_string, domain, obs_domain_list, angle_addr, address, group_name, msg_id, date, time, received_tokens, major_digits, minor_digits, param_val, param_attr, main_type, sub_type, encoding, disp_type, ctime_date, token_string
EventType = enum
enter, exit
Event = object
tokentype: TokenType
position: int
import macros
macro makeEmitPrag(lit:static[string]): stmt =
result = newStmtList().add(
newNimNode(nnkPragma).add(
newNimNode(nnkExprColonExpr).add(
newIdentNode("emit"),
newStrLitNode(lit)
)
)
type RenderTexture = object
type RenderWindow = object
type Sprite = object
type RenderTarget* = RenderTexture | RenderWindow
type RenderTargetProc* = proc(s:Sprite, r:RenderTarget)
type Drawable* = generic d, r
r is RenderTarget
d.draw(r)