Merge remote-tracking branch 'schungx/closures' into closures

This commit is contained in:
Ilya Lakhin
2020-07-31 13:05:16 +07:00
22 changed files with 665 additions and 322 deletions

View File

@@ -25,7 +25,7 @@ use crate::stdlib::{
borrow::Cow,
boxed::Box,
char,
collections::HashMap,
collections::{HashMap, HashSet},
fmt, format,
hash::{Hash, Hasher},
iter::empty,
@@ -355,7 +355,7 @@ impl fmt::Display for FnAccess {
/// ## WARNING
///
/// This type is volatile and may change.
#[derive(Debug, Clone, Hash)]
#[derive(Debug, Clone)]
pub struct ScriptFnDef {
/// Function name.
pub name: ImmutableString,
@@ -363,6 +363,9 @@ pub struct ScriptFnDef {
pub access: FnAccess,
/// Names of function parameters.
pub params: StaticVec<String>,
/// Access to external variables.
#[cfg(not(feature = "no_capture"))]
pub externals: HashSet<String>,
/// Function body.
pub body: Stmt,
/// Position of the function definition.
@@ -455,7 +458,7 @@ impl<'e> ParseState<'e> {
/// The return value is the offset to be deducted from `Stack::len`,
/// i.e. the top element of the `ParseState` is offset 1.
/// 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
.stack
.iter()
@@ -467,7 +470,7 @@ impl<'e> ParseState<'e> {
#[cfg(not(feature = "no_capture"))]
if self.capture {
if index.is_none() && !self.externals.contains_key(name) {
self.externals.insert(name.to_string(), pos);
self.externals.insert(name.to_string(), _pos);
}
} else {
self.capture = true
@@ -756,12 +759,12 @@ pub enum Expr {
Stmt(Box<(Stmt, Position)>),
/// Wrapped expression - should not be optimized away.
Expr(Box<Expr>),
/// func(expr, ... ) - ((function name, native_only, position), optional modules, hash, arguments, optional default value)
/// func(expr, ... ) - ((function name, native_only, capture, position), optional modules, hash, arguments, optional default value)
/// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls
/// and the function names are predictable, so no need to allocate a new `String`.
FnCall(
Box<(
(Cow<'static, str>, bool, Position),
(Cow<'static, str>, bool, bool, Position),
Option<Box<ModuleRef>>,
u64,
StaticVec<Expr>,
@@ -879,7 +882,7 @@ impl Expr {
Self::Property(x) => x.1,
Self::Stmt(x) => x.1,
Self::Variable(x) => (x.0).1,
Self::FnCall(x) => (x.0).2,
Self::FnCall(x) => (x.0).3,
Self::Assignment(x) => x.0.position(),
Self::And(x) | Self::Or(x) | Self::In(x) => x.2,
@@ -911,7 +914,7 @@ impl Expr {
Self::Variable(x) => (x.0).1 = new_pos,
Self::Property(x) => x.1 = new_pos,
Self::Stmt(x) => x.1 = new_pos,
Self::FnCall(x) => (x.0).2 = new_pos,
Self::FnCall(x) => (x.0).3 = new_pos,
Self::And(x) => x.2 = new_pos,
Self::Or(x) => x.2 = new_pos,
Self::In(x) => x.2 = new_pos,
@@ -1017,6 +1020,7 @@ impl Expr {
#[cfg(not(feature = "no_index"))]
Token::LeftBracket => true,
Token::LeftParen => true,
Token::Bang => true,
Token::DoubleColon => true,
_ => false,
},
@@ -1109,6 +1113,7 @@ fn parse_fn_call(
state: &mut ParseState,
lib: &mut FunctionsLib,
id: String,
capture: bool,
mut modules: Option<Box<ModuleRef>>,
settings: ParseSettings,
) -> Result<Expr, ParseError> {
@@ -1151,7 +1156,7 @@ fn parse_fn_call(
};
return Ok(Expr::FnCall(Box::new((
(id.into(), false, settings.pos),
(id.into(), false, capture, settings.pos),
modules,
hash_script,
args,
@@ -1193,7 +1198,7 @@ fn parse_fn_call(
};
return Ok(Expr::FnCall(Box::new((
(id.into(), false, settings.pos),
(id.into(), false, capture, settings.pos),
modules,
hash_script,
args,
@@ -1602,6 +1607,8 @@ fn parse_primary(
_ => input.next().unwrap(),
};
let (next_token, _) = input.peek().unwrap();
let mut root_expr = match token {
Token::IntegerConstant(x) => Expr::IntegerConstant(Box::new((x, settings.pos))),
#[cfg(not(feature = "no_float"))]
@@ -1613,7 +1620,7 @@ fn parse_primary(
Expr::Variable(Box::new(((s, settings.pos), None, 0, index)))
}
// Function call is allowed to have reserved keyword
Token::Reserved(s) if input.peek().unwrap().0 == Token::LeftParen => {
Token::Reserved(s) if *next_token == Token::LeftParen || *next_token == Token::Bang => {
if is_keyword_function(&s) {
Expr::Variable(Box::new(((s, settings.pos), None, 0, None)))
} else {
@@ -1621,7 +1628,7 @@ fn parse_primary(
}
}
// Access to `this` as a variable is OK
Token::Reserved(s) if s == KEYWORD_THIS && input.peek().unwrap().0 != Token::LeftParen => {
Token::Reserved(s) if s == KEYWORD_THIS && *next_token != Token::LeftParen => {
if !settings.is_function_scope {
return Err(
PERR::BadInput(format!("'{}' can only be used in functions", s))
@@ -1661,11 +1668,26 @@ fn parse_primary(
settings.pos = token_pos;
root_expr = match (root_expr, token) {
// Function call
#[cfg(not(feature = "no_capture"))]
(Expr::Variable(x), Token::Bang) => {
if !match_token(input, Token::LeftParen)? {
return Err(PERR::MissingToken(
Token::LeftParen.syntax().into(),
"to start arguments list of function call".into(),
)
.into_err(input.peek().unwrap().1));
}
let ((name, pos), modules, _, _) = *x;
settings.pos = pos;
parse_fn_call(input, state, lib, name, true, modules, settings.level_up())?
}
// Function call
(Expr::Variable(x), Token::LeftParen) => {
let ((name, pos), modules, _, _) = *x;
settings.pos = pos;
parse_fn_call(input, state, lib, name, modules, settings.level_up())?
parse_fn_call(input, state, lib, name, false, modules, settings.level_up())?
}
(Expr::Property(_), _) => unreachable!(),
// module access
@@ -1775,7 +1797,7 @@ fn parse_unary(
args.push(expr);
Ok(Expr::FnCall(Box::new((
(op.into(), true, pos),
(op.into(), true, false, pos),
None,
hash,
args,
@@ -1800,7 +1822,7 @@ fn parse_unary(
let hash = calc_fn_hash(empty(), op, 2, empty());
Ok(Expr::FnCall(Box::new((
(op.into(), true, pos),
(op.into(), true, false, pos),
None,
hash,
args,
@@ -1995,7 +2017,14 @@ fn make_dot_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result<Expr, ParseEr
op_pos,
)))
}
// lhs.func()
// lhs.func!(...)
(_, Expr::FnCall(x)) if (x.0).2 => {
return Err(PERR::MalformedCapture(
"method-call style does not support capturing".into(),
)
.into_err((x.0).3))
}
// lhs.func(...)
(lhs, func @ Expr::FnCall(_)) => Expr::Dot(Box::new((lhs, func, op_pos))),
// lhs.rhs
(_, rhs) => return Err(PERR::PropertyExpected.into_err(rhs.position())),
@@ -2212,7 +2241,7 @@ fn parse_binary_op(
let cmp_def = Some(false);
let op = op_token.syntax();
let hash = calc_fn_hash(empty(), &op, 2, empty());
let op = (op, true, pos);
let op = (op, true, false, pos);
let mut args = StaticVec::new();
args.push(root);
@@ -2273,7 +2302,7 @@ fn parse_binary_op(
.unwrap_or(false) =>
{
// Accept non-native functions for custom operators
let op = (op.0, false, op.2);
let op = (op.0, false, op.2, op.3);
Expr::FnCall(Box::new((op, None, hash, args, None)))
}
@@ -2867,7 +2896,7 @@ fn parse_stmt(
match input.next().unwrap() {
(Token::Fn, pos) => {
let mut state = ParseState::new(
let mut new_state = ParseState::new(
state.engine,
#[cfg(not(feature = "unchecked"))]
state.max_function_expr_depth,
@@ -2886,7 +2915,7 @@ fn parse_stmt(
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.
let hash = calc_fn_hash(empty(), &func.name, func.params.len(), empty());
@@ -2991,10 +3020,12 @@ fn parse_fn(
let (token, pos) = input.next().unwrap();
let name = token.into_function_name().map_err(|t| match t {
Token::Reserved(s) => PERR::Reserved(s).into_err(pos),
_ => PERR::FnMissingName.into_err(pos),
})?;
let name = token
.into_function_name_for_override()
.map_err(|t| match t {
Token::Reserved(s) => PERR::Reserved(s).into_err(pos),
_ => PERR::FnMissingName.into_err(pos),
})?;
match input.peek().unwrap() {
(Token::LeftParen, _) => eat_token(input, Token::LeftParen),
@@ -3058,12 +3089,23 @@ fn parse_fn(
(_, 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 {
name: name.into(),
access,
params,
#[cfg(not(feature = "no_capture"))]
externals,
body,
pos: settings.pos,
})
@@ -3073,40 +3115,31 @@ fn parse_fn(
#[cfg(not(feature = "no_capture"))]
fn make_curry_from_externals(
fn_expr: Expr,
state: &mut ParseState,
settings: &ParseSettings,
externals: StaticVec<(String, Position)>,
pos: Position,
) -> Expr {
if state.externals.is_empty() {
if externals.is_empty() {
return fn_expr;
}
let num_externals = externals.len();
let mut args: StaticVec<_> = Default::default();
state.externals.iter().for_each(|(var_name, pos)| {
args.push(Expr::Variable(Box::new((
(var_name.clone(), *pos),
None,
0,
None,
))));
externals.into_iter().for_each(|(var_name, pos)| {
args.push(Expr::Variable(Box::new(((var_name, pos), None, 0, None))));
});
let hash = calc_fn_hash(
empty(),
KEYWORD_FN_PTR_CURRY,
state.externals.len(),
empty(),
);
let hash = calc_fn_hash(empty(), KEYWORD_FN_PTR_CURRY, num_externals, empty());
let fn_call = Expr::FnCall(Box::new((
(KEYWORD_FN_PTR_CURRY.into(), false, settings.pos),
(KEYWORD_FN_PTR_CURRY.into(), false, false, pos),
None,
hash,
args,
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.
@@ -3179,11 +3212,20 @@ fn parse_anon_fn(
#[cfg(feature = "no_capture")]
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
#[cfg(not(feature = "no_capture"))]
let params: StaticVec<_> = state
.externals
.keys()
let params: StaticVec<_> = externals
.iter()
.map(|(k, _)| k)
.cloned()
.chain(params.into_iter().map(|(v, _)| v))
.collect();
@@ -3202,10 +3244,13 @@ fn parse_anon_fn(
// Create unique function name
let fn_name: ImmutableString = format!("{}{:16x}", FN_ANONYMOUS, hash).into();
// Define the function
let script = ScriptFnDef {
name: fn_name.clone(),
access: FnAccess::Public,
params,
#[cfg(not(feature = "no_capture"))]
externals: Default::default(),
body,
pos: settings.pos,
};
@@ -3213,7 +3258,7 @@ fn parse_anon_fn(
let expr = Expr::FnPointer(Box::new((fn_name, settings.pos)));
#[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))
}