diff --git a/src/evaluator.rs b/src/evaluator.rs index cd2dcbe..3654dd8 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -73,7 +73,7 @@ impl Debug for Evaluator<'_, '_> { } impl<'context, 'code> Evaluator<'context, 'code> { - fn new(mut parser: Parser, context: &'context mut Map, source: &'code str) -> Self { + pub fn new(mut parser: Parser, context: &'context mut Map, source: &'code str) -> Self { let syntax_tree = parser.parse(source, None).unwrap(); Evaluator { @@ -84,7 +84,7 @@ impl<'context, 'code> Evaluator<'context, 'code> { } } - fn run(self) -> Result { + pub fn run(self) -> Result { let mut cursor = self.syntax_tree.walk(); let root_node = cursor.node(); let mut prev_result = Ok(Value::Empty); @@ -96,6 +96,10 @@ impl<'context, 'code> Evaluator<'context, 'code> { prev_result } + + pub fn syntax_tree(&self) -> String { + self.syntax_tree.root_node().to_sexp() + } } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index 564d02d..ca994ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,10 +9,11 @@ use rustyline::{ history::DefaultHistory, Completer, Context, Editor, Helper, Validator, }; +use tree_sitter::Parser as TSParser; use std::borrow::Cow; -use dust_lang::{evaluate_with_context, Map, Value}; +use dust_lang::{evaluate_with_context, language, Evaluator, Map, Value}; /// Command-line arguments to be parsed. #[derive(Parser, Debug)] @@ -30,6 +31,10 @@ struct Args { #[arg(short = 'p', long)] input_path: Option, + /// A path to file whose contents will be assigned to the "input" variable. + #[arg(short = 't', long = "tree")] + show_syntax_tree: bool, + /// Location of the file to run. path: Option, } @@ -68,7 +73,16 @@ async fn main() { .insert("input".to_string(), Value::String(file_contents)); } - let eval_result = evaluate_with_context(&source, &mut context); + let mut parser = TSParser::new(); + parser.set_language(language()).unwrap(); + + let evaluator = Evaluator::new(parser, &mut context, &source); + + if args.show_syntax_tree { + println!("{}", evaluator.syntax_tree()); + } + + let eval_result = evaluator.run(); match eval_result { Ok(value) => {