Skip to content

Commit

Permalink
fixed order of pack; better support for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamBindle committed Aug 25, 2023
1 parent bb1d017 commit 9cb4173
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
6 changes: 3 additions & 3 deletions collagen/standard/stack_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def pack_(state: cvm.State, *, n: int, forward: bool = True, keys: List[str] = N
[forward]: bool (default: true)
If false, the items are packed in reverse order.
[keys]: list
If provided, the items are packed as key-value pairs. The first key
If provided, the items are packed as key-value pairs. The last key
applies to the TOS item.
Inputs
Expand All @@ -106,7 +106,7 @@ def pack_(state: cvm.State, *, n: int, forward: bool = True, keys: List[str] = N
if not forward:
items.reverse()
if keys is not None:
items = {k: v for k, v in zip(reversed(keys), items)}
items = {k: v for k, v in zip(keys, items)}
else:
items = tuple(items)
return items
Expand All @@ -121,7 +121,7 @@ def unpack_(state: cvm.State, *, keys: List[str] = None):
Parameters
----------
[keys]: list
Required to unpack a list of key-value pairs. The first key refers to the
Required to unpack a list of key-value pairs. The last key refers to the
new TOS item.
Outputs
Expand Down
34 changes: 24 additions & 10 deletions collagen/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,24 @@ def __init__(self, init_stack: list = None) -> None:
def stack(self) -> List[Any]:
return self._stack

def _include(self, name: str, url_or_op: Union[str, dict], breadth_first_callback: Callable = None, depth_first_callback: Callable = None):
def _include(self, name: str, url_or_op: Union[str, dict], parameters: dict = None, breadth_first_callback: Callable = None, depth_first_callback: Callable = None):
global _static_ops
if callable(url_or_op):
print('here')
if parameters:
self._root_frame._parameters = parameters
state = State(self, self._root_frame)
if isinstance(url_or_op, str):
url = urllib.parse.urlparse(url_or_op)
if url.scheme == 'parameters':
url_or_op = _static_getters['parameters'][None](state, url.path)
url = urllib.parse.urlparse(url_or_op)

if isinstance(url_or_op, str):
if url.path.endswith(".json5"):
data = _static_getters[url.scheme]['application/json5'](self, url_or_op)
data = _static_getters[url.scheme]['application/json5'](state, url_or_op)
elif url.path.endswith(".hjson"):
data = _static_getters[url.scheme]['application/hjson'](self, url_or_op)
data = _static_getters[url.scheme]['application/hjson'](state, url_or_op)
else:
data = _static_getters[url.scheme]['application/json'](self, url_or_op)
data = _static_getters[url.scheme]['application/json'](state, url_or_op)

# breadth-first callback
if breadth_first_callback:
Expand All @@ -222,18 +228,26 @@ def _import(self, imports: list):
for module in imports:
importlib.import_module(module)

def eval(self, line: str):
def eval(self, line: str, parameters: dict = None):
url = urllib.parse.urlparse(line)
if line.startswith("import "):
self._import([line.removeprefix("import ")])
elif bool(url.scheme) and bool(url.path):
op = _static_getters[url.scheme]['application/json'](self, line)
self.exec(op)
state = State(self, self._root_frame)
if url.path.endswith(".json5"):
op = _static_getters[url.scheme]['application/json5'](state, line)
elif url.path.endswith(".hjson"):
op = _static_getters[url.scheme]['application/hjson'](state, line)
else:
op = _static_getters[url.scheme]['application/json'](state, line)
self.exec(op, parameters=parameters)
else:
op = ast.literal_eval(line)
self._root_frame.run(self, [op])

def exec(self, op: dict[str, Any]):
def exec(self, op: dict[str, Any], parameters: dict = None):
if parameters:
self._root_frame._parameters.update(parameters)
self._import(op.get("import", []))
for name, url_or_op in op.get("include", {}).items():
self._include(name, url_or_op)
Expand Down
2 changes: 1 addition & 1 deletion tests/json/test-std-pack-kwargs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"run": [
"bar",
"foo",
{"op": "pack", "n": 2, "keys": ["k1", "k2"]},
{"op": "pack", "n": 2, "keys": ["k2", "k1"]},
{"op": "dup"},
{"op": "set_next_params"},
{"op": "fstring", "fmt": "{k1}{k2}"},
Expand Down

0 comments on commit 9cb4173

Please sign in to comment.