Skip to content

Commit

Permalink
more tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky committed Dec 14, 2023
1 parent 90fb89c commit 55a514d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
21 changes: 9 additions & 12 deletions clvm/CLVMObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,20 @@ class CLVMObject:
@staticmethod
def __new__(
class_: typing.Type[_T_CLVMObject],
# TODO: which? review?
# v: typing.Union[CLVMObject, CLVMStorage, typing.Tuple[CLVMObject, CLVMObject], bytes],
v: typing.Union[CLVMObject, bytes, PairType],
v: typing.Union[_T_CLVMObject, bytes, PairType],
) -> _T_CLVMObject:
if isinstance(v, class_):
return v
# mypy does not realize that the isinstance check is type narrowing like this
narrowed_v: typing.Union[bytes, PairType] = v # type: ignore[assignment]

self = super(CLVMObject, class_).__new__(class_)
if isinstance(v, tuple):
if len(v) != 2:
raise ValueError("tuples must be of size 2, cannot create CLVMObject from: %s" % str(v))
self.pair = v
if isinstance(narrowed_v, tuple):
if len(narrowed_v) != 2:
raise ValueError("tuples must be of size 2, cannot create CLVMObject from: %s" % str(narrowed_v))
self.pair = narrowed_v
self.atom = None
# TODO: discussing this
# elif isinstance(v, bytes):
else:
self.atom = v # type: ignore[assignment]
self.atom = narrowed_v
self.pair = None
# else:
# raise ValueError(f"cannot create CLVMObject from: {v!r}")
return self
44 changes: 20 additions & 24 deletions clvm/as_python.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
from __future__ import annotations

from typing import Any, Callable, List, Tuple, TYPE_CHECKING, Union
from typing import Any, Callable, List, Tuple, TYPE_CHECKING, Union, cast

if TYPE_CHECKING:
from clvm.SExp import SExp

OpCallable = Callable[["OpStackType", "ValStackType"], None]

ValType = Union["SExp", List["ValType"], Tuple["ValType", ...]]
ValStackType = List[Union[ValType, "ValStackType"]]
PythonReturnType = Union[bytes, Tuple["PythonReturnType", "PythonReturnType"], List["PythonReturnType"]]

ValType = Union["SExp", PythonReturnType]
ValStackType = List[ValType]

OpStackType = List[OpCallable]


def _roll(op_stack: OpStackType, val_stack: List[object]) -> None:
def _roll(op_stack: OpStackType, val_stack: ValStackType) -> None:
v1 = val_stack.pop()
v2 = val_stack.pop()
val_stack.append(v1)
val_stack.append(v2)


# MakeTupleValType = Union[bytes,
MakeTupleValStackType = List[Union[bytes, Tuple[object, object], "MakeTupleValStackType"]]


def _make_tuple(op_stack: OpStackType, val_stack: MakeTupleValStackType) -> None:
left = val_stack.pop()
right = val_stack.pop()
def _make_tuple(op_stack: OpStackType, val_stack: ValStackType) -> None:
left: PythonReturnType = val_stack.pop() # type: ignore[assignment]
right: PythonReturnType = val_stack.pop() # type: ignore[assignment]
if right == b"":
val_stack.append([left])
elif isinstance(right, list):
Expand All @@ -35,32 +37,26 @@ def _make_tuple(op_stack: OpStackType, val_stack: MakeTupleValStackType) -> None
val_stack.append((left, right))


def _as_python(op_stack: OpStackType, val_stack: List[SExp]) -> None:
t = val_stack.pop()
def _as_python(op_stack: OpStackType, val_stack: ValStackType) -> None:
t: SExp = val_stack.pop() # type: ignore[assignment]
pair = t.as_pair()
if pair:
left, right = pair
# TODO: make this work, ignoring to look at other things
op_stack.append(_make_tuple) # type: ignore[arg-type]
# TODO: make this work, ignoring to look at other things
op_stack.append(_as_python) # type: ignore[arg-type]
# TODO: make this work, ignoring to look at other things
op_stack.append(_roll) # type: ignore[arg-type]
# TODO: make this work, ignoring to look at other things
op_stack.append(_as_python) # type: ignore[arg-type]
op_stack.append(_make_tuple)
op_stack.append(_as_python)
op_stack.append(_roll)
op_stack.append(_as_python)
val_stack.append(left)
val_stack.append(right)
else:
# TODO: do we have to ignore?
# we know that t.atom is not None here because the pair is None
val_stack.append(t.atom) # type:ignore[arg-type]


def as_python(sexp: SExp) -> Any:
# TODO: make this work, ignoring to look at other things
op_stack: OpStackType = [_as_python] # type: ignore[list-item]
val_stack: List[SExp] = [sexp]
op_stack: OpStackType = [_as_python]
val_stack: ValStackType = [sexp]
while op_stack:
op_f = op_stack.pop()
# TODO: make this work, ignoring to look at other things
op_f(op_stack, val_stack) # type: ignore[arg-type]
op_f(op_stack, val_stack)
return val_stack[-1]
4 changes: 2 additions & 2 deletions clvm/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def args_len(op_name: str, args: SExp) -> Iterator[int]:
for arg in args.as_iter():
if arg.pair:
raise EvalError("%s requires int args" % op_name, arg)
assert arg.atom is not None
yield len(arg.atom)
# not a pair so must be an atom
yield len(arg.atom) # type: ignore[arg-type]


# unknown ops are reserved if they start with 0xffff
Expand Down

0 comments on commit 55a514d

Please sign in to comment.