Skip to content

Commit b350269

Browse files
authored
Merge b467812 into a25c8ef
2 parents a25c8ef + b467812 commit b350269

31 files changed

+16080
-350
lines changed

compiler/qsc_qasm/src/compiler.rs

Lines changed: 59 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,34 @@ 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 => build_convert_cast_call_by_name(
1707+
"ResultArrayAsAngleBE",
1708+
expr,
1709+
name_span,
1710+
operand_span,
1711+
),
1712+
Type::Int(Some(width), _) | Type::UInt(Some(width), _) if *width == size => {
1713+
build_convert_cast_call_by_name("ResultArrayAsIntBE", expr, name_span, operand_span)
1714+
}
1715+
Type::Int(None, _) | Type::UInt(None, _) => {
1716+
build_convert_cast_call_by_name("ResultArrayAsIntBE", expr, name_span, operand_span)
1717+
}
1718+
_ => err_expr(span),
16991719
}
17001720
}
17011721

@@ -1734,6 +1754,16 @@ impl QasmCompiler {
17341754
};
17351755
build_convert_cast_call_by_name(function, expr, name_span, operand_span)
17361756
}
1757+
Type::BitArray(size, _) => {
1758+
let size_expr = build_lit_int_expr(i64::from(*size), Span::default());
1759+
build_qasmstd_convert_call_with_two_params(
1760+
"BoolAsResultArrayBE",
1761+
expr,
1762+
size_expr,
1763+
name_span,
1764+
operand_span,
1765+
)
1766+
}
17371767
_ => err_expr(span),
17381768
}
17391769
}
@@ -1776,6 +1806,8 @@ impl QasmCompiler {
17761806
span: Span,
17771807
) -> qsast::Expr {
17781808
assert!(matches!(expr_ty, Type::Float(..)));
1809+
let name_span = expr.span;
1810+
let operand_span = span;
17791811

17801812
match ty {
17811813
&Type::Complex(..) => build_complex_from_expr(expr),
@@ -1818,6 +1850,9 @@ impl QasmCompiler {
18181850
span,
18191851
)
18201852
}
1853+
&Type::Bit(..) => {
1854+
build_convert_cast_call_by_name("DoubleAsResult", expr, name_span, operand_span)
1855+
}
18211856
_ => err_expr(span),
18221857
}
18231858
}

0 commit comments

Comments
 (0)