From 2ff2789326245a5b716f57c67de662f10bce2056 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 9 Mar 2021 23:48:40 +0800 Subject: [PATCH] Fix Stmt size. --- src/ast.rs | 16 ++++++++-------- src/engine.rs | 12 ++++++------ src/optimize.rs | 4 ++-- src/parser.rs | 18 +++++++++++++----- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index a8ed9a4e..c773d760 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -832,7 +832,7 @@ pub enum Stmt { /// `do` `{` stmt `}` `while`|`until` expr Do(Box, Expr, bool, Position), /// `for` id `in` expr `{` stmt `}` - For(Expr, String, Box, Position), + For(Expr, Box<(String, Stmt)>, Position), /// \[`export`\] `let` id `=` expr Let(Expr, Ident, Position), /// \[`export`\] `const` id `=` expr @@ -853,7 +853,7 @@ pub enum Stmt { Return(ReturnType, Option, Position), /// `import` expr `as` var #[cfg(not(feature = "no_module"))] - Import(Expr, Option, Position), + Import(Expr, Ident, Position), /// `export` var `as` var `,` ... #[cfg(not(feature = "no_module"))] Export(Vec<(Ident, Option)>, Position), @@ -890,7 +890,7 @@ impl Stmt { | Self::Switch(_, _, pos) | Self::While(_, _, pos) | Self::Do(_, _, _, pos) - | Self::For(_, _, _, pos) + | Self::For(_, _, pos) | Self::Return(_, _, pos) | Self::Let(_, _, pos) | Self::Const(_, _, pos) @@ -919,7 +919,7 @@ impl Stmt { | Self::Switch(_, _, pos) | Self::While(_, _, pos) | Self::Do(_, _, _, pos) - | Self::For(_, _, _, pos) + | Self::For(_, _, pos) | Self::Return(_, _, pos) | Self::Let(_, _, pos) | Self::Const(_, _, pos) @@ -946,7 +946,7 @@ impl Stmt { Self::If(_, _, _) | Self::Switch(_, _, _) | Self::While(_, _, _) - | Self::For(_, _, _, _) + | Self::For(_, _, _) | Self::Block(_, _) | Self::TryCatch(_, _, _) => true, @@ -985,7 +985,7 @@ impl Stmt { Self::While(condition, block, _) | Self::Do(block, condition, _, _) => { 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::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()), Self::Continue(_) | Self::Break(_) | Self::Return(_, _, _) => false, @@ -1024,9 +1024,9 @@ impl Stmt { e.walk(path, on_node); s.walk(path, on_node); } - Self::For(e, _, s, _) => { + Self::For(e, x, _) => { e.walk(path, on_node); - s.walk(path, on_node); + x.1.walk(path, on_node); } Self::Assignment(x, _) => { x.0.walk(path, on_node); diff --git a/src/engine.rs b/src/engine.rs index 62cf0ac7..d061c54b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -2126,8 +2126,8 @@ impl Engine { }, // For loop - Stmt::For(expr, name, x, _) => { - let stmt = x.as_ref(); + Stmt::For(expr, x, _) => { + let (name, stmt) = x.as_ref(); let iter_obj = self .eval_expr(scope, mods, state, lib, this_ptr, expr, level)? .flatten(); @@ -2348,7 +2348,7 @@ impl Engine { // Import statement #[cfg(not(feature = "no_module"))] - Stmt::Import(expr, alias, _pos) => { + Stmt::Import(expr, Ident { name, public, .. }, _pos) => { // Guard against too many modules #[cfg(not(feature = "unchecked"))] if state.modules >= self.max_modules() { @@ -2375,14 +2375,14 @@ impl Engine { }) .unwrap_or_else(|| self.module_resolver.resolve(self, &path, expr_pos))?; - if let Some(name_def) = alias { + if *public { if !module.is_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); module.build_index(); - mods.push(name_def.name.clone(), module); + mods.push(name.clone(), module); } else { - mods.push(name_def.name.clone(), module); + mods.push(name.clone(), module); } } diff --git a/src/optimize.rs b/src/optimize.rs index 571ebd01..ad3c3f72 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -428,9 +428,9 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { optimize_expr(condition, state); } // for id in expr { block } - Stmt::For(iterable, _, block, _) => { + Stmt::For(iterable, x, _) => { optimize_expr(iterable, state); - optimize_stmt(block, state, false); + optimize_stmt(&mut x.1, state, false); } // let id = expr; Stmt::Let(expr, _, _) => optimize_expr(expr, state), diff --git a/src/parser.rs b/src/parser.rs index e0c58dc9..b8088159 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2138,7 +2138,7 @@ fn parse_for( 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. @@ -2210,7 +2210,15 @@ fn parse_import( // import expr as ... 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 ... @@ -2228,11 +2236,11 @@ fn parse_import( Ok(Stmt::Import( expr, - Some(Ident { + Ident { name, pos: name_pos, - public: false, - }), + public: true, + }, settings.pos, )) }