Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Support for Unary Negate and Positive Operators #58

Merged
merged 7 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/target
/.vscode
21 changes: 19 additions & 2 deletions compiler/qsc_eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{
use qir_backend::Pauli;
use qsc_ast::ast::{
self, Block, CallableDecl, Expr, ExprKind, Lit, Mutability, NodeId, Package, Pat, PatKind,
Span, Stmt, StmtKind,
Span, Stmt, StmtKind, UnOp,
};
use qsc_frontend::{
compile::{CompileUnit, Context},
Expand Down Expand Up @@ -173,6 +173,24 @@ impl<'a> Evaluator<'a> {
}
ControlFlow::Continue(Value::Tuple(val_tup))
}
ExprKind::UnOp(op, rhs) => {
let val = self.eval_expr(rhs)?;
match op {
UnOp::Neg => val.arithmetic_negate().with_span(rhs.span),
UnOp::Pos => match val {
Value::BigInt(_) | Value::Int(_) | Value::Double(_) => {
ControlFlow::Continue(val)
}
_ => ControlFlow::Break(Reason::Error(
rhs.span,
ErrorKind::Type("Int, BigInt, or Double", val.type_name()),
)),
},
UnOp::Functor(_) | UnOp::NotB | UnOp::NotL | UnOp::Unwrap => {
ControlFlow::Break(Reason::Error(expr.span, ErrorKind::Unimplemented))
}
}
}
ExprKind::ArrayRepeat(_, _)
| ExprKind::AssignOp(_, _, _)
| ExprKind::AssignUpdate(_, _, _)
Expand All @@ -186,7 +204,6 @@ impl<'a> Evaluator<'a> {
| ExprKind::Lambda(_, _, _)
| ExprKind::Repeat(_, _, _)
| ExprKind::TernOp(_, _, _, _)
| ExprKind::UnOp(_, _)
| ExprKind::While(_, _) => {
ControlFlow::Break(Reason::Error(expr.span, ErrorKind::Unimplemented))
}
Expand Down
105 changes: 105 additions & 0 deletions compiler/qsc_eval/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,24 @@ fn array_index_expr() {
check_expression("[1, 2, 3][1]", &expect!["2"]);
}

#[test]
fn array_index_negative_expr() {
check_expression(
"[1, 2, 3][-2]",
&expect![[r#"
Error {
span: Span {
lo: 10,
hi: 12,
},
kind: Index(
-2,
),
}
"#]],
);
}

#[test]
fn array_index_out_of_range_expr() {
check_expression(
Expand Down Expand Up @@ -452,6 +470,93 @@ fn tuple_expr() {
check_expression("(1, 2, 3)", &expect!["(1, 2, 3)"]);
}

#[test]
ScottCarda-MS marked this conversation as resolved.
Show resolved Hide resolved
fn unop_negate_big_int_expr() {
check_expression(
"-(9_223_372_036_854_775_808L)",
&expect!["-9223372036854775808"],
);
}

#[test]
fn unop_negate_bool_expr() {
check_expression(
"-(false)",
&expect![[r#"
Error {
span: Span {
lo: 1,
hi: 8,
},
kind: Type(
"Int, BigInt, or Double",
"Bool",
),
}
"#]],
);
}

#[test]
fn unop_negate_double_expr() {
check_expression("-(3.4)", &expect!["-3.4"]);
}

#[test]
fn unop_negate_int_expr() {
check_expression("-(13)", &expect!["-13"]);
}

#[test]
fn unop_negate_int_overflow_expr() {
check_expression(
"-(9_223_372_036_854_775_808)",
&expect!["-9223372036854775808"],
);
}

#[test]
fn unop_negate_negative_int_expr() {
check_expression("-(-(13))", &expect!["13"]);
}

#[test]
fn unop_positive_big_int_expr() {
check_expression(
"+(9_223_372_036_854_775_808L)",
&expect!["9223372036854775808"],
);
}

#[test]
fn unop_positive_bool_expr() {
check_expression(
"+(false)",
&expect![[r#"
Error {
span: Span {
lo: 1,
hi: 8,
},
kind: Type(
"Int, BigInt, or Double",
"Bool",
),
}
"#]],
);
}

#[test]
fn unop_positive_double_expr() {
check_expression("+(3.4)", &expect!["3.4"]);
}

#[test]
fn unop_positive_int_expr() {
check_expression("+(13)", &expect!["13"]);
}

#[test]
fn if_true_expr() {
check_expression(r#"if true {return "Got Here!";}"#, &expect!["Got Here!"]);
Expand Down
19 changes: 18 additions & 1 deletion compiler/qsc_eval/src/val.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use std::{collections::HashMap, ffi::c_void, fmt::Display};
use std::{collections::HashMap, ffi::c_void, fmt::Display, ops::Neg};

use num_bigint::BigInt;
use qir_backend::Pauli;
Expand Down Expand Up @@ -180,6 +180,23 @@ impl Value {
Value::Udt => "Udt",
}
}

/// Returns the arithmetic negate of this [`Value`].
///
/// # Errors
///
/// This function will return an error if the value is not an `Int`, `BigInt`, or `Double`.
pub fn arithmetic_negate(&self) -> Result<Self, ConversionError> {
match self {
Value::BigInt(v) => Ok(Value::BigInt(v.neg())),
Value::Double(v) => Ok(Value::Double(v.neg())),
Value::Int(v) => Ok(Value::Int(v.wrapping_neg())),
_ => Err(ConversionError {
expected: "Int, BigInt, or Double",
actual: self.type_name(),
}),
}
}
}

fn join<'a>(
Expand Down