added test cases from strange loop event

This commit is contained in:
Araq 2014-01-13 01:05:09 +01:00
commit 9ab1a3c724
6 changed files with 152 additions and 0 deletions

View file

@ -0,0 +1,57 @@
discard """
output: '''(a: 3
b: 4
s: abc
)'''
"""
import macros
proc invalidFormatString() =
echo "invalidFormatString"
template formatImpl(handleChar: expr) =
var i = 0
while i < f.len:
if f[i] == '$':
case f[i+1]
of '1'..'9':
var j = 0
i += 1
while f[i] in {'0'..'9'}:
j = j * 10 + ord(f[i]) - ord('0')
i += 1
result.add(a[j-1])
else:
invalidFormatString()
else:
result.add(handleChar(f[i]))
i += 1
proc `%`*(f: string, a: openArray[string]): string =
template identity(x: expr): expr = x
result = ""
formatImpl(identity)
macro optFormat{`%`(f, a)}(f: string{lit}, a: openArray[string]): expr =
result = newNimNode(nnkBracket)
let f = f.strVal
formatImpl(newLit)
result = nestList(!"&", result)
template optAdd1{x = y; add(x, z)}(x, y, z: string) =
x = y & z
proc `/&` [T: object](x: T): string =
result = "("
for name, value in fieldPairs(x):
result.add("$1: $2\n" % [name, $value])
result.add(")")
type
MyObject = object
a, b: int
s: string
let obj = MyObject(a: 3, b: 4, s: "abc")
echo(/&obj)

11
tests/showoff/thello2.nim Normal file
View file

@ -0,0 +1,11 @@
discard """
output: '''(a: 3, b: 4, s: abc)'''
"""
type
MyObject = object
a, b: int
s: string
let obj = MyObject(a: 3, b: 4, s: "abc")
echo obj

11
tests/showoff/thtml1.nim Normal file
View file

@ -0,0 +1,11 @@
discard """
output: "<br>"
"""
template htmlTag(tag: expr) {.immediate.} =
proc tag(): string = "<" & astToStr(tag) & ">"
htmlTag(br)
htmlTag(html)
echo br()

37
tests/showoff/thtml2.nim Normal file
View file

@ -0,0 +1,37 @@
discard """
output: "<html><head><title>now look at this</title></head><body><ul><li>Nimrod is quite capable</li></ul></body></html>"
"""
import strutils
template html(name: expr, matter: stmt) {.immediate.} =
proc name(): string =
result = "<html>"
matter
result.add("</html>")
template nestedTag(tag: expr) {.immediate.} =
template tag(matter: stmt) {.immediate.} =
result.add("<" & astToStr(tag) & ">")
matter
result.add("</" & astToStr(tag) & ">")
template simpleTag(tag: expr) {.immediate.} =
template tag(matter: expr) {.immediate.} =
result.add("<$1>$2</$1>" % [astToStr(tag), matter])
nestedTag body
nestedTag head
nestedTag ul
simpleTag title
simpleTag li
html mainPage:
head:
title "now look at this"
body:
ul:
li "Nimrod is quite capable"
echo mainPage()

22
tests/showoff/tonce.nim Normal file
View file

@ -0,0 +1,22 @@
discard """
output: '''first call of p
some call of p
new instantiation
some call of p'''
"""
template once(body: stmt) =
var x {.global.} = false
if not x:
x = true
body
proc p() =
once:
echo "first call of p"
echo "some call of p"
p()
once:
echo "new instantiation"
p()

View file

@ -0,0 +1,14 @@
discard """
outputsub: '''tquasiquote.nim(14,8): Check failed: 1 > 2'''
"""
import macros
macro check(ex: expr): stmt =
var info = ex.lineInfo
var expString = ex.toStrLit
result = quote do:
if not `ex`:
echo `info`, ": Check failed: ", `expString`
check 1 > 2