Optimize in-place.

This commit is contained in:
Stephen Chung 2020-11-12 12:37:42 +08:00
parent 7b27dcdd62
commit 41c815f355
2 changed files with 322 additions and 368 deletions

View File

@ -1,6 +1,6 @@
//! Module implementing the AST optimizer. //! Module implementing the AST optimizer.
use crate::ast::{BinaryExpr, CustomExpr, Expr, FnCallExpr, ScriptFnDef, Stmt, AST}; use crate::ast::{Expr, ScriptFnDef, Stmt, AST};
use crate::dynamic::Dynamic; use crate::dynamic::Dynamic;
use crate::engine::{ use crate::engine::{
Engine, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_IS_DEF_FN, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, Engine, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_IS_DEF_FN, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT,
@ -10,7 +10,7 @@ use crate::fn_call::run_builtin_binary_op;
use crate::module::Module; use crate::module::Module;
use crate::parser::map_dynamic_to_expr; use crate::parser::map_dynamic_to_expr;
use crate::scope::Scope; use crate::scope::Scope;
use crate::token::{is_valid_identifier, NO_POS}; use crate::token::{is_valid_identifier, Position, NO_POS};
use crate::{calc_native_fn_hash, StaticVec}; use crate::{calc_native_fn_hash, StaticVec};
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
@ -19,6 +19,7 @@ use crate::ast::ReturnType;
use crate::stdlib::{ use crate::stdlib::{
boxed::Box, boxed::Box,
iter::empty, iter::empty,
mem,
string::{String, ToString}, string::{String, ToString},
vec, vec,
vec::Vec, vec::Vec,
@ -154,158 +155,45 @@ fn call_fn_with_constant_arguments(
.map(|(v, _)| v) .map(|(v, _)| v)
} }
/// Optimize a statement. /// Optimize a block of statements.
fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt { fn optimize_stmt_block(
match stmt { mut statements: Vec<Stmt>,
// expr op= expr pos: Position,
Stmt::Assignment(x, pos) => match x.0 { state: &mut State,
Expr::Variable(_) => { preserve_result: bool,
Stmt::Assignment(Box::new((x.0, x.1, optimize_expr(x.2, state))), pos) count_promote_as_dirty: bool,
} ) -> Stmt {
_ => Stmt::Assignment(
Box::new((optimize_expr(x.0, state), x.1, optimize_expr(x.2, state))),
pos,
),
},
// if false { if_block } -> Noop
Stmt::IfThenElse(Expr::False(pos), x, _) if x.1.is_none() => {
state.set_dirty();
Stmt::Noop(pos)
}
// if true { if_block } -> if_block
Stmt::IfThenElse(Expr::True(_), x, _) if x.1.is_none() => optimize_stmt(x.0, state, true),
// if expr { Noop }
Stmt::IfThenElse(condition, x, _) if x.1.is_none() && matches!(x.0, Stmt::Noop(_)) => {
state.set_dirty();
let pos = condition.position();
let expr = optimize_expr(condition, state);
if preserve_result {
// -> { expr, Noop }
let mut statements = Vec::new();
statements.push(Stmt::Expr(expr));
statements.push(x.0);
Stmt::Block(statements, pos)
} else {
// -> expr
Stmt::Expr(expr)
}
}
// if expr { if_block }
Stmt::IfThenElse(condition, x, pos) if x.1.is_none() => Stmt::IfThenElse(
optimize_expr(condition, state),
Box::new((optimize_stmt(x.0, state, true), None)),
pos,
),
// if false { if_block } else { else_block } -> else_block
Stmt::IfThenElse(Expr::False(_), x, _) if x.1.is_some() => {
optimize_stmt(x.1.unwrap(), state, true)
}
// if true { if_block } else { else_block } -> if_block
Stmt::IfThenElse(Expr::True(_), x, _) => optimize_stmt(x.0, state, true),
// if expr { if_block } else { else_block }
Stmt::IfThenElse(condition, x, pos) => Stmt::IfThenElse(
optimize_expr(condition, state),
Box::new((
optimize_stmt(x.0, state, true),
match optimize_stmt(x.1.unwrap(), state, true) {
Stmt::Noop(_) => None, // Noop -> no else block
stmt => Some(stmt),
},
)),
pos,
),
// while false { block } -> Noop
Stmt::While(Expr::False(pos), _, _) => {
state.set_dirty();
Stmt::Noop(pos)
}
// while true { block } -> loop { block }
Stmt::While(Expr::True(_), block, pos) => {
Stmt::Loop(Box::new(optimize_stmt(*block, state, false)), pos)
}
// while expr { block }
Stmt::While(condition, block, pos) => {
match optimize_stmt(*block, state, false) {
// while expr { break; } -> { expr; }
Stmt::Break(pos) => {
// Only a single break statement - turn into running the guard expression once
state.set_dirty();
let mut statements = Vec::new();
statements.push(Stmt::Expr(optimize_expr(condition, state)));
if preserve_result {
statements.push(Stmt::Noop(pos))
}
Stmt::Block(statements, pos)
}
// while expr { block }
stmt => Stmt::While(optimize_expr(condition, state), Box::new(stmt), pos),
}
}
// loop { block }
Stmt::Loop(block, pos) => match optimize_stmt(*block, state, false) {
// loop { break; } -> Noop
Stmt::Break(pos) => {
// Only a single break statement
state.set_dirty();
Stmt::Noop(pos)
}
// loop { block }
stmt => Stmt::Loop(Box::new(stmt), pos),
},
// for id in expr { block }
Stmt::For(iterable, x, pos) => {
let (var_name, block) = *x;
Stmt::For(
optimize_expr(iterable, state),
Box::new((var_name, optimize_stmt(block, state, false))),
pos,
)
}
// let id = expr;
Stmt::Let(name, Some(expr), export, pos) => {
Stmt::Let(name, Some(optimize_expr(expr, state)), export, pos)
}
// let id;
stmt @ Stmt::Let(_, None, _, _) => stmt,
// import expr as var;
#[cfg(not(feature = "no_module"))]
Stmt::Import(expr, alias, pos) => Stmt::Import(optimize_expr(expr, state), alias, pos),
// { block }
Stmt::Block(statements, pos) => {
let orig_len = statements.len(); // Original number of statements in the block, for change detection let orig_len = statements.len(); // Original number of statements in the block, for change detection
let orig_constants_len = state.constants.len(); // Original number of constants in the state, for restore later let orig_constants_len = state.constants.len(); // Original number of constants in the state, for restore later
// Optimize each statement in the block // Optimize each statement in the block
let mut result: Vec<_> = statements statements.iter_mut().for_each(|stmt| match stmt {
.into_iter()
.map(|stmt| match stmt {
// Add constant literals into the state // Add constant literals into the state
Stmt::Const(var_def, Some(expr), _, pos) if expr.is_literal() => { Stmt::Const(var_def, Some(expr), _, pos) if expr.is_literal() => {
state.set_dirty(); state.set_dirty();
state.push_constant(&var_def.name, expr); state.push_constant(&var_def.name, mem::take(expr));
Stmt::Noop(pos) // No need to keep constants *stmt = Stmt::Noop(*pos); // No need to keep constants
} }
Stmt::Const(var_def, None, _, pos) => { Stmt::Const(var_def, None, _, pos) => {
state.set_dirty(); state.set_dirty();
state.push_constant(&var_def.name, Expr::Unit(var_def.pos)); state.push_constant(&var_def.name, Expr::Unit(var_def.pos));
Stmt::Noop(pos) // No need to keep constants *stmt = Stmt::Noop(*pos); // No need to keep constants
} }
// Optimize the statement // Optimize the statement
stmt => optimize_stmt(stmt, state, preserve_result), _ => optimize_stmt(stmt, state, preserve_result),
}) });
.collect();
// Remove all raw expression statements that are pure except for the very last statement // Remove all raw expression statements that are pure except for the very last statement
let last_stmt = if preserve_result { result.pop() } else { None }; let last_stmt = if preserve_result {
statements.pop()
} else {
None
};
result.retain(|stmt| !stmt.is_pure()); statements.retain(|stmt| !stmt.is_pure());
if let Some(stmt) = last_stmt { if let Some(stmt) = last_stmt {
result.push(stmt); statements.push(stmt);
} }
// Remove all let/import statements at the end of a block - the new variables will go away anyway. // Remove all let/import statements at the end of a block - the new variables will go away anyway.
@ -313,15 +201,13 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt {
// otherwise there may be side effects. // otherwise there may be side effects.
let mut removed = false; let mut removed = false;
while let Some(expr) = result.pop() { while let Some(expr) = statements.pop() {
match expr { match expr {
Stmt::Let(_, expr, _, _) => { Stmt::Let(_, expr, _, _) => removed = expr.as_ref().map(Expr::is_pure).unwrap_or(true),
removed = expr.as_ref().map(Expr::is_pure).unwrap_or(true)
}
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Stmt::Import(expr, _, _) => removed = expr.is_pure(), Stmt::Import(expr, _, _) => removed = expr.is_pure(),
_ => { _ => {
result.push(expr); statements.push(expr);
break; break;
} }
} }
@ -329,23 +215,21 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt {
if preserve_result { if preserve_result {
if removed { if removed {
result.push(Stmt::Noop(pos)) statements.push(Stmt::Noop(pos))
} }
// Optimize all the statements again // Optimize all the statements again
result = result let num_statements = statements.len();
.into_iter() statements
.rev() .iter_mut()
.enumerate() .enumerate()
.map(|(i, stmt)| optimize_stmt(stmt, state, i == 0)) .for_each(|(i, stmt)| optimize_stmt(stmt, state, i == num_statements));
.rev()
.collect();
} }
// Remove everything following the the first return/throw // Remove everything following the the first return/throw
let mut dead_code = false; let mut dead_code = false;
result.retain(|stmt| { statements.retain(|stmt| {
if dead_code { if dead_code {
return false; return false;
} }
@ -359,80 +243,205 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt {
}); });
// Change detection // Change detection
if orig_len != result.len() { if orig_len != statements.len() {
state.set_dirty(); state.set_dirty();
} }
// Pop the stack and remove all the local constants // Pop the stack and remove all the local constants
state.restore_constants(orig_constants_len); state.restore_constants(orig_constants_len);
match &result[..] { match &statements[..] {
// No statements in block - change to No-op // No statements in block - change to No-op
[] => { [] => {
state.set_dirty(); state.set_dirty();
Stmt::Noop(pos) Stmt::Noop(pos)
} }
// Only one let statement - leave it alone // Only one let statement - leave it alone
[x] if matches!(x, Stmt::Let(_, _, _, _)) => Stmt::Block(result, pos), [x] if matches!(x, Stmt::Let(_, _, _, _)) => Stmt::Block(statements, pos),
// Only one import statement - leave it alone // Only one import statement - leave it alone
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
[x] if matches!(x, Stmt::Import(_, _, _)) => Stmt::Block(result, pos), [x] if matches!(x, Stmt::Import(_, _, _)) => Stmt::Block(statements, pos),
// Only one statement - promote // Only one statement - promote
[_] => { [_] => {
if count_promote_as_dirty {
state.set_dirty(); state.set_dirty();
result.remove(0)
} }
_ => Stmt::Block(result, pos), statements.remove(0)
} }
_ => Stmt::Block(statements, pos),
}
}
/// Optimize a statement.
fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) {
match stmt {
// expr op= expr
Stmt::Assignment(ref mut x, _) => match x.0 {
Expr::Variable(_) => optimize_expr(&mut x.2, state),
_ => {
optimize_expr(&mut x.0, state);
optimize_expr(&mut x.2, state);
}
},
// if false { if_block } -> Noop
Stmt::IfThenElse(Expr::False(pos), x, _) if x.1.is_none() => {
state.set_dirty();
*stmt = Stmt::Noop(*pos);
}
// if true { if_block } -> if_block
Stmt::IfThenElse(Expr::True(_), x, _) if x.1.is_none() => {
*stmt = mem::take(&mut x.0);
optimize_stmt(stmt, state, true);
}
// if expr { Noop }
Stmt::IfThenElse(ref mut condition, x, _)
if x.1.is_none() && matches!(x.0, Stmt::Noop(_)) =>
{
state.set_dirty();
let pos = condition.position();
let mut expr = mem::take(condition);
optimize_expr(&mut expr, state);
*stmt = if preserve_result {
// -> { expr, Noop }
let mut statements = Vec::new();
statements.push(Stmt::Expr(expr));
statements.push(mem::take(&mut x.0));
Stmt::Block(statements, pos)
} else {
// -> expr
Stmt::Expr(expr)
};
}
// if expr { if_block }
Stmt::IfThenElse(ref mut condition, ref mut x, _) if x.1.is_none() => {
optimize_expr(condition, state);
optimize_stmt(&mut x.0, state, true);
}
// if false { if_block } else { else_block } -> else_block
Stmt::IfThenElse(Expr::False(_), x, _) if x.1.is_some() => {
*stmt = mem::take(x.1.as_mut().unwrap());
optimize_stmt(stmt, state, true);
}
// if true { if_block } else { else_block } -> if_block
Stmt::IfThenElse(Expr::True(_), x, _) => {
*stmt = mem::take(&mut x.0);
optimize_stmt(stmt, state, true);
}
// if expr { if_block } else { else_block }
Stmt::IfThenElse(ref mut condition, ref mut x, _) => {
optimize_expr(condition, state);
optimize_stmt(&mut x.0, state, true);
if let Some(else_block) = x.1.as_mut() {
optimize_stmt(else_block, state, true);
match else_block {
Stmt::Noop(_) => x.1 = None, // Noop -> no else block
_ => (),
}
}
}
// while false { block } -> Noop
Stmt::While(Expr::False(pos), _, _) => {
state.set_dirty();
*stmt = Stmt::Noop(*pos)
}
// while true { block } -> loop { block }
Stmt::While(Expr::True(_), block, pos) => {
optimize_stmt(block, state, false);
*stmt = Stmt::Loop(Box::new(mem::take(block)), *pos)
}
// while expr { block }
Stmt::While(condition, block, _) => {
optimize_stmt(block, state, false);
optimize_expr(condition, state);
match **block {
// while expr { break; } -> { expr; }
Stmt::Break(pos) => {
// Only a single break statement - turn into running the guard expression once
state.set_dirty();
let mut statements = Vec::new();
statements.push(Stmt::Expr(mem::take(condition)));
if preserve_result {
statements.push(Stmt::Noop(pos))
}
*stmt = Stmt::Block(statements, pos);
}
_ => (),
}
}
// loop { block }
Stmt::Loop(block, _) => {
optimize_stmt(block, state, false);
match **block {
// loop { break; } -> Noop
Stmt::Break(pos) => {
// Only a single break statement
state.set_dirty();
*stmt = Stmt::Noop(pos)
}
_ => (),
}
}
// for id in expr { block }
Stmt::For(ref mut iterable, ref mut x, _) => {
optimize_expr(iterable, state);
optimize_stmt(&mut x.1, state, false);
}
// let id = expr;
Stmt::Let(_, Some(ref mut expr), _, _) => optimize_expr(expr, state),
// let id;
Stmt::Let(_, None, _, _) => (),
// import expr as var;
#[cfg(not(feature = "no_module"))]
Stmt::Import(ref mut expr, _, _) => optimize_expr(expr, state),
// { block }
Stmt::Block(statements, pos) => {
*stmt = optimize_stmt_block(mem::take(statements), *pos, state, preserve_result, true);
} }
// try { block } catch ( var ) { block } // try { block } catch ( var ) { block }
Stmt::TryCatch(x, _, _) if x.0.is_pure() => { Stmt::TryCatch(x, _, _) if x.0.is_pure() => {
// If try block is pure, there will never be any exceptions // If try block is pure, there will never be any exceptions
state.set_dirty(); state.set_dirty();
let pos = x.0.position(); let pos = x.0.position();
let mut statements = match optimize_stmt(x.0, state, preserve_result) { optimize_stmt(&mut x.0, state, preserve_result);
let mut statements = match mem::take(&mut x.0) {
Stmt::Block(statements, _) => statements, Stmt::Block(statements, _) => statements,
stmt => vec![stmt], stmt => vec![stmt],
}; };
statements.push(Stmt::Noop(pos)); statements.push(Stmt::Noop(pos));
Stmt::Block(statements, pos) *stmt = Stmt::Block(statements, pos);
} }
// try { block } catch ( var ) { block } // try { block } catch ( var ) { block }
Stmt::TryCatch(x, try_pos, catch_pos) => { Stmt::TryCatch(ref mut x, _, _) => {
let (try_block, var_name, catch_block) = *x; optimize_stmt(&mut x.0, state, false);
Stmt::TryCatch( optimize_stmt(&mut x.2, state, false);
Box::new((
optimize_stmt(try_block, state, false),
var_name,
optimize_stmt(catch_block, state, false),
)),
try_pos,
catch_pos,
)
} }
// {} // {}
Stmt::Expr(Expr::Stmt(x, pos)) if x.is_empty() => { Stmt::Expr(Expr::Stmt(x, pos)) if x.is_empty() => {
state.set_dirty(); state.set_dirty();
Stmt::Noop(pos) *stmt = Stmt::Noop(*pos);
} }
// {...}; // {...};
Stmt::Expr(Expr::Stmt(x, pos)) => { Stmt::Expr(Expr::Stmt(x, pos)) => {
state.set_dirty(); state.set_dirty();
Stmt::Block(x.into_vec(), pos) *stmt = Stmt::Block(mem::take(x).into_vec(), *pos);
} }
// expr; // expr;
Stmt::Expr(expr) => Stmt::Expr(optimize_expr(expr, state)), Stmt::Expr(ref mut expr) => optimize_expr(expr, state),
// return expr; // return expr;
Stmt::ReturnWithVal(ret, Some(expr), pos) => { Stmt::ReturnWithVal(_, Some(ref mut expr), _) => optimize_expr(expr, state),
Stmt::ReturnWithVal(ret, Some(optimize_expr(expr, state)), pos)
}
// All other statements - skip // All other statements - skip
stmt => stmt, _ => (),
} }
} }
/// Optimize an expression. /// Optimize an expression.
fn optimize_expr(expr: Expr, state: &mut State) -> Expr { fn optimize_expr(expr: &mut Expr, state: &mut State) {
// These keywords are handled specially // These keywords are handled specially
const DONT_EVAL_KEYWORDS: &[&str] = &[ const DONT_EVAL_KEYWORDS: &[&str] = &[
KEYWORD_PRINT, // side effects KEYWORD_PRINT, // side effects
@ -444,296 +453,253 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
match expr { match expr {
// expr - do not promote because there is a reason it is wrapped in an `Expr::Expr` // expr - do not promote because there is a reason it is wrapped in an `Expr::Expr`
Expr::Expr(x) => Expr::Expr(Box::new(optimize_expr(*x, state))), Expr::Expr(x) => optimize_expr(x, state),
// {} // {}
Expr::Stmt(x, pos) if x.is_empty() => { Expr::Stmt(x, pos) if x.is_empty() => { state.set_dirty(); *expr = Expr::Unit(*pos) }
state.set_dirty(); // { stmt; ... } - do not count promotion as dirty because it gets turned back into an array
Expr::Unit(pos) Expr::Stmt(x, pos) => match optimize_stmt_block(mem::take(x).into_vec(), *pos, state, true, false) {
} // {}
Stmt::Noop(_) => { state.set_dirty(); *expr = Expr::Unit(*pos); }
// { stmt, .. }
Stmt::Block(statements, _) => *x = Box::new(statements.into()),
// { expr }
Stmt::Expr(inner) => { state.set_dirty(); *expr = inner; }
// { stmt } // { stmt }
Expr::Stmt(mut x, pos) if x.len() == 1 => match x.pop().unwrap() { stmt => x.push(stmt),
// {} -> ()
Stmt::Noop(_) => {
state.set_dirty();
Expr::Unit(pos)
} }
// { expr } -> expr
Stmt::Expr(expr) => {
state.set_dirty();
optimize_expr(expr, state)
}
// { stmt }
stmt => Expr::Stmt(Box::new(vec![optimize_stmt(stmt, state, true)].into()), pos)
}
// { stmt; ... }
Expr::Stmt(x, pos) => Expr::Stmt(Box::new(
x.into_iter().map(|stmt| optimize_stmt(stmt, state, true)).collect(),
), pos),
// lhs.rhs // lhs.rhs
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Expr::Dot(x, dot_pos) => match (x.lhs, x.rhs) { Expr::Dot(x, _) => match (&mut x.lhs, &mut x.rhs) {
// map.string // map.string
(Expr::Map(m, pos), Expr::Property(p)) if m.iter().all(|(_, x)| x.is_pure()) => { (Expr::Map(m, pos), Expr::Property(p)) if m.iter().all(|(_, x)| x.is_pure()) => {
let prop = &p.1.name; let prop = &p.1.name;
// Map literal where everything is pure - promote the indexed item. // Map literal where everything is pure - promote the indexed item.
// All other items can be thrown away. // All other items can be thrown away.
state.set_dirty(); state.set_dirty();
m.into_iter().find(|(x, _)| &x.name == prop) *expr = mem::take(m).into_iter().find(|(x, _)| &x.name == prop)
.map(|(_, mut expr)| { expr.set_position(pos); expr }) .map(|(_, mut expr)| { expr.set_position(*pos); expr })
.unwrap_or_else(|| Expr::Unit(pos)) .unwrap_or_else(|| Expr::Unit(*pos));
} }
// var.rhs // var.rhs
(lhs @ Expr::Variable(_), rhs) => Expr::Dot(Box::new(BinaryExpr { (Expr::Variable(_), rhs) => optimize_expr(rhs, state),
lhs,
rhs: optimize_expr(rhs, state),
}), dot_pos),
// lhs.rhs // lhs.rhs
(lhs, rhs) => Expr::Dot(Box::new(BinaryExpr { (lhs, rhs) => { optimize_expr(lhs, state); optimize_expr(rhs, state); }
lhs: optimize_expr(lhs, state),
rhs: optimize_expr(rhs, state),
}), dot_pos)
} }
// lhs[rhs] // lhs[rhs]
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Expr::Index(x, idx_pos) => match (x.lhs, x.rhs) { Expr::Index(x, _) => match (&mut x.lhs, &mut x.rhs) {
// array[int] // array[int]
(Expr::Array(mut a, pos), Expr::IntegerConstant(i, _)) (Expr::Array(a, pos), Expr::IntegerConstant(i, _))
if i >= 0 && (i as usize) < a.len() && a.iter().all(Expr::is_pure) => if *i >= 0 && (*i as usize) < a.len() && a.iter().all(Expr::is_pure) =>
{ {
// Array literal where everything is pure - promote the indexed item. // Array literal where everything is pure - promote the indexed item.
// All other items can be thrown away. // All other items can be thrown away.
state.set_dirty(); state.set_dirty();
let mut expr = a.remove(i as usize); let mut result = a.remove(*i as usize);
expr.set_position(pos); result.set_position(*pos);
expr *expr = result;
} }
// map[string] // map[string]
(Expr::Map(m, pos), Expr::StringConstant(s)) if m.iter().all(|(_, x)| x.is_pure()) => { (Expr::Map(m, pos), Expr::StringConstant(s)) if m.iter().all(|(_, x)| x.is_pure()) => {
// Map literal where everything is pure - promote the indexed item. // Map literal where everything is pure - promote the indexed item.
// All other items can be thrown away. // All other items can be thrown away.
state.set_dirty(); state.set_dirty();
m.into_iter().find(|(x, _)| x.name == s.name) *expr = mem::take(m).into_iter().find(|(x, _)| x.name == s.name)
.map(|(_, mut expr)| { expr.set_position(pos); expr }) .map(|(_, mut expr)| { expr.set_position(*pos); expr })
.unwrap_or_else(|| Expr::Unit(pos)) .unwrap_or_else(|| Expr::Unit(*pos));
} }
// string[int] // string[int]
(Expr::StringConstant(s), Expr::IntegerConstant(i, _)) if i >= 0 && (i as usize) < s.name.chars().count() => { (Expr::StringConstant(s), Expr::IntegerConstant(i, _)) if *i >= 0 && (*i as usize) < s.name.chars().count() => {
// String literal indexing - get the character // String literal indexing - get the character
state.set_dirty(); state.set_dirty();
Expr::CharConstant(s.name.chars().nth(i as usize).unwrap(), s.pos) *expr = Expr::CharConstant(s.name.chars().nth(*i as usize).unwrap(), s.pos);
} }
// var[rhs] // var[rhs]
(lhs @ Expr::Variable(_), rhs) => Expr::Index(Box::new(BinaryExpr { (Expr::Variable(_), rhs) => optimize_expr(rhs, state),
lhs,
rhs: optimize_expr(rhs, state),
}), idx_pos),
// lhs[rhs] // lhs[rhs]
(lhs, rhs) => Expr::Index(Box::new(BinaryExpr { (lhs, rhs) => { optimize_expr(lhs, state); optimize_expr(rhs, state); }
lhs: optimize_expr(lhs, state),
rhs: optimize_expr(rhs, state),
}), idx_pos),
}, },
// [ items .. ] // [ items .. ]
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Expr::Array(a, pos) => Expr::Array(Box::new(a Expr::Array(a, _) => a.iter_mut().for_each(|expr| optimize_expr(expr, state)),
.into_iter().map(|expr| optimize_expr(expr, state)) // #{ key:value, .. }
.collect()), pos),
// [ items .. ]
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Expr::Map(m, pos) => Expr::Map(Box::new(m Expr::Map(m, _) => m.iter_mut().for_each(|(_, expr)| optimize_expr(expr, state)),
.into_iter().map(|(key, expr)| (key, optimize_expr(expr, state)))
.collect()), pos),
// lhs in rhs // lhs in rhs
Expr::In(x, in_pos) => match (x.lhs, x.rhs) { Expr::In(x, _) => match (&mut x.lhs, &mut x.rhs) {
// "xxx" in "xxxxx" // "xxx" in "xxxxx"
(Expr::StringConstant(a), Expr::StringConstant(b)) => { (Expr::StringConstant(a), Expr::StringConstant(b)) => {
state.set_dirty(); state.set_dirty();
if b.name.contains(a.name.as_str()) { Expr::True(a.pos) } else { Expr::False(a.pos) } *expr = if b.name.contains(a.name.as_str()) { Expr::True(a.pos) } else { Expr::False(a.pos) };
} }
// 'x' in "xxxxx" // 'x' in "xxxxx"
(Expr::CharConstant(a, pos), Expr::StringConstant(b)) => { (Expr::CharConstant(a, pos), Expr::StringConstant(b)) => {
state.set_dirty(); state.set_dirty();
if b.name.contains(a) { Expr::True(pos) } else { Expr::False(pos) } *expr = if b.name.contains(*a) { Expr::True(*pos) } else { Expr::False(*pos) };
} }
// "xxx" in #{...} // "xxx" in #{...}
(Expr::StringConstant(a), Expr::Map(b, _)) => { (Expr::StringConstant(a), Expr::Map(b, _)) => {
state.set_dirty(); state.set_dirty();
if b.iter().find(|(x, _)| x.name == a.name).is_some() { *expr = if b.iter().find(|(x, _)| x.name == a.name).is_some() {
Expr::True(a.pos) Expr::True(a.pos)
} else { } else {
Expr::False(a.pos) Expr::False(a.pos)
} };
} }
// 'x' in #{...} // 'x' in #{...}
(Expr::CharConstant(a, pos), Expr::Map(b, _)) => { (Expr::CharConstant(a, pos), Expr::Map(b, _)) => {
state.set_dirty(); state.set_dirty();
let ch = a.to_string(); let ch = a.to_string();
if b.iter().find(|(x, _)| x.name == &ch).is_some() { *expr = if b.iter().find(|(x, _)| x.name == &ch).is_some() {
Expr::True(pos) Expr::True(*pos)
} else { } else {
Expr::False(pos) Expr::False(*pos)
} };
} }
// lhs in rhs // lhs in rhs
(lhs, rhs) => Expr::In(Box::new(BinaryExpr { (lhs, rhs) => { optimize_expr(lhs, state); optimize_expr(rhs, state); }
lhs: optimize_expr(lhs, state),
rhs: optimize_expr(rhs, state),
}), in_pos),
}, },
// lhs && rhs // lhs && rhs
Expr::And(x, and_pos) => match (x.lhs, x.rhs) { Expr::And(x, _) => match (&mut x.lhs, &mut x.rhs) {
// true && rhs -> rhs // true && rhs -> rhs
(Expr::True(_), rhs) => { (Expr::True(_), rhs) => {
state.set_dirty(); state.set_dirty();
rhs optimize_expr(rhs, state);
*expr = mem::take(rhs);
} }
// false && rhs -> false // false && rhs -> false
(Expr::False(pos), _) => { (Expr::False(pos), _) => {
state.set_dirty(); state.set_dirty();
Expr::False(pos) *expr = Expr::False(*pos);
} }
// lhs && true -> lhs // lhs && true -> lhs
(lhs, Expr::True(_)) => { (lhs, Expr::True(_)) => {
state.set_dirty(); state.set_dirty();
optimize_expr(lhs, state) optimize_expr(lhs, state);
*expr = mem::take(lhs);
} }
// lhs && rhs // lhs && rhs
(lhs, rhs) => Expr::And(Box::new(BinaryExpr { (lhs, rhs) => { optimize_expr(lhs, state); optimize_expr(rhs, state); }
lhs: optimize_expr(lhs, state),
rhs: optimize_expr(rhs, state),
}), and_pos),
}, },
// lhs || rhs // lhs || rhs
Expr::Or(x, or_pos) => match (x.lhs, x.rhs) { Expr::Or(ref mut x, _) => match (&mut x.lhs, &mut x.rhs) {
// false || rhs -> rhs // false || rhs -> rhs
(Expr::False(_), rhs) => { (Expr::False(_), rhs) => {
state.set_dirty(); state.set_dirty();
rhs optimize_expr(rhs, state);
*expr = mem::take(rhs);
} }
// true || rhs -> true // true || rhs -> true
(Expr::True(pos), _) => { (Expr::True(pos), _) => {
state.set_dirty(); state.set_dirty();
Expr::True(pos) *expr = Expr::True(*pos);
} }
// lhs || false // lhs || false
(lhs, Expr::False(_)) => { (lhs, Expr::False(_)) => {
state.set_dirty(); state.set_dirty();
optimize_expr(lhs, state) optimize_expr(lhs, state);
*expr = mem::take(lhs);
} }
// lhs || rhs // lhs || rhs
(lhs, rhs) => Expr::Or(Box::new(BinaryExpr { (lhs, rhs) => { optimize_expr(lhs, state); optimize_expr(rhs, state); }
lhs: optimize_expr(lhs, state),
rhs: optimize_expr(rhs, state),
}), or_pos),
}, },
// Do not call some special keywords // Do not call some special keywords
Expr::FnCall(mut x, pos) if DONT_EVAL_KEYWORDS.contains(&x.name.as_ref()) => { Expr::FnCall(x, _) if DONT_EVAL_KEYWORDS.contains(&x.name.as_ref()) => {
x.args = x.args.into_iter().map(|a| optimize_expr(a, state)).collect(); x.args.iter_mut().for_each(|a| optimize_expr(a, state));
Expr::FnCall(x, pos)
} }
// Call built-in operators // Call built-in operators
Expr::FnCall(mut x, pos) Expr::FnCall(x, pos)
if x.namespace.is_none() // Non-qualified if x.namespace.is_none() // Non-qualified
&& state.optimization_level == OptimizationLevel::Simple // simple optimizations && state.optimization_level == OptimizationLevel::Simple // simple optimizations
&& x.args.len() == 2 // binary call && x.args.len() == 2 // binary call
&& x.args.iter().all(Expr::is_constant) // all arguments are constants && x.args.iter().all(Expr::is_constant) // all arguments are constants
&& !is_valid_identifier(x.name.chars()) // cannot be scripted && !is_valid_identifier(x.name.chars()) // cannot be scripted
=> { => {
let FnCallExpr { name, args, .. } = x.as_mut(); let arg_values: StaticVec<_> = x.args.iter().map(|e| e.get_constant_value().unwrap()).collect();
let arg_values: StaticVec<_> = args.iter().map(|e| e.get_constant_value().unwrap()).collect();
let arg_types: StaticVec<_> = arg_values.iter().map(Dynamic::type_id).collect(); let arg_types: StaticVec<_> = arg_values.iter().map(Dynamic::type_id).collect();
// Search for overloaded operators (can override built-in). // Search for overloaded operators (can override built-in).
if !state.engine.has_override_by_name_and_arguments(state.lib, name, arg_types.as_ref(), false) { if !state.engine.has_override_by_name_and_arguments(state.lib, x.name.as_ref(), arg_types.as_ref(), false) {
if let Some(expr) = run_builtin_binary_op(name, &arg_values[0], &arg_values[1]) if let Some(result) = run_builtin_binary_op(x.name.as_ref(), &arg_values[0], &arg_values[1])
.ok().flatten() .ok().flatten()
.and_then(|result| map_dynamic_to_expr(result, pos)) .and_then(|result| map_dynamic_to_expr(result, *pos))
{ {
state.set_dirty(); state.set_dirty();
return expr; *expr = result;
return;
} }
} }
x.args = x.args.into_iter().map(|a| optimize_expr(a, state)).collect(); x.args.iter_mut().for_each(|a| optimize_expr(a, state));
Expr::FnCall(x, pos)
} }
// Eagerly call functions // Eagerly call functions
Expr::FnCall(mut x, pos) Expr::FnCall(x, pos)
if x.namespace.is_none() // Non-qualified if x.namespace.is_none() // Non-qualified
&& state.optimization_level == OptimizationLevel::Full // full optimizations && state.optimization_level == OptimizationLevel::Full // full optimizations
&& x.args.iter().all(Expr::is_constant) // all arguments are constants && x.args.iter().all(Expr::is_constant) // all arguments are constants
=> { => {
let FnCallExpr { name, args, def_value, .. } = x.as_mut();
// First search for script-defined functions (can override built-in) // First search for script-defined functions (can override built-in)
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
let has_script_fn = state.lib.iter().any(|&m| m.get_script_fn(name, args.len(), false).is_some()); let has_script_fn = state.lib.iter().any(|&m| m.get_script_fn(x.name.as_ref(), x.args.len(), false).is_some());
#[cfg(feature = "no_function")] #[cfg(feature = "no_function")]
let has_script_fn = false; let has_script_fn = false;
if !has_script_fn { if !has_script_fn {
let mut arg_values: StaticVec<_> = args.iter().map(|e| e.get_constant_value().unwrap()).collect(); let mut arg_values: StaticVec<_> = x.args.iter().map(|e| e.get_constant_value().unwrap()).collect();
// Save the typename of the first argument if it is `type_of()` // Save the typename of the first argument if it is `type_of()`
// This is to avoid `call_args` being passed into the closure // This is to avoid `call_args` being passed into the closure
let arg_for_type_of = if name == KEYWORD_TYPE_OF && arg_values.len() == 1 { let arg_for_type_of = if x.name == KEYWORD_TYPE_OF && arg_values.len() == 1 {
state.engine.map_type_name(arg_values[0].type_name()) state.engine.map_type_name(arg_values[0].type_name())
} else { } else {
"" ""
}; };
if let Some(expr) = call_fn_with_constant_arguments(&state, name, arg_values.as_mut()) if let Some(result) = call_fn_with_constant_arguments(&state, x.name.as_ref(), arg_values.as_mut())
.or_else(|| { .or_else(|| {
if !arg_for_type_of.is_empty() { if !arg_for_type_of.is_empty() {
// Handle `type_of()` // Handle `type_of()`
Some(arg_for_type_of.to_string().into()) Some(arg_for_type_of.to_string().into())
} else { } else {
// Otherwise use the default value, if any // Otherwise use the default value, if any
def_value.map(|v| v.into()) x.def_value.map(|v| v.into())
} }
}) })
.and_then(|result| map_dynamic_to_expr(result, pos)) .and_then(|result| map_dynamic_to_expr(result, *pos))
{ {
state.set_dirty(); state.set_dirty();
return expr; *expr = result;
return;
} }
} }
x.args = x.args.into_iter().map(|a| optimize_expr(a, state)).collect(); x.args.iter_mut().for_each(|a| optimize_expr(a, state));
Expr::FnCall(x, pos)
} }
// id(args ..) -> optimize function call arguments // id(args ..) -> optimize function call arguments
Expr::FnCall(mut x, pos) => { Expr::FnCall(x, _) => x.args.iter_mut().for_each(|a| optimize_expr(a, state)),
x.args = x.args.into_iter().map(|a| optimize_expr(a, state)).collect();
Expr::FnCall(x, pos)
}
// constant-name // constant-name
Expr::Variable(x) if x.1.is_none() && state.contains_constant(&x.3.name) => { Expr::Variable(x) if x.1.is_none() && state.contains_constant(&x.3.name) => {
state.set_dirty(); state.set_dirty();
// Replace constant with value // Replace constant with value
let mut expr = state.find_constant(&x.3.name).unwrap().clone(); let mut result = state.find_constant(&x.3.name).unwrap().clone();
expr.set_position(x.3.pos); result.set_position(x.3.pos);
expr *expr = result;
} }
// Custom syntax // Custom syntax
Expr::Custom(x, pos) => Expr::Custom(Box::new(CustomExpr { Expr::Custom(x, _) => x.keywords.iter_mut().for_each(|expr| optimize_expr(expr, state)),
keywords: x.keywords.into_iter().map(|expr| optimize_expr(expr, state)).collect(),
..*x
}), pos),
// All other expressions - skip // All other expressions - skip
expr => expr, _ => (),
} }
} }
@ -773,32 +739,25 @@ fn optimize(
let num_statements = result.len(); let num_statements = result.len();
result = result result.iter_mut().enumerate().for_each(|(i, stmt)| {
.into_iter()
.enumerate()
.map(|(i, stmt)| {
match stmt { match stmt {
Stmt::Const(var_def, Some(expr), export, pos) => { Stmt::Const(var_def, expr, _, _) if expr.is_some() => {
// Load constants // Load constants
let expr = optimize_expr(expr, &mut state); let value_expr = expr.as_mut().unwrap();
optimize_expr(value_expr, &mut state);
if expr.is_literal() { if value_expr.is_literal() {
state.push_constant(&var_def.name, expr.clone()); state.push_constant(&var_def.name, value_expr.clone());
} }
// Keep it in the global scope // Keep it in the global scope
if expr.is_unit() { if value_expr.is_unit() {
state.set_dirty(); state.set_dirty();
Stmt::Const(var_def, None, export, pos) *expr = None;
} else {
Stmt::Const(var_def, Some(expr), export, pos)
} }
} }
Stmt::Const(ref var_def, None, _, _) => { Stmt::Const(var_def, None, _, _) => {
state.push_constant(&var_def.name, Expr::Unit(var_def.pos)); state.push_constant(&var_def.name, Expr::Unit(var_def.pos));
// Keep it in the global scope
stmt
} }
_ => { _ => {
// Keep all variable declarations at this level // Keep all variable declarations at this level
@ -809,11 +768,10 @@ fn optimize(
Stmt::Import(_, _, _) => true, Stmt::Import(_, _, _) => true,
_ => i == num_statements - 1, _ => i == num_statements - 1,
}; };
optimize_stmt(stmt, &mut state, keep) optimize_stmt(stmt, &mut state, keep);
} }
} }
}) });
.collect();
if !state.is_dirty() { if !state.is_dirty() {
break; break;

View File

@ -40,7 +40,6 @@ fn test_bool_op_short_circuit() -> Result<(), Box<EvalAltResult>> {
engine.eval::<bool>( engine.eval::<bool>(
r" r"
let x = true; let x = true;
x || { throw; }; x || { throw; };
" "
)?, )?,
@ -51,7 +50,6 @@ fn test_bool_op_short_circuit() -> Result<(), Box<EvalAltResult>> {
engine.eval::<bool>( engine.eval::<bool>(
r" r"
let x = false; let x = false;
x && { throw; }; x && { throw; };
" "
)?, )?,
@ -69,7 +67,6 @@ fn test_bool_op_no_short_circuit1() {
.eval::<bool>( .eval::<bool>(
r" r"
let x = true; let x = true;
x | { throw; } x | { throw; }
" "
) )
@ -84,7 +81,6 @@ fn test_bool_op_no_short_circuit2() {
.eval::<bool>( .eval::<bool>(
r" r"
let x = false; let x = false;
x & { throw; } x & { throw; }
" "
) )