1
0

Add tree option to CLI args

This commit is contained in:
Jeff 2023-11-14 19:37:19 -05:00
parent 364fed810b
commit 781d475794
2 changed files with 22 additions and 4 deletions

View File

@ -73,7 +73,7 @@ impl Debug for Evaluator<'_, '_> {
} }
impl<'context, 'code> Evaluator<'context, 'code> { 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(); let syntax_tree = parser.parse(source, None).unwrap();
Evaluator { Evaluator {
@ -84,7 +84,7 @@ impl<'context, 'code> Evaluator<'context, 'code> {
} }
} }
fn run(self) -> Result<Value> { pub fn run(self) -> Result<Value> {
let mut cursor = self.syntax_tree.walk(); let mut cursor = self.syntax_tree.walk();
let root_node = cursor.node(); let root_node = cursor.node();
let mut prev_result = Ok(Value::Empty); let mut prev_result = Ok(Value::Empty);
@ -96,6 +96,10 @@ impl<'context, 'code> Evaluator<'context, 'code> {
prev_result prev_result
} }
pub fn syntax_tree(&self) -> String {
self.syntax_tree.root_node().to_sexp()
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -9,10 +9,11 @@ use rustyline::{
history::DefaultHistory, history::DefaultHistory,
Completer, Context, Editor, Helper, Validator, Completer, Context, Editor, Helper, Validator,
}; };
use tree_sitter::Parser as TSParser;
use std::borrow::Cow; 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. /// Command-line arguments to be parsed.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -30,6 +31,10 @@ struct Args {
#[arg(short = 'p', long)] #[arg(short = 'p', long)]
input_path: Option<String>, input_path: Option<String>,
/// 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. /// Location of the file to run.
path: Option<String>, path: Option<String>,
} }
@ -68,7 +73,16 @@ async fn main() {
.insert("input".to_string(), Value::String(file_contents)); .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 { match eval_result {
Ok(value) => { Ok(value) => {