Add and pass block tests
This commit is contained in:
parent
459acb2d63
commit
5cb86b80df
@ -19,14 +19,54 @@ impl<'src> Block<'src> {
|
|||||||
|
|
||||||
impl<'src> AbstractTree for Block<'src> {
|
impl<'src> AbstractTree for Block<'src> {
|
||||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||||
todo!()
|
let final_statement = self.statements.last().unwrap();
|
||||||
|
|
||||||
|
final_statement.expected_type(_context)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
||||||
todo!()
|
for statement in &self.statements {
|
||||||
|
statement.validate(_context)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(self, _: &Context) -> Result<Value, RuntimeError> {
|
fn run(self, _context: &Context) -> Result<Value, RuntimeError> {
|
||||||
todo!()
|
let mut previous = Value::none();
|
||||||
|
|
||||||
|
for statement in self.statements {
|
||||||
|
previous = statement.run(_context)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(previous)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::abstract_tree::{Expression, ValueNode};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_returns_value_of_final_statement() {
|
||||||
|
let block = Block::new(vec![
|
||||||
|
Statement::Expression(Expression::Value(ValueNode::Integer(1))),
|
||||||
|
Statement::Expression(Expression::Value(ValueNode::Integer(2))),
|
||||||
|
Statement::Expression(Expression::Value(ValueNode::Integer(42))),
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert_eq!(block.run(&Context::new()), Ok(Value::integer(42)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn expected_type_returns_type_of_final_statement() {
|
||||||
|
let block = Block::new(vec![
|
||||||
|
Statement::Expression(Expression::Value(ValueNode::String("42"))),
|
||||||
|
Statement::Expression(Expression::Value(ValueNode::Integer(42))),
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert_eq!(block.expected_type(&Context::new()), Ok(Type::Integer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,11 @@ pub enum Expression<'src> {
|
|||||||
|
|
||||||
impl<'src> AbstractTree for Expression<'src> {
|
impl<'src> AbstractTree for Expression<'src> {
|
||||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||||
todo!()
|
match self {
|
||||||
|
Expression::Identifier(identifier) => identifier.expected_type(_context),
|
||||||
|
Expression::Logic(logic) => logic.expected_type(_context),
|
||||||
|
Expression::Value(value_node) => value_node.expected_type(_context),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
||||||
|
@ -15,7 +15,12 @@ pub enum Statement<'src> {
|
|||||||
|
|
||||||
impl<'src> AbstractTree for Statement<'src> {
|
impl<'src> AbstractTree for Statement<'src> {
|
||||||
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||||
todo!()
|
match self {
|
||||||
|
Statement::Assignment(assignment) => assignment.expected_type(_context),
|
||||||
|
Statement::Block(block) => block.expected_type(_context),
|
||||||
|
Statement::Expression(expression) => expression.expected_type(_context),
|
||||||
|
Statement::Loop(r#loop) => r#loop.expected_type(_context),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
||||||
|
Loading…
Reference in New Issue
Block a user