Skip to content

Commit

Permalink
Make scopes more memory efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Jul 26, 2023
1 parent 4a51744 commit ac091a3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
5 changes: 2 additions & 3 deletions auto_editor/lang/palet.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,7 @@ def __call__(self, *args: Any) -> Any:
largs = list(args)
args = tuple([largs[len(self.parms) - 1 :]])

inner_env = self.env.copy()
inner_env.update(dict(zip(self.parms, args)))
inner_env = Env(dict(zip(self.parms, args)), self.env)

for item in self.body[0:-1]:
my_eval(inner_env, item)
Expand Down Expand Up @@ -1158,7 +1157,7 @@ def my_eval(env: Env, node: object) -> Any:


# fmt: off
env = Env()
env = Env({})
env.update({
# constants
"true": True,
Expand Down
38 changes: 24 additions & 14 deletions auto_editor/lib/data_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,43 @@


class Env:
__slots__ = "data"
__slots__ = ("data", "outer")

def __init__(self) -> None:
self.data: dict[str, Any] = {}
def __init__(self, data: dict[str, Any], outer: Env | None = None) -> None:
self.data = data
self.outer = outer

def __getitem__(self, key: str) -> Any:
return self.data[key]
if key in self.data:
return self.data[key]
if self.outer is not None:
return self.outer[key]

def __setitem__(self, key: str, val: Any) -> None:
self.data[key] = val

def __delitem__(self, key: str) -> None:
del self.data[key]

def __contains__(self, item: Any) -> bool:
return item in self.data
if key in self.data:
del self.data[key]
elif self.outer is not None:
del self.outer[key]

def __contains__(self, key: str) -> bool:
if key in self.data:
return True
if self.outer is not None:
return key in self.outer
return False

def update(self, my_dict: dict[str, Any]) -> None:
self.data.update(my_dict)

def get(self, key: str) -> Any:
return self.data.get(key)

def copy(self) -> Env:
new_env = Env()
new_env.update(self.data.copy())
return new_env
if key in self.data:
return self.data[key]
if self.outer is not None:
return self.outer.get(key)
return None


class Sym:
Expand Down
6 changes: 1 addition & 5 deletions auto_editor/utils/cmdkw.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ def parse_with_palet(

def go(text: str, c: Any) -> Any:
try:
if isinstance(_env, Env):
env = _env
else:
env = Env()
env.update(_env)
env = _env if isinstance(_env, Env) else Env(_env)
results = interpret(env, Parser(Lexer(build.name, text)))
except MyError as e:
raise ParserError(e)
Expand Down

0 comments on commit ac091a3

Please sign in to comment.