Merge branch 'constants'

This commit is contained in:
Stephen Chung
2020-03-14 14:33:56 +08:00
12 changed files with 619 additions and 182 deletions

View File

@@ -101,8 +101,14 @@ impl<'e> Engine<'e> {
/// Compile a string into an AST.
pub fn compile(&self, input: &str) -> Result<AST, ParseError> {
self.compile_with_scope(&Scope::new(), input)
}
/// Compile a string into an AST using own scope.
/// The scope is useful for passing constants into the script for optimization.
pub fn compile_with_scope(&self, scope: &Scope, input: &str) -> Result<AST, ParseError> {
let tokens_stream = lex(input);
parse(&mut tokens_stream.peekable(), self.optimize)
parse(&mut tokens_stream.peekable(), scope, self.optimize)
}
fn read_file(path: PathBuf) -> Result<String, EvalAltResult> {
@@ -118,8 +124,20 @@ impl<'e> Engine<'e> {
/// Compile a file into an AST.
pub fn compile_file(&self, path: PathBuf) -> Result<AST, EvalAltResult> {
Self::read_file(path)
.and_then(|contents| self.compile(&contents).map_err(|err| err.into()))
self.compile_file_with_scope(&Scope::new(), path)
}
/// Compile a file into an AST using own scope.
/// The scope is useful for passing constants into the script for optimization.
pub fn compile_file_with_scope(
&self,
scope: &Scope,
path: PathBuf,
) -> Result<AST, EvalAltResult> {
Self::read_file(path).and_then(|contents| {
self.compile_with_scope(scope, &contents)
.map_err(|err| err.into())
})
}
/// Evaluate a file.
@@ -235,7 +253,7 @@ impl<'e> Engine<'e> {
) -> Result<(), EvalAltResult> {
let tokens_stream = lex(input);
let ast = parse(&mut tokens_stream.peekable(), self.optimize)
let ast = parse(&mut tokens_stream.peekable(), scope, self.optimize)
.map_err(EvalAltResult::ErrorParsing)?;
self.consume_ast_with_scope(scope, retain_functions, &ast)