Skip to content

Commit 85b1d4b

Browse files
committed
Fix lookup with Not directive
1 parent 2e3a549 commit 85b1d4b

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/necsus/compiletime/lookupGen.nim

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import macros, sequtils, options, tables
22
import tupleDirective, tools, commonVars, archetype, componentDef, worldEnum, systemGen
3-
import ../runtime/[world, archetypeStore, directives]
3+
import ../runtime/[world, archetypeStore, directives], ../util/bits
44

55
let entityId {.compileTime.} = ident("entityId")
66

@@ -30,10 +30,6 @@ proc buildArchetypeLookup(
3030
proc worldFields(name: string, dir: TupleDirective): seq[WorldField] =
3131
@[ (name, nnkBracketExpr.newTree(bindSym("Lookup"), dir.asTupleType)) ]
3232

33-
proc canCreateFrom(lookup: TupleDirective, archetype: Archetype[ComponentDef]): bool =
34-
## Returns whether a lookup can be created from an archetype
35-
lookup.items.toSeq.allIt(it in archetype)
36-
3733
proc generate(details: GenerateContext, arg: SystemArg, name: string, lookup: TupleDirective): NimNode =
3834
## Generates the code for instantiating queries
3935

@@ -51,7 +47,7 @@ proc generate(details: GenerateContext, arg: SystemArg, name: string, lookup: Tu
5147
# Create a case statement where each branch is one of the archetypes
5248
var needsElse = false
5349
for (ofBranch, archetype) in archetypeCases(details):
54-
if lookup.canCreateFrom(archetype):
50+
if archetype.bitset.matches(lookup.filter):
5551
cases.add(nnkOfBranch.newTree(ofBranch, details.buildArchetypeLookup(lookup, archetype)))
5652
else:
5753
needsElse = true

tests/t_lookupNot.nim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest, necsus, options
2+
3+
type
4+
A = int
5+
B = string
6+
C = float
7+
8+
proc spawn(ab: Spawn[(A, B)], abc: Spawn[(A, B, C)]) =
9+
ab.with(1, "foo")
10+
abc.with(2, "bar", 3.14)
11+
12+
proc assertions(query: FullQuery[(A, )], lookup: Lookup[(B, Not[C])]) =
13+
for eid, (a) in query:
14+
if a == 1:
15+
check(lookup(eid).get[0] == "foo")
16+
else:
17+
check(not lookup(eid).isSome)
18+
19+
proc runner(tick: proc(): void) = tick()
20+
21+
proc testLookup() {.necsus(runner, [~spawn, ~assertions], newNecsusConf()).}
22+
23+
test "Lookup with a 'Not' directive":
24+
testLookup()

0 commit comments

Comments
 (0)