summary refs log tree commit diff stats
path: root/tests/patterns
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2012-09-04 00:55:13 +0200
committerAraq <rumpf_a@web.de>2012-09-04 00:55:13 +0200
commit6753d3685448eb2ba5760de0bcc842a887d09a84 (patch)
treec4689853edb97b78dbff6edb337735a873e6bb97 /tests/patterns
parentb4cd119800c96361ff74ab804002c32d303233fc (diff)
downloadNim-6753d3685448eb2ba5760de0bcc842a887d09a84.tar.gz
further improvements for term rewriting macros
Diffstat (limited to 'tests/patterns')
-rw-r--r--tests/patterns/targlist.nim9
-rw-r--r--tests/patterns/tor.nim9
-rw-r--r--tests/patterns/tstar.nim19
-rw-r--r--tests/patterns/tstmtlist.nim18
4 files changed, 55 insertions, 0 deletions
diff --git a/tests/patterns/targlist.nim b/tests/patterns/targlist.nim
new file mode 100644
index 000000000..a2fa1fa48
--- /dev/null
+++ b/tests/patterns/targlist.nim
@@ -0,0 +1,9 @@
+discard """
+  output: "12false3ha"
+"""
+
+proc f(x: varargs[string, `$`]) = nil
+template optF{f(X)}(x: varargs[expr]) = 
+  writeln(stdout, x)
+
+f 1, 2, false, 3, "ha"
diff --git a/tests/patterns/tor.nim b/tests/patterns/tor.nim
new file mode 100644
index 000000000..304e1c692
--- /dev/null
+++ b/tests/patterns/tor.nim
@@ -0,0 +1,9 @@
+discard """
+  output: "110"
+"""
+
+template arithOps: expr = (`+` | `-` | `*`)
+template testOr{ (arithOps{f})(a, b) }(a, b, f: expr): expr = f(a+1, b)
+
+let xx = 10
+echo 10*xx
diff --git a/tests/patterns/tstar.nim b/tests/patterns/tstar.nim
new file mode 100644
index 000000000..6dbff3cd6
--- /dev/null
+++ b/tests/patterns/tstar.nim
@@ -0,0 +1,19 @@
+discard """
+  output: "my awesome concat"
+"""
+
+var
+  calls = 0
+  
+proc `&&`(s: varargs[string]): string =
+  result = s[0]
+  for i in 1..len(s)-1: result.add s[i]
+  inc calls
+
+template optConc{ `&&` * a }(a: expr): expr = &&a
+
+let space = " "
+echo "my" && (space & "awe" && "some " ) && "concat"
+
+# check that it's been properly optimized:
+doAssert calls == 1
diff --git a/tests/patterns/tstmtlist.nim b/tests/patterns/tstmtlist.nim
new file mode 100644
index 000000000..391c93d47
--- /dev/null
+++ b/tests/patterns/tstmtlist.nim
@@ -0,0 +1,18 @@
+discard """
+  output: '''0
+|12|34
+'''
+"""
+
+template optWrite{
+  write(stdout, x)
+  write(stdout, y)
+}(x, y: string) =
+  write(stdout, "|", x, y, "|")
+
+if true:
+  echo "0"
+  write stdout, "1"
+  write stdout, "2"
+  write stdout, "3"
+  echo "4"