Improve error reports; Add example

This commit is contained in:
Jeff 2024-03-20 16:15:45 -04:00
parent bb7cda1242
commit 96afe7d3a3
3 changed files with 32 additions and 4 deletions

View File

@ -46,7 +46,7 @@ impl Error {
( (
Report::build( Report::build(
ReportKind::Custom("Parsing Error", Color::White), ReportKind::Custom("Parsing Error", Color::Yellow),
"input", "input",
span.1, span.1,
) )
@ -73,7 +73,7 @@ impl Error {
( (
Report::build( Report::build(
ReportKind::Custom("Lexing Error", Color::White), ReportKind::Custom("Lexing Error", Color::Yellow),
"input", "input",
span.1, span.1,
) )
@ -89,9 +89,16 @@ impl Error {
} }
Error::Runtime { error, position } => ( Error::Runtime { error, position } => (
Report::build( Report::build(
ReportKind::Custom("Runtime Error", Color::White), ReportKind::Custom("Runtime Error", Color::Red),
"input", "input",
position.1, position.1,
)
.with_message("An error occured that forced the program to exit.")
.with_note(
"There may be unexpected side-effects because the program could not finish.",
)
.with_help(
"This is the interpreter's fault. Please submit a bug with this error message.",
), ),
if let RuntimeError::ValidationFailure(validation_error) = error { if let RuntimeError::ValidationFailure(validation_error) = error {
Some(validation_error) Some(validation_error)
@ -102,10 +109,11 @@ impl Error {
), ),
Error::Validation { error, position } => ( Error::Validation { error, position } => (
Report::build( Report::build(
ReportKind::Custom("Validation Error", Color::White), ReportKind::Custom("Validation Error", Color::Magenta),
"input", "input",
position.1, position.1,
) )
.with_message("The syntax is valid but this code is not sound.")
.with_note("This error was detected by the interpreter before running the code."), .with_note("This error was detected by the interpreter before running the code."),
Some(error), Some(error),
position, position,

19
examples/guessing_game.ds Normal file
View File

@ -0,0 +1,19 @@
io.write_line("Guess the number.")
secret_number = int.random_range(0..100)
loop {
io.write_line("Input your guess.")
input = io.read_line() ? "Failed to read input."
guess = int.parse(input) ? "Failed to parse input."
if guess < secret_number {
io.write_line("Too low!")
} else if guess > secret_number {
io.write_line("Too high!")
} else {
io.write_line("You win!")
break
}
}

1
examples/hello_world.ds Normal file
View File

@ -0,0 +1 @@
io.write_line("Hello, world!")