dust/dust-lang/src/lib.rs

49 lines
1.1 KiB
Rust
Raw Normal View History

2024-02-25 18:49:26 +00:00
pub mod abstract_tree;
2024-03-23 12:15:48 +00:00
pub mod built_in_functions;
2024-02-25 18:49:26 +00:00
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-23 12:15:48 +00:00
use abstract_tree::Identifier;
use built_in_functions::BUILT_IN_FUNCTIONS;
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;
2024-03-19 21:12:32 +00:00
use parser::parse;
2024-02-26 21:27:01 +00:00
pub use value::Value;
2024-02-25 08:12:09 +00:00
2024-03-08 17:24:11 +00:00
pub fn interpret(source: &str) -> Result<Option<Value>, Vec<Error>> {
2024-03-06 23:15:25 +00:00
let context = Context::new();
2024-03-23 12:15:48 +00:00
for function in BUILT_IN_FUNCTIONS {
context
.set_value(Identifier::new(function.name()), function.as_value())
.unwrap();
}
2024-03-06 23:15:25 +00:00
let mut interpreter = Interpreter::new(context);
2024-03-23 12:15:48 +00:00
interpreter.run(include_str!("../../std/io.ds"))?;
2024-03-06 23:15:25 +00:00
interpreter.run(source)
}
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 }
}
2024-03-08 17:24:11 +00:00
pub fn run(&mut self, source: &str) -> Result<Option<Value>, Vec<Error>> {
2024-03-06 20:36:58 +00:00
let tokens = lex(source)?;
let abstract_tree = parse(&tokens)?;
let value_option = abstract_tree.run(&self.context)?;
2024-03-06 21:50:44 +00:00
Ok(value_option)
2023-09-28 19:58:01 +00:00
}
}