Skip to content

Instantly share code, notes, and snippets.

#define HTTP_STATUS_CODES(_) \
_(200, OK) \
_(404, NOT_FOUND) \
_(505, INTERNAL_ERROR)
enum StatusCodes {
#define GENERATE_ENUM(CODE, MSG) MSG = CODE,
HTTP_STATUS_CODES(GENERATE_ENUM)
}
@zah
zah / gist:1140235
Created August 11, 2011 17:25
Nimrod ctags config
--langdef=nimrod
--langmap=nimrod:.nim
--regex-nimrod=/(\w+)\*?\s*=\s*object/\1/c,Class/
--regex-nimrod=/proc\s+(\w+)/\1/m,Procedure/
--regex-nimrod=/proc\s+`([^`]+)`/\1/o,Operator/
proc testConcat(a: string, b: string): string {.compileTime pure.} =
return a & b
const X = testConcat("test", "me")
proc test =
when len(X) > 2:
echo "big"
else:
echo "small"
@zah
zah / gist:1173554
Created August 26, 2011 14:41
Type associations in Nimrod
type 3DSpace = ...
type Plane = ...
type Line = ...
proc SpaceSplitterType(p: Place) : Line
proc SpaceSplitterType(p: 3DSpace) : Plane
# This is a generic algorithm that should work both in 2D and 3D context
proc ComputeProjection[S](space: S) =
# instantiate the correct space splitter type (needed for the algorithm)
@zah
zah / stringinterp.nim
Created August 28, 2011 10:43
String interpolation macro
import macros, parseutils
proc isEscaped(s: string, pos: int) : bool =
var
backslashes = 0
j = pos - 1
while j >= 0:
if s[j] == '\\':
inc backslashes
@zah
zah / gist:1176526
Created August 28, 2011 10:49
Tupfile for Nimrod projects (with automatic execution after build)
!nimrod = |> ^ %f^ nimrod c %f | tee build.log |> %B build.log
!run = |> ^ running...^ ./%f > run.log && cat run.log |> run.log
: hello.nim |> !nimrod |>
: hello |> !run |>
@zah
zah / gist:1178497
Created August 29, 2011 14:26
Surprise, Surprise
template `:=` (lhs: expr, rhs: expr) : stmt =
var lhs = rhs
proc foo =
bar := 20
@zah
zah / gist:1179016
Created August 29, 2011 18:23
Typesafe variadics hack
# Some generic functions that convert nimrod values to lua
proc push(ls: PState, s: string) = pushstring(ls, s)
proc push(ls: PState, i: int) = pushinteger(ls, i)
proc push(ls: PState, f: float) = pushnumber(ls, f)
# helpers for creating type safe variadic functions
proc argsToTuple(expr: PNimrodNode) : PNimrodNode {.compileTime.} =
result = newNimNode(nnkPar)
for i in 1..expr.len-1: add(result, expr[i])
proc myTypeMapper(x: int) : MegaInt
proc myTypeMapper(x: float) : SmartFloat
proc MappedTupleType[T, U](tp: tuple[T, U]) : tuple[
type(myTypeMapper (valueOf[T]())),
type(myTypeMapper (valueOf[U]())),
]
proc MappedTupleType[T](tp: tuple[T]) : tuple[
type(myTypeMapper (valueOf[T]()))
template repeatStmt(N: int, code: stmt) : stmt =
when N > 0:
code
repeatStmt(N-1, code)
template repeatTemplate(N: int, tmpl: expr) : stmt =
when > 0:
tmpl(N)
repeatTemplate(N-1, tmpl)