Skip to content

Commit

Permalink
Make env a custom object
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Jul 26, 2023
1 parent 1f83e51 commit 4a51744
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
5 changes: 2 additions & 3 deletions auto_editor/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from numpy.typing import NDArray

from auto_editor.ffwrapper import FileInfo
from auto_editor.lib.data_structs import Env
from auto_editor.output import Ensure
from auto_editor.utils.bar import Bar
from auto_editor.utils.log import Log
Expand Down Expand Up @@ -489,9 +490,7 @@ def pixeldiff(self, s: int) -> NDArray[np.uint64]:
return self.cache("pixeldiff", pobj, result)


def edit_method(
val: str, filesetup: FileSetup, env: dict[str, Any]
) -> NDArray[np.bool_]:
def edit_method(val: str, filesetup: FileSetup, env: Env) -> NDArray[np.bool_]:
assert isinstance(filesetup, FileSetup)
src = filesetup.src
tb = filesetup.tb
Expand Down
8 changes: 5 additions & 3 deletions auto_editor/lang/palet.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
Number = Union[int, float, complex, Fraction]
Real = Union[int, float, Fraction]
BoolList = NDArray[np.bool_]
Env = dict[str, Any]


class ClosingError(MyError):
Expand Down Expand Up @@ -1108,7 +1107,7 @@ def my_eval(env: Env, node: object) -> Any:
if type(node) is Sym:
val = env.get(node.val)
if val is None:
if mat := get_close_matches(node.val, env):
if mat := get_close_matches(node.val, env.data):
raise MyError(f"'{node.val}' not found. Did you mean: {mat[0]}")
raise MyError(f"'{node.val}' not found.")
return val
Expand Down Expand Up @@ -1157,7 +1156,9 @@ def my_eval(env: Env, node: object) -> Any:

return node

env: Env = {}

# fmt: off
env = Env()
env.update({
# constants
"true": True,
Expand Down Expand Up @@ -1337,6 +1338,7 @@ def my_eval(env: Env, node: object) -> Any:
"rename": Syntax(syn_rename),
"delete": Syntax(syn_delete),
})
# fmt: on


def interpret(env: Env, parser: Parser) -> list:
Expand Down
31 changes: 31 additions & 0 deletions auto_editor/lib/data_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,41 @@

from fractions import Fraction
from io import StringIO
from typing import Any

import numpy as np


class Env:
__slots__ = "data"

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

def __getitem__(self, key: str) -> Any:
return self.data[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

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


class Sym:
__slots__ = ("val", "hash")

Expand Down
10 changes: 9 additions & 1 deletion auto_editor/utils/cmdkw.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from difflib import get_close_matches
from typing import TYPE_CHECKING, NamedTuple

from auto_editor.lib.data_structs import Env
from auto_editor.utils.types import CoerceError

if TYPE_CHECKING:
Expand Down Expand Up @@ -103,7 +104,9 @@ def get_next_token(self) -> str | None:
return None


def parse_with_palet(text: str, build: pAttrs, env: dict) -> dict[str, Any]:
def parse_with_palet(
text: str, build: pAttrs, _env: Env | dict[str, Any]
) -> dict[str, Any]:
from auto_editor.lang.palet import Lexer, Parser, interpret
from auto_editor.lib.data_structs import print_str
from auto_editor.lib.err import MyError
Expand All @@ -118,6 +121,11 @@ def parse_with_palet(text: str, build: pAttrs, env: dict) -> dict[str, Any]:

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

0 comments on commit 4a51744

Please sign in to comment.