Skip to content

Commit

Permalink
Add hash procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Aug 7, 2023
1 parent d404e54 commit 35587aa
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
37 changes: 33 additions & 4 deletions auto_editor/lang/palet.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,24 @@ def palet_hash(*args: Any) -> dict:
return result


def hash_ref(h: dict, k: object) -> object:
try:
return h[k]
except Exception:
raise MyError("hash-ref: invalid key")


def hash_set(h: dict, k: object, v: object) -> None:
h[k] = v


def hash_remove(h: dict, v: object) -> None:
try:
del h[v]
except Exception:
pass


def palet_assert(expr: object, msg: str | bool = False) -> None:
if expr is not True:
raise MyError("assert-error" if msg is False else f"assert-error: {msg}")
Expand Down Expand Up @@ -1175,8 +1193,12 @@ def my_eval(env: Env, node: object) -> Any:
val = env.get(node.val)
if val is None:
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: {mat[0]}"
)
raise MyError(
f'variable `{node.val}` not found. Did you mean: "{node.val}"'
)
return val

if isinstance(node, Method):
Expand Down Expand Up @@ -1376,7 +1398,7 @@ def my_eval(env: Env, node: object) -> Any:
# generic iterables
"len": Proc("len", len, (1, 1), [is_iterable]),
"reverse": Proc("reverse", lambda v: v[::-1], (1, 1), [is_sequence]),
"ref": Proc("ref", ref, (2, 2), [is_iterable, is_int]),
"ref": Proc("ref", ref, (2, 2), [is_sequence, is_int]),
"slice": Proc("slice", p_slice, (2, 4), [is_sequence, is_int]),
# procedures
"map": Proc("map", palet_map, (2, 2), [is_proc, is_sequence]),
Expand All @@ -1385,8 +1407,15 @@ def my_eval(env: Env, node: object) -> Any:
"or/c": Proc("or/c", orc, (1, None), [is_cont]),
"not/c": Proc("not/c", notc, (1, 1), [is_cont]),
# hashs
"hash": Proc("hash", palet_hash),
"hash": Proc("hash", palet_hash, (0, None)),
"hash-ref": Proc("hash", hash_ref, (2, 2), [is_hash, any_p]),
"hash-set!": Proc("hash-set!", hash_set, (3, 3), [is_hash, any_p, any_p]),
"has-key?": Proc("has-key?", lambda h, k: k in h, (2, 2), [is_hash, any_p]),
"hash-remove!": Proc("hash-remove!", hash_remove, (2, 2), [is_hash, any_p]),
"hash-update!": UserProc(env, "hash-update!", ["h", "v", "up"],
[[Sym("hash-set!"), Sym("h"), Sym("v"), [Sym("up"), [Sym("hash-ref"), Sym("h"), Sym("v")]]]],
[is_hash, any_p, any_p],
),
# actions
"assert": Proc("assert", palet_assert, (1, 2), [any_p, orc(is_str, False)]),
"display": Proc("display", lambda v: print(display_str(v), end=""), (1, 1)),
Expand Down
3 changes: 1 addition & 2 deletions site/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ def build_var_sig(sigs: list[str | tuple]) -> str:
file.write(
f'<div id="{some["name"]}" class="palet-block">\n'
f'<p class="mono">(<b>{san(some["name"])}</b>&nbsp;{build_sig(varsigs)})'
+ ("" if rname == "none" else f'&nbsp;→&nbsp;{build_var(rname)}')
+ '&nbsp;&nbsp;Procedure</p>\n'
f'&nbsp;→&nbsp;{build_var(rname)}&nbsp;&nbsp;Procedure</p>\n'
)
file.write(build_var_sig(varsigs))
file.write(f"</div>\n<p>{text_to_str(some['summary'])}</p>\n")
Expand Down
54 changes: 37 additions & 17 deletions site/paletdoc.pt
Original file line number Diff line number Diff line change
Expand Up @@ -442,50 +442,48 @@
(proc
"make-vector"
#(#("size" "uint?") #("v" "any" "0") "vector?")
(text
"Returns a new vector with " 'size " slots all filled with " 'v "s."
)
(text "Returns a new vector with "'size" slots all filled with "'v"s.")
)
(proc
"vector-pop!"
#(#("vec" "vector?") "any")
(text "Remove the last element of " 'vec " and return it.")
(text "Remove the last element of "'vec" and return it.")
)
(proc
"vector-add!"
#(#("vec" "vector?") #("v" "any") "none")
(text "Append " 'v " to the end of " 'vec ".")
#(#("vec" "vector?") #("v" "any") "void?")
(text "Append "'v" to the end of "'vec".")
)
(proc
"vector-set!"
#(#("vec" "vector?") #("pos" "int?") #("v" "any") "none")
(text "Set slot " 'pos " of " 'vec " to " 'v ".")
#(#("vec" "vector?") #("pos" "int?") #("v" "any") "void?")
(text "Set slot "'pos" of "'vec" to "'v".")
)
(proc
"vector-append"
#(#("vec" "vector?") "..." "vector?")
(text
"Returns a new vector with all elements of " 'vec "s appended in order."
"Returns a new vector with all elements of "'vec"s appended in order."
)
)
(proc
"vector-extend!"
#(#("vec" "vector?") #("vec2" "vector?") "..." "none")
#(#("vec" "vector?") #("vec2" "vector?") "..." "void?")
(text
"Modify " 'vec " so that all elements of " 'vec2
"s are appended to the end of " 'vec " in order."
"Modify "'vec" so that all elements of "
'vec2"s are appended to the end of "'vec" in order."
)
)
(proc
"string->vector"
#(#("str" "string?") "vector?")
(text "Returns a new string filled with the characters of " 'str ".")
(text "Returns a new string filled with the characters of "'str".")
)
]
"Arrays" #[
(pred
"array?"
(text "Returns " #t " if " 'v " is an array, " #f " otherwise.")
(text "Returns "#t" if "'v" is an array, "#f" otherwise.")
)
(proc
"array"
Expand Down Expand Up @@ -660,7 +658,7 @@
)
(proc
"ref"
#(#("seq" "iterable?") #("pos" "int?") "any")
#(#("seq" "sequence?") #("pos" "int?") "any")
(text
"Returns the element of " 'seq " at position " 'pos
" where the first element is at position 0. For sequences other than "
Expand Down Expand Up @@ -698,10 +696,32 @@
#(#("key" "any") #("val" "any") "..." "hash?")
(text "Returns a newly constructed hash map from key-value pairs.")
)
(proc
"hash-ref"
#(#("hash" "hash?") #("key" "any") "any")
(text "Returns the value for "'key" in "'hash".")
)
(proc
"hash-set!"
#(#("hash" "hash?") #("key" "any") #("v" "any") "void?")
(text "Set "'key" to "'v" in "'hash", overwriting any existing mappings.")
)
(proc
"has-key?"
#(#("hash" "hash?") #("key" "any") "bool?")
(text "Returns " #t " if "'key" is in the hash map, "#f" otherwise.")
(text "Returns "#t" if "'key" is in the hash map, "#f" otherwise.")
)
(proc
"hash-update!"
#(#("hash" "hash?") #("key" "any") #("updater" "any") "void?")
(text "Updates the value mapped by "'key" in "'hash" by applying "'updater
" to the value. The value returned by "'updater
" becomes the new mapping for "'key", overwriting the original value in "'hash".")
)
(proc
"hash-remove!"
#(#("hash" "hash?") #("key" "any") "void?")
(text "Removes any existing mapping for "'key" in "'hash".")
)
]
"Actions" #[
Expand All @@ -717,7 +737,7 @@
)
(proc
"error"
#(#("msg" "string?") "none")
#(#("msg" "string?") "void?")
(text "Raises an exception with "'msg" as the message.")
)
(proc
Expand Down

0 comments on commit 35587aa

Please sign in to comment.