Skip to content

Commit 727aa46

Browse files
authored
Merge d8c3a42 into 285baa4
2 parents 285baa4 + d8c3a42 commit 727aa46

32 files changed

+16079
-352
lines changed

compiler/qsc_qasm/src/compiler.rs

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ impl QasmCompiler {
16181618
}
16191619
Type::Bit(..) => build_angle_cast_call_by_name("AngleAsResult", expr, span, span),
16201620
Type::BitArray(..) => {
1621-
build_angle_cast_call_by_name("AngleAsResultArray", expr, span, span)
1621+
build_angle_cast_call_by_name("AngleAsResultArrayBE", expr, span, span)
16221622
}
16231623
Type::Bool(..) => build_angle_cast_call_by_name("AngleAsBool", expr, span, span),
16241624
_ => err_expr(span),
@@ -1644,21 +1644,18 @@ impl QasmCompiler {
16441644
let operand_span = expr.span;
16451645
let name_span = span;
16461646
match ty {
1647-
&Type::Angle(..) => {
1647+
Type::Angle(..) => {
16481648
build_angle_cast_call_by_name("ResultAsAngle", expr, name_span, operand_span)
16491649
}
1650-
&Type::Bool(..) => {
1650+
Type::Bool(..) => {
16511651
build_convert_cast_call_by_name("ResultAsBool", expr, name_span, operand_span)
16521652
}
1653-
&Type::Float(..) => {
1654-
// The spec says that this cast isn't supported, but it
1655-
// casts to other types that case to float. For now, we'll
1656-
// say it is invalid like the spec.
1657-
err_expr(span)
1653+
Type::Float(..) => {
1654+
build_convert_cast_call_by_name("ResultAsDouble", expr, name_span, operand_span)
16581655
}
1659-
&Type::Int(w, _) | &Type::UInt(w, _) => {
1656+
Type::Int(w, _) | Type::UInt(w, _) => {
16601657
let function = if let Some(width) = w {
1661-
if width > 64 {
1658+
if *width > 64 {
16621659
"ResultAsBigInt"
16631660
} else {
16641661
"ResultAsInt"
@@ -1669,6 +1666,16 @@ impl QasmCompiler {
16691666

16701667
build_convert_cast_call_by_name(function, expr, name_span, operand_span)
16711668
}
1669+
Type::BitArray(size, _) => {
1670+
let size_expr = build_lit_int_expr(i64::from(*size), Span::default());
1671+
build_qasmstd_convert_call_with_two_params(
1672+
"ResultAsResultArrayBE",
1673+
expr,
1674+
size_expr,
1675+
name_span,
1676+
operand_span,
1677+
)
1678+
}
16721679
_ => err_expr(span),
16731680
}
16741681
}
@@ -1681,21 +1688,31 @@ impl QasmCompiler {
16811688
span: Span,
16821689
) -> qsast::Expr {
16831690
assert!(matches!(expr_ty, Type::BitArray(_, _)));
1691+
// There is no operand, choosing the span of the node
1692+
// but we could use the expr span as well.
1693+
let operand_span = expr.span;
1694+
let name_span = span;
16841695

1685-
let name_span = expr.span;
1686-
let operand_span = span;
1687-
1688-
if !matches!(ty, Type::Int(..) | Type::UInt(..)) {
1689-
return err_expr(span);
1690-
}
1691-
// we know we have a bit array being cast to an int/uint
1692-
// verfiy widths
1693-
let int_width = ty.width();
1694-
1695-
if int_width.is_none() || (int_width == Some(size)) {
1696-
build_convert_cast_call_by_name("ResultArrayAsIntBE", expr, name_span, operand_span)
1697-
} else {
1698-
err_expr(span)
1696+
match ty {
1697+
Type::Bit(..) => build_convert_cast_call_by_name(
1698+
"ResultArrayAsResultBE",
1699+
expr,
1700+
name_span,
1701+
operand_span,
1702+
),
1703+
Type::Bool(..) => {
1704+
build_convert_cast_call_by_name("ResultArrayAsBool", expr, name_span, operand_span)
1705+
}
1706+
Type::Angle(Some(width), _) if *width == size => {
1707+
build_angle_cast_call_by_name("ResultArrayAsAngleBE", expr, name_span, operand_span)
1708+
}
1709+
Type::Int(Some(width), _) | Type::UInt(Some(width), _) if *width == size => {
1710+
build_convert_cast_call_by_name("ResultArrayAsIntBE", expr, name_span, operand_span)
1711+
}
1712+
Type::Int(None, _) | Type::UInt(None, _) => {
1713+
build_convert_cast_call_by_name("ResultArrayAsIntBE", expr, name_span, operand_span)
1714+
}
1715+
_ => err_expr(span),
16991716
}
17001717
}
17011718

@@ -1734,6 +1751,16 @@ impl QasmCompiler {
17341751
};
17351752
build_convert_cast_call_by_name(function, expr, name_span, operand_span)
17361753
}
1754+
Type::BitArray(size, _) => {
1755+
let size_expr = build_lit_int_expr(i64::from(*size), Span::default());
1756+
build_qasmstd_convert_call_with_two_params(
1757+
"BoolAsResultArrayBE",
1758+
expr,
1759+
size_expr,
1760+
name_span,
1761+
operand_span,
1762+
)
1763+
}
17371764
_ => err_expr(span),
17381765
}
17391766
}
@@ -1776,6 +1803,8 @@ impl QasmCompiler {
17761803
span: Span,
17771804
) -> qsast::Expr {
17781805
assert!(matches!(expr_ty, Type::Float(..)));
1806+
let name_span = expr.span;
1807+
let operand_span = span;
17791808

17801809
match ty {
17811810
&Type::Complex(..) => build_complex_from_expr(expr),
@@ -1818,6 +1847,9 @@ impl QasmCompiler {
18181847
span,
18191848
)
18201849
}
1850+
&Type::Bit(..) => {
1851+
build_convert_cast_call_by_name("DoubleAsResult", expr, name_span, operand_span)
1852+
}
18211853
_ => err_expr(span),
18221854
}
18231855
}

0 commit comments

Comments
 (0)