Skip to content

Commit

Permalink
add hexbytes node
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Oct 5, 2024
1 parent 711604f commit d86e7f5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
19 changes: 19 additions & 0 deletions vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,25 @@ def s(self):
return self.value


class HexBytes(Constant):
__slots__ = ()
_translated_fields = {"s": "value"}

def __init__(self, parent: Optional["VyperNode"] = None, **kwargs: dict):
super().__init__(parent, **kwargs)
if isinstance(self.value, str):
self.value = bytes.fromhex(self.value)

def to_dict(self):
ast_dict = super().to_dict()
ast_dict["value"] = f"0x{self.value.hex()}"
return ast_dict

@property
def s(self):
return self.value


class List(ExprNode):
__slots__ = ("elements",)
_translated_fields = {"elts": "elements"}
Expand Down
6 changes: 1 addition & 5 deletions vyper/ast/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,7 @@ def visit_Constant(self, node):
node.lineno,
node.col_offset,
)

byte_val = bytes.fromhex(node.value)

node.ast_type = "Bytes"
node.value = byte_val
node.ast_type = "HexBytes"
else:
node.ast_type = "Str"
elif isinstance(node.value, bytes):
Expand Down
6 changes: 6 additions & 0 deletions vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ def parse_Str(self):

# Byte literals
def parse_Bytes(self):
return self._parse_bytes()

def parse_HexBytes(self):
return self._parse_bytes()

def _parse_bytes(self, bytes):
bytez = self.expr.value
bytez_length = len(self.expr.value)
typ = BytesT(bytez_length)
Expand Down
2 changes: 1 addition & 1 deletion vyper/semantics/types/bytestrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class BytesT(_BytestringT):
typeclass = "bytes"

_id = "Bytes"
_valid_literal = (vy_ast.Bytes,)
_valid_literal = (vy_ast.Bytes, vy_ast.HexBytes)

@property
def abi_type(self) -> ABIType:
Expand Down

0 comments on commit d86e7f5

Please sign in to comment.