Skip to content

Commit

Permalink
Merge branch 'main' into from_qubit_list
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre committed May 31, 2024
2 parents c8727f7 + 5d521be commit 51c6d64
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 41 deletions.
88 changes: 52 additions & 36 deletions crates/oq3_semantics/src/syntax_to_semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,54 +543,70 @@ fn from_paren_expr(paren_expr: synast::ParenExpr, context: &mut Context) -> Opti
from_expr(paren_expr.expr(), context)
}

fn negative_float(f: synast::FloatNumber) -> asg::FloatLiteral {
let num = f.value().unwrap();
let float = format!("-{num}");
asg::FloatLiteral::new(float)
}

fn negative_int(n: synast::IntNumber) -> asg::IntLiteral {
let num = n.value_u128().unwrap(); // fn value_u128 is kind of a hack
asg::IntLiteral::new(num, false) // `false` means negative
}

fn from_expr(expr_maybe: Option<synast::Expr>, context: &mut Context) -> Option<asg::TExpr> {
let expr = expr_maybe?;
match expr {
// FIXME: Ugh. could clean up logic here
// It is convenient to rewrite literals wrapped in unary minus as literals
// with negative values.
synast::Expr::PrefixExpr(prefix_expr) => {
match prefix_expr.op_kind() {
Some(synast::UnaryOp::Neg) => {
match prefix_expr.expr() {
Some(synast::Expr::Literal(ref literal)) => {
match literal.kind() {
synast::LiteralKind::FloatNumber(float_num) => {
let num = float_num.value().unwrap();
let float = format!("-{num}");
Some(asg::FloatLiteral::new(float).to_texpr())
synast::Expr::PrefixExpr(prefix_expr) => match prefix_expr.op_kind() {
Some(synast::UnaryOp::Neg) => match prefix_expr.expr() {
Some(synast::Expr::Literal(ref literal)) => Some(match literal.kind() {
synast::LiteralKind::FloatNumber(f) => negative_float(f).to_texpr(),
synast::LiteralKind::IntNumber(n) => negative_int(n).to_texpr(),
_ => {
panic!("Only integers and floats are supported as operands to unary minus.")
}
}),

Some(synast::Expr::TimingLiteral(ref timing_literal)) => {
match timing_literal.time_unit().unwrap() {
synast::TimeUnit::Imaginary => {
Some(match timing_literal.literal().unwrap().kind() {
synast::LiteralKind::FloatNumber(f) => {
negative_float(f).to_imaginary_texpr()
}
synast::LiteralKind::IntNumber(int_num) => {
let num = int_num.value_u128().unwrap(); // fn value_u128 is kind of a hack
Some(asg::IntLiteral::new(num, false).to_texpr())
// `false` means negative
synast::LiteralKind::IntNumber(n) => {
negative_int(n).to_imaginary_texpr()
}
_ => panic!("Only integers and floats are supported as operands to unary minus."),
}
_ => panic!("You have found a bug in oq3_syntax or oq3_parser"),
})
}
_ => {
panic!("Only floats are supported as operands to unary minus.")
}

Some(synexpr) => Some(
asg::UnaryExpr::new(
asg::UnaryOp::Minus,
from_expr(Some(synexpr), context).unwrap(),
)
.to_texpr(),
),

None => panic!(
"You have found a bug in oq3_parser. No operand to unary minus found."
),
}
}
Some(op) => panic!(
"Unary operators other than minus are not supported. Found '{:?}.'",
op
),
_ => panic!(
"You have found a bug in oq3_parser. No operand to unary operator found."

Some(synexpr) => Some(
asg::UnaryExpr::new(
asg::UnaryOp::Minus,
from_expr(Some(synexpr), context).unwrap(),
)
.to_texpr(),
),
}
}

None => {
panic!("You have found a bug in oq3_parser. No operand to unary minus found.")
}
},
Some(op) => panic!(
"Unary operators other than minus are not supported. Found '{:?}.'",
op
),
_ => panic!("You have found a bug in oq3_parser. No operand to unary operator found."),
},

synast::Expr::ParenExpr(paren_expr) => from_paren_expr(paren_expr, context),

Expand Down
30 changes: 28 additions & 2 deletions crates/oq3_semantics/tests/from_string_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,10 @@ fn expr_from_expr_stmt(stmt: &asg::Stmt) -> asg::Expr {
fn literal_value(stmt: &asg::Stmt) -> Option<String> {
match expr_from_expr_stmt(stmt) {
asg::Expr::Literal(lit) => match lit {
asg::Literal::Float(float) => Some(float.value().to_string()),
asg::Literal::Int(int) => {
asg::Literal::Float(float) | asg::Literal::ImaginaryFloat(float) => {
Some(float.value().to_string())
}
asg::Literal::Int(int) | asg::Literal::ImaginaryInt(int) => {
if *int.sign() {
Some(format!("{}", int.value()))
} else {
Expand Down Expand Up @@ -471,6 +473,30 @@ fn test_from_string_neg_lit_int() {
assert_eq!(expr, "-123");
}

#[test]
fn test_from_string_neg_lit_int_im() {
let code = r##"
-1 im;
"##;
let (program, errors, _symbol_table) = parse_string(code);
assert!(errors.is_empty());
assert_eq!(program.len(), 1);
let expr = literal_value(&program[0]).unwrap();
assert_eq!(expr, "-1");
}

#[test]
fn test_from_string_neg_lit_float_im() {
let code = r##"
-10.1 im;
"##;
let (program, errors, _symbol_table) = parse_string(code);
assert!(errors.is_empty());
assert_eq!(program.len(), 1);
let expr = literal_value(&program[0]).unwrap();
assert_eq!(expr, "-10.1");
}

#[test]
fn test_from_string_neg_spc_lit_int() {
let code = r##"
Expand Down
2 changes: 1 addition & 1 deletion crates/oq3_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ rowan = "0.15.11"
rustc-hash = "1.1.0"
smol_str = "0.2.0"
stdx = { version = "0.0.188", package = "ra_ap_stdx"}
triomphe = { version = "0.1.8", default-features = false, features = ["std"] }
triomphe = { version = "<= 0.1.11", default-features = false, features = ["std"] }
xshell = "0.2.2"
rustversion = "1.0"
# local crates
Expand Down
4 changes: 2 additions & 2 deletions crates/oq3_syntax/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<N: AstNode> Clone for AstPtr<N> {
#[rustversion::before(1.74)]
fn clone(&self) -> AstPtr<N> {
AstPtr {
raw: self.raw.clone(),
raw: self.raw,
_ty: PhantomData,
}
}
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<N: AstNode> AstPtr<N> {

#[rustversion::before(1.74)]
pub fn syntax_node_ptr(&self) -> SyntaxNodePtr {
self.raw.clone()
self.raw
}

pub fn text_range(&self) -> TextRange {
Expand Down

0 comments on commit 51c6d64

Please sign in to comment.