diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-04-04 02:45:43 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2016-04-04 02:45:43 +0200 |
commit | 0acdaea3342e13317ac9956add08e606208b2986 (patch) | |
tree | 6440d2c6868f2543d8245a6dffbb724b79c6ba22 /tests/vm/tanonproc.nim | |
parent | 86e79f5cec118587c7e52f614c245176cf480597 (diff) | |
download | Nim-0acdaea3342e13317ac9956add08e606208b2986.tar.gz |
fixes #3561, fixes #2409
Diffstat (limited to 'tests/vm/tanonproc.nim')
-rw-r--r-- | tests/vm/tanonproc.nim | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/vm/tanonproc.nim b/tests/vm/tanonproc.nim new file mode 100644 index 000000000..474b768ca --- /dev/null +++ b/tests/vm/tanonproc.nim @@ -0,0 +1,52 @@ +discard """ + output: '''`Test`''' +""" + +# bug #3561 + +import macros, future, strutils + +type + Option[T] = ref object + case valid: bool + of true: + data: T + else: + discard + +proc some[T](v: T): Option[T] = Option[T](valid: true, data: v) +proc none[T](v: T): Option[T] = Option[T](valid: false) +proc none(T: typedesc): Option[T] = Option[T](valid: false) + +proc map[T,U](o: Option[T], f: T -> U): Option[U] = + case o.valid + of true: + f(o.data).some + else: + U.none + +proc notEmpty(o: Option[string]): Option[string] = + case o.valid + of true: + if o.data.strip == "": string.none else: o.data.strip.some + else: + o + +proc getOrElse[T](o: Option[T], def: T): T = + case o.valid + of true: + o.data + else: + def + +proc quoteStr(s: string): Option[string] = + s.some.notEmpty.map(v => "`" & v & "`") + +macro str(s: string): stmt = + let x = s.strVal + let y = quoteStr(x) + let sn = newStrLitNode(y.getOrElse("NONE")) + result = quote do: + echo `sn` + +str"Test" |