Fix Stmt size.

This commit is contained in:
Stephen Chung 2021-03-09 23:48:40 +08:00
parent b11b8d6d39
commit 2ff2789326
4 changed files with 29 additions and 21 deletions

View File

@ -832,7 +832,7 @@ pub enum Stmt {
/// `do` `{` stmt `}` `while`|`until` expr /// `do` `{` stmt `}` `while`|`until` expr
Do(Box<Stmt>, Expr, bool, Position), Do(Box<Stmt>, Expr, bool, Position),
/// `for` id `in` expr `{` stmt `}` /// `for` id `in` expr `{` stmt `}`
For(Expr, String, Box<Stmt>, Position), For(Expr, Box<(String, Stmt)>, Position),
/// \[`export`\] `let` id `=` expr /// \[`export`\] `let` id `=` expr
Let(Expr, Ident, Position), Let(Expr, Ident, Position),
/// \[`export`\] `const` id `=` expr /// \[`export`\] `const` id `=` expr
@ -853,7 +853,7 @@ pub enum Stmt {
Return(ReturnType, Option<Expr>, Position), Return(ReturnType, Option<Expr>, Position),
/// `import` expr `as` var /// `import` expr `as` var
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Import(Expr, Option<Ident>, Position), Import(Expr, Ident, Position),
/// `export` var `as` var `,` ... /// `export` var `as` var `,` ...
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Export(Vec<(Ident, Option<Ident>)>, Position), Export(Vec<(Ident, Option<Ident>)>, Position),
@ -890,7 +890,7 @@ impl Stmt {
| Self::Switch(_, _, pos) | Self::Switch(_, _, pos)
| Self::While(_, _, pos) | Self::While(_, _, pos)
| Self::Do(_, _, _, pos) | Self::Do(_, _, _, pos)
| Self::For(_, _, _, pos) | Self::For(_, _, pos)
| Self::Return(_, _, pos) | Self::Return(_, _, pos)
| Self::Let(_, _, pos) | Self::Let(_, _, pos)
| Self::Const(_, _, pos) | Self::Const(_, _, pos)
@ -919,7 +919,7 @@ impl Stmt {
| Self::Switch(_, _, pos) | Self::Switch(_, _, pos)
| Self::While(_, _, pos) | Self::While(_, _, pos)
| Self::Do(_, _, _, pos) | Self::Do(_, _, _, pos)
| Self::For(_, _, _, pos) | Self::For(_, _, pos)
| Self::Return(_, _, pos) | Self::Return(_, _, pos)
| Self::Let(_, _, pos) | Self::Let(_, _, pos)
| Self::Const(_, _, pos) | Self::Const(_, _, pos)
@ -946,7 +946,7 @@ impl Stmt {
Self::If(_, _, _) Self::If(_, _, _)
| Self::Switch(_, _, _) | Self::Switch(_, _, _)
| Self::While(_, _, _) | Self::While(_, _, _)
| Self::For(_, _, _, _) | Self::For(_, _, _)
| Self::Block(_, _) | Self::Block(_, _)
| Self::TryCatch(_, _, _) => true, | Self::TryCatch(_, _, _) => true,
@ -985,7 +985,7 @@ impl Stmt {
Self::While(condition, block, _) | Self::Do(block, condition, _, _) => { Self::While(condition, block, _) | Self::Do(block, condition, _, _) => {
condition.is_pure() && block.is_pure() condition.is_pure() && block.is_pure()
} }
Self::For(iterable, _, block, _) => iterable.is_pure() && block.is_pure(), Self::For(iterable, x, _) => iterable.is_pure() && x.1.is_pure(),
Self::Let(_, _, _) | Self::Const(_, _, _) | Self::Assignment(_, _) => false, Self::Let(_, _, _) | Self::Const(_, _, _) | Self::Assignment(_, _) => false,
Self::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()), Self::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()),
Self::Continue(_) | Self::Break(_) | Self::Return(_, _, _) => false, Self::Continue(_) | Self::Break(_) | Self::Return(_, _, _) => false,
@ -1024,9 +1024,9 @@ impl Stmt {
e.walk(path, on_node); e.walk(path, on_node);
s.walk(path, on_node); s.walk(path, on_node);
} }
Self::For(e, _, s, _) => { Self::For(e, x, _) => {
e.walk(path, on_node); e.walk(path, on_node);
s.walk(path, on_node); x.1.walk(path, on_node);
} }
Self::Assignment(x, _) => { Self::Assignment(x, _) => {
x.0.walk(path, on_node); x.0.walk(path, on_node);

View File

@ -2126,8 +2126,8 @@ impl Engine {
}, },
// For loop // For loop
Stmt::For(expr, name, x, _) => { Stmt::For(expr, x, _) => {
let stmt = x.as_ref(); let (name, stmt) = x.as_ref();
let iter_obj = self let iter_obj = self
.eval_expr(scope, mods, state, lib, this_ptr, expr, level)? .eval_expr(scope, mods, state, lib, this_ptr, expr, level)?
.flatten(); .flatten();
@ -2348,7 +2348,7 @@ impl Engine {
// Import statement // Import statement
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Stmt::Import(expr, alias, _pos) => { Stmt::Import(expr, Ident { name, public, .. }, _pos) => {
// Guard against too many modules // Guard against too many modules
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
if state.modules >= self.max_modules() { if state.modules >= self.max_modules() {
@ -2375,14 +2375,14 @@ impl Engine {
}) })
.unwrap_or_else(|| self.module_resolver.resolve(self, &path, expr_pos))?; .unwrap_or_else(|| self.module_resolver.resolve(self, &path, expr_pos))?;
if let Some(name_def) = alias { if *public {
if !module.is_indexed() { if !module.is_indexed() {
// Index the module (making a clone copy if necessary) if it is not indexed // Index the module (making a clone copy if necessary) if it is not indexed
let mut module = crate::fn_native::shared_take_or_clone(module); let mut module = crate::fn_native::shared_take_or_clone(module);
module.build_index(); module.build_index();
mods.push(name_def.name.clone(), module); mods.push(name.clone(), module);
} else { } else {
mods.push(name_def.name.clone(), module); mods.push(name.clone(), module);
} }
} }

View File

@ -428,9 +428,9 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) {
optimize_expr(condition, state); optimize_expr(condition, state);
} }
// for id in expr { block } // for id in expr { block }
Stmt::For(iterable, _, block, _) => { Stmt::For(iterable, x, _) => {
optimize_expr(iterable, state); optimize_expr(iterable, state);
optimize_stmt(block, state, false); optimize_stmt(&mut x.1, state, false);
} }
// let id = expr; // let id = expr;
Stmt::Let(expr, _, _) => optimize_expr(expr, state), Stmt::Let(expr, _, _) => optimize_expr(expr, state),

View File

@ -2138,7 +2138,7 @@ fn parse_for(
state.stack.truncate(prev_stack_len); state.stack.truncate(prev_stack_len);
Ok(Stmt::For(expr, name, Box::new(body), settings.pos)) Ok(Stmt::For(expr, Box::new((name, body)), settings.pos))
} }
/// Parse a variable definition statement. /// Parse a variable definition statement.
@ -2210,7 +2210,15 @@ fn parse_import(
// import expr as ... // import expr as ...
if !match_token(input, Token::As).0 { if !match_token(input, Token::As).0 {
return Ok(Stmt::Import(expr, None, settings.pos)); return Ok(Stmt::Import(
expr,
Ident {
name: "".into(),
pos: Position::NONE,
public: false,
},
settings.pos,
));
} }
// import expr as name ... // import expr as name ...
@ -2228,11 +2236,11 @@ fn parse_import(
Ok(Stmt::Import( Ok(Stmt::Import(
expr, expr,
Some(Ident { Ident {
name, name,
pos: name_pos, pos: name_pos,
public: false, public: true,
}), },
settings.pos, settings.pos,
)) ))
} }