Skip to content

Commit b569405

Browse files
committed
Add symbol dependency
All Symbol instances now have a set which points which others it requires and which ones requires the given instance. An symbol that requires another means that all the parents of the former will also requires it. Whenever a symbol "depends" on another (i.e. it needs some attribute of it, or makes use of it for whatever) this is now referenced in a counter collection. The counter allows to signal the same reference multiple times, so for example: LET c = a LET c = c + a Can be denoted as "c" requires "a" twice.
1 parent 48b2e27 commit b569405

1 file changed

Lines changed: 23 additions & 21 deletions

File tree

symbols/symbol_.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
# ----------------------------------------------------------------------
1111

1212
import re
13-
from functools import reduce
14-
from typing import Set, Optional
13+
from collections import Counter
1514

1615
from ast_ import Ast
1716
import api.global_
@@ -27,33 +26,36 @@ def __init__(self, *children):
2726
assert isinstance(child, Symbol)
2827
self.appendChild(child)
2928

30-
self._required_by: Set['Symbol'] = set() # Symbols that depends on this one
31-
self._requires: Set['Symbol'] = set() # Symbols this one depends on
32-
self._cached_required_by: Optional[Set['Symbol']] = set()
29+
self._required_by: Counter = Counter() # Symbols that depends on this one
30+
self._requires: Counter = Counter() # Symbols this one depends on
3331

3432
@property
35-
def required_by(self) -> Set['Symbol']:
36-
if self._cached_required_by is not None:
37-
return self._cached_required_by
38-
39-
self._cached_required_by = reduce(lambda x: x.union, (x.required_by for x in self.children),
40-
set(self._required_by))
41-
return self._cached_required_by
33+
def required_by(self) -> Counter:
34+
return self._required_by
4235

4336
@property
44-
def requires(self) -> Set['Symbol']:
45-
return set(self._requires)
37+
def requires(self) -> Counter:
38+
return Counter(self._requires)
4639

4740
def mark_as_required_by(self, other: 'Symbol'):
48-
self._required_by.add(other)
49-
self._cached_required_by.add(other)
50-
if self.parent is not None:
51-
assert isinstance(self.parent, Symbol)
52-
self.parent.mark_as_required_by(other)
41+
if self is other:
42+
return
43+
44+
self._required_by.update([other])
45+
other._requires.update([self])
46+
47+
for sym in other.required_by:
48+
sym.add_required_symbol(self)
5349

5450
def add_required_symbol(self, other: 'Symbol'):
55-
self._requires.add(other)
56-
other.mark_as_required_by(self)
51+
if self is other:
52+
return
53+
54+
self._requires.update([other])
55+
other._required_by.update([self])
56+
57+
for sym in other.requires:
58+
sym.mark_as_required_by(self)
5759

5860
@property
5961
def token(self):

0 commit comments

Comments
 (0)