Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add compile-time evaluation of slice() #3667

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
33 changes: 19 additions & 14 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,25 @@ class Slice(BuiltinFunction):

def evaluate(self, node):
(lit, st, le) = node.args[:3]
(st_val, le_val) = (st.value, le.value)
if isinstance(lit, vy_ast.Bytes):
sublit = lit.value[st_val : (st_val + le_val)]
return vy_ast.Bytes.from_node(node, value=sublit)
elif isinstance(lit, vy_ast.Str):
sublit = lit.value[st_val : (st_val + le_val)]
return vy_ast.Str.from_node(node, value=sublit)
elif isinstance(lit, vy_ast.Hex):
length = len(lit.value) // 2 - 1
if length != 32:
# TODO unreachable?
raise UnfoldableNode
sublit = lit.value[st_val : (2 + st_val + (le_val * 2))]
return vy_ast.Bytes.from_node(node, value=sublit)
if (
isinstance(lit, (vy_ast.Bytes, vy_ast.Str, vy_ast.Hex))
and isinstance(st, vy_ast.Int)
and isinstance(le, vy_ast.Int)
):
(st_val, le_val) = (st.value, le.value)
if isinstance(lit, vy_ast.Bytes):
sublit = lit.value[st_val : (st_val + le_val)]
return vy_ast.Bytes.from_node(node, value=sublit)
elif isinstance(lit, vy_ast.Str):
sublit = lit.value[st_val : (st_val + le_val)]
return vy_ast.Str.from_node(node, value=sublit)
else:
length = len(lit.value) // 2 - 1
if length != 32:
# TODO unreachable?
raise UnfoldableNode
sublit = lit.value[st_val : (2 + st_val + (le_val * 2))]
return vy_ast.Bytes.from_node(node, value=sublit)
else:
raise UnfoldableNode

Expand Down