Last active
September 15, 2017 17:45
-
-
Save saethlin/b1fb9b9eee7c1b55258381ad9a2fa5e2 to your computer and use it in GitHub Desktop.
Matching on box attempt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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