Add pretty errors example; Add validation in math
This commit is contained in:
parent
d3f5585d07
commit
9640feb65b
29
dust-lang/examples/pretty_errors.rs
Normal file
29
dust-lang/examples/pretty_errors.rs
Normal file
@ -0,0 +1,29 @@
|
||||
// It's very easy to get nice-looking error messages from the Dust's top-level error type.
|
||||
|
||||
use std::{io::stderr, sync::Arc};
|
||||
|
||||
use ariadne::sources;
|
||||
use dust_lang::Interpreter;
|
||||
|
||||
fn main() {
|
||||
let interpreter = Interpreter::new();
|
||||
|
||||
// First, we'll run some bad code.
|
||||
let error = interpreter
|
||||
.run(
|
||||
Arc::from("bad code"),
|
||||
Arc::from(
|
||||
"
|
||||
x = 1 + 'a'
|
||||
y: float = 'hello'
|
||||
",
|
||||
),
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
for report in error.build_reports() {
|
||||
report
|
||||
.write_for_stdout(sources(interpreter.sources()), stderr())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
@ -40,20 +40,24 @@ impl AbstractNode for Math {
|
||||
return Err(ValidationError::ExpectedValueStatement(right.position()));
|
||||
};
|
||||
|
||||
if let Type::Integer | Type::Float | Type::String = left_type {
|
||||
if let Type::Integer | Type::Float | Type::String = right_type {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ValidationError::ExpectedIntegerFloatOrString {
|
||||
match (&left_type, &right_type) {
|
||||
(Type::Integer, Type::Integer) => Ok(()),
|
||||
(Type::Float, Type::Float) => Ok(()),
|
||||
(Type::String, Type::String) => Ok(()),
|
||||
(Type::Integer, _) => {
|
||||
Err(ValidationError::ExpectedIntegerOrFloat(right.position()))
|
||||
}
|
||||
(Type::Float, _) => {
|
||||
Err(ValidationError::ExpectedIntegerOrFloat(right.position()))
|
||||
}
|
||||
(Type::String, _) => Err(ValidationError::ExpectedString {
|
||||
actual: right_type,
|
||||
position: right.position(),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
Err(ValidationError::ExpectedIntegerFloatOrString {
|
||||
actual: left_type,
|
||||
position: left.position(),
|
||||
})
|
||||
}),
|
||||
(_, _) => Err(ValidationError::ExpectedIntegerFloatOrString {
|
||||
actual: right_type,
|
||||
position: right.position(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
Math::Subtract(left, right)
|
||||
|
Loading…
Reference in New Issue
Block a user