Skip to content

Commit

Permalink
Replace more todo!s with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
doonv committed Jan 1, 2024
1 parent cd8b43a commit 6827cdd
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 35 deletions.
6 changes: 4 additions & 2 deletions examples/custom_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ fn time_since_epoch() {
info!("The unix epoch was {} seconds ago", time.as_secs());
}

/// Function with parameters and return value
fn add(num1: Spanned<Number>, num2: Spanned<Number>) -> Result<Number, RunError> {
/// Function with parameters and return value.
///
/// Note that this will cause an error if an integer to passed to this function.
fn add(num1: f64, num2: f64) -> f64 {
num1 + num2
}

Expand Down
3 changes: 3 additions & 0 deletions src/builtin_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ pub(crate) mod runner;
pub use number::*;
pub use runner::{environment::Environment, error::RunError, unique_rc::*, Value};

/// Additonal traits for span.
pub trait SpanExtension {
/// Wrap this value with a [`Spanned`].
fn wrap<T>(self, value: T) -> Spanned<T>;
/// Combine two [`Span`]s into one.
fn join(self, span: Self) -> Self;
}
impl SpanExtension for Span {
Expand Down
24 changes: 12 additions & 12 deletions src/builtin_parser/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ impl Number {
/// Returns a [`&'static str`](str) represents the kind of the number.
pub fn kind(&self) -> &'static str {
match self {
Number::Float(_) => "(float)",
Number::Integer(_) => "(integer)",
Number::u8(_) => "u8",
Number::u16(_) => "u16",
Number::u32(_) => "u32",
Number::u64(_) => "u64",
Number::i8(_) => "i8",
Number::i16(_) => "i16",
Number::i32(_) => "i32",
Number::i64(_) => "i64",
Number::f32(_) => "f32",
Number::f64(_) => "f64",
Number::Float(_) => "a float",
Number::Integer(_) => "an integer",
Number::u8(_) => "a u8",
Number::u16(_) => "a u16",
Number::u32(_) => "a u32",
Number::u64(_) => "a u64",
Number::i8(_) => "a i8",
Number::i16(_) => "a i16",
Number::i32(_) => "a i32",
Number::i64(_) => "a i64",
Number::f32(_) => "a f32",
Number::f64(_) => "a f64",
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/builtin_parser/runner/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ macro_rules! impl_into_function {
world,
environment,
registrations
)
.unwrap_or_else(|_| todo!());
)?;

res
}),+)?
Expand Down Expand Up @@ -320,7 +319,7 @@ impl Environment {

/// Registers a function for use inside the language.
///
/// All parameters must implement [`TryFrom<Value>`].
/// All parameters must implement [`FunctionParam`].
/// There is a limit of 8 parameters.
///
/// The return value of the function must implement [`Into<Value>`]
Expand Down
13 changes: 13 additions & 0 deletions src/builtin_parser/runner/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::Value;

/// An error occuring during the while executing the [`AST`](Ast) of the command.
#[derive(Debug)]
#[allow(missing_docs)]
pub enum RunError {
/// A custom text message. Contains very little contextual information, try to find an existing error instead.
Custom {
Expand Down Expand Up @@ -43,6 +44,11 @@ pub enum RunError {
right: &'static str,
span: Span,
},
IncompatibleFunctionParameter {
expected: &'static str,
actual: &'static str,
span: Span,
},
}

impl RunError {
Expand All @@ -65,6 +71,7 @@ impl RunError {
CannotMoveOutOfResource(Spanned { span, .. }) => vec![span.clone()],
CannotNegateUnsignedInteger(Spanned { span, .. }) => vec![span.clone()],
IncompatibleNumberTypes { span, .. } => vec![span.clone()],
IncompatibleFunctionParameter { span, .. } => vec![span.clone()],
}
}
pub fn hints(&self) -> Vec<CommandHint> {
Expand Down Expand Up @@ -109,6 +116,12 @@ impl RunError {
format!("Incompatible number types; `{left}` and `{right}` are incompatible.")
.into()
}
IncompatibleFunctionParameter {
expected, actual, ..
} => {
format!("Mismatched function paramater type. Expected {expected} but got {actual}")
.into()
}
}
}
}
44 changes: 29 additions & 15 deletions src/builtin_parser/runner/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Value {
pub fn kind(&self) -> &'static str {
match self {
Value::None => "nothing",
Value::Number(_) => "a number",
Value::Number(number) => number.kind(),
Value::Boolean(_) => "a boolean",
Value::String(_) => "a string",
Value::Reference(_) => "a reference",
Expand Down Expand Up @@ -290,7 +290,7 @@ impl FunctionParam for Spanned<Value> {
Ok(value.unwrap())
}
}
impl<T: TryFrom<Value, Error = RunError>> FunctionParam for Spanned<T> {
impl<T: TryFrom<Spanned<Value>, Error = RunError>> FunctionParam for Spanned<T> {
type Item<'world, 'env, 'reg> = Self;
const USES_VALUE: bool = true;

Expand All @@ -302,8 +302,8 @@ impl<T: TryFrom<Value, Error = RunError>> FunctionParam for Spanned<T> {
) -> Result<Self::Item<'world, 'env, 'reg>, RunError> {
let value = value.unwrap();
Ok(Spanned {
span: value.span,
value: T::try_from(value.value)?,
span: value.span.clone(),
value: T::try_from(value)?,
})
}
}
Expand Down Expand Up @@ -333,18 +333,23 @@ macro_rules! impl_function_param_for_value {
_: &mut Option<&'env mut Environment>,
_: &'reg [&'reg TypeRegistration],
) -> Result<Self::Item<'world, 'env, 'reg>, RunError> {
if let $value_pattern = value.unwrap().value {
let value = value.unwrap();
if let $value_pattern = value.value {
Ok($return)
} else {
todo!()
Err(RunError::IncompatibleFunctionParameter {
expected: stringify!($type),
actual: value.value.kind(),
span: value.span,
})
}
}
}
impl TryFrom<Value> for $type {
impl TryFrom<Spanned<Value>> for $type {
type Error = RunError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
if let $value_pattern = value {
fn try_from(value: Spanned<Value>) -> Result<Self, Self::Error> {
if let $value_pattern = value.value {
Ok($return)
} else {
todo!()
Expand All @@ -366,21 +371,30 @@ macro_rules! impl_function_param_for_numbers {
_: &mut Option<&'env mut Environment>,
_: &'reg [&'reg TypeRegistration],
) -> Result<Self::Item<'world, 'env, 'reg>, RunError> {
match value.unwrap().value {
let value = value.unwrap();
match value.value {
Value::Number(Number::$number(value)) => Ok(value),
Value::Number(Number::$generic(value)) => Ok(value as $number),
_ => todo!()
_ => Err(RunError::IncompatibleFunctionParameter {
expected: concat!("a ", stringify!($number)),
actual: value.value.kind(),
span: value.span,
})
}
}
}
impl TryFrom<Value> for $number {
impl TryFrom<Spanned<Value>> for $number {
type Error = RunError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
fn try_from(value: Spanned<Value>) -> Result<Self, Self::Error> {
match value.value {
Value::Number(Number::$number(value)) => Ok(value),
Value::Number(Number::$generic(value)) => Ok(value as $number),
_ => todo!()
_ => Err(RunError::IncompatibleFunctionParameter {
expected: concat!("a ", stringify!($number)),
actual: value.value.kind(),
span: value.span
})
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub enum CommandHintColor {

/// A resource where hints (errors/warnings/etc) are stored
/// to be displayed in the developer console.
#[derive(Resource, Default, Deref)]
#[derive(Resource, Debug, Default, Deref)]
pub struct CommandHints {
#[deref]
hints: Vec<Vec<CommandHint>>,
Expand All @@ -95,13 +95,16 @@ impl CommandHints {
hints.into()
)
} else {
self.hint_added = true;
self.hints.push(hints.into());
}
}
pub(crate) fn reset_hint_added(&mut self) {
if self.hint_added {
dbg!("yeah");
self.hint_added = false;
} else {
dbg!("nah");
self.push([]);
}
}
Expand All @@ -121,7 +124,7 @@ impl CommandHints {
/// fn parse(&self, command: &str, world: &mut World) {
/// // The `name: COMMAND_RESULT_NAME` tells the console this is a result from
/// // the parser and then formats it accordingly.
/// // TODO: figure out better solution for this
/// # // TODO: figure out better solution for this
/// info!(name: COMMAND_RESULT_NAME, "You just entered the command {command}")
/// }
/// }
Expand Down
3 changes: 2 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ fn format_line(
}
let hints = &hints[*command_index];

*command_index += 1;

// TODO: Handle more than just he first element
if let Some(hint) = hints.first() {
const PREFIX_LEN: usize = COMMAND_MESSAGE_PREFIX.len();
Expand All @@ -239,7 +241,6 @@ fn format_line(
);
return text;
}
*command_index += 1;
}
text.append(message.as_str(), 0.0, config.theme.format_text());

Expand Down

0 comments on commit 6827cdd

Please sign in to comment.