Skip to content

Instantly share code, notes, and snippets.

View PhilipWitte's full-sized avatar

𝔉𝔦𝔩𝔴𝔦𝔱 PhilipWitte

View GitHub Profile
type A = object
type B = object
proc a(x: A) = echo "a"
proc b(x: B) = echo "b"
proc test(x: A|B) =
when x is A:
a(x)
else:
@PhilipWitte
PhilipWitte / gist:653d26e83d312293cae8
Last active August 29, 2015 14:07
Nim xmmintrin wrap
import StrUtils
# ---
type Vec4* {.incompleteStruct, importc:"__m128", header:"<xmmintrin.h>".} = object
type Vec4Array = array[4, float32]
# ---
proc newVec4 (w, z, y, x:float32): Vec4 {.importc:"_mm_set_ps".}
import StrUtils
# ---
type Foo =
ref object
value: int
type Bar =
ref object
result.insert(0,
if baseName == nil:
(quote do:
type `typeName` = ref object)[0]
else:
(quote do:
type `typeName` = ref object of `baseName`)[0]
)
# Inspect the tree structure:
#
body {
background:#f5f7f6;
line-height:20px; }
a:link {
color:#B45D47; }
#main pre code {
border-radius: 3px;
/* REMOVE: border: ... */
trait IActor:
method update*
trait IVisual:
method render* {.optional.}
class Person of TObject is IActor IVisual:
var name: string
var age: uint
import macros
macro class*(head:expr, body:stmt): stmt {.immediate.} =
# The macro is immediate so that it doesn't
# resolve identifiers passed to it
var typeName, baseName: PNimrodNode
if head.kind == NnkIdent:
@PhilipWitte
PhilipWitte / gist:11306682
Last active August 29, 2015 14:00
Nimrod - @Keyword for OOP
macro class* (head:expr, body:stmt): stmt @keyword =
## builds OOP-style type/procs...
# @keyword makes this behave in the following ways..
# - is compiled (no VM) for compile-time performance
# - is used with '=' instead of ':' (like 'proc' not 'for')
macro init* (head:expr, params:expr{param}, body:stmt): stmt @keyword =
## builds OOP-style alloc/init procs for a type
# NOTE: {param} means 'params' only accepts parameter syntax (a:T, b:U, ...)
@PhilipWitte
PhilipWitte / gist:11272741
Last active August 29, 2015 14:00
Nimrod syntax suggestions - OOP & @pragma
type Person: ref object @inheritable =
var
name*: string
age+: int
proc new*(name:string, age = 0) @init =
this.name = name
this.age = age
proc greet*() @pure =
@PhilipWitte
PhilipWitte / gist:11251113
Created April 24, 2014 11:29
Game Engine Update
# engine.nim
proc update* =
Time.update()
Input.update() # update input every cycle (raw input)
GameParts.rawUpdate() # respond to input changes (often minimal, if needed at all)
if Time.elapsed >= Time.fixedStep:
Physics.update() # either unique thread, or uses thread pool