Beispiel #1
0
    def execute(self, code: str) -> InterpreterResult:
        """Execute some code preserving current context and stack

        :param code: Michelson code
        """
        result = InterpreterResult(stdout=[])
        stack_backup = deepcopy(self.stack)
        context_backup = deepcopy(self.context)

        try:
            code_section = CodeSection.match(michelson_to_micheline(code))
            instructions = code_section.args[0].execute(
                self.stack, result.stdout, self.context)
            result.instructions = MichelineSequence([instructions])
            result.stack = self.stack
        except (MichelsonParserError, MichelsonRuntimeError) as e:
            if self.context.debug:
                raise

            self.stack = stack_backup
            self.context = context_backup
            result.stdout.append(e.format_stdout())
            result.error = e

        return result
Beispiel #2
0
    def _execute_code(self, code, parameter=None, storage=None) -> None:
        micheline = michelson_to_micheline(code)
        sequence = MichelineSequence.match(micheline)

        program = MichelsonProgram.create(sequence)(
            'default', parameter or ParameterSection(UnitType()), storage or StorageSection(UnitType())
        )
        program.execute(self.stack, self.stdout, self.context)
Beispiel #3
0
 def to_literal(self) -> Type[Micheline]:
     if self.ptr is not None:
         return MichelineLiteral.create(self.ptr)
     else:
         return MichelineSequence.create_type(args=[
             EltLiteral.create_type(
                 args=[k.to_literal(), v.to_literal()])
             for k, v in self.items
         ])
Beispiel #4
0
 def _find_stack_items(self, instructions: MichelineSequence, stack: MichelsonStack) -> Optional[List[MichelsonInstruction]]:
     for operation in instructions.items[::-1]:
         items = getattr(operation, 'items', None)
         if isinstance(items, list):
             stack_items = self._find_stack_items(MichelineSequence(items), stack)
             if stack_items:
                 return stack_items
         if not isinstance(operation, MichelsonInstruction):
             continue
         if operation.stack_items_added:
             return cast(List[MichelsonInstruction], stack.items[-operation.stack_items_added :])
     return None
Beispiel #5
0
    def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
        left, lambda_ = cast(Tuple[MichelsonType, LambdaType], stack.pop2())
        lambda_.assert_type_in(LambdaType)
        lambda_.args[0].assert_type_in(PairType)
        left_type, right_type = lambda_.args[0].args
        left.assert_type_equal(left_type)

        new_value = MichelineSequence.create_type(args=[
            PushInstruction.create_type(args=[left_type, left.to_literal()]),
            PairInstruction,
            lambda_.value
        ])
        res = LambdaType.create_type(args=[right_type, lambda_.args[1]])(new_value)  # type: ignore
        stack.push(res)
        stdout.append(format_stdout(cls.prim, [left, lambda_], [res]))  # type: ignore
        return cls(stack_items_added=1)
Beispiel #6
0
 def to_literal(self) -> Type[Micheline]:
     return MichelineSequence.create_type(args=[
         EltLiteral.create_type(
             args=[k.to_literal(), v.to_literal()]) for k, v in self.items
     ])
Beispiel #7
0
 def to_literal(self) -> Type[Micheline]:
     if self.ptr is not None:
         return MichelineLiteral.create(self.ptr)
     else:
         return MichelineSequence.create_type(args=[])
Beispiel #8
0
 def match(expr) -> Type['MichelsonProgram']:
     seq = cast(Type[MichelineSequence], MichelineSequence.match(expr))
     if not issubclass(seq, MichelineSequence):
         raise Exception(f'Expected sequence, got {seq.prim}')
     return MichelsonProgram.create(seq)
Beispiel #9
0
 def to_literal(self) -> Type[Micheline]:
     return MichelineSequence.create_type(
         args=[item.to_literal() for item in self.items])
Beispiel #10
0
 def match(expr) -> Type['MichelsonProgram']:
     seq = cast(Type[MichelineSequence], MichelineSequence.match(expr))
     assert issubclass(seq, MichelineSequence), f'expected sequence, got {seq.prim}'
     return MichelsonProgram.create(seq)