From 89d75b1b11d3f80ea2f780e3725dbd50b5251a0d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 8 May 2020 14:50:48 +0800 Subject: [PATCH] Fix compilation errors for no_module. --- src/engine.rs | 9 +++++--- src/parser.rs | 63 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 00532895..0029684d 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,6 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::module::ModuleRef; use crate::optimize::OptimizationLevel; use crate::packages::{ CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, @@ -15,7 +14,10 @@ use crate::token::Position; use crate::utils::{StaticVec, EMPTY_TYPE_ID}; #[cfg(not(feature = "no_module"))] -use crate::module::{resolvers, Module, ModuleResolver}; +use crate::module::{resolvers, Module, ModuleRef, ModuleResolver}; + +#[cfg(feature = "no_module")] +use crate::parser::ModuleRef; use crate::stdlib::{ any::TypeId, @@ -432,7 +434,8 @@ fn default_print(s: &str) { fn search_scope<'a>( scope: &'a mut Scope, name: &str, - modules: &Option>, + #[cfg(not(feature = "no_module"))] mut modules: &Option>, + #[cfg(feature = "no_module")] _: &Option, index: Option, pos: Position, ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box> { diff --git a/src/parser.rs b/src/parser.rs index 3e648828..2499cdca 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,11 +4,17 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; use crate::error::{LexError, ParseError, ParseErrorType}; -use crate::module::ModuleRef; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token, TokenIterator}; -use crate::utils::{StaticVec, EMPTY_TYPE_ID}; +use crate::utils::EMPTY_TYPE_ID; + +#[cfg(not(feature = "no_module"))] +use crate::module::ModuleRef; + +#[cfg(feature = "no_module")] +#[derive(Debug, Clone, Copy)] +pub struct ModuleRef; use crate::stdlib::{ borrow::Cow, @@ -357,7 +363,8 @@ pub enum Expr { /// Variable access - (variable name, optional modules, hash, optional index, position) Variable( Box, - Option>, + #[cfg(not(feature = "no_module"))] Option>, + #[cfg(feature = "no_module")] Option, Option, Position, ), @@ -370,7 +377,8 @@ pub enum Expr { /// and the function names are predictable, so no need to allocate a new `String`. FnCall( Box>, - Option>, + #[cfg(not(feature = "no_module"))] Option>, + #[cfg(feature = "no_module")] Option, Box>, Option>, Position, @@ -677,7 +685,8 @@ fn parse_call_expr<'a>( input: &mut Peekable>, stack: &mut Stack, id: String, - mut modules: Option>, + #[cfg(not(feature = "no_module"))] mut modules: Option>, + #[cfg(feature = "no_module")] modules: Option, begin: Position, allow_stmt_expr: bool, ) -> Result> { @@ -698,12 +707,14 @@ fn parse_call_expr<'a>( (Token::RightParen, _) => { eat_token(input, Token::RightParen); - if let Some(modules) = modules.as_mut() { - // Calculate hash - let hash = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); - modules.set_key(hash); + #[cfg(not(feature = "no_module"))] + { + if let Some(modules) = modules.as_mut() { + // Calculate hash + let hash = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); + modules.set_key(hash); + } } - return Ok(Expr::FnCall( Box::new(id.into()), modules, @@ -724,16 +735,18 @@ fn parse_call_expr<'a>( (Token::RightParen, _) => { eat_token(input, Token::RightParen); - if let Some(modules) = modules.as_mut() { - // Calculate hash - let hash = calc_fn_hash( - modules.iter().map(|(m, _)| m.as_str()), - &id, - repeat(EMPTY_TYPE_ID()).take(args.len()), - ); - modules.set_key(hash); + #[cfg(not(feature = "no_module"))] + { + if let Some(modules) = modules.as_mut() { + // Calculate hash + let hash = calc_fn_hash( + modules.iter().map(|(m, _)| m.as_str()), + &id, + repeat(EMPTY_TYPE_ID()).take(args.len()), + ); + modules.set_key(hash); + } } - return Ok(Expr::FnCall( Box::new(id.into()), modules, @@ -1136,9 +1149,9 @@ fn parse_primary<'a>( } else { index = stack.find_module(id.as_ref()); - let mut vec = StaticVec::new(); - vec.push((*id, pos)); - modules = Some(Box::new(vec.into())); + let mut m: ModuleRef = Default::default(); + m.push((*id, pos)); + modules = Some(Box::new(m)); } Expr::Variable(Box::new(id2), modules, index, pos2) @@ -1158,6 +1171,7 @@ fn parse_primary<'a>( match &mut root_expr { // Calculate hash key for module-qualified variables + #[cfg(not(feature = "no_module"))] Expr::Variable(id, Some(modules), _, _) => { let hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty()); modules.set_key(hash); @@ -1347,7 +1361,10 @@ fn make_dot_expr( } // lhs.module::id - syntax error (_, Expr::Variable(_, Some(modules), _, _)) => { - return Err(PERR::PropertyExpected.into_err(modules.get(0).1)) + #[cfg(feature = "no_module")] + unreachable!(); + #[cfg(not(feature = "no_module"))] + return Err(PERR::PropertyExpected.into_err(modules.get(0).1)); } // lhs.dot_lhs.dot_rhs (lhs, Expr::Dot(dot_lhs, dot_rhs, dot_pos)) => Expr::Dot(