Merge branch 'master' into plugins

This commit is contained in:
Stephen Chung 2020-05-06 22:57:16 +08:00
commit 898bb229e8

View File

@ -221,11 +221,11 @@ impl Stack {
}) })
.and_then(|(i, _)| NonZeroUsize::new(i + 1)) .and_then(|(i, _)| NonZeroUsize::new(i + 1))
} }
/// Find a sub-scope by name in the `Stack`, searching in reverse. /// Find a module by name in the `Stack`, searching in reverse.
/// The return value is the offset to be deducted from `Stack::len`, /// The return value is the offset to be deducted from `Stack::len`,
/// i.e. the top element of the `Stack` is offset 1. /// i.e. the top element of the `Stack` is offset 1.
/// Return zero when the variable name is not found in the `Stack`. /// Return zero when the variable name is not found in the `Stack`.
pub fn find_sub_scope(&self, name: &str) -> Option<NonZeroUsize> { pub fn find_module(&self, name: &str) -> Option<NonZeroUsize> {
self.0 self.0
.iter() .iter()
.rev() .rev()
@ -1115,7 +1115,7 @@ fn parse_primary<'a>(
modules = Some(Box::new(vec)); modules = Some(Box::new(vec));
let root = modules.as_ref().unwrap().iter().next().unwrap(); let root = modules.as_ref().unwrap().iter().next().unwrap();
index = stack.find_sub_scope(&root.0); index = stack.find_module(&root.0);
} }
Expr::Variable(Box::new(id2), modules, index, pos2) Expr::Variable(Box::new(id2), modules, index, pos2)
@ -1831,13 +1831,24 @@ fn parse_let<'a>(
ScopeEntryType::Constant => { ScopeEntryType::Constant => {
Err(PERR::ForbiddenConstantExpr(name).into_err(init_value.position())) Err(PERR::ForbiddenConstantExpr(name).into_err(init_value.position()))
} }
// Variable cannot be a sub-scope // Variable cannot be a module
ScopeEntryType::Module => unreachable!(), ScopeEntryType::Module => unreachable!(),
} }
} else { } else {
// let name // let name
match var_type {
ScopeEntryType::Normal => {
stack.push((name.clone(), ScopeEntryType::Normal));
Ok(Stmt::Let(Box::new(name), None, pos)) Ok(Stmt::Let(Box::new(name), None, pos))
} }
ScopeEntryType::Constant => {
stack.push((name.clone(), ScopeEntryType::Constant));
Ok(Stmt::Const(Box::new(name), Box::new(Expr::Unit(pos)), pos))
}
// Variable cannot be a module
ScopeEntryType::Module => unreachable!(),
}
}
} }
/// Parse an import statement. /// Parse an import statement.