From 96afe7d3a3262d7c7fccdb03dfaebeae8a8303b3 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 20 Mar 2024 16:15:45 -0400 Subject: [PATCH] Improve error reports; Add example --- dust-lang/src/error.rs | 16 ++++++++++++---- examples/guessing_game.ds | 19 +++++++++++++++++++ examples/hello_world.ds | 1 + 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 examples/guessing_game.ds create mode 100644 examples/hello_world.ds diff --git a/dust-lang/src/error.rs b/dust-lang/src/error.rs index 388bf03..b8f85c9 100644 --- a/dust-lang/src/error.rs +++ b/dust-lang/src/error.rs @@ -46,7 +46,7 @@ impl Error { ( Report::build( - ReportKind::Custom("Parsing Error", Color::White), + ReportKind::Custom("Parsing Error", Color::Yellow), "input", span.1, ) @@ -73,7 +73,7 @@ impl Error { ( Report::build( - ReportKind::Custom("Lexing Error", Color::White), + ReportKind::Custom("Lexing Error", Color::Yellow), "input", span.1, ) @@ -89,9 +89,16 @@ impl Error { } Error::Runtime { error, position } => ( Report::build( - ReportKind::Custom("Runtime Error", Color::White), + ReportKind::Custom("Runtime Error", Color::Red), "input", 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 { Some(validation_error) @@ -102,10 +109,11 @@ impl Error { ), Error::Validation { error, position } => ( Report::build( - ReportKind::Custom("Validation Error", Color::White), + ReportKind::Custom("Validation Error", Color::Magenta), "input", 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."), Some(error), position, diff --git a/examples/guessing_game.ds b/examples/guessing_game.ds new file mode 100644 index 0000000..cc38745 --- /dev/null +++ b/examples/guessing_game.ds @@ -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 + } +} diff --git a/examples/hello_world.ds b/examples/hello_world.ds new file mode 100644 index 0000000..c07ac03 --- /dev/null +++ b/examples/hello_world.ds @@ -0,0 +1 @@ +io.write_line("Hello, world!")