Module opshin.rewrite.rewrite_import_integrity_check
Expand source code
import re
from copy import copy
from typing import Optional
from enum import Enum
from ..type_inference import INITIAL_SCOPE
from ..util import CompilingNodeTransformer
from ..typed_ast import *
"""
Injects the integrity checker function if it is imported
"""
FunctionName = "check_integrity"
class IntegrityCheckImpl(PolymorphicFunction):
def type_from_args(self, args: typing.List[Type]) -> FunctionType:
assert (
len(args) == 1
), f"'integrity_check' takes only one argument, but {len(args)} were given"
typ = args[0]
assert isinstance(typ, InstanceType), "Can only check integrity of instances"
assert any(
isinstance(typ.typ, t) for t in (RecordType, UnionType)
), "Can only check integrity of PlutusData and Union types"
return FunctionType(args, NoneInstanceType)
def impl_from_args(self, args: typing.List[Type]) -> plt.AST:
arg = args[0]
assert isinstance(arg, InstanceType), "Can only check integrity of instances"
return OLambda(
["x"],
plt.Ite(
plt.EqualsData(
OVar("x"),
plt.Apply(arg.typ.copy_only_attributes(), OVar("x")),
),
plt.Unit(),
plt.TraceError("ValueError: datum integrity check failed"),
),
)
class RewriteImportIntegrityCheck(CompilingNodeTransformer):
step = "Resolving imports and usage of integrity check"
def visit_ImportFrom(self, node: ImportFrom) -> Optional[AST]:
if node.module != "opshin.std.integrity":
return node
for n in node.names:
assert (
n.name == FunctionName
), "Imports something other from the integrity check than the integrity check builtin"
renamed = n.asname if n.asname is not None else n.name
INITIAL_SCOPE[renamed] = InstanceType(
PolymorphicFunctionType(IntegrityCheckImpl())
)
return None
Classes
class IntegrityCheckImpl (*args, **kwargs)
-
Expand source code
class IntegrityCheckImpl(PolymorphicFunction): def type_from_args(self, args: typing.List[Type]) -> FunctionType: assert ( len(args) == 1 ), f"'integrity_check' takes only one argument, but {len(args)} were given" typ = args[0] assert isinstance(typ, InstanceType), "Can only check integrity of instances" assert any( isinstance(typ.typ, t) for t in (RecordType, UnionType) ), "Can only check integrity of PlutusData and Union types" return FunctionType(args, NoneInstanceType) def impl_from_args(self, args: typing.List[Type]) -> plt.AST: arg = args[0] assert isinstance(arg, InstanceType), "Can only check integrity of instances" return OLambda( ["x"], plt.Ite( plt.EqualsData( OVar("x"), plt.Apply(arg.typ.copy_only_attributes(), OVar("x")), ), plt.Unit(), plt.TraceError("ValueError: datum integrity check failed"), ), )
Ancestors
Methods
def impl_from_args(self, args: List[Type]) ‑> pluthon.pluthon_ast.AST
def type_from_args(self, args: List[Type]) ‑> FunctionType
class RewriteImportIntegrityCheck
-
A :class:
NodeVisitor
subclass that walks the abstract syntax tree and allows modification of nodes.The
NodeTransformer
will walk the AST and use the return value of the visitor methods to replace or remove the old node. If the return value of the visitor method isNone
, the node will be removed from its location, otherwise it is replaced with the return value. The return value may be the original node in which case no replacement takes place.Here is an example transformer that rewrites all occurrences of name lookups (
foo
) todata['foo']
::class RewriteName(NodeTransformer):
def visit_Name(self, node): return Subscript( value=Name(id='data', ctx=Load()), slice=Constant(value=node.id), ctx=node.ctx )
Keep in mind that if the node you're operating on has child nodes you must either transform the child nodes yourself or call the :meth:
generic_visit
method for the node first.For nodes that were part of a collection of statements (that applies to all statement nodes), the visitor may also return a list of nodes rather than just a single node.
Usually you use the transformer like this::
node = YourTransformer().visit(node)
Expand source code
class RewriteImportIntegrityCheck(CompilingNodeTransformer): step = "Resolving imports and usage of integrity check" def visit_ImportFrom(self, node: ImportFrom) -> Optional[AST]: if node.module != "opshin.std.integrity": return node for n in node.names: assert ( n.name == FunctionName ), "Imports something other from the integrity check than the integrity check builtin" renamed = n.asname if n.asname is not None else n.name INITIAL_SCOPE[renamed] = InstanceType( PolymorphicFunctionType(IntegrityCheckImpl()) ) return None
Ancestors
- CompilingNodeTransformer
- TypedNodeTransformer
- ast.NodeTransformer
- ast.NodeVisitor
Class variables
var step
Methods
def visit(self, node)
-
Inherited from:
CompilingNodeTransformer
.visit
Visit a node.
def visit_ImportFrom(self, node: ast.ImportFrom) ‑> Optional[ast.AST]