1
0

Implement is_odd and length functions; Pass all tests

This commit is contained in:
Jeff 2024-08-07 18:43:24 -04:00
parent b17da5ad3c
commit 285e9e7217
2 changed files with 90 additions and 2 deletions

View File

@ -207,8 +207,36 @@ impl BuiltInFunction {
Err(BuiltInFunctionError::WrongNumberOfValueArguments) Err(BuiltInFunctionError::WrongNumberOfValueArguments)
} }
} }
BuiltInFunction::IsOdd => todo!(), BuiltInFunction::IsOdd => {
BuiltInFunction::Length => todo!(), 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)
}
}
} }
} }

View File

@ -283,6 +283,66 @@ impl<'src> Parser<'src> {
left_span, 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())), _ => Err(ParseError::UnexpectedToken(self.current.0.clone())),
} }
} }