Skip to content

Commit 07d7983

Browse files
committed
Added other macros from other repos.
1 parent 5f3cfb4 commit 07d7983

9 files changed

+120
-0
lines changed

nimspice.nimble

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Package
2+
3+
version = "0.1.0"
4+
author = "CodeDoes"
5+
description = "A bunch of macros. sugar if you would"
6+
license = "MIT"
7+
srcDir = "src"
8+
9+
# Dependencies
10+
11+
requires "nim >= 0.18.1"

nimspice/awsome_nim_macros.nim

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# awsome_nim_macros
2+
# Copyright CodeDoes
3+
# A bunch of macros. sugar if you would
File renamed without changes.

nimspice/delegate_macro.nim

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import macros
2+
import sugar
3+
import strutils
4+
5+
type
6+
Delegate[Proc: proc] = seq[Proc]
7+
8+
{.push.}
9+
{.experimental.}
10+
# macro unpackVarargs*(callee: untyped; args: untyped): untyped =
11+
# result = newCall(callee)
12+
# for i in 0 ..< args.len:
13+
# result.add args[i]
14+
proc `+=`[T](x: var Delegate[T]; y: T)=
15+
x.add( y)
16+
proc `-=`[T](x: var Delegate[T]; y: T)=
17+
x.del( x.find y )
18+
# macro add[T](x: var Delegate[T]; y: varargs[T]):untyped=
19+
# result = newStmtList()
20+
# for i in y:
21+
# result.add quote do:
22+
# system.add(`x`,`i`)
23+
macro `()`[A](delegate: Delegate[A]; args: varargs[typed]):untyped =
24+
# var ex = newCall(getAst(delegate))
25+
# result = newStmtList()
26+
var call_ident = "call".ident
27+
var call = newCall(call_ident)
28+
for a in args:
29+
call.add a
30+
var proc_caller_ident = genSym(nskProc,"procCaller")
31+
result = quote do:
32+
proc `proc_caller_ident`(
33+
`call_ident`:proc) {.inline.} =
34+
`call`
35+
for item in `delegate`:
36+
`proc_caller_ident`(item)
37+
hint repr result
38+
# var call = newCall(delegate.)
39+
# for call in delegate:
40+
# unpackVarargs(call, args)
41+
{.pop.}
42+
# makeDelegate proc(), Event
43+
44+
var onEvent: Delegate[proc(k: string)]
45+
import strformat
46+
onEvent += proc(k: string)= (
47+
echo fmt"1 Hi 1 on {k}";
48+
)
49+
onEvent += proc(k: string)= (
50+
echo fmt"2 12312 {k}";
51+
)
52+
onEvent += proc(k: string)= (
53+
echo fmt"3 zxczxc {k}";
54+
)
55+
onEvent += proc(k: string)= (
56+
echo fmt"4 asdasddzxczxc {k}";
57+
)
58+
onEvent += proc(k: string)= (
59+
echo fmt"5 124523sdfsdzxczxc {k}";
60+
)
61+
62+
onEvent("fasasdasd")
63+
64+
65+
66+

nimspice/listener.nim

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import macros
2+
template makeListener(T: typed): untyped=
3+
# doAssert(T.type is RootObj.type)
4+
type
5+
Listener=object
6+
# proc test(Listener: Listener, value: any): bool =
7+
# debugEcho("value is not a object instance", repr value)
8+
# return false
9+
# proc test(Listener: Listener, value: typedesc): bool {.compiletime.} =
10+
# debugEcho("value is type and not a object instance", value.getType())
11+
# return false
12+
method test(listener: Listener, value: ref RootObj): bool {.base.} =
13+
debugEcho "type is T ", value of T
14+
return false
15+
method test(listener: Listener, value: T): bool #[{.base.}]# =
16+
return true
17+
Listener()
18+
19+
type Foo = ref object of RootObj
20+
21+
var foo_listener = makeListener(Foo)
22+
# var o:object
23+
24+
# echo foo_listener.test(typedesc[object])
25+
echo foo_listener.test(new(RootObj))
26+
echo foo_listener.test(new(Foo))
27+
28+
var o= newseq[ref RootObj]()
29+
var a = new(Foo)
30+
# o.add(a)
31+
o.add(new(RootObj))
32+
o.add(new(Foo))
33+
o.add(a)
34+
for i in o:
35+
echo foo_listener.test(i)
36+
37+
38+
File renamed without changes.

tests/test1.nim

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
doAssert(1 + 1 == 2)

tests/test1.nims

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
switch("path", "$projectDir/../src")

0 commit comments

Comments
 (0)