tgettype3.nim 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. discard """
  2. output: "vec2"
  3. """
  4. # bug #5131
  5. import macros
  6. type
  7. vecBase[I: static[int], T] = distinct array[I, T]
  8. vec2* = vecBase[2, float32]
  9. proc isRange(n: NimNode, rangeLen: int = -1): bool =
  10. if n.kind == nnkBracketExpr and $(n[0]) == "range":
  11. if rangeLen == -1:
  12. result = true
  13. elif n[2].intVal - n[1].intVal + 1 == rangeLen:
  14. result = true
  15. proc getTypeName(t: NimNode, skipVar = false): string =
  16. case t.kind
  17. of nnkBracketExpr:
  18. if $(t[0]) == "array" and t[1].isRange(2) and $(t[2]) == "float32":
  19. result = "vec2"
  20. elif $(t[0]) == "array" and t[1].isRange(3) and $(t[2]) == "float32":
  21. result = "vec3"
  22. elif $(t[0]) == "array" and t[1].isRange(4) and $(t[2]) == "float32":
  23. result = "vec4"
  24. elif $(t[0]) == "distinct":
  25. result = getTypeName(t[1], skipVar)
  26. of nnkSym:
  27. case $t
  28. of "vecBase": result = getTypeName(getType(t), skipVar)
  29. of "float32": result = "float"
  30. else:
  31. result = $t
  32. of nnkVarTy:
  33. result = getTypeName(t[0])
  34. if not skipVar:
  35. result = "inout " & result
  36. else:
  37. echo "UNKNOWN TYPE: ", treeRepr(t)
  38. assert(false, "Unknown type")
  39. macro typeName(t: typed): string =
  40. result = newLit(getTypeName(getType(t)))
  41. var tt : vec2
  42. echo typeName(tt)