Skip to content

Instantly share code, notes, and snippets.

View planetis-m's full-sized avatar
💭
Make everything from scratch. Hardcore Mode.

Antonis Geralis planetis-m

💭
Make everything from scratch. Hardcore Mode.
View GitHub Profile
import macros, sets
macro lcSet(body): untyped =
# analyse the body, find the deepest expression 'it' and replace it via
# 'result.add it'
let res = genSym(nskVar, "lcResult")
proc t(n, res: NimNode): NimNode =
# Looks for the last statement of the last statement, etc...
case n.kind
import strutils, algorithm
const wordlistFilename = "words.txt"
template with(f, fn, actions: untyped): untyped =
var f: File
if open(f, fn):
try:
actions
finally:
close(f)
# based on: https://dewitters.com/dewitters-gameloop/
# http://lspiroengine.com/?p=378
proc run(game: var Game) =
const
ticksPerSec = 25
skippedTicks = 1_000_000_000 div ticksPerSec # to nanosecs per tick
maxFramesSkipped = 5 # 20% of ticksPerSec
var lastTime = getMonoTime().ticks
import macros
{.experimental: "forLoopMacros".}
macro zip*(x: ForLoopStmt): untyped =
expectKind x, nnkForStmt
result = newStmtList()
echo x.repr
var args: seq[NimNode]
for i in 1 ..< x[^2].len:
args.add x[^2][i]
import macros, parsejson, streams
template loadArray() {.dirty.} =
if p.kind != jsonArrayStart: raiseParseErr(p, "'[' expected for an array")
next(p)
var i = 0
while p.kind != jsonArrayEnd and p.kind != jsonEof:
inc(i)
if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "']' end of array expected")
import macros, sets, tables
proc transLastStmt(n, res, bracketExpr: NimNode): (NimNode, NimNode, NimNode) =
# Looks for the last statement of the last statement, etc...
case n.kind
of nnkStmtList, nnkStmtListExpr, nnkBlockStmt, nnkBlockExpr, nnkWhileStmt,
nnkForStmt, nnkIfExpr, nnkIfStmt, nnkTryStmt, nnkCaseStmt,
nnkElifBranch, nnkElse, nnkElifExpr:
result[0] = copyNimTree(n)
result[1] = copyNimTree(n)
import sugar, deques, sets, algorithm
include tables
proc at[A, B](t: Table[A, B]; i: int): (A, B) =
(t.data[i].key, t.data[i].val)
proc index[A, B](t: Table[A, B]; key: A): int =
var hc: Hash
result = rawGet(t, key, hc)
when declared(use_pkg_config) or declared(use_pkg_config_static):
{.pragma: libcairo, cdecl.}
when defined(use_pkg_config_static):
{.passl: gorge("pkg-config cairo --libs --static").}
else:
{.passl: gorge("pkg-config cairo --libs").}
else:
when defined(windows):
const LibCairo* = "libcairo-2.dll"
elif defined(macosx):
## This template provides syntactic sugar and
## that emulates/substitutes an optional type
type
SomePointer = ref | ptr | pointer | proc
template `?=`*(value, call: untyped): bool =
## Operator that unwraps the "optional" value that `call` returns, into `value`.
## This can be `(bool, T)` or any pointer type.
## Used only as an if condition
type
Entity = uint16
Version = uint8
Registry = object
destroyed: Entity
entities: seq[Entity]
const