This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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 |> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import macros, parseutils | |
proc isEscaped(s: string, pos: int) : bool = | |
var | |
backslashes = 0 | |
j = pos - 1 | |
while j >= 0: | |
if s[j] == '\\': | |
inc backslashes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template `:=` (lhs: expr, rhs: expr) : stmt = | |
var lhs = rhs | |
proc foo = | |
bar := 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#the problem is this | |
proc `+=`, 10, left (a: int, b: int) | |
proc `+=`, 20, right (a: string, b: string) | |
# what about this? | |
operator `+=`, 10, left | |
proc `+=` (a: int, b: int) | |
proc `+=` (a: string, b: string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First, we introduce advices that modify existing methods: | |
before foo(b: string): int = | |
b = "input parameters could be modified" & b | |
after foo(b: string): int = | |
result = result & "results too" | |
wrap foo(b: string): int = | |
# here, we can write arbitrary code | |
if flipCoin(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc foo[T](x: T) = | |
var y = ... | |
bar(baz) | |
macro: | |
var typeOfY = inferType(y) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc apply[OpSymbol, T: tuple](t: T) : auto = | |
macro: | |
var retType = newNimrodType(ntkTuple) | |
for fields in T: | |
retType.add(inferType(newCall(OpSymbol, T[1])) #skiping some detail | |
var transformedExp = .. build and expression of the type (OpSymbol(T[0]), OpSymbol(T[1]), ) | |
template: | |
var x : retType = transformedExp |