Skip to content

Commit

Permalink
Support ternary operators (#92)
Browse files Browse the repository at this point in the history
This change adds support for ternary operators, ie conditional `x ? y |
z` and update `x w/ y <- z`, as well as the assign update expression
`set x w/= y <- z`. It includes corresponding test cases.
  • Loading branch information
swernli committed Mar 27, 2023
1 parent 44c6abc commit cccc6cf
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 5 deletions.
53 changes: 48 additions & 5 deletions compiler/qsc_eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use qir_backend::__quantum__rt__qubit_allocate;
use qsc_ast::ast::{
self, BinOp, Block, CallableBody, CallableDecl, Expr, ExprKind, Functor, Lit, Mutability,
NodeId, Pat, PatKind, QubitInit, QubitInitKind, Span, Spec, SpecBody, SpecGen, Stmt, StmtKind,
UnOp,
TernOp, UnOp,
};
use qsc_frontend::{
compile::{PackageId, PackageStore},
Expand Down Expand Up @@ -282,6 +282,10 @@ impl<'a> Evaluator<'a> {
let update = self.eval_binop(*op, lhs, rhs)?;
self.update_binding(lhs, update)
}
ExprKind::AssignUpdate(lhs, mid, rhs) => {
let update = self.eval_ternop_update(lhs, mid, rhs)?;
self.update_binding(lhs, update)
}
ExprKind::BinOp(op, lhs, rhs) => self.eval_binop(*op, lhs, rhs),
ExprKind::Block(block) => self.eval_block(block),
ExprKind::Call(call, args) => self.eval_call(call, args),
Expand Down Expand Up @@ -325,6 +329,10 @@ impl<'a> Evaluator<'a> {
ExprKind::Return(expr) => {
ControlFlow::Break(Reason::Return(self.eval_expr_impl(expr)?))
}
ExprKind::TernOp(ternop, lhs, mid, rhs) => match *ternop {
TernOp::Cond => self.eval_ternop_cond(lhs, mid, rhs),
TernOp::Update => self.eval_ternop_update(lhs, mid, rhs),
},
ExprKind::Tuple(tup) => {
let mut val_tup = vec![];
for expr in tup {
Expand All @@ -339,13 +347,11 @@ impl<'a> Evaluator<'a> {
ControlFlow::Continue(Value::UNIT)
}
ExprKind::UnOp(op, rhs) => self.eval_unop(expr, *op, rhs),
ExprKind::AssignUpdate(..)
| ExprKind::Conjugate(..)
ExprKind::Conjugate(..)
| ExprKind::Err
| ExprKind::Field(..)
| ExprKind::Hole
| ExprKind::Lambda(..)
| ExprKind::TernOp(..) => {
| ExprKind::Lambda(..) => {
ControlFlow::Break(Reason::Error(Error::Unimplemented(expr.span)))
}
}
Expand Down Expand Up @@ -692,6 +698,43 @@ impl<'a> Evaluator<'a> {
))
}

fn eval_ternop_cond(
&mut self,
lhs: &Expr,
mid: &Expr,
rhs: &Expr,
) -> ControlFlow<Reason, Value> {
if self.eval_expr_impl(lhs)?.try_into().with_span(lhs.span)? {
self.eval_expr_impl(mid)
} else {
self.eval_expr_impl(rhs)
}
}

fn eval_ternop_update(
&mut self,
lhs: &Expr,
mid: &Expr,
rhs: &Expr,
) -> ControlFlow<Reason, Value> {
let mut arr = self
.eval_expr_impl(lhs)?
.try_into_array()
.with_span(lhs.span)?;
let index: i64 = self.eval_expr_impl(mid)?.try_into().with_span(mid.span)?;
if index < 0 {
ControlFlow::Break(Reason::Error(Error::Negative(index, mid.span)))
} else {
match arr.get_mut(index.as_index(mid.span)?) {
Some(v) => {
*v = self.eval_expr_impl(rhs)?;
ControlFlow::Continue(Value::Array(arr))
}
None => ControlFlow::Break(Reason::Error(Error::OutOfRange(index, mid.span))),
}
}
}

fn enter_scope(&mut self) {
self.env.0.push(HashMap::default());
}
Expand Down
146 changes: 146 additions & 0 deletions compiler/qsc_eval/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,152 @@ fn while_invalid_type_expr() {
);
}

#[test]
fn ternop_cond_expr() {
check_expr("", "true ? 1 | 0", &expect!["1"]);
}

#[test]
fn ternop_cond_false_expr() {
check_expr("", "false ? 1 | 0", &expect!["0"]);
}

#[test]
fn ternop_cond_shortcircuit_expr() {
check_expr("", r#"true ? 1 | fail "Shouldn't fail""#, &expect!["1"]);
}

#[test]
fn ternop_cond_false_shortcircuit_expr() {
check_expr("", r#"false ? fail "Shouldn't fail" | 0"#, &expect!["0"]);
}

#[test]
fn ternop_cond_invalid_type_expr() {
check_expr(
"",
"7 ? 1 | 0",
&expect![[r#"
Type(
"Bool",
"Int",
Span {
lo: 0,
hi: 1,
},
)
"#]],
);
}

#[test]
fn ternop_update_expr() {
check_expr("", "[1, 2, 3] w/ 2 <- 4", &expect!["[1, 2, 4]"]);
}

#[test]
fn ternop_update_invalid_target_expr() {
check_expr(
"",
"(1, 2, 3) w/ 2 <- 4",
&expect![[r#"
Type(
"Array",
"Tuple",
Span {
lo: 0,
hi: 9,
},
)
"#]],
);
}

#[test]
fn ternop_update_invalid_index_type_expr() {
check_expr(
"",
"[1, 2, 3] w/ false <- 4",
&expect![[r#"
Type(
"Int",
"Bool",
Span {
lo: 13,
hi: 18,
},
)
"#]],
);
}

#[test]
fn ternop_update_invalid_index_range_expr() {
check_expr(
"",
"[1, 2, 3] w/ 7 <- 4",
&expect![[r#"
OutOfRange(
7,
Span {
lo: 13,
hi: 14,
},
)
"#]],
);
}

#[test]
fn ternop_update_invalid_index_negative_expr() {
check_expr(
"",
"[1, 2, 3] w/ -1 <- 4",
&expect![[r#"
Negative(
-1,
Span {
lo: 13,
hi: 15,
},
)
"#]],
);
}

#[test]
fn assignupdate_expr() {
check_expr(
"",
indoc! {"{
mutable x = [1, 2, 3];
set x w/= 2 <- 4;
x
}"},
&expect!["[1, 2, 4]"],
);
}

#[test]
fn assignupdate_immutable_expr() {
check_expr(
"",
indoc! {"{
let x = [1, 2, 3];
set x w/= 2 <- 4;
x
}"},
&expect![[r#"
Mutability(
Span {
lo: 33,
hi: 34,
},
)
"#]],
);
}

#[test]
fn unop_bitwise_not_int_expr() {
check_expr("", "~~~(13)", &expect!["-14"]);
Expand Down

0 comments on commit cccc6cf

Please sign in to comment.