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()));
|
return Err(ValidationError::ExpectedValueStatement(right.position()));
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Type::Integer | Type::Float | Type::String = left_type {
|
match (&left_type, &right_type) {
|
||||||
if let Type::Integer | Type::Float | Type::String = right_type {
|
(Type::Integer, Type::Integer) => Ok(()),
|
||||||
Ok(())
|
(Type::Float, Type::Float) => Ok(()),
|
||||||
} else {
|
(Type::String, Type::String) => Ok(()),
|
||||||
Err(ValidationError::ExpectedIntegerFloatOrString {
|
(Type::Integer, _) => {
|
||||||
actual: right_type,
|
Err(ValidationError::ExpectedIntegerOrFloat(right.position()))
|
||||||
position: right.position(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
} else {
|
(Type::Float, _) => {
|
||||||
Err(ValidationError::ExpectedIntegerFloatOrString {
|
Err(ValidationError::ExpectedIntegerOrFloat(right.position()))
|
||||||
actual: left_type,
|
}
|
||||||
position: left.position(),
|
(Type::String, _) => Err(ValidationError::ExpectedString {
|
||||||
})
|
actual: right_type,
|
||||||
|
position: right.position(),
|
||||||
|
}),
|
||||||
|
(_, _) => Err(ValidationError::ExpectedIntegerFloatOrString {
|
||||||
|
actual: right_type,
|
||||||
|
position: right.position(),
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Math::Subtract(left, right)
|
Math::Subtract(left, right)
|
||||||
|
Loading…
Reference in New Issue
Block a user