From 3c729bea6e01344e2747e1335a14de6997a3a73d Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 29 Dec 2023 14:52:51 -0500 Subject: [PATCH] Improve Map interface; Clean up --- src/abstract_tree/use.rs | 4 ++-- src/interpret.rs | 18 ++++++++---------- src/main.rs | 8 ++++---- test.txt | 1 - 4 files changed, 14 insertions(+), 17 deletions(-) delete mode 100644 test.txt diff --git a/src/abstract_tree/use.rs b/src/abstract_tree/use.rs index 69678e8..877b6db 100644 --- a/src/abstract_tree/use.rs +++ b/src/abstract_tree/use.rs @@ -27,9 +27,9 @@ impl AbstractTree for Use { fn run(&self, _source: &str, context: &Map) -> Result { let file_contents = read_to_string(&self.path)?; - let mut file_context = Map::new(); + let file_context = Map::new(); - interpret_with_context(&file_contents, &mut file_context)?; + interpret_with_context(&file_contents, file_context.clone())?; for (key, (value, r#type)) in file_context.variables()?.iter() { context.set(key.clone(), value.clone(), Some(r#type.clone()))?; diff --git a/src/interpret.rs b/src/interpret.rs index c214738..f218807 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -18,9 +18,7 @@ use crate::{language, AbstractTree, Error, Map, Result, Root, Value}; /// assert_eq!(interpret("1 + 2 + 3"), Ok(Value::Integer(6))); /// ``` pub fn interpret(source: &str) -> Result { - let mut context = Map::new(); - - interpret_with_context(source, &mut context) + interpret_with_context(source, Map::new()) } /// Interpret the given source code with the given context. @@ -29,7 +27,7 @@ pub fn interpret(source: &str) -> Result { /// /// ```rust /// # use dust_lang::*; -/// let mut context = Map::new(); +/// let context = Map::new(); /// /// context.set("one".into(), 1.into(), None); /// context.set("two".into(), 2.into(), None); @@ -38,11 +36,11 @@ pub fn interpret(source: &str) -> Result { /// let dust_code = "four = 4 one + two + three + four"; /// /// assert_eq!( -/// interpret_with_context(dust_code, &mut context), +/// interpret_with_context(dust_code, context), /// Ok(Value::Integer(10)) /// ); /// ``` -pub fn interpret_with_context(source: &str, context: &mut Map) -> Result { +pub fn interpret_with_context(source: &str, context: Map) -> Result { let mut parser = Parser::new(); parser.set_language(language())?; @@ -53,16 +51,16 @@ pub fn interpret_with_context(source: &str, context: &mut Map) -> Result } /// A source code interpreter for the Dust language. -pub struct Interpreter<'c, 's> { +pub struct Interpreter<'s> { parser: Parser, - context: &'c mut Map, + context: Map, source: &'s str, syntax_tree: Option, abstract_tree: Option, } -impl<'c, 's> Interpreter<'c, 's> { - pub fn new(context: &'c mut Map, source: &'s str) -> Result { +impl<'s> Interpreter<'s> { + pub fn new(context: Map, source: &'s str) -> Result { let mut parser = Parser::new(); parser.set_language(language())?; diff --git a/src/main.rs b/src/main.rs index 6725fb5..f690b28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,7 @@ fn main() { "".to_string() }; - let mut context = Map::new(); + let context = Map::new(); if let Some(input) = args.input { context @@ -76,7 +76,7 @@ fn main() { let mut parser = TSParser::new(); parser.set_language(language()).unwrap(); - let mut interpreter = Interpreter::new(&mut context, &source).unwrap(); + let mut interpreter = Interpreter::new(context, &source).unwrap(); if args.interactive { loop { @@ -180,7 +180,7 @@ impl Highlighter for DustReadline { } fn run_cli_shell() { - let mut context = Map::new(); + let context = Map::new(); let mut rl: Editor = Editor::new().unwrap(); rl.set_helper(Some(DustReadline::new())); @@ -197,7 +197,7 @@ fn run_cli_shell() { rl.add_history_entry(line).unwrap(); - let eval_result = interpret_with_context(line, &mut context); + let eval_result = interpret_with_context(line, context.clone()); match eval_result { Ok(value) => println!("{value}"), diff --git a/test.txt b/test.txt deleted file mode 100644 index 8b13789..0000000 --- a/test.txt +++ /dev/null @@ -1 +0,0 @@ -