Example #1
0
def test_flow_tracking_converge_references(refs):
    flow_tracking = FlowTracking()
    flow_tracking.add_flow_to_label(ScopedName.from_string('a'), 7)
    flow_tracking.add_flow_to_label(ScopedName.from_string('b'), 5)

    # Label a.
    flow_tracking.revoke()
    flow_tracking.converge_with_label(ScopedName.from_string('a'))
    flow_tracking.add_reference(
        name=ScopedName.from_string('x'),
        ref=Reference(
            pc=0,
            value=refs.expr_a,
            ap_tracking_data=flow_tracking.get_ap_tracking(),
        ))
    flow_tracking.add_ap(13)
    flow_tracking.add_flow_to_label(ScopedName.from_string('c'), 0)

    # Label b.
    flow_tracking.revoke()
    flow_tracking.converge_with_label(ScopedName.from_string('b'))
    flow_tracking.add_reference(
        name=ScopedName.from_string('x'),
        ref=Reference(
            pc=0,
            value=refs.expr_b,
            ap_tracking_data=flow_tracking.get_ap_tracking(),
        ))
    flow_tracking.add_ap(15)
    flow_tracking.add_flow_to_label(ScopedName.from_string('c'), 0)

    # Label c - convergence.
    flow_tracking.revoke()
    flow_tracking.converge_with_label(ScopedName.from_string('c'))

    if refs.valid:
        flow_tracking.resolve_reference(ScopedName.from_string('x'))
    else:
        with pytest.raises(FlowTrackingError):
            flow_tracking.resolve_reference(ScopedName.from_string('x'))
 def type_struct(self, value):
     assert len(value) == 1 and isinstance(value[0], ExprIdentifier)
     return TypeStruct(scope=ScopedName.from_string(value[0].name),
                       is_fully_resolved=False,
                       location=value[0].location)
Example #3
0
class CodeElementFunction(CodeElement):
    """
    Represents either a 'func', 'namespace' or 'struct' statement.
    For example:
      func foo(x, y) -> (z, w):
          return (z=x, w=y)
      end
    """
    # The type of the code element. Either 'func', 'namespace' or 'struct'.
    element_type: str
    identifier: ExprIdentifier
    arguments: IdentifierList
    implicit_arguments: Optional[IdentifierList]
    returns: Optional[IdentifierList]
    code_block: CodeBlock

    ARGUMENT_SCOPE = ScopedName.from_string('Args')
    IMPLICIT_ARGUMENT_SCOPE = ScopedName.from_string('ImplicitArgs')
    RETURN_SCOPE = ScopedName.from_string('Return')

    @property
    def name(self):
        return self.identifier.name

    def format(self, allowed_line_length):
        code = self.code_block.format(allowed_line_length=allowed_line_length -
                                      INDENTATION)
        code = indent(code, INDENTATION)
        if self.element_type in ['struct', 'namespace']:
            particles = [f'{self.element_type} {self.name}:']
        else:
            if self.implicit_arguments is not None:
                first_particle_suffix = '{'
                implicit_args_particles = [
                    create_particle_sublist(
                        self.implicit_arguments.get_particles(), '}(')
                ]
            else:
                first_particle_suffix = '('
                implicit_args_particles = []

            if self.returns is not None:
                particles = [
                    f'{self.element_type} {self.name}{first_particle_suffix}',
                    *implicit_args_particles,
                    create_particle_sublist(self.arguments.get_particles(),
                                            ') -> ('),
                    create_particle_sublist(self.returns.get_particles(), '):')
                ]
            else:
                particles = [
                    f'{self.element_type} {self.name}{first_particle_suffix}',
                    *implicit_args_particles,
                    create_particle_sublist(self.arguments.get_particles(),
                                            '):')
                ]

        header = particles_in_lines(
            particles=particles,
            config=ParticleFormattingConfig(
                allowed_line_length=allowed_line_length,
                line_indent=INDENTATION * 2))
        return f'{header}\n{code}end'

    def get_children(self) -> Sequence[Optional[AstNode]]:
        return [
            self.identifier, self.arguments, self.implicit_arguments,
            self.returns, self.code_block
        ]
Example #4
0
from starkware.cairo.lang.compiler.scoped_name import ScopedName

LIBS_DIR_ENVVAR = 'CAIRO_PATH'
MAIN_SCOPE = ScopedName.from_string('__main__')
START_FILE_NAME = '<start>'
SIZE_CONSTANT = ScopedName.from_string('SIZE')
Example #5
0
 def get_reference_type(name):
     identifier_definition = program.identifiers.get_by_full_name(ScopedName.from_string(name))
     assert isinstance(identifier_definition, ReferenceDefinition)
     assert len(identifier_definition.references) == 1
     _, expr_type = simplify_type_system(identifier_definition.references[0].value)
     return expr_type