diff --git a/examples/custom_functions.rs b/examples/custom_functions.rs index 3254309..ec66fd1 100644 --- a/examples/custom_functions.rs +++ b/examples/custom_functions.rs @@ -62,7 +62,7 @@ fn increment_number(number: Spanned>) -> Result<(), RunError> { } } -// For more example take a look at the standard library. +// For more examples take a look at the standard library. // Register our functions by creating and inserting our own environment fn custom_environment() -> Environment { diff --git a/src/builtin_parser/runner.rs b/src/builtin_parser/runner.rs index 5d4e28e..20ad11f 100644 --- a/src/builtin_parser/runner.rs +++ b/src/builtin_parser/runner.rs @@ -14,7 +14,7 @@ use self::{ use super::{ parser::{Ast, Expression, Operator}, - Number, Spanned, + Number, Spanned, SpanExtension, }; use bevy::{ prelude::*, @@ -482,7 +482,7 @@ fn eval_path( value: Path::Resource(resource), }) } - Path::NewVariable(_) => Err(RunError::VariableNotFound(left.span)), + Path::NewVariable(name) => Err(RunError::VariableNotFound(left.span.wrap(name))), } } _ => todo!(), diff --git a/src/builtin_parser/runner/environment.rs b/src/builtin_parser/runner/environment.rs index 1a396b8..d3b1d7e 100644 --- a/src/builtin_parser/runner/environment.rs +++ b/src/builtin_parser/runner/environment.rs @@ -5,6 +5,8 @@ use std::collections::HashMap; use bevy::{ecs::world::World, log::warn, reflect::TypeRegistration}; use logos::Span; +use crate::builtin_parser::SpanExtension; + use super::{ super::{parser::Expression, Spanned}, error::RunError, @@ -258,9 +260,9 @@ impl Environment { match env.variables.get(name) { Some(Variable::Unmoved(value)) => Ok(value), - Some(Variable::Moved) => Err(RunError::VariableMoved(span)), + Some(Variable::Moved) => Err(RunError::VariableMoved(span.wrap(name.to_string()))), Some(Variable::Function(_)) => todo!(), - None => Err(RunError::VariableNotFound(span)), + None => Err(RunError::VariableNotFound(span.wrap(name.to_string()))), } } @@ -272,7 +274,7 @@ impl Environment { let (env, span) = self.resolve_mut(name, span)?; match env.variables.get_mut(name) { - Some(Variable::Moved) => Err(RunError::VariableMoved(span)), + Some(Variable::Moved) => Err(RunError::VariableMoved(span.wrap(name.to_string()))), Some(Variable::Function(_)) => todo!(), Some(variable_reference) => { let Variable::Unmoved(reference) = variable_reference else { @@ -292,7 +294,7 @@ impl Environment { }; Ok(value.into_inner()) } - None => Err(RunError::VariableNotFound(span)), + None => Err(RunError::VariableNotFound(span.wrap(name.to_string()))), } } @@ -303,7 +305,7 @@ impl Environment { match &self.parent { Some(parent) => parent.resolve(name, span), - None => Err(RunError::VariableNotFound(span)), + None => Err(RunError::VariableNotFound(span.wrap(name.to_string()))), } } fn resolve_mut(&mut self, name: &str, span: Span) -> Result<(&mut Self, Span), RunError> { @@ -313,7 +315,7 @@ impl Environment { match &mut self.parent { Some(parent) => parent.resolve_mut(name, span), - None => Err(RunError::VariableNotFound(span)), + None => Err(RunError::VariableNotFound(span.wrap(name.to_string()))), } } diff --git a/src/builtin_parser/runner/error.rs b/src/builtin_parser/runner/error.rs index 9ecac18..2c4d5cc 100644 --- a/src/builtin_parser/runner/error.rs +++ b/src/builtin_parser/runner/error.rs @@ -19,14 +19,14 @@ pub enum RunError { text: String, span: Span, }, - VariableNotFound(Span), + VariableNotFound(Spanned), ExpectedNumberAfterUnaryOperator(Spanned), InvalidVariantForResource(String, String), CannotIndexValue(Span), FieldNotFoundInStruct(Span), CouldntDereferenceValue(Span), ReferenceToMovedData(Span), - VariableMoved(Span), + VariableMoved(Spanned), CannotBorrowValue(Span), IncompatibleReflectTypes { expected: String, @@ -58,14 +58,14 @@ impl RunError { match self { Custom { span, .. } => vec![span.clone()], - VariableNotFound(span) => vec![span.clone()], + VariableNotFound(Spanned { span, .. }) => vec![span.clone()], ExpectedNumberAfterUnaryOperator(Spanned { span, .. }) => vec![span.clone()], InvalidVariantForResource(_, _) => todo!(), CannotIndexValue(span) => vec![span.clone()], FieldNotFoundInStruct(span) => vec![span.clone()], CouldntDereferenceValue(span) => vec![span.clone()], ReferenceToMovedData(span) => vec![span.clone()], - VariableMoved(span) => vec![span.clone()], + VariableMoved(Spanned { span, .. }) => vec![span.clone()], CannotBorrowValue(span) => vec![span.clone()], IncompatibleReflectTypes { span, .. } => vec![span.clone()], EnumVariantNotFound { span, .. } => vec![span.clone()], @@ -86,7 +86,9 @@ impl RunError { match self { Custom { text, .. } => text.clone().into(), - VariableNotFound(_) => "Variable not found.".into(), + VariableNotFound(Spanned { value, .. }) => { + format!("Variable `{value}` not found.").into() + } ExpectedNumberAfterUnaryOperator(Spanned { value, .. }) => format!( "Expected a number after unary operator (-) but got {} instead.", value.kind() @@ -97,7 +99,7 @@ impl RunError { FieldNotFoundInStruct(_) => todo!(), CouldntDereferenceValue(_) => todo!(), ReferenceToMovedData(_) => todo!(), - VariableMoved(_) => todo!(), + VariableMoved(Spanned { value, .. }) => format!("Variable `{value}` was moved.").into(), CannotBorrowValue(_) => todo!(), IncompatibleReflectTypes { expected, diff --git a/src/ui.rs b/src/ui.rs index 33a6edd..3686d25 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -17,12 +17,13 @@ use crate::{ prelude::ConsoleConfig, }; -/// The prefix for commands -const COMMAND_MESSAGE_PREFIX: &str = "$ "; -const COMMAND_RESULT_PREFIX: &str = "> "; +/// Prefix for log messages that show a previous command. +pub const COMMAND_MESSAGE_PREFIX: &str = "$ "; +/// Prefix for log messages that show the result of a command. +pub const COMMAND_RESULT_PREFIX: &str = "> "; /// Identifier for log messages that show a previous command. pub const COMMAND_MESSAGE_NAME: &str = "console_command"; -/// Identifier for log messages that show the result of a history +/// Identifier for log messages that show the result of a command. pub const COMMAND_RESULT_NAME: &str = "console_result"; #[derive(Default, Resource)] @@ -210,8 +211,8 @@ fn format_line( 0.0, config.theme.format_dark(), ); - if *name == COMMAND_MESSAGE_NAME || *name == COMMAND_RESULT_NAME { - if *name == COMMAND_MESSAGE_NAME { + match *name { + COMMAND_MESSAGE_NAME => { if new { hints.reset_hint_added(); } @@ -244,14 +245,18 @@ fn format_line( return text; } text.append(message.as_str(), 0.0, config.theme.format_text()); - return text; - } else { - // COMMAND_RESULT_NAME - text.append(COMMAND_RESULT_PREFIX, 0.0, config.theme.format_dark()) + text } - } - text.append(level.as_str(), 0.0, config.theme.format_level(*level)); - text.append(&format!(" {message}"), 0.0, config.theme.format_text()); + COMMAND_RESULT_NAME => { + text.append(COMMAND_RESULT_PREFIX, 0.0, config.theme.format_dark()); + text.append(message.as_str(), 0.0, config.theme.format_text()); + text + } + _ => { + text.append(level.as_str(), 0.0, config.theme.format_level(*level)); + text.append(&format!(" {message}"), 0.0, config.theme.format_text()); - text + text + } + } }