Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "xpr-lang"
version = "0.2.1"
version = "0.3.0"
description = "XPR expression language - Python runtime"
requires-python = ">=3.10"
license = {text = "MIT"}
Expand Down
26 changes: 21 additions & 5 deletions src/xpr/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@
GLOBAL_FUNCTION_ARITY,
)


def _expand_args(raw_args, nxt):
result = []
for arg in raw_args:
if isinstance(arg, SpreadElement):
val = nxt(arg.argument)
if val is None:
raise XprError("Cannot spread null", arg.position)
if not isinstance(val, list):
raise XprError("Cannot spread non-array", arg.position)
result.extend(val)
else:
result.append(nxt(arg))
return result


BLOCKED_PROPS = frozenset(
[
"__proto__",
Expand Down Expand Up @@ -150,8 +166,8 @@ def nxt(n: Expression) -> Any:
if not isinstance(obj, list):
raise XprError("Cannot index non-array with number", node.position)
if idx < 0:
raise XprError("negative indexing not supported", node.position)
return obj[idx] if idx < len(obj) else None
idx = len(obj) + idx
return obj[idx] if 0 <= idx < len(obj) else None
prop_name = str(key)
else:
prop_name = node.property
Expand Down Expand Up @@ -310,7 +326,7 @@ def arrow(*args):
f"Access denied: '{method_name}' is a restricted property", pos
)

args = [nxt(a) for a in node.arguments]
args = _expand_args(node.arguments, nxt)

if isinstance(obj, str):
return call_string_method(obj, method_name, args, pos)
Expand All @@ -326,7 +342,7 @@ def arrow(*args):

if isinstance(node.callee, Identifier):
name = node.callee.name
args = [nxt(a) for a in node.arguments]
args = _expand_args(node.arguments, nxt)
if name in ctx and callable(ctx[name]):
return ctx[name](*args)
if name in GLOBAL_FUNCTIONS:
Expand All @@ -344,7 +360,7 @@ def arrow(*args):
callee = nxt(node.callee)
if node.optional and callee is None:
return None
args = [nxt(a) for a in node.arguments]
args = _expand_args(node.arguments, nxt)
if callable(callee):
return callee(*args)
raise XprError("Cannot call non-function", pos)
Expand Down
Loading
Loading