Improve Interpreter API
This commit is contained in:
parent
3c729bea6e
commit
43ee989eec
@ -44,23 +44,22 @@ 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())?;
|
||||||
|
|
||||||
let mut interpreter = Interpreter::new(context, source)?;
|
let mut interpreter = Interpreter::new(context)?;
|
||||||
let value = interpreter.run()?;
|
let value = interpreter.run(source)?;
|
||||||
|
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A source code interpreter for the Dust language.
|
/// A source code interpreter for the Dust language.
|
||||||
pub struct Interpreter<'s> {
|
pub struct Interpreter {
|
||||||
parser: Parser,
|
parser: Parser,
|
||||||
context: Map,
|
context: Map,
|
||||||
source: &'s str,
|
|
||||||
syntax_tree: Option<TSTree>,
|
syntax_tree: Option<TSTree>,
|
||||||
abstract_tree: Option<Root>,
|
abstract_tree: Option<Root>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s> Interpreter<'s> {
|
impl Interpreter {
|
||||||
pub fn new(context: Map, source: &'s str) -> Result<Self> {
|
pub fn new(context: Map) -> Result<Self> {
|
||||||
let mut parser = Parser::new();
|
let mut parser = Parser::new();
|
||||||
|
|
||||||
parser.set_language(language())?;
|
parser.set_language(language())?;
|
||||||
@ -68,25 +67,20 @@ impl<'s> Interpreter<'s> {
|
|||||||
Ok(Interpreter {
|
Ok(Interpreter {
|
||||||
parser,
|
parser,
|
||||||
context,
|
context,
|
||||||
source,
|
|
||||||
syntax_tree: None,
|
syntax_tree: None,
|
||||||
abstract_tree: None,
|
abstract_tree: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_source(&mut self, source: &'s str) {
|
pub fn parse_only(&mut self, source: &str) {
|
||||||
self.source = source;
|
self.syntax_tree = self.parser.parse(source, self.syntax_tree.as_ref());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_only(&mut self) {
|
pub fn run(&mut self, source: &str) -> Result<Value> {
|
||||||
self.syntax_tree = self.parser.parse(self.source, self.syntax_tree.as_ref());
|
self.syntax_tree = self.parser.parse(source, self.syntax_tree.as_ref());
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(&mut self) -> Result<Value> {
|
|
||||||
self.syntax_tree = self.parser.parse(self.source, self.syntax_tree.as_ref());
|
|
||||||
self.abstract_tree = if let Some(syntax_tree) = &self.syntax_tree {
|
self.abstract_tree = if let Some(syntax_tree) = &self.syntax_tree {
|
||||||
Some(Root::from_syntax_node(
|
Some(Root::from_syntax_node(
|
||||||
self.source,
|
source,
|
||||||
syntax_tree.root_node(),
|
syntax_tree.root_node(),
|
||||||
&self.context,
|
&self.context,
|
||||||
)?)
|
)?)
|
||||||
@ -95,7 +89,7 @@ impl<'s> Interpreter<'s> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Some(abstract_tree) = &self.abstract_tree {
|
if let Some(abstract_tree) = &self.abstract_tree {
|
||||||
abstract_tree.run(self.source, &self.context)
|
abstract_tree.run(source, &self.context)
|
||||||
} else {
|
} else {
|
||||||
Ok(Value::Option(None))
|
Ok(Value::Option(None))
|
||||||
}
|
}
|
||||||
|
18
src/main.rs
18
src/main.rs
@ -34,10 +34,6 @@ struct Args {
|
|||||||
#[arg(short = 't', long = "tree")]
|
#[arg(short = 't', long = "tree")]
|
||||||
show_syntax_tree: bool,
|
show_syntax_tree: bool,
|
||||||
|
|
||||||
/// Launch in interactive mode.
|
|
||||||
#[arg(short = 'n', long)]
|
|
||||||
interactive: bool,
|
|
||||||
|
|
||||||
/// Location of the file to run.
|
/// Location of the file to run.
|
||||||
path: Option<String>,
|
path: Option<String>,
|
||||||
}
|
}
|
||||||
@ -76,23 +72,15 @@ 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(context, &source).unwrap();
|
let mut interpreter = Interpreter::new(context).unwrap();
|
||||||
|
|
||||||
if args.interactive {
|
|
||||||
loop {
|
|
||||||
let result = interpreter.run();
|
|
||||||
|
|
||||||
println!("{result:?}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.show_syntax_tree {
|
if args.show_syntax_tree {
|
||||||
interpreter.parse_only();
|
interpreter.parse_only(&source);
|
||||||
|
|
||||||
println!("{}", interpreter.syntax_tree().unwrap());
|
println!("{}", interpreter.syntax_tree().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let eval_result = interpreter.run();
|
let eval_result = interpreter.run(&source);
|
||||||
|
|
||||||
match eval_result {
|
match eval_result {
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user