rustfmt
This commit is contained in:
parent
bb74bee382
commit
406bfe0e05
@ -60,7 +60,7 @@ impl Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn unmatched_partial_token(first: PartialToken, second: Option<PartialToken>) -> Self {
|
pub fn unmatched_partial_token(first: PartialToken, second: Option<PartialToken>) -> Self {
|
||||||
Error::UnmatchedPartialToken {first, second}
|
Error::UnmatchedPartialToken { first, second }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,4 +84,4 @@ pub fn expect_boolean(actual: &Value) -> Result<bool, Error> {
|
|||||||
Value::Boolean(boolean) => Ok(*boolean),
|
Value::Boolean(boolean) => Ok(*boolean),
|
||||||
_ => Err(Error::expected_boolean(actual.clone())),
|
_ => Err(Error::expected_boolean(actual.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,10 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_boolean_examples() {
|
fn test_boolean_examples() {
|
||||||
assert_eq!(eval("true && false"), Ok(Value::Boolean(false)));
|
assert_eq!(eval("true && false"), Ok(Value::Boolean(false)));
|
||||||
assert_eq!(eval("true && false || true && true"), Ok(Value::Boolean(true)));
|
assert_eq!(
|
||||||
|
eval("true && false || true && true"),
|
||||||
|
Ok(Value::Boolean(true))
|
||||||
|
);
|
||||||
assert_eq!(eval("5 > 4 && 1 <= 1"), Ok(Value::Boolean(true)));
|
assert_eq!(eval("5 > 4 && 1 <= 1"), Ok(Value::Boolean(true)));
|
||||||
assert_eq!(eval("5.0 <= 4.9 || !(4 > 3.5)"), Ok(Value::Boolean(false)));
|
assert_eq!(eval("5.0 <= 4.9 || !(4 > 3.5)"), Ok(Value::Boolean(false)));
|
||||||
}
|
}
|
||||||
|
@ -400,11 +400,11 @@ impl Operator for And {
|
|||||||
let a = expect_boolean(&arguments[0])?;
|
let a = expect_boolean(&arguments[0])?;
|
||||||
let b = expect_boolean(&arguments[1])?;
|
let b = expect_boolean(&arguments[1])?;
|
||||||
|
|
||||||
if a && b {
|
if a && b {
|
||||||
Ok(Value::Boolean(true))
|
Ok(Value::Boolean(true))
|
||||||
} else {
|
} else {
|
||||||
Ok(Value::Boolean(false))
|
Ok(Value::Boolean(false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use value::{FloatType, IntType};
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
use value::{FloatType, IntType};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
pub enum Token {
|
pub enum Token {
|
||||||
@ -139,7 +139,10 @@ fn resolve_literals(mut tokens: &[PartialToken]) -> Result<Vec<Token>, Error> {
|
|||||||
let mut cutoff = 2;
|
let mut cutoff = 2;
|
||||||
|
|
||||||
result.push(match first {
|
result.push(match first {
|
||||||
PartialToken::Token(token) => {cutoff = 1; token},
|
PartialToken::Token(token) => {
|
||||||
|
cutoff = 1;
|
||||||
|
token
|
||||||
|
}
|
||||||
PartialToken::Literal(literal) => {
|
PartialToken::Literal(literal) => {
|
||||||
cutoff = 1;
|
cutoff = 1;
|
||||||
if let Ok(number) = literal.parse::<IntType>() {
|
if let Ok(number) = literal.parse::<IntType>() {
|
||||||
@ -151,42 +154,39 @@ fn resolve_literals(mut tokens: &[PartialToken]) -> Result<Vec<Token>, Error> {
|
|||||||
} else {
|
} else {
|
||||||
Token::Identifier(literal.to_string())
|
Token::Identifier(literal.to_string())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
PartialToken::Eq => match second {
|
||||||
|
Some(PartialToken::Eq) => Token::Eq,
|
||||||
|
_ => return Err(Error::unmatched_partial_token(first, second)),
|
||||||
},
|
},
|
||||||
PartialToken::Eq => {
|
PartialToken::ExclamationMark => match second {
|
||||||
match second {
|
Some(PartialToken::Eq) => Token::Eq,
|
||||||
Some(PartialToken::Eq) => Token::Eq,
|
_ => {
|
||||||
_ => return Err(Error::unmatched_partial_token(first, second)),
|
cutoff = 1;
|
||||||
|
Token::Not
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
PartialToken::ExclamationMark => {
|
PartialToken::Gt => match second {
|
||||||
match second {
|
Some(PartialToken::Eq) => Token::Geq,
|
||||||
Some(PartialToken::Eq) => Token::Eq,
|
_ => {
|
||||||
_ => {cutoff = 1; Token::Not},
|
cutoff = 1;
|
||||||
|
Token::Gt
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
PartialToken::Gt => {
|
PartialToken::Lt => match second {
|
||||||
match second {
|
Some(PartialToken::Eq) => Token::Leq,
|
||||||
Some(PartialToken::Eq) => Token::Geq,
|
_ => {
|
||||||
_ => {cutoff = 1; Token::Gt},
|
cutoff = 1;
|
||||||
|
Token::Lt
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
PartialToken::Lt => {
|
PartialToken::Ampersand => match second {
|
||||||
match second {
|
Some(PartialToken::Ampersand) => Token::And,
|
||||||
Some(PartialToken::Eq) => Token::Leq,
|
_ => return Err(Error::unmatched_partial_token(first, second)),
|
||||||
_ => {cutoff = 1; Token::Lt},
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
PartialToken::Ampersand => {
|
PartialToken::VerticalBar => match second {
|
||||||
match second {
|
Some(PartialToken::VerticalBar) => Token::Or,
|
||||||
Some(PartialToken::Ampersand) => Token::And,
|
_ => return Err(Error::unmatched_partial_token(first, second)),
|
||||||
_ => return Err(Error::unmatched_partial_token(first, second)),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
PartialToken::VerticalBar => {
|
|
||||||
match second {
|
|
||||||
Some(PartialToken::VerticalBar) => Token::Or,
|
|
||||||
_ => return Err(Error::unmatched_partial_token(first, second)),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -43,7 +43,8 @@ impl Node {
|
|||||||
if self.operator().is_leaf() {
|
if self.operator().is_leaf() {
|
||||||
Err(Error::AppendedToLeafNode)
|
Err(Error::AppendedToLeafNode)
|
||||||
} else if self.has_correct_amount_of_children() {
|
} else if self.has_correct_amount_of_children() {
|
||||||
if self.children.last_mut().unwrap().operator().precedence() < node.operator().precedence()
|
if self.children.last_mut().unwrap().operator().precedence()
|
||||||
|
< node.operator().precedence()
|
||||||
{
|
{
|
||||||
self.children
|
self.children
|
||||||
.last_mut()
|
.last_mut()
|
||||||
@ -98,11 +99,11 @@ pub fn tokens_to_operator_tree(tokens: Vec<Token>) -> Result<Node, Error> {
|
|||||||
Token::And => Some(Node::new(And)),
|
Token::And => Some(Node::new(And)),
|
||||||
Token::Or => Some(Node::new(Or)),
|
Token::Or => Some(Node::new(Or)),
|
||||||
Token::Not => Some(Node::new(Not)),
|
Token::Not => Some(Node::new(Not)),
|
||||||
|
|
||||||
Token::LBrace => {
|
Token::LBrace => {
|
||||||
root.push(Node::root_node());
|
root.push(Node::root_node());
|
||||||
None
|
None
|
||||||
},
|
}
|
||||||
Token::RBrace => {
|
Token::RBrace => {
|
||||||
if root.len() < 2 {
|
if root.len() < 2 {
|
||||||
return Err(Error::UnmatchedRBrace);
|
return Err(Error::UnmatchedRBrace);
|
||||||
@ -111,7 +112,7 @@ pub fn tokens_to_operator_tree(tokens: Vec<Token>) -> Result<Node, Error> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Token::Whitespace => None,
|
Token::Whitespace => None,
|
||||||
|
|
||||||
Token::Identifier(identifier) => Some(Node::new(Identifier::new(identifier))),
|
Token::Identifier(identifier) => Some(Node::new(Identifier::new(identifier))),
|
||||||
Token::Float(number) => Some(Node::new(Const::new(Value::Float(number)))),
|
Token::Float(number) => Some(Node::new(Const::new(Value::Float(number)))),
|
||||||
Token::Int(number) => Some(Node::new(Const::new(Value::Int(number)))),
|
Token::Int(number) => Some(Node::new(Const::new(Value::Int(number)))),
|
||||||
|
Loading…
Reference in New Issue
Block a user