diff options
author | Araq <rumpf_a@web.de> | 2011-02-21 20:06:34 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-02-21 20:06:34 +0100 |
commit | fdde4d3a9266e8a393f0303b1982a2dc9ca7ca6e (patch) | |
tree | a076ec612bb98d6dd1a8eff18505f2b3c90610f7 /tests | |
parent | 4e7a22cac31a0a703c080c7fe0d2569f56ab16a0 (diff) | |
download | Nim-fdde4d3a9266e8a393f0303b1982a2dc9ca7ca6e.tar.gz |
refactoring: suggest can import sigmatch for type matching
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/accept/run/tgenerics1.nim | 57 | ||||
-rw-r--r-- | tests/accept/run/tmacros1.nim | 32 |
2 files changed, 89 insertions, 0 deletions
diff --git a/tests/accept/run/tgenerics1.nim b/tests/accept/run/tgenerics1.nim new file mode 100755 index 000000000..b30171831 --- /dev/null +++ b/tests/accept/run/tgenerics1.nim @@ -0,0 +1,57 @@ +discard """ + output: "256 100" +""" + +# A min-heap. +type + TNode[T] = tuple[priority: int, data: T] + + TBinHeap[T] = object + heap: seq[TNode[T]] + last: int + + PBinHeap[T] = ref TBinHeap[T] + +proc newBinHeap*[T](heap: var PBinHeap[T], size: int) = + new(heap) + heap.last = 0 + newSeq(heap.seq, size) + +when false: + + proc parent(elem: int): int = + return (elem-1) div 2 + + proc siftUp[T](heap: PBinHeap[T], elem: int) = + var idx = elem + while idx != 0: + var p = parent(idx) + if heap.heap[idx] < heap.heap[p]: + var tmp = heap.heap[idx] + heap.heap[idx] = heap.heap[p] + heap.heap[p] = tmp + idx = p + else: + break + + proc add*[T](heap: PBinHeap[T], priority: int, data: T) = + var node: TNode + new(node) + node.priority = int + node.data = data + heap.heap[heap.last] = node + siftUp(heap, heap.last) + inc(heap.last) + + proc print*[T](heap: PBinHeap[T]) = + for i in countup(0, heap.last): + echo($heap.heap[i]) + +var + heap: PBinHeap[int] + +newBinHeap(heap, 256) +add(heap, 1, 100) +print(heap) + + diff --git a/tests/accept/run/tmacros1.nim b/tests/accept/run/tmacros1.nim new file mode 100644 index 000000000..572ebb914 --- /dev/null +++ b/tests/accept/run/tmacros1.nim @@ -0,0 +1,32 @@ +discard """ + output: "hellow" +""" + +import + macros, strutils + +macro outterMacro*(n: stmt): stmt = + var j : string = "hi" + proc innerProc(i: int): string = + echo "Using arg ! " & n.repr + result = "Got: '" & $n.kind & "' " & $j + if n.kind != TNimrodNodeKind.nnkMacroStmt: + error("Macro " & n[0].repr & " requires a block.") + var callNode = n[0] + expectKind(callNode, TNimrodNodeKind.nnkCall) + if callNode.len != 2 or callNode[1].kind != TNimrodNodeKind.nnkIdent: + error("Macro " & callNode.repr & + " requires the ident passed as parameter (eg: " & callNode.repr & + "(the_name_you_want)): statements.") + result = newNimNode(TNimrodNodeKind.nnkStmtList) + var ass : PNimrodNode = newNimNode(TNimrodNodeKind.nnkAsgn) + ass.add(newIdentNode(callNode[1].ident)) + ass.add(newStrLitNode(innerProc(4))) + result.add(ass) + +var str: string +outterMacro(str): + "hellow" +echo str + + |