2024-02-25 18:49:26 +00:00
|
|
|
pub mod abstract_tree;
|
|
|
|
pub mod context;
|
|
|
|
pub mod error;
|
|
|
|
pub mod lexer;
|
|
|
|
pub mod parser;
|
2024-02-26 21:27:01 +00:00
|
|
|
pub mod value;
|
2024-02-23 12:40:01 +00:00
|
|
|
|
2024-03-06 20:36:58 +00:00
|
|
|
use abstract_tree::AbstractTree;
|
2024-02-25 18:49:26 +00:00
|
|
|
use context::Context;
|
|
|
|
use error::Error;
|
2024-03-06 20:36:58 +00:00
|
|
|
use lexer::lex;
|
|
|
|
pub use parser::{parse, parser, DustParser};
|
2024-02-26 21:27:01 +00:00
|
|
|
pub use value::Value;
|
2024-02-25 08:12:09 +00:00
|
|
|
|
2024-03-06 20:36:58 +00:00
|
|
|
pub struct Interpreter {
|
|
|
|
context: Context,
|
2024-02-25 08:12:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 20:36:58 +00:00
|
|
|
impl Interpreter {
|
|
|
|
pub fn new(context: Context) -> Self {
|
|
|
|
Interpreter { context }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self, source: &str) -> Result<Value, Vec<Error>> {
|
|
|
|
let tokens = lex(source)?;
|
|
|
|
let statements = parse(&tokens)?;
|
|
|
|
|
|
|
|
let mut value = Value::none();
|
2024-02-23 17:14:15 +00:00
|
|
|
|
2024-03-06 20:36:58 +00:00
|
|
|
for (statement, _span) in statements {
|
|
|
|
value = match statement.run(&self.context) {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(runtime_error) => return Err(vec![Error::Runtime(runtime_error)]),
|
|
|
|
}
|
|
|
|
}
|
2024-02-23 12:40:01 +00:00
|
|
|
|
2024-03-06 20:36:58 +00:00
|
|
|
Ok(value)
|
2023-09-28 19:58:01 +00:00
|
|
|
}
|
|
|
|
}
|