Implement is_odd and length functions; Pass all tests
This commit is contained in:
parent
b17da5ad3c
commit
285e9e7217
@ -207,8 +207,36 @@ impl BuiltInFunction {
|
||||
Err(BuiltInFunctionError::WrongNumberOfValueArguments)
|
||||
}
|
||||
}
|
||||
BuiltInFunction::IsOdd => todo!(),
|
||||
BuiltInFunction::Length => todo!(),
|
||||
BuiltInFunction::IsOdd => {
|
||||
if let Some(value_arguments) = value_arguments {
|
||||
if value_arguments.len() == 1 {
|
||||
if let Some(integer) = value_arguments[0].as_integer() {
|
||||
Ok(Value::boolean(integer % 2 != 0))
|
||||
} else {
|
||||
Err(BuiltInFunctionError::ExpectedInteger)
|
||||
}
|
||||
} else {
|
||||
Err(BuiltInFunctionError::WrongNumberOfValueArguments)
|
||||
}
|
||||
} else {
|
||||
Err(BuiltInFunctionError::WrongNumberOfValueArguments)
|
||||
}
|
||||
}
|
||||
BuiltInFunction::Length => {
|
||||
if let Some(value_arguments) = value_arguments {
|
||||
if value_arguments.len() == 1 {
|
||||
if let Some(list) = value_arguments[0].as_list() {
|
||||
Ok(Value::integer(list.len() as i64))
|
||||
} else {
|
||||
Err(BuiltInFunctionError::ExpectedInteger)
|
||||
}
|
||||
} else {
|
||||
Err(BuiltInFunctionError::WrongNumberOfValueArguments)
|
||||
}
|
||||
} else {
|
||||
Err(BuiltInFunctionError::WrongNumberOfValueArguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -283,6 +283,66 @@ impl<'src> Parser<'src> {
|
||||
left_span,
|
||||
))
|
||||
}
|
||||
(Token::IsOdd, left_span) => {
|
||||
self.next_token()?;
|
||||
|
||||
if let (Token::LeftParenthesis, _) = self.current {
|
||||
self.next_token()?;
|
||||
} else {
|
||||
return Err(ParseError::ExpectedOpeningParenthesis {
|
||||
actual: self.current.0.clone(),
|
||||
span: self.current.1,
|
||||
});
|
||||
}
|
||||
|
||||
if let (Token::RightParenthesis, _) = self.current {
|
||||
self.next_token()?;
|
||||
} else {
|
||||
return Err(ParseError::ExpectedClosingParenthesis {
|
||||
actual: self.current.0.clone(),
|
||||
span: self.current.1,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Node::new(
|
||||
Statement::BuiltInFunctionCall {
|
||||
function: BuiltInFunction::IsOdd,
|
||||
type_arguments: None,
|
||||
value_arguments: None,
|
||||
},
|
||||
left_span,
|
||||
))
|
||||
}
|
||||
(Token::Length, left_span) => {
|
||||
self.next_token()?;
|
||||
|
||||
if let (Token::LeftParenthesis, _) = self.current {
|
||||
self.next_token()?;
|
||||
} else {
|
||||
return Err(ParseError::ExpectedOpeningParenthesis {
|
||||
actual: self.current.0.clone(),
|
||||
span: self.current.1,
|
||||
});
|
||||
}
|
||||
|
||||
if let (Token::RightParenthesis, _) = self.current {
|
||||
self.next_token()?;
|
||||
} else {
|
||||
return Err(ParseError::ExpectedClosingParenthesis {
|
||||
actual: self.current.0.clone(),
|
||||
span: self.current.1,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Node::new(
|
||||
Statement::BuiltInFunctionCall {
|
||||
function: BuiltInFunction::Length,
|
||||
type_arguments: None,
|
||||
value_arguments: None,
|
||||
},
|
||||
left_span,
|
||||
))
|
||||
}
|
||||
_ => Err(ParseError::UnexpectedToken(self.current.0.clone())),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user