From 43ee989eecaf025016673749a312b972e8cc96cf Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 29 Dec 2023 16:27:13 -0500 Subject: [PATCH] Improve Interpreter API --- src/interpret.rs | 28 +++++++++++----------------- src/main.rs | 18 +++--------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/src/interpret.rs b/src/interpret.rs index f218807..0ebc40c 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -44,23 +44,22 @@ pub fn interpret_with_context(source: &str, context: Map) -> Result { let mut parser = Parser::new(); parser.set_language(language())?; - let mut interpreter = Interpreter::new(context, source)?; - let value = interpreter.run()?; + let mut interpreter = Interpreter::new(context)?; + let value = interpreter.run(source)?; Ok(value) } /// A source code interpreter for the Dust language. -pub struct Interpreter<'s> { +pub struct Interpreter { parser: Parser, context: Map, - source: &'s str, syntax_tree: Option, abstract_tree: Option, } -impl<'s> Interpreter<'s> { - pub fn new(context: Map, source: &'s str) -> Result { +impl Interpreter { + pub fn new(context: Map) -> Result { let mut parser = Parser::new(); parser.set_language(language())?; @@ -68,25 +67,20 @@ impl<'s> Interpreter<'s> { Ok(Interpreter { parser, context, - source, syntax_tree: None, abstract_tree: None, }) } - pub fn set_source(&mut self, source: &'s str) { - self.source = source; + pub fn parse_only(&mut self, source: &str) { + self.syntax_tree = self.parser.parse(source, self.syntax_tree.as_ref()); } - pub fn parse_only(&mut self) { - self.syntax_tree = self.parser.parse(self.source, self.syntax_tree.as_ref()); - } - - pub fn run(&mut self) -> Result { - self.syntax_tree = self.parser.parse(self.source, self.syntax_tree.as_ref()); + pub fn run(&mut self, source: &str) -> Result { + self.syntax_tree = self.parser.parse(source, self.syntax_tree.as_ref()); self.abstract_tree = if let Some(syntax_tree) = &self.syntax_tree { Some(Root::from_syntax_node( - self.source, + source, syntax_tree.root_node(), &self.context, )?) @@ -95,7 +89,7 @@ impl<'s> Interpreter<'s> { }; if let Some(abstract_tree) = &self.abstract_tree { - abstract_tree.run(self.source, &self.context) + abstract_tree.run(source, &self.context) } else { Ok(Value::Option(None)) } diff --git a/src/main.rs b/src/main.rs index f690b28..3df2867 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,10 +34,6 @@ struct Args { #[arg(short = 't', long = "tree")] show_syntax_tree: bool, - /// Launch in interactive mode. - #[arg(short = 'n', long)] - interactive: bool, - /// Location of the file to run. path: Option, } @@ -76,23 +72,15 @@ fn main() { let mut parser = TSParser::new(); parser.set_language(language()).unwrap(); - let mut interpreter = Interpreter::new(context, &source).unwrap(); - - if args.interactive { - loop { - let result = interpreter.run(); - - println!("{result:?}") - } - } + let mut interpreter = Interpreter::new(context).unwrap(); if args.show_syntax_tree { - interpreter.parse_only(); + interpreter.parse_only(&source); println!("{}", interpreter.syntax_tree().unwrap()); } - let eval_result = interpreter.run(); + let eval_result = interpreter.run(&source); match eval_result { Ok(value) => {