Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Nov 1, 2023
1 parent 27edf0b commit 55359e7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
4 changes: 2 additions & 2 deletions tests/ast/test_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def test_replace_constant(source):
unmodified_ast = vy_ast.parse_to_ast(source)
folded_ast = vy_ast.parse_to_ast(source)

folding.replace_constant(folded_ast, "FOO", vy_ast.Int(value=31337), UINT256_T, 31337, True)
folding.replace_constant(folded_ast, "FOO", vy_ast.Int(value=31337), UINT256_T, True)

assert not vy_ast.compare_nodes(unmodified_ast, folded_ast)

Expand All @@ -223,7 +223,7 @@ def test_replace_constant_no(source):
unmodified_ast = vy_ast.parse_to_ast(source)
folded_ast = vy_ast.parse_to_ast(source)

folding.replace_constant(folded_ast, "FOO", vy_ast.Int(value=31337), UINT256_T, 31337, True)
folding.replace_constant(folded_ast, "FOO", vy_ast.Int(value=31337), UINT256_T, True)

assert vy_ast.compare_nodes(unmodified_ast, folded_ast)

Expand Down
12 changes: 6 additions & 6 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ class Floor(BuiltinFunction):

def evaluate(self, node, skip_typecheck=False):
validate_call_args(node, 1)
input_val = node.args[0]._metadata.get("folded_value")
if not isinstance(input_val, vy_ast.Decimal):
arg = node.args[0]._metadata.get("folded_value")
if not isinstance(arg, vy_ast.Decimal):
raise UnfoldableNode

value = math.floor(input_val)
value = math.floor(arg.value)
return vy_ast.Int.from_node(node, value=value)

@process_inputs
Expand All @@ -172,11 +172,11 @@ class Ceil(BuiltinFunction):

def evaluate(self, node, skip_typecheck=False):
validate_call_args(node, 1)
input_val = node.args[0]._metadata.get("folded_value")
if not isinstance(input_val, vy_ast.Decimal):
arg = node.args[0]._metadata.get("folded_value")
if not isinstance(arg, vy_ast.Decimal):
raise UnfoldableNode

value = math.ceil(input_val)
value = math.ceil(arg.value)
return vy_ast.Int.from_node(node, value=value)

@process_inputs
Expand Down
10 changes: 3 additions & 7 deletions vyper/semantics/analysis/pre_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def _subscriptable_helper(self, node):

values = [get_folded_value(e) for e in node.elements]
if None not in values:
node._metadata["folded_value"] = values
node._metadata["folded_value"] = type(node).from_node(node, elts=values)

def visit_List(self, node):
self._subscriptable_helper(node)
Expand All @@ -269,8 +269,8 @@ def visit_Subscript(self, node):
self.visit(node.slice)
self.visit(node.value)

sliced = get_folded_value(node.slice)
index = get_folded_value(node.value)
index = get_folded_value(node.slice)
sliced = get_folded_value(node.value)
if None not in (sliced, index):
node._metadata["folded_value"] = sliced.elements[index.value]

Expand All @@ -293,9 +293,5 @@ def visit_IfExp(self, node):
def get_folded_value(node: vy_ast.VyperNode) -> Optional[vy_ast.VyperNode]:
if isinstance(node, vy_ast.Constant):
return node
elif isinstance(node, (vy_ast.List, vy_ast.Tuple)):
values = [get_folded_value(i) for i in node.elements]
if None not in values:
return values

return node._metadata.get("folded_value")

0 comments on commit 55359e7

Please sign in to comment.