Skip to content

Instantly share code, notes, and snippets.

@saethlin
Last active September 15, 2017 17:45
Show Gist options
  • Select an option

  • Save saethlin/b1fb9b9eee7c1b55258381ad9a2fa5e2 to your computer and use it in GitHub Desktop.

Select an option

Save saethlin/b1fb9b9eee7c1b55258381ad9a2fa5e2 to your computer and use it in GitHub Desktop.
Matching on box attempt
enum ExprNode {
Number(f64),
Variable(CString),
BinaryOperation(char, Box<ExprNode>, Box<ExprNode>),
FunctionCall(CString, Vec<Box<ExprNode>>),
IfElse(Box<ExprNode>, Box<ExprNode>, Box<ExprNode>),
ForLoop(Box<ExprNode>, Box<ExprNode>, Option<Box<ExprNode>>, Box<ExprNode>),
}
if let (ExprNode::Number(l), ExprNode::Number(r)) = (*lhs, *rhs) {
let value = match binop {
'+' => l + r,
'-' => l - r,
'*' => l * r,
'<' => {
match (l < r) {
true => 1.0,
false => 0.0,
}
}
_ => unreachable!("Unimplemented binary operation"),
};
lhs = Box::new(ExprNode::Number(value))
} else {
lhs = Box::new(ExprNode::BinaryOperation(binop, lhs, rhs));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment