Skip to content

Commit

Permalink
Fix some palet bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Oct 15, 2023
1 parent 5617283 commit e49591c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
47 changes: 36 additions & 11 deletions auto_editor/lang/palet.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,8 @@ def __str__(self) -> str:
is_cont = Contract("contract?", is_contract)
is_iterable = Contract(
"iterable?",
lambda v: type(v) in (str, range, Quoted)
or isinstance(v, (list, dict, np.ndarray)),
lambda v: type(v) in (str, range, list, tuple, dict, Quoted)
or isinstance(v, np.ndarray),
)
is_sequence = Contract(
"sequence?",
Expand Down Expand Up @@ -1124,6 +1124,28 @@ def syn_for(env: Env, node: Node) -> None:
my_eval(env, c)


def syn_for_items(env: Env, node: Node) -> None:
if len(node) < 2:
raise MyError(f"{node[0]}: bad syntax")

if type(node[1]) is not tuple or len(node[1]) != 3:
raise MyError(f"{node[0]}: Invalid id body")

key, val, dic = node[1]
if type(key) is not Sym or type(val) is not Sym:
raise MyError(f"{node[0]}: key and val must be identifiers")

dic = my_eval(env, dic)
if type(dic) is not dict:
raise MyError(f"{node[0]}: dict must be a hash?")

for k, v in dic.items():
env[key.val] = k
env[val.val] = v
for c in node[2:]:
my_eval(env, c)


def syn_quote(env: Env, node: Node) -> Any:
guard_term(node, 2, 2)
if type(node[1]) is Keyword:
Expand Down Expand Up @@ -1352,14 +1374,12 @@ def edit_all() -> np.ndarray:
def my_eval(env: Env, node: object) -> Any:
if type(node) is Sym:
val = env.get(node.val)
if val is None:
if type(val) is NotFound:
if mat := get_close_matches(node.val, env.data):
raise MyError(
f"variable `{node.val}` not found. Did you mean: {mat[0]}"
)
raise MyError(
f'variable `{node.val}` not found. Did you mean: "{node.val}"'
)
raise MyError(f'variable `{node.val}` not found. Did you mean a string literal.')
return val

if isinstance(node, Method):
Expand Down Expand Up @@ -1431,6 +1451,7 @@ def my_eval(env: Env, node: object) -> Any:
"@r": Syntax(attr),
# loops
"for": Syntax(syn_for),
"for-items": Syntax(syn_for_items),
# contracts
"number?": is_num,
"real?": is_real,
Expand All @@ -1447,7 +1468,7 @@ def my_eval(env: Env, node: object) -> Any:
"symbol?": (is_symbol := Contract("symbol?", lambda v: type(v) is Sym)),
"string?": is_str,
"char?": (is_char := Contract("char?", lambda v: type(v) is Char)),
"list?": (is_list := Contract("list?", lambda v: type(v) is Quoted)),
"list?": (is_list := Contract("list?", lambda v: type(v) is Quoted or type(v) is tuple)),
"vector?": (is_vector := Contract("vector?", lambda v: type(v) is list)),
"array?": (is_array := Contract("array?", lambda v: isinstance(v, np.ndarray))),
"bool-array?": is_boolarr,
Expand Down Expand Up @@ -1631,9 +1652,13 @@ def my_eval(env: Env, node: object) -> Any:

def interpret(env: Env, parser: Parser) -> list:
result = []
while parser.current_token.type != EOF:
result.append(my_eval(env, parser.expr()))

if type(result[-1]) is Keyword:
raise MyError(f"Keyword misused in expression. `{result[-1]}`")
try:
while parser.current_token.type != EOF:
result.append(my_eval(env, parser.expr()))

if type(result[-1]) is Keyword:
raise MyError(f"Keyword misused in expression. `{result[-1]}`")
except RecursionError:
raise MyError("maximum recursion depth exceeded")
return result
5 changes: 4 additions & 1 deletion auto_editor/lib/data_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import numpy as np


class NotFound:
pass

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

Expand Down Expand Up @@ -45,7 +48,7 @@ def get(self, key: str) -> Any:
return self.data[key]
if self.outer is not None:
return self.outer.get(key)
return None
return NotFound()


class Sym:
Expand Down

0 comments on commit e49591c

Please sign in to comment.