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

fix: Encode opaque symbolic constants #273

Merged
merged 1 commit into from
Dec 15, 2023
Merged
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
31 changes: 18 additions & 13 deletions tket2/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,30 @@ pub fn symbolic_constant_op(s: &str) -> OpType {
}

/// match against a symbolic constant
pub(crate) fn match_symb_const_op(op: &OpType) -> Option<&str> {
pub(crate) fn match_symb_const_op(op: &OpType) -> Option<String> {
// Extract the symbol for a symbolic operation node.
let symbol_from_typeargs = |args: &[TypeArg]| -> String {
args.first()
.and_then(|arg| match arg {
TypeArg::Opaque { arg } => match &arg.value {
serde_yaml::Value::String(s) => Some(s.clone()),
_ => None,
},
_ => None,
})
.unwrap_or_else(|| panic!("Found an invalid type arg in a symbolic operation node."))
};

if let OpType::LeafOp(LeafOp::CustomOp(e)) = op {
match e.as_ref() {
ExternalOp::Extension(e)
if e.def().name() == &SYM_OP_ID && e.def().extension() == &EXTENSION_ID =>
{
// TODO also check extension name

let Some(TypeArg::Opaque { arg }) = e.args().first() else {
panic!("should be an opaque type arg.")
};

let serde_yaml::Value::String(s) = &arg.value else {
panic!("unexpected yaml value.")
};

Some(s)
Some(symbol_from_typeargs(e.args()))
}
ExternalOp::Opaque(e) if e.name() == &SYM_OP_ID && e.extension() == &EXTENSION_ID => {
Some(symbol_from_typeargs(e.args()))
}
ExternalOp::Opaque(_) => todo!(),
_ => None,
}
} else {
Expand Down