Skip to content

Instantly share code, notes, and snippets.

@tangentstorm
Created March 24, 2018 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tangentstorm/e69a78361cfff6ac6d5e75c14217a827 to your computer and use it in GitHub Desktop.
Save tangentstorm/e69a78361cfff6ac6d5e75c14217a827 to your computer and use it in GitHub Desktop.
my first rust macro
fn when(&mut self, v:VID, val:NID, nid:NID)->NID {
macro_rules! op {
[not $x:ident] => {{ let x1 = self.when(v, val, $x); self.not(x1) }};
[$f:ident $x:ident $y:ident] => {{
let x1 = self.when(v, val, $x);
let y1 = self.when(v, val, $y);
self.$f(x1,y1) }}}
if v >= self.vars.len() { nid }
else { match self[nid] {
Op::Var(x) if x==v => val,
Op::O | Op::I | Op::Var(_) => nid,
Op::Not(x) => op![not x],
Op::And(x, y) => op![and x y],
Op::Xor(x, y) => op![xor x y],
other => { println!("unhandled match: {:?}", other); nid }}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment