Put externals in ScriptFnDef.
This commit is contained in:
parent
721c578407
commit
691e04292f
@ -5,10 +5,10 @@ use crate::calc_fn_hash;
|
|||||||
use crate::engine::{
|
use crate::engine::{
|
||||||
Engine, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_PRINT, KEYWORD_TYPE_OF,
|
Engine, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_PRINT, KEYWORD_TYPE_OF,
|
||||||
};
|
};
|
||||||
|
use crate::fn_native::FnPtr;
|
||||||
use crate::module::Module;
|
use crate::module::Module;
|
||||||
use crate::parser::{map_dynamic_to_expr, Expr, ScriptFnDef, Stmt, AST};
|
use crate::parser::{map_dynamic_to_expr, Expr, ScriptFnDef, Stmt, AST};
|
||||||
use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
|
use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
|
||||||
use crate::token::is_valid_identifier;
|
|
||||||
use crate::utils::StaticVec;
|
use crate::utils::StaticVec;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
@ -19,6 +19,7 @@ use crate::parser::CustomExpr;
|
|||||||
|
|
||||||
use crate::stdlib::{
|
use crate::stdlib::{
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
|
convert::TryFrom,
|
||||||
iter::empty,
|
iter::empty,
|
||||||
string::{String, ToString},
|
string::{String, ToString},
|
||||||
vec,
|
vec,
|
||||||
@ -418,7 +419,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
|
|||||||
// All other items can be thrown away.
|
// All other items can be thrown away.
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
let pos = m.1;
|
let pos = m.1;
|
||||||
m.0.into_iter().find(|((name, _), _)| name.as_str() == prop.as_str())
|
m.0.into_iter().find(|((name, _), _)| 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))
|
||||||
}
|
}
|
||||||
@ -495,7 +496,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
|
|||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
let ch = a.0.to_string();
|
let ch = a.0.to_string();
|
||||||
|
|
||||||
if b.0.iter().find(|((name, _), _)| name.as_str() == ch.as_str()).is_some() {
|
if b.0.iter().find(|((name, _), _)| name == &ch).is_some() {
|
||||||
Expr::True(a.1)
|
Expr::True(a.1)
|
||||||
} else {
|
} else {
|
||||||
Expr::False(a.1)
|
Expr::False(a.1)
|
||||||
@ -558,9 +559,14 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
|
|||||||
&& x.3.len() == 1
|
&& x.3.len() == 1
|
||||||
&& matches!(x.3[0], Expr::StringConstant(_))
|
&& matches!(x.3[0], Expr::StringConstant(_))
|
||||||
=> {
|
=> {
|
||||||
match &x.3[0] {
|
if let Expr::StringConstant(s) = &x.3[0] {
|
||||||
Expr::StringConstant(s) if is_valid_identifier(s.0.chars()) => Expr::FnPointer(s.clone()),
|
if let Ok(fn_ptr) = FnPtr::try_from(s.0.as_str()) {
|
||||||
_ => Expr::FnCall(x)
|
Expr::FnPointer(Box::new((fn_ptr.take_data().0, s.1)))
|
||||||
|
} else {
|
||||||
|
Expr::FnCall(x)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -578,7 +584,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
|
|||||||
let _has_script_fn = state.lib.iter_fn().find(|(_, _, _, f)| {
|
let _has_script_fn = state.lib.iter_fn().find(|(_, _, _, f)| {
|
||||||
if !f.is_script() { return false; }
|
if !f.is_script() { return false; }
|
||||||
let fn_def = f.get_fn_def();
|
let fn_def = f.get_fn_def();
|
||||||
fn_def.name.as_str() == name && (args.len()..=args.len() + 1).contains(&fn_def.params.len())
|
fn_def.name == name && (args.len()..=args.len() + 1).contains(&fn_def.params.len())
|
||||||
}).is_some();
|
}).is_some();
|
||||||
|
|
||||||
#[cfg(feature = "no_function")]
|
#[cfg(feature = "no_function")]
|
||||||
@ -765,6 +771,8 @@ pub fn optimize_into_ast(
|
|||||||
access: fn_def.access,
|
access: fn_def.access,
|
||||||
body: Default::default(),
|
body: Default::default(),
|
||||||
params: fn_def.params.clone(),
|
params: fn_def.params.clone(),
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
externals: fn_def.externals.clone(),
|
||||||
pos: fn_def.pos,
|
pos: fn_def.pos,
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
|
@ -363,6 +363,9 @@ pub struct ScriptFnDef {
|
|||||||
pub access: FnAccess,
|
pub access: FnAccess,
|
||||||
/// Names of function parameters.
|
/// Names of function parameters.
|
||||||
pub params: StaticVec<String>,
|
pub params: StaticVec<String>,
|
||||||
|
/// Access to external variables.
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
pub externals: StaticVec<String>,
|
||||||
/// Function body.
|
/// Function body.
|
||||||
pub body: Stmt,
|
pub body: Stmt,
|
||||||
/// Position of the function definition.
|
/// Position of the function definition.
|
||||||
@ -448,7 +451,7 @@ impl<'e> ParseState<'e> {
|
|||||||
/// The return value is the offset to be deducted from `Stack::len`,
|
/// The return value is the offset to be deducted from `Stack::len`,
|
||||||
/// i.e. the top element of the `ParseState` is offset 1.
|
/// i.e. the top element of the `ParseState` is offset 1.
|
||||||
/// Return `None` when the variable name is not found in the `stack`.
|
/// Return `None` when the variable name is not found in the `stack`.
|
||||||
fn access_var(&mut self, name: &str, pos: Position) -> Option<NonZeroUsize> {
|
fn access_var(&mut self, name: &str, _pos: Position) -> Option<NonZeroUsize> {
|
||||||
let index = self
|
let index = self
|
||||||
.stack
|
.stack
|
||||||
.iter()
|
.iter()
|
||||||
@ -459,7 +462,7 @@ impl<'e> ParseState<'e> {
|
|||||||
|
|
||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
if index.is_none() && !self.externals.contains_key(name) {
|
if index.is_none() && !self.externals.contains_key(name) {
|
||||||
self.externals.insert(name.to_string(), pos);
|
self.externals.insert(name.to_string(), _pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
index
|
index
|
||||||
@ -2848,7 +2851,7 @@ fn parse_stmt(
|
|||||||
|
|
||||||
match input.next().unwrap() {
|
match input.next().unwrap() {
|
||||||
(Token::Fn, pos) => {
|
(Token::Fn, pos) => {
|
||||||
let mut state = ParseState::new(
|
let mut new_state = ParseState::new(
|
||||||
state.engine,
|
state.engine,
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
state.max_function_expr_depth,
|
state.max_function_expr_depth,
|
||||||
@ -2867,7 +2870,7 @@ fn parse_stmt(
|
|||||||
pos: pos,
|
pos: pos,
|
||||||
};
|
};
|
||||||
|
|
||||||
let func = parse_fn(input, &mut state, lib, access, settings)?;
|
let func = parse_fn(input, &mut new_state, lib, access, settings)?;
|
||||||
|
|
||||||
// Qualifiers (none) + function name + number of arguments.
|
// Qualifiers (none) + function name + number of arguments.
|
||||||
let hash = calc_fn_hash(empty(), &func.name, func.params.len(), empty());
|
let hash = calc_fn_hash(empty(), &func.name, func.params.len(), empty());
|
||||||
@ -3039,12 +3042,23 @@ fn parse_fn(
|
|||||||
(_, pos) => return Err(PERR::FnMissingBody(name).into_err(*pos)),
|
(_, pos) => return Err(PERR::FnMissingBody(name).into_err(*pos)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let params = params.into_iter().map(|(p, _)| p).collect();
|
let params: StaticVec<_> = params.into_iter().map(|(p, _)| p).collect();
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
let externals = state
|
||||||
|
.externals
|
||||||
|
.iter()
|
||||||
|
.map(|(name, _)| name)
|
||||||
|
.filter(|name| !params.contains(name))
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
Ok(ScriptFnDef {
|
Ok(ScriptFnDef {
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
access,
|
access,
|
||||||
params,
|
params,
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
externals,
|
||||||
body,
|
body,
|
||||||
pos: settings.pos,
|
pos: settings.pos,
|
||||||
})
|
})
|
||||||
@ -3054,40 +3068,31 @@ fn parse_fn(
|
|||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
fn make_curry_from_externals(
|
fn make_curry_from_externals(
|
||||||
fn_expr: Expr,
|
fn_expr: Expr,
|
||||||
state: &mut ParseState,
|
externals: StaticVec<(String, Position)>,
|
||||||
settings: &ParseSettings,
|
pos: Position,
|
||||||
) -> Expr {
|
) -> Expr {
|
||||||
if state.externals.is_empty() {
|
if externals.is_empty() {
|
||||||
return fn_expr;
|
return fn_expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let num_externals = externals.len();
|
||||||
let mut args: StaticVec<_> = Default::default();
|
let mut args: StaticVec<_> = Default::default();
|
||||||
|
|
||||||
state.externals.iter().for_each(|(var_name, pos)| {
|
externals.into_iter().for_each(|(var_name, pos)| {
|
||||||
args.push(Expr::Variable(Box::new((
|
args.push(Expr::Variable(Box::new(((var_name, pos), None, 0, None))));
|
||||||
(var_name.clone(), *pos),
|
|
||||||
None,
|
|
||||||
0,
|
|
||||||
None,
|
|
||||||
))));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let hash = calc_fn_hash(
|
let hash = calc_fn_hash(empty(), KEYWORD_FN_PTR_CURRY, num_externals, empty());
|
||||||
empty(),
|
|
||||||
KEYWORD_FN_PTR_CURRY,
|
|
||||||
state.externals.len(),
|
|
||||||
empty(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let fn_call = Expr::FnCall(Box::new((
|
let fn_call = Expr::FnCall(Box::new((
|
||||||
(KEYWORD_FN_PTR_CURRY.into(), false, settings.pos),
|
(KEYWORD_FN_PTR_CURRY.into(), false, pos),
|
||||||
None,
|
None,
|
||||||
hash,
|
hash,
|
||||||
args,
|
args,
|
||||||
None,
|
None,
|
||||||
)));
|
)));
|
||||||
|
|
||||||
Expr::Dot(Box::new((fn_expr, fn_call, settings.pos)))
|
Expr::Dot(Box::new((fn_expr, fn_call, pos)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an anonymous function definition.
|
/// Parse an anonymous function definition.
|
||||||
@ -3160,11 +3165,20 @@ fn parse_anon_fn(
|
|||||||
#[cfg(feature = "no_capture")]
|
#[cfg(feature = "no_capture")]
|
||||||
let params: StaticVec<_> = params.into_iter().map(|(v, _)| v).collect();
|
let params: StaticVec<_> = params.into_iter().map(|(v, _)| v).collect();
|
||||||
|
|
||||||
|
// External variables may need to be processed in a consistent order,
|
||||||
|
// so extract them into a list.
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
let externals: StaticVec<_> = state
|
||||||
|
.externals
|
||||||
|
.iter()
|
||||||
|
.map(|(k, &v)| (k.clone(), v))
|
||||||
|
.collect();
|
||||||
|
|
||||||
// Add parameters that are auto-curried
|
// Add parameters that are auto-curried
|
||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
let params: StaticVec<_> = state
|
let params: StaticVec<_> = externals
|
||||||
.externals
|
.iter()
|
||||||
.keys()
|
.map(|(k, _)| k)
|
||||||
.cloned()
|
.cloned()
|
||||||
.chain(params.into_iter().map(|(v, _)| v))
|
.chain(params.into_iter().map(|(v, _)| v))
|
||||||
.collect();
|
.collect();
|
||||||
@ -3183,10 +3197,13 @@ fn parse_anon_fn(
|
|||||||
// Create unique function name
|
// Create unique function name
|
||||||
let fn_name: ImmutableString = format!("{}{:16x}", FN_ANONYMOUS, hash).into();
|
let fn_name: ImmutableString = format!("{}{:16x}", FN_ANONYMOUS, hash).into();
|
||||||
|
|
||||||
|
// Define the function
|
||||||
let script = ScriptFnDef {
|
let script = ScriptFnDef {
|
||||||
name: fn_name.clone(),
|
name: fn_name.clone(),
|
||||||
access: FnAccess::Public,
|
access: FnAccess::Public,
|
||||||
params,
|
params,
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
externals: Default::default(),
|
||||||
body,
|
body,
|
||||||
pos: settings.pos,
|
pos: settings.pos,
|
||||||
};
|
};
|
||||||
@ -3194,7 +3211,7 @@ fn parse_anon_fn(
|
|||||||
let expr = Expr::FnPointer(Box::new((fn_name, settings.pos)));
|
let expr = Expr::FnPointer(Box::new((fn_name, settings.pos)));
|
||||||
|
|
||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
let expr = make_curry_from_externals(expr, state, &settings);
|
let expr = make_curry_from_externals(expr, externals, settings.pos);
|
||||||
|
|
||||||
Ok((expr, script))
|
Ok((expr, script))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user