Improve Map interface; Clean up
This commit is contained in:
parent
37a9a37c72
commit
3c729bea6e
@ -27,9 +27,9 @@ impl AbstractTree for Use {
|
|||||||
|
|
||||||
fn run(&self, _source: &str, context: &Map) -> Result<Value> {
|
fn run(&self, _source: &str, context: &Map) -> Result<Value> {
|
||||||
let file_contents = read_to_string(&self.path)?;
|
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() {
|
for (key, (value, r#type)) in file_context.variables()?.iter() {
|
||||||
context.set(key.clone(), value.clone(), Some(r#type.clone()))?;
|
context.set(key.clone(), value.clone(), Some(r#type.clone()))?;
|
||||||
|
@ -18,9 +18,7 @@ use crate::{language, AbstractTree, Error, Map, Result, Root, Value};
|
|||||||
/// assert_eq!(interpret("1 + 2 + 3"), Ok(Value::Integer(6)));
|
/// assert_eq!(interpret("1 + 2 + 3"), Ok(Value::Integer(6)));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn interpret(source: &str) -> Result<Value> {
|
pub fn interpret(source: &str) -> Result<Value> {
|
||||||
let mut context = Map::new();
|
interpret_with_context(source, Map::new())
|
||||||
|
|
||||||
interpret_with_context(source, &mut context)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Interpret the given source code with the given context.
|
/// Interpret the given source code with the given context.
|
||||||
@ -29,7 +27,7 @@ pub fn interpret(source: &str) -> Result<Value> {
|
|||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use dust_lang::*;
|
/// # use dust_lang::*;
|
||||||
/// let mut context = Map::new();
|
/// let context = Map::new();
|
||||||
///
|
///
|
||||||
/// context.set("one".into(), 1.into(), None);
|
/// context.set("one".into(), 1.into(), None);
|
||||||
/// context.set("two".into(), 2.into(), None);
|
/// context.set("two".into(), 2.into(), None);
|
||||||
@ -38,11 +36,11 @@ pub fn interpret(source: &str) -> Result<Value> {
|
|||||||
/// let dust_code = "four = 4 one + two + three + four";
|
/// let dust_code = "four = 4 one + two + three + four";
|
||||||
///
|
///
|
||||||
/// assert_eq!(
|
/// assert_eq!(
|
||||||
/// interpret_with_context(dust_code, &mut context),
|
/// interpret_with_context(dust_code, context),
|
||||||
/// Ok(Value::Integer(10))
|
/// Ok(Value::Integer(10))
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn interpret_with_context(source: &str, context: &mut Map) -> Result<Value> {
|
pub fn interpret_with_context(source: &str, context: Map) -> Result<Value> {
|
||||||
let mut parser = Parser::new();
|
let mut parser = Parser::new();
|
||||||
parser.set_language(language())?;
|
parser.set_language(language())?;
|
||||||
|
|
||||||
@ -53,16 +51,16 @@ pub fn interpret_with_context(source: &str, context: &mut Map) -> Result<Value>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A source code interpreter for the Dust language.
|
/// A source code interpreter for the Dust language.
|
||||||
pub struct Interpreter<'c, 's> {
|
pub struct Interpreter<'s> {
|
||||||
parser: Parser,
|
parser: Parser,
|
||||||
context: &'c mut Map,
|
context: Map,
|
||||||
source: &'s str,
|
source: &'s str,
|
||||||
syntax_tree: Option<TSTree>,
|
syntax_tree: Option<TSTree>,
|
||||||
abstract_tree: Option<Root>,
|
abstract_tree: Option<Root>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'c, 's> Interpreter<'c, 's> {
|
impl<'s> Interpreter<'s> {
|
||||||
pub fn new(context: &'c mut Map, source: &'s str) -> Result<Self> {
|
pub fn new(context: Map, source: &'s str) -> Result<Self> {
|
||||||
let mut parser = Parser::new();
|
let mut parser = Parser::new();
|
||||||
|
|
||||||
parser.set_language(language())?;
|
parser.set_language(language())?;
|
||||||
|
@ -57,7 +57,7 @@ fn main() {
|
|||||||
"".to_string()
|
"".to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut context = Map::new();
|
let context = Map::new();
|
||||||
|
|
||||||
if let Some(input) = args.input {
|
if let Some(input) = args.input {
|
||||||
context
|
context
|
||||||
@ -76,7 +76,7 @@ fn main() {
|
|||||||
let mut parser = TSParser::new();
|
let mut parser = TSParser::new();
|
||||||
parser.set_language(language()).unwrap();
|
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 {
|
if args.interactive {
|
||||||
loop {
|
loop {
|
||||||
@ -180,7 +180,7 @@ impl Highlighter for DustReadline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run_cli_shell() {
|
fn run_cli_shell() {
|
||||||
let mut context = Map::new();
|
let context = Map::new();
|
||||||
let mut rl: Editor<DustReadline, DefaultHistory> = Editor::new().unwrap();
|
let mut rl: Editor<DustReadline, DefaultHistory> = Editor::new().unwrap();
|
||||||
|
|
||||||
rl.set_helper(Some(DustReadline::new()));
|
rl.set_helper(Some(DustReadline::new()));
|
||||||
@ -197,7 +197,7 @@ fn run_cli_shell() {
|
|||||||
|
|
||||||
rl.add_history_entry(line).unwrap();
|
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 {
|
match eval_result {
|
||||||
Ok(value) => println!("{value}"),
|
Ok(value) => println!("{value}"),
|
||||||
|
Loading…
Reference in New Issue
Block a user