Skip to content

Commit

Permalink
More refactoring and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
doonv committed Jan 4, 2024
1 parent 88163b0 commit 2f27011
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/custom_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn increment_number(number: Spanned<StrongRef<Value>>) -> 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 {
Expand Down
4 changes: 2 additions & 2 deletions src/builtin_parser/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use self::{

use super::{
parser::{Ast, Expression, Operator},
Number, Spanned,
Number, Spanned, SpanExtension,
};
use bevy::{
prelude::*,
Expand Down Expand Up @@ -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!(),
Expand Down
14 changes: 8 additions & 6 deletions src/builtin_parser/runner/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()))),
}
}

Expand All @@ -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 {
Expand All @@ -292,7 +294,7 @@ impl Environment {
};
Ok(value.into_inner())
}
None => Err(RunError::VariableNotFound(span)),
None => Err(RunError::VariableNotFound(span.wrap(name.to_string()))),
}
}

Expand All @@ -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> {
Expand All @@ -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()))),
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/builtin_parser/runner/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ pub enum RunError {
text: String,
span: Span,
},
VariableNotFound(Span),
VariableNotFound(Spanned<String>),
ExpectedNumberAfterUnaryOperator(Spanned<Value>),
InvalidVariantForResource(String, String),
CannotIndexValue(Span),
FieldNotFoundInStruct(Span),
CouldntDereferenceValue(Span),
ReferenceToMovedData(Span),
VariableMoved(Span),
VariableMoved(Spanned<String>),
CannotBorrowValue(Span),
IncompatibleReflectTypes {
expected: String,
Expand Down Expand Up @@ -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()],
Expand All @@ -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()
Expand All @@ -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,
Expand Down
33 changes: 19 additions & 14 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 2f27011

Please sign in to comment.