1
0

Pass assignment test

This commit is contained in:
Jeff 2024-03-01 19:15:03 -05:00
parent 0ed30c7220
commit 459acb2d63
5 changed files with 41 additions and 20 deletions

View File

@ -32,35 +32,36 @@ impl<'src> AbstractTree for Assignment<'src> {
todo!()
}
fn run(self, _context: &Context) -> Result<Value, RuntimeError> {
todo!()
// let value = self.statement.run(context)?;
fn run(self, context: &Context) -> Result<Value, RuntimeError> {
let value = self.statement.run(context)?;
// context.set(self.identifier, value)?;
context.set(self.identifier, value)?;
// Ok(Value::none())
Ok(Value::none())
}
}
#[cfg(test)]
mod tests {
// use super::*;
use crate::abstract_tree::{Expression, ValueNode};
use super::*;
#[test]
fn assign_value() {
todo!()
// let context = Context::new();
let context = Context::new();
// Assignment::new(
// Identifier::new("foobar"),
// Statement::Value(Value::integer(42)),
// )
// .run(&context)
// .unwrap();
Assignment::new(
Identifier::new("foobar"),
None,
Statement::Expression(Expression::Value(ValueNode::Integer(42))),
)
.run(&context)
.unwrap();
// assert_eq!(
// context.get(&Identifier::new("foobar")).unwrap(),
// Some(Value::integer(42))
// )
assert_eq!(
context.get(&Identifier::new("foobar")),
Ok(Some(Value::integer(42)))
)
}
}

View File

@ -15,6 +15,10 @@ impl Identifier {
pub fn new<T: ToString>(string: T) -> Self {
Identifier(Arc::new(string.to_string()))
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl AbstractTree for Identifier {

View File

@ -26,7 +26,7 @@ impl<'src> AbstractTree for Statement<'src> {
match self {
Statement::Assignment(assignment) => assignment.run(_context),
Statement::Block(_) => todo!(),
Statement::Expression(_) => todo!(),
Statement::Expression(expression) => expression.run(_context),
Statement::Loop(_) => todo!(),
}
}

View File

@ -69,7 +69,13 @@ impl<'src> AbstractTree for ValueNode<'src> {
}
ValueNode::Range(range) => Value::range(range),
ValueNode::String(string) => Value::string(string),
ValueNode::Enum(name, variant) => Value::r#enum(name, variant),
ValueNode::Enum(name, variant) => {
if name.as_str() == "Option" && variant.as_str() == "None" {
Value::none()
} else {
Value::r#enum(name, variant)
}
}
};
Ok(value)

View File

@ -17,6 +17,16 @@ impl Value {
&self.0
}
pub fn none() -> Self {
NONE.get_or_init(|| {
Value(Arc::new(ValueInner::Enum(
Identifier::new("Option"),
Identifier::new("None"),
)))
})
.clone()
}
pub fn boolean(boolean: bool) -> Self {
Value(Arc::new(ValueInner::Boolean(boolean)))
}