Skip to content

Instantly share code, notes, and snippets.

import macros
macro lc(init, body): untyped =
# analyse the body, find the deepest expression 'it' and replace it via
# 'result.add it'
let res = genSym(nskVar, "lcResult")
proc detectForLoopVar(n: NimNode): NimNode =
if n.kind == nnkForStmt:
result = n[0]
import macros, strutils
macro since*(version: string; symbol: untyped): untyped =
proc toVersion(s: string): int =
let v = s.split('.')
var w: array[3, int]
for i in 0 .. min(2, v.len):
w[i] = parseInt(v[i])
result = w[0] * 1000_000 + w[1] * 1_000 + w[2]
type
HalfStepUp[T] = object
upper, step: T
StepUp[T] = object
lower, upper, step: T
HalfStepDown[T] = object
upper, step: T
StepDown[T] = object
lower, upper, step: T
import macros, sets, strutils
type
Elem = object
x, y: int
Elems = seq[Elem]
ElemId = distinct uint16 # as ptr
include "system/timers"
import os
let a = getTicks()
var x = readFile(r"lib\system.nim")
writeFile(getTempDir() / "deleteme.nim", x)

Pointer free programming

What do ParaSail, "modern" C++ and Rust have in common? They focus on "pointer free programming" (ok, maybe Rust doesn't, but it uses similar mechanisms). In this blog post I am exploring how we can move Nim into this direction. My goals are:

import macros
proc generateAccessors(name, hidden, typ, fieldTyp: NimNode): NimNode =
template helper(name, hidden, typ, fieldTyp) {.dirty.} =
proc name*(self: typ): auto = self.hidden
proc `name=`*(self: var typ; val: fieldTyp) =
self.hidden = val
result = getAst(helper(name, hidden, typ, fieldTyp))
proc transform(n: NimNode; stmts, obj: NimNode): NimNode =
364 LdDeref r3, r1, r0 #deques:169
365 LdObj r5, r3, r3 #deques:69
366 NodeToReg r4, r5, r0 #deques:69
367 LdImmInt r5, 1 #deques:69
368 LtInt r2, r4, r5 #deques:69
369 FJmp r2, L379 #deques:69
370 LdNull r6, 8 #system:2683
371 New r6, 9 #system:2684
372 LdConst r7, "Empty deque." (31) #deques:70
373 AsgnStr r8, r7, r0 #system:2685
type
CanMove = concept x
x.move(0.0)
ICanMove = ref object
move: proc(tick: float)
Mover = ref object
movees: seq[ICanMove]
import strutils, os, pegs
proc modFile(f: string) =
transformFile f, f & paramStr(1), [(peg"\_[A-Za-z0-9]+", "")]
proc main(dir: string) =
for f in walkPattern(dir):
modFile f