1
0

Continue implementing as expression

This commit is contained in:
Jeff 2024-05-21 19:48:27 -04:00
parent 7b78250eca
commit a0999e30f1

View File

@ -1,4 +1,13 @@
use super::{AbstractNode, Expression, Type, WithPosition}; use std::borrow::Borrow;
use crate::{
context::Context,
error::{RuntimeError, ValidationError},
value::ValueInner,
Value,
};
use super::{AbstractNode, Action, Expression, Type, WithPosition};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct As { pub struct As {
@ -13,26 +22,44 @@ impl As {
} }
impl AbstractNode for As { impl AbstractNode for As {
fn expected_type( fn expected_type(&self, _: &mut Context) -> Result<Type, ValidationError> {
&self, Ok(self.r#type.item.clone())
context: &mut crate::context::Context,
) -> Result<Type, crate::error::ValidationError> {
todo!()
} }
fn validate( fn validate(
&self, &self,
context: &mut crate::context::Context, _context: &mut Context,
manage_memory: bool, _manage_memory: bool,
) -> Result<(), crate::error::ValidationError> { ) -> Result<(), ValidationError> {
todo!() match self.r#type.item {
Type::Boolean | Type::Float | Type::Integer | Type::String => {}
_ => todo!("Create an error for this occurence."),
};
match self.expression.expected_type(_context)? {
Type::Boolean | Type::Float | Type::Integer | Type::String => Ok(()),
_ => todo!("Create an error for this occurence."),
}
} }
fn run( fn run(self, _context: &mut Context, _manage_memory: bool) -> Result<Action, RuntimeError> {
self, let expression_position = self.expression.position();
context: &mut crate::context::Context, let action = self.expression.run(_context, _manage_memory)?;
manage_memory: bool, let value = if let Action::Return(value) = action {
) -> Result<super::Action, crate::error::RuntimeError> { value
todo!() } else {
return Err(RuntimeError::ValidationFailure(
ValidationError::InterpreterExpectedReturn(expression_position),
));
};
let (from_value, to_type): (&ValueInner, Type) = (value.inner().borrow(), self.r#type.item);
let converted = match (from_value, to_type) {
(ValueInner::Boolean(boolean), Type::String) => Value::string(boolean.to_string()),
(ValueInner::Integer(integer), Type::String) => Value::string(integer.to_string()),
_ => todo!("Create an error for this occurence."),
};
Ok(Action::Return(converted))
} }
} }