Skip to content

Commit

Permalink
feat: cli reports errors
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed Jun 15, 2024
1 parent 1dc9c9c commit c1fcd4c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ For the language to be almost operational it should have:
- return
- recursion
- [x] comments
- [ ] stdin, stdout
- [x] stdin, stdout
- [x] imports
- [x] error reporting with line numbers

Expand Down
31 changes: 26 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, path::PathBuf};
use std::{fs, path::PathBuf, process::exit};

use clap::Parser;

Expand All @@ -17,10 +17,31 @@ fn main() {
fn run(path: PathBuf) {
let input = match fs::read_to_string(path) {
Ok(input) => input,
Err(err) => panic!("Failed to read input file: {err}"),
Err(err) => {
println!("Failed to read input file: {err}");
exit(1);
}
};

// TODO: Better error handling
let program = parser::parse(&input).unwrap();
runtime::run(&program).unwrap();
let program = match parser::parse(&input) {
Ok(program) => program,
Err(err) => {
println!(
"Syntax error on line {}, character {}:\n {}",
err.range.start.line, err.range.start.character, err
);
exit(1);
}
};

match runtime::run(&program) {
Ok(_) => (),
Err(err) => {
println!(
"Runtime error on line {}, character {}:\n {}",
err.range.start.line, err.range.start.character, err
);
exit(1);
}
}
}

0 comments on commit c1fcd4c

Please sign in to comment.