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

Adding from_designator + notes for usage #222

Closed
wants to merge 6 commits into from
Closed
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
140 changes: 79 additions & 61 deletions crates/oq3_semantics/src/syntax_to_semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ fn from_stmt(stmt: synast::Stmt, context: &mut Context) -> Option<asg::Stmt> {
return Some(asg::DeclareHardwareQubit::new(ast_hardware_qubit(&hw_qubit)).to_stmt());
};
let qubit_type = q_decl.qubit_type().unwrap();
let width = match qubit_type.designator().and_then(|x| x.expr()) {
let width = match from_designator(qubit_type.designator()) {
Some(synast::Expr::Literal(ref literal)) => {
match literal.kind() {
synast::LiteralKind::IntNumber(int_num) => {
Expand Down Expand Up @@ -402,29 +402,19 @@ fn from_stmt(stmt: synast::Stmt, context: &mut Context) -> Option<asg::Stmt> {
}

synast::Stmt::Barrier(barrier) => {
let gate_operands = barrier.qubit_list().map(|operands| {
operands
.gate_operands()
.map(|qubit| from_gate_operand(qubit, context))
.collect()
});
Some(asg::Stmt::Barrier(asg::Barrier::new(gate_operands)))
let gate_operands = from_qubit_list(barrier.qubit_list(), context);
Some(asg::Stmt::Barrier(asg::Barrier::new(Some(gate_operands))))
}

synast::Stmt::DelayStmt(delay_stmt) => {
let gate_operands = delay_stmt.qubit_list().map(|operands| {
operands
.gate_operands()
.map(|qubit| from_gate_operand(qubit, context))
.collect()
});
let gate_operands = from_qubit_list(delay_stmt.qubit_list(), context);
let duration = from_expr(delay_stmt.designator().unwrap().expr(), context).unwrap();
if !matches!(duration.get_type(), Type::Duration(_)) {
context.insert_error(IncompatibleTypesError, &delay_stmt.designator().unwrap());
}
Some(asg::Stmt::Delay(asg::DelayStmt::new(
duration,
gate_operands.unwrap(),
gate_operands,
)))
}

Expand Down Expand Up @@ -553,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 Expand Up @@ -751,15 +757,7 @@ fn from_gate_call_expr(
modifiers: Vec<asg::GateModifier>,
context: &mut Context,
) -> Option<asg::Stmt> {
// Warning, I think map overlooks None. This can cause a bug in the present case.
// Because None means a coding error upstream. Better to blow up here.
let gate_operands: Vec<_> = gate_call_expr
.qubit_list()
.unwrap()
.gate_operands()
.map(|qubit| from_gate_operand(qubit, context))
.collect();

let gate_operands: Vec<_> = from_qubit_list(gate_call_expr.qubit_list(), context);
let param_list = gate_call_expr
.arg_list()
.map(|ex| inner_expression_list(ex.expression_list().unwrap(), context));
Expand Down Expand Up @@ -855,6 +853,19 @@ fn from_expression_list(
asg::ExpressionList::new(inner_expression_list(expression_list, context))
}

fn from_qubit_list(
qubit_list: Option<synast::QubitList>,
context: &mut Context,
) -> Vec<asg::TExpr> {
// Warning, I think map overlooks None. This can cause a bug in the present case.
// Because None means a coding error upstream. Better to blow up here.
qubit_list
.unwrap()
.gate_operands()
.map(|qubit| from_gate_operand(qubit, context))
.collect()
}

// Return a Vec of TExpr.
fn inner_expression_list(
expression_list: synast::ExpressionList,
Expand Down Expand Up @@ -958,7 +969,7 @@ fn from_scalar_type(
// not complex
scalar_type.designator()
};
let width = match designator.and_then(|desg| desg.expr()) {
let width = match from_designator(designator) {
// We only support literal integer designators at the moment.
Some(synast::Expr::Literal(ref literal)) => {
match literal.kind() {
Expand Down Expand Up @@ -1232,6 +1243,13 @@ fn bind_typed_parameter_list(
})
}

fn from_designator(arg: Option<synast::Designator>) -> Option<synast::Expr> {
arg.and_then(|desg| desg.expr())
// NOTE: Other uses of this pattern above have certain error checking,
// but because it was not standardized, it is not included
// in this helper function
}

// This works, but using it is pretty clumsy.
// fn with_scope<F>(context: &mut Context, scope: ScopeType, func: F) where
// F: FnOnce(&mut 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
Loading