def test_slice(): p = """ let a = b[1:1:10]; let c = d[1:10]; """.strip() program = parse(p) assert program == Program(statements=[ AliasStatement( target=Identifier(name="a"), value=Slice( name="b", range=RangeDefinition( start=IntegerLiteral(value=1), end=IntegerLiteral(value=10), step=IntegerLiteral(value=1), ), ), ), AliasStatement( target=Identifier(name="c"), value=Slice( name="d", range=RangeDefinition( start=IntegerLiteral(value=1), end=IntegerLiteral(value=10), step=None, ), ), ), ]) SpanGuard().visit(program) slice_ = program.statements[0] assert slice_.span == Span(1, 0, 1, 17) assert slice_.target.span == Span(1, 4, 1, 4) assert slice_.value.span == Span(1, 8, 1, 16)
def test_concatenation(): p = """ let a = b[1:1:10] ++ c; """.strip() program = parse(p) assert program == Program(statements=[ AliasStatement( target=Identifier(name="a"), value=Concatenation( lhs=Slice( name="b", range=RangeDefinition( start=IntegerLiteral(value=1), end=IntegerLiteral(value=10), step=IntegerLiteral(value=1), ), ), rhs=Identifier(name="c"), ), ) ]) SpanGuard().visit(program) slice_ = program.statements[0] assert slice_.span == Span(1, 0, 1, 22) assert slice_.target.span == Span(1, 4, 1, 4) assert slice_.value.span == Span(1, 8, 1, 21)
def test_alias_statement(): p = """ let a = b; """.strip() program = parse(p) assert program == Program(statements=[ AliasStatement(target=Identifier(name="a"), value=Identifier(name="b")) ]) SpanGuard().visit(program) alias_statement = program.statements[0] assert alias_statement.span == Span(1, 0, 1, 9) assert alias_statement.target.span == Span(1, 4, 1, 4) assert alias_statement.value.span == Span(1, 8, 1, 8)
def test_subscript(): p = """ let a = b[10]; """.strip() program = parse(p) assert program == Program(statements=[ AliasStatement( target=Identifier(name="a"), value=Subscript(name="b", index=IntegerLiteral(value=10)), ) ]) SpanGuard().visit(program) subscript = program.statements[0] assert subscript.span == Span(1, 0, 1, 13) assert subscript.target.span == Span(1, 4, 1, 4) assert subscript.value.span == Span(1, 8, 1, 12)
def test_selection(): p = """ let a = b[1, 2]; """.strip() program = parse(p) assert program == Program(statements=[ AliasStatement( target=Identifier(name="a"), value=Selection( name="b", indices=[IntegerLiteral( value=1), IntegerLiteral(value=2)]), ) ]) SpanGuard().visit(program) selection = program.statements[0] assert selection.span == Span(1, 0, 1, 15) assert selection.target.span == Span(1, 4, 1, 4) assert selection.value.span == Span(1, 8, 1, 14)
def visitAliasStatement(self, ctx: qasm3Parser.AliasStatementContext): target = Identifier(name=ctx.Identifier().getText()) value = self.visit(ctx.indexIdentifier()) return AliasStatement(add_span(target, get_span(ctx.Identifier())), value)
def test_alias_assignment(): p = """ let a = b; let a = b[0:1]; let a = b[{0, 1, 2}]; let a = b ++ c; let a = b[{0, 1}] ++ b[2:2:4] ++ c; """.strip() program = parse(p) a, b, c = Identifier(name="a"), Identifier(name="b"), Identifier(name="c") assert program == Program(statements=[ AliasStatement(target=a, value=b), AliasStatement( target=a, value=IndexExpression( collection=b, index=[ RangeDefinition( start=IntegerLiteral(0), end=IntegerLiteral(1), step=None, ), ], ), ), AliasStatement( target=a, value=IndexExpression( collection=b, index=DiscreteSet(values=[ IntegerLiteral(0), IntegerLiteral(1), IntegerLiteral(2), ]), ), ), AliasStatement(target=a, value=Concatenation(lhs=b, rhs=c)), AliasStatement( target=a, value=Concatenation( lhs=Concatenation( lhs=IndexExpression( collection=b, index=DiscreteSet( values=[IntegerLiteral(0), IntegerLiteral(1)], ), ), rhs=IndexExpression( collection=b, index=[ RangeDefinition( start=IntegerLiteral(2), end=IntegerLiteral(4), step=IntegerLiteral(2), ), ], ), ), rhs=c, ), ), ], ) SpanGuard().visit(program)