Skip to content

Instantly share code, notes, and snippets.

@mratsim
Created November 10, 2018 01:33
Show Gist options
  • Save mratsim/c10257b670039fddb187103aee59bb86 to your computer and use it in GitHub Desktop.
Save mratsim/c10257b670039fddb187103aee59bb86 to your computer and use it in GitHub Desktop.
import macros
type Kernel = object
mr*, nr*: int
epilogue*: NimNode
template impl(c: var int, x, y: int,
mr, nr: static int, epilogue: untyped
): untyped =
c = x*mr + y*nr
template it(): untyped = c # inject
c = epilogue
template op_generator(
op_name: untyped, epilogue: untyped
): untyped =
proc `op_name`(c: var int, x, y: int) =
if x < 10:
impl(c, x, y, 10, 10, epilogue)
else:
impl(c, x, y, 100, 100, epilogue)
op_generator(min10):
min(it, 10)
var c = 0
min10(c, 1, 2)
echo c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment