tanonproc.nim 988 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. discard """
  2. output: '''`Test`'''
  3. """
  4. # bug #3561
  5. import macros, sugar, strutils
  6. type
  7. Option[T] = ref object
  8. case valid: bool
  9. of true:
  10. data: T
  11. else:
  12. discard
  13. proc some[T](v: T): Option[T] = Option[T](valid: true, data: v)
  14. proc none[T](v: T): Option[T] = Option[T](valid: false)
  15. proc none(T: typedesc): Option[T] = Option[T](valid: false)
  16. proc map[T,U](o: Option[T], f: T -> U): Option[U] =
  17. case o.valid
  18. of true:
  19. f(o.data).some
  20. else:
  21. U.none
  22. proc notEmpty(o: Option[string]): Option[string] =
  23. case o.valid
  24. of true:
  25. if o.data.strip == "": string.none else: o.data.strip.some
  26. else:
  27. o
  28. proc getOrElse[T](o: Option[T], def: T): T =
  29. case o.valid
  30. of true:
  31. o.data
  32. else:
  33. def
  34. proc quoteStr(s: string): Option[string] =
  35. s.some.notEmpty.map(v => "`" & v & "`")
  36. macro str(s: string): void =
  37. let x = s.strVal
  38. let y = quoteStr(x)
  39. let sn = newStrLitNode(y.getOrElse("NONE"))
  40. result = quote do:
  41. echo `sn`
  42. str"Test"