From 4726288b9a26c23a6ea99d4162833ac0b2ab0356 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 21 Apr 2024 18:22:59 -0400 Subject: [PATCH] Continue built-in function revision --- .../abstract_tree/built_in_function_call.rs | 29 ++++++++++++++----- dust-lang/src/abstract_tree/mod.rs | 1 + dust-lang/src/value.rs | 9 ------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/dust-lang/src/abstract_tree/built_in_function_call.rs b/dust-lang/src/abstract_tree/built_in_function_call.rs index 79a09d9..882240b 100644 --- a/dust-lang/src/abstract_tree/built_in_function_call.rs +++ b/dust-lang/src/abstract_tree/built_in_function_call.rs @@ -40,8 +40,8 @@ impl AbstractNode for BuiltInFunctionCall { Ok(Action::Return(Value::string(buffer))) } BuiltInFunctionCall::Sleep(expression) => { - let expression_run = expression.clone().run(context)?; - let expression_value = if let Action::Return(value) = expression_run { + let action = expression.clone().run(context)?; + let value = if let Action::Return(value) = action { value } else { return Err(RuntimeError::ValidationFailure( @@ -49,15 +49,28 @@ impl AbstractNode for BuiltInFunctionCall { )); }; - if let ValueInner::Integer(milliseconds) = expression_value.inner().as_ref() { + if let ValueInner::Integer(milliseconds) = value.inner().as_ref() { thread::sleep(Duration::from_millis(*milliseconds as u64)); - - Ok(Action::None) - } else { - panic!("Expected an integer."); } + + Ok(Action::None) + } + BuiltInFunctionCall::WriteLine(expression) => { + let action = expression.clone().run(context)?; + let value = if let Action::Return(value) = action { + value + } else { + return Err(RuntimeError::ValidationFailure( + ValidationError::InterpreterExpectedReturn(expression.position()), + )); + }; + + if let ValueInner::String(output) = value.inner().as_ref() { + println!("{output}"); + } + + Ok(Action::None) } - BuiltInFunctionCall::WriteLine(_) => todo!(), } } } diff --git a/dust-lang/src/abstract_tree/mod.rs b/dust-lang/src/abstract_tree/mod.rs index 8c78271..9579325 100644 --- a/dust-lang/src/abstract_tree/mod.rs +++ b/dust-lang/src/abstract_tree/mod.rs @@ -109,6 +109,7 @@ impl AbstractTree { match run { Ok(action) => match action { Action::Return(value) => previous_value = Some(value), + Action::None => previous_value = None, _ => {} }, Err(runtime_error) => { diff --git a/dust-lang/src/value.rs b/dust-lang/src/value.rs index 4df9962..dd3ce88 100644 --- a/dust-lang/src/value.rs +++ b/dust-lang/src/value.rs @@ -160,9 +160,6 @@ impl Display for Value { write!(f, "): {} {:?}", return_type.node, body.node) } - ValueInner::Function(function) => { - write!(f, "{function}") - } ValueInner::Structure { name, fields } => { let mut table = create_table(); @@ -329,9 +326,3 @@ impl Function { self.body.node.run(&context) } } - -impl Display for Function { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - todo!() - } -}