Skip to content

Instantly share code, notes, and snippets.

View fowlmouth's full-sized avatar
🦃

Tim E fowlmouth

🦃
  • United States of America
View GitHub Profile
import macros
macro mac(n: expr): expr =
return quote do:
(proc(): int =
`n`.int)
template samething(n): expr =
(proc (): int = n.int)
import jester, asyncdispatch, htmlgen
proc foo(s:string):string = return s&"!"
when isMainModule:
let d:string="asdf"
routes: get "/": resp foo(d) # self.render_movie(self.latest_movie_id)
runForever()
import macros
macro setIndices (obj; args: varargs[expr]): stmt =
assert args.len mod 2 == 0, "need even # of args"
result = newStmtList()
for i in countup(0, len(args)-2, 2):
result.add newAssignment(
newNimNode(nnkBracketExpr).add(obj, args[i]),
args[i+1]
)

getType() is the fucking shit

justify the cursewords here

examples

showty.nim attached can be used as a "dumptree" equivalent for this new api

@fowlmouth
fowlmouth / xcurry.nim
Last active August 9, 2020 05:09
nim getType() fun
# here is an implementation of curry, arguments passed are wrapped up in a new
# closure and a function is returned that accepts the remaining arguments
# usage:
# proc foo (a,b: int): int = a + b
# let f = curry(foo, 10)
# assert f(10) == 20
#
# note: to use an overloaded function you must annotate its type
# curry((proc(c:char,len:int):string)strutils.repeat, 'x')
#
BracketExpr(Sym(typeDesc), Sym(test_obj))
ObjectTy(Empty(), RecList(Sym(x), Sym(y), Sym(z), Sym(a)))
x:int
y:float
z:string
a:array[range[0, 4], int]
BracketExpr(Sym(typeDesc), BracketExpr(Sym(array), BracketExpr(Sym(range), IntLi
t(0), IntLit(4)), Sym(int)))
Call
Sym "m"
IntLit 1
HiddenStdConv
Empty
Bracket
StrLit str
FloatLit 4.3
Sym "x"
Bracket
import macros
macro foo (t: expr): stmt =
t.treerepr.echo
t.getType.typeKind.echo
quit 0
type MyObj = ref object of RootObj
method output(self: MyObj) =
echo "Output something"
type ChildObj = ref object of MyObj
customvar: string
method newOutput(self: ChildObj) =
self.customvar = "Child output custom var too"
import macros
dumptree:
type x = array[0..1, int]
macro matrixType (t): expr {.immediate.} =
let cs = callsite()
result = t
var i = len(cs)-1