Skip to content

Commit 55efcea

Browse files
feat: converting error handing to anyhow (#49)
1 parent b0311eb commit 55efcea

File tree

6 files changed

+48
-54
lines changed

6 files changed

+48
-54
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pretty_env_logger = "0.5.0"
1919
# For custom errors.
2020
thiserror = "1.0.61"
2121

22+
# For error handling.
23+
anyhow = "1.0.89"
24+
2225

2326
[dev-dependencies]
2427
# For parameterised testing.

src/evaluator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::evaluator::model::evaluation_error::EvaluationError;
33
use crate::evaluator::model::object::Object;
44
use crate::syntax_analysis::model::syntax_tree_node::*;
55

6-
mod model;
6+
pub mod model;
77

88
mod expression;
99
mod statement;

src/evaluator/model/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pub(super) mod environment;
22
pub(super) mod evaluation_error;
3-
pub(super) mod object;
3+
pub mod object;

src/interpreter/mod.rs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/main.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,48 @@
22
extern crate log;
33
extern crate pretty_env_logger;
44

5+
use std::io::{stdin, stdout, Write};
6+
7+
use anyhow::{Context, Result};
8+
9+
use crate::evaluator::Evaluator;
10+
use crate::lexical_analysis::LexicalAnalysis;
11+
use crate::syntax_analysis::SyntaxAnalysis;
12+
513
mod evaluator;
6-
mod interpreter;
714
mod lexical_analysis;
815
mod syntax_analysis;
916

1017
fn main() {
1118
pretty_env_logger::init();
12-
interpreter::repl();
19+
trace!("Version {}.", env!("CARGO_PKG_VERSION"));
20+
let mut evaluator = crate::evaluator::Evaluator::new();
21+
22+
loop {
23+
if let Err(error) = repl(&mut evaluator) {
24+
error!("{}", error);
25+
}
26+
}
27+
}
28+
fn repl(evaluator: &mut Evaluator) -> Result<()> {
29+
print!(" >>> ");
30+
let input = read()?;
31+
let tokens = LexicalAnalysis::from(&input)?;
32+
let abstract_syntax_tree = SyntaxAnalysis::from(tokens)?;
33+
let object = evaluator.evaluate(abstract_syntax_tree)?;
34+
println!("{:?}", object);
35+
Ok(())
36+
}
37+
38+
fn read() -> Result<String> {
39+
let mut buffer = String::new();
40+
41+
let _ = stdout().flush();
42+
stdin()
43+
.read_line(&mut buffer)
44+
.context("Unable to read user input from standard input.")?;
45+
46+
Ok(buffer)
1347
}
1448

1549
#[cfg(test)]

0 commit comments

Comments
 (0)