This commit is contained in:
Araq
2015-03-16 23:02:03 +01:00
parent bc264618f5
commit bf90b9c833
9 changed files with 107 additions and 97 deletions

View File

@@ -12,6 +12,7 @@ e
s
t
'''
disabled: "true"
"""
template accept(e: expr) =

View File

@@ -3,6 +3,7 @@ discard """
[1, 3]
[2, 1, 2]
'''
disabled: "true"
"""
import macros, strutils
@@ -18,14 +19,14 @@ proc swizzleIdx(c: char): int =
of 'x': 0
of 'y': 1
of 'z': 2
of 'w': 3
of 'w': 3
of 'r': 0
of 'g': 1
of 'b': 2
of 'a': 3
of 'a': 3
else: 0
proc isSwizzle(s: string): bool =
proc isSwizzle(s: string): bool {.compileTime.} =
template trySet(name, set) =
block search:
for c in s:
@@ -35,10 +36,10 @@ proc isSwizzle(s: string): bool =
trySet coords, {'x', 'y', 'z', 'w'}
trySet colors, {'r', 'g', 'b', 'a'}
return false
type
type
StringIsSwizzle = generic value
value.isSwizzle
@@ -47,33 +48,33 @@ type
proc foo(x: SwizzleStr) =
echo "sw"
accept foo("xx")
#foo("xx")
reject foo("xe")
type
type
Vec[N: static[int]; T] = array[N, T]
when false:
proc card(x: Vec): int = x.N
proc `$`(x: Vec): string = x.repr.strip
proc card(x: Vec): int = x.N
proc `$`(x: Vec): string = x.repr.strip
macro `.`(x: Vec, swizzle: SwizzleStr): expr =
var
cardinality = swizzle.len
values = newNimNode(nnkBracket)
v = genSym()
macro `.`(x: Vec, swizzle: SwizzleStr): expr =
var
cardinality = swizzle.len
values = newNimNode(nnkBracket)
v = genSym()
for c in swizzle:
values.add newNimNode(nnkBracketExpr).add(
v, c.swizzleIdx.newIntLitNode)
for c in swizzle:
values.add newNimNode(nnkBracketExpr).add(
v, c.swizzleIdx.newIntLitNode)
return quote do:
let `v` = `x`
Vec[`cardinality`, `v`.T](`values`)
return quote do:
let `v` = `x`
Vec[`cardinality`, `v`.T](`values`)
var z = Vec([1, 2, 3])
echo z.card
echo z.xz
echo z.yxy
#echo z.card
#echo z.xz
#echo z.yxy