Pass all tests

This commit is contained in:
Jeff 2024-06-24 09:09:38 -04:00
parent 2e9a523058
commit 63f648c3ac
5 changed files with 51 additions and 21 deletions

View File

@ -155,7 +155,24 @@ impl FunctionLogic for WriteLine {
} }
fn call(context: &Context, manage_memory: bool) -> Result<Option<Value>, RuntimeError> { fn call(context: &Context, manage_memory: bool) -> Result<Option<Value>, RuntimeError> {
todo!() let value = if let Some(value) = context.get_value(&Identifier::new("output"))? {
value
} else {
return Err(RuntimeError::ValidationFailure(
ValidationError::BuiltInFunctionFailure("output does not exist"),
));
};
let input = if let ValueInner::String(string) = value.inner().as_ref() {
string
} else {
return Err(RuntimeError::ValidationFailure(
ValidationError::BuiltInFunctionFailure("output is not a string"),
));
};
println!("{input}");
Ok(None)
} }
} }

View File

@ -104,6 +104,16 @@ impl AbstractNode for FunctionCall {
context: &Context, context: &Context,
manage_memory: bool, manage_memory: bool,
) -> Result<Option<Evaluation>, RuntimeError> { ) -> Result<Option<Evaluation>, RuntimeError> {
if let Expression::Value(WithPosition {
node: ValueNode::BuiltInFunction(function),
..
}) = *self.function_expression
{
return function
.call(context, manage_memory)
.map(|value_option| value_option.map(|value| Evaluation::Return(value)));
}
let function_position = self.function_expression.position(); let function_position = self.function_expression.position();
let evaluation = self.function_expression.evaluate(context, manage_memory)?; let evaluation = self.function_expression.evaluate(context, manage_memory)?;
let value = if let Some(Evaluation::Return(value)) = evaluation { let value = if let Some(Evaluation::Return(value)) = evaluation {

View File

@ -173,8 +173,6 @@ impl AbstractNode for ValueNode {
{ {
body.node.validate(&context_template, _manage_memory)?; body.node.validate(&context_template, _manage_memory)?;
println!("beep");
let ((expected_return, expected_position), actual_return) = let ((expected_return, expected_position), actual_return) =
match (return_type, body.node.expected_type(&context_template)?) { match (return_type, body.node.expected_type(&context_template)?) {
(Some(constructor), Some(r#type)) => ( (Some(constructor), Some(r#type)) => (
@ -373,11 +371,7 @@ impl AbstractNode for ValueNode {
Value::structure(name, fields) Value::structure(name, fields)
} }
ValueNode::BuiltInFunction(built_in_function) => { ValueNode::BuiltInFunction(function) => Value::built_in_function(function),
return built_in_function
.call(context, manage_memory)
.map(|value_option| value_option.map(|value| Evaluation::Return(value)));
}
}; };
Ok(Some(Evaluation::Return(value))) Ok(Some(Evaluation::Return(value)))

View File

@ -13,7 +13,7 @@ use std::{
}; };
use abstract_tree::{AbstractTree, Type}; use abstract_tree::{AbstractTree, Type};
use ariadne::{Color, Config, Fmt, Label, Report, ReportKind}; use ariadne::{Color, Fmt, Label, Report, ReportKind};
use chumsky::prelude::*; use chumsky::prelude::*;
use context::Context; use context::Context;
use error::{DustError, RuntimeError, TypeConflict, ValidationError}; use error::{DustError, RuntimeError, TypeConflict, ValidationError};
@ -307,13 +307,12 @@ impl InterpreterError {
.with_label( .with_label(
Label::new((self.source_id.clone(), position.0..position.1)).with_message("Error occured here.") Label::new((self.source_id.clone(), position.0..position.1)).with_message("Error occured here.")
), ),
if let RuntimeError::ValidationFailure(validation_error) = error {
if let RuntimeError::ValidationFailure(validation_error) = error { Some(validation_error)
Some(validation_error) } else {
} else { None
None },
}, )
)
} }
}; };
@ -432,7 +431,14 @@ impl InterpreterError {
Label::new((self.source_id.clone(), position.0..position.1)) Label::new((self.source_id.clone(), position.0..position.1))
.with_message("Expected a statement that ends in an expression."), .with_message("Expected a statement that ends in an expression."),
), ),
ValidationError::ExpectedFunction { .. } => todo!(), ValidationError::ExpectedFunction { actual, position } => builder.add_label(
Label::new((self.source_id.clone(), position.0..position.1)).with_message(
format!(
"Expected a function value but got {}.",
actual.fg(type_color)
),
),
),
ValidationError::ExpectedValue(_) => todo!(), ValidationError::ExpectedValue(_) => todo!(),
ValidationError::FieldNotFound { ValidationError::FieldNotFound {
identifier, identifier,
@ -481,12 +487,11 @@ impl InterpreterError {
} }
ValidationError::EnumVariantNotFound { .. } => todo!(), ValidationError::EnumVariantNotFound { .. } => todo!(),
ValidationError::ExpectedList { .. } => todo!(), ValidationError::ExpectedList { .. } => todo!(),
ValidationError::BuiltInFunctionFailure(_) => todo!(), ValidationError::BuiltInFunctionFailure(reason) => builder
.add_label(Label::new((self.source_id.clone(), 0..0)).with_message(reason)),
} }
} }
builder = builder.with_config(Config::default().with_multiline_arrows(false));
let report = builder.finish(); let report = builder.finish();
reports.push(report); reports.push(report);

View File

@ -32,6 +32,10 @@ impl Value {
Value(Arc::new(ValueInner::Boolean(boolean))) Value(Arc::new(ValueInner::Boolean(boolean)))
} }
pub fn built_in_function(function: BuiltInFunction) -> Self {
Value(Arc::new(ValueInner::BuiltInFunction(function)))
}
pub fn enum_instance( pub fn enum_instance(
type_name: Identifier, type_name: Identifier,
variant: Identifier, variant: Identifier,
@ -631,7 +635,7 @@ impl ValueInner {
}); });
} }
} }
ValueInner::BuiltInFunction(_) => todo!(), ValueInner::BuiltInFunction(function) => function.r#type(),
}; };
Ok(r#type) Ok(r#type)