Fix compilation errors for no_module.

This commit is contained in:
Stephen Chung 2020-05-08 14:50:48 +08:00
parent e0745ef069
commit 89d75b1b11
2 changed files with 46 additions and 26 deletions

View File

@ -3,7 +3,6 @@
use crate::any::{Dynamic, Union}; use crate::any::{Dynamic, Union};
use crate::calc_fn_hash; use crate::calc_fn_hash;
use crate::error::ParseErrorType; use crate::error::ParseErrorType;
use crate::module::ModuleRef;
use crate::optimize::OptimizationLevel; use crate::optimize::OptimizationLevel;
use crate::packages::{ use crate::packages::{
CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage,
@ -15,7 +14,10 @@ use crate::token::Position;
use crate::utils::{StaticVec, EMPTY_TYPE_ID}; use crate::utils::{StaticVec, EMPTY_TYPE_ID};
#[cfg(not(feature = "no_module"))] #[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::{ use crate::stdlib::{
any::TypeId, any::TypeId,
@ -432,7 +434,8 @@ fn default_print(s: &str) {
fn search_scope<'a>( fn search_scope<'a>(
scope: &'a mut Scope, scope: &'a mut Scope,
name: &str, name: &str,
modules: &Option<Box<ModuleRef>>, #[cfg(not(feature = "no_module"))] mut modules: &Option<Box<ModuleRef>>,
#[cfg(feature = "no_module")] _: &Option<ModuleRef>,
index: Option<NonZeroUsize>, index: Option<NonZeroUsize>,
pos: Position, pos: Position,
) -> Result<(&'a mut Dynamic, ScopeEntryType), Box<EvalAltResult>> { ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box<EvalAltResult>> {

View File

@ -4,11 +4,17 @@ use crate::any::{Dynamic, Union};
use crate::calc_fn_hash; use crate::calc_fn_hash;
use crate::engine::{Engine, FunctionsLib}; use crate::engine::{Engine, FunctionsLib};
use crate::error::{LexError, ParseError, ParseErrorType}; use crate::error::{LexError, ParseError, ParseErrorType};
use crate::module::ModuleRef;
use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::optimize::{optimize_into_ast, OptimizationLevel};
use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::scope::{EntryType as ScopeEntryType, Scope};
use crate::token::{Position, Token, TokenIterator}; 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::{ use crate::stdlib::{
borrow::Cow, borrow::Cow,
@ -357,7 +363,8 @@ pub enum Expr {
/// Variable access - (variable name, optional modules, hash, optional index, position) /// Variable access - (variable name, optional modules, hash, optional index, position)
Variable( Variable(
Box<String>, Box<String>,
Option<Box<ModuleRef>>, #[cfg(not(feature = "no_module"))] Option<Box<ModuleRef>>,
#[cfg(feature = "no_module")] Option<ModuleRef>,
Option<NonZeroUsize>, Option<NonZeroUsize>,
Position, Position,
), ),
@ -370,7 +377,8 @@ pub enum Expr {
/// and the function names are predictable, so no need to allocate a new `String`. /// and the function names are predictable, so no need to allocate a new `String`.
FnCall( FnCall(
Box<Cow<'static, str>>, Box<Cow<'static, str>>,
Option<Box<ModuleRef>>, #[cfg(not(feature = "no_module"))] Option<Box<ModuleRef>>,
#[cfg(feature = "no_module")] Option<ModuleRef>,
Box<Vec<Expr>>, Box<Vec<Expr>>,
Option<Box<Dynamic>>, Option<Box<Dynamic>>,
Position, Position,
@ -677,7 +685,8 @@ fn parse_call_expr<'a>(
input: &mut Peekable<TokenIterator<'a>>, input: &mut Peekable<TokenIterator<'a>>,
stack: &mut Stack, stack: &mut Stack,
id: String, id: String,
mut modules: Option<Box<ModuleRef>>, #[cfg(not(feature = "no_module"))] mut modules: Option<Box<ModuleRef>>,
#[cfg(feature = "no_module")] modules: Option<ModuleRef>,
begin: Position, begin: Position,
allow_stmt_expr: bool, allow_stmt_expr: bool,
) -> Result<Expr, Box<ParseError>> { ) -> Result<Expr, Box<ParseError>> {
@ -698,12 +707,14 @@ fn parse_call_expr<'a>(
(Token::RightParen, _) => { (Token::RightParen, _) => {
eat_token(input, Token::RightParen); eat_token(input, Token::RightParen);
if let Some(modules) = modules.as_mut() { #[cfg(not(feature = "no_module"))]
// Calculate hash {
let hash = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); if let Some(modules) = modules.as_mut() {
modules.set_key(hash); // Calculate hash
let hash = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty());
modules.set_key(hash);
}
} }
return Ok(Expr::FnCall( return Ok(Expr::FnCall(
Box::new(id.into()), Box::new(id.into()),
modules, modules,
@ -724,16 +735,18 @@ fn parse_call_expr<'a>(
(Token::RightParen, _) => { (Token::RightParen, _) => {
eat_token(input, Token::RightParen); eat_token(input, Token::RightParen);
if let Some(modules) = modules.as_mut() { #[cfg(not(feature = "no_module"))]
// Calculate hash {
let hash = calc_fn_hash( if let Some(modules) = modules.as_mut() {
modules.iter().map(|(m, _)| m.as_str()), // Calculate hash
&id, let hash = calc_fn_hash(
repeat(EMPTY_TYPE_ID()).take(args.len()), modules.iter().map(|(m, _)| m.as_str()),
); &id,
modules.set_key(hash); repeat(EMPTY_TYPE_ID()).take(args.len()),
);
modules.set_key(hash);
}
} }
return Ok(Expr::FnCall( return Ok(Expr::FnCall(
Box::new(id.into()), Box::new(id.into()),
modules, modules,
@ -1136,9 +1149,9 @@ fn parse_primary<'a>(
} else { } else {
index = stack.find_module(id.as_ref()); index = stack.find_module(id.as_ref());
let mut vec = StaticVec::new(); let mut m: ModuleRef = Default::default();
vec.push((*id, pos)); m.push((*id, pos));
modules = Some(Box::new(vec.into())); modules = Some(Box::new(m));
} }
Expr::Variable(Box::new(id2), modules, index, pos2) Expr::Variable(Box::new(id2), modules, index, pos2)
@ -1158,6 +1171,7 @@ fn parse_primary<'a>(
match &mut root_expr { match &mut root_expr {
// Calculate hash key for module-qualified variables // Calculate hash key for module-qualified variables
#[cfg(not(feature = "no_module"))]
Expr::Variable(id, Some(modules), _, _) => { Expr::Variable(id, Some(modules), _, _) => {
let hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty()); let hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty());
modules.set_key(hash); modules.set_key(hash);
@ -1347,7 +1361,10 @@ fn make_dot_expr(
} }
// lhs.module::id - syntax error // lhs.module::id - syntax error
(_, Expr::Variable(_, Some(modules), _, _)) => { (_, 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.dot_lhs.dot_rhs
(lhs, Expr::Dot(dot_lhs, dot_rhs, dot_pos)) => Expr::Dot( (lhs, Expr::Dot(dot_lhs, dot_rhs, dot_pos)) => Expr::Dot(