Clean up the scope struct to lookup in the parent as well

This commit is contained in:
Sameer Rahmani 2020-09-10 23:28:20 +01:00
parent 317726975c
commit 5c6635fd0c
3 changed files with 32 additions and 18 deletions

View File

@ -64,7 +64,7 @@ pub struct Compiler<'ctx> {
// /// is `abc.xyz` in this case and the otherone would be
// /// `/path/to/abc/xyz.srn` file that contains the ns.
pub namespaces: HashMap<&'ctx str, Namespace<'ctx>>,
// pub fpm: &'a PassManager<FunctionValue<'ctx>>,
//pub fpm: &'a PassManager<FunctionValue<'ctx>>,
// current_ns_name: Option<&'a str>,
}

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::scope::Scope;
//use crate::scope::Scope;
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::values::FunctionValue;
@ -24,28 +24,29 @@ pub struct Namespace<'ctx> {
/// think of modules as compilation units. Object files if you prefer.
/// This way we should be able to hot swap the namespaces.
pub module: Module<'ctx>,
scope: Scope<'ctx>,
// // The option of the current function being compiled
// current_fn_opt: Option<FunctionValue<'ctx>>,
//scope: Scope<'ctx>,
// The option of the current function being compiled
current_fn_opt: Option<FunctionValue<'ctx>>,
}
impl<'ctx> Namespace<'ctx> {
pub fn new(context: &'ctx Context, name: &str) -> Namespace<'ctx> {
Namespace {
module: context.create_module(&name),
scope: Scope::new(None),
// current_fn_opt: None,
//scope: Scope::new(None),
current_fn_opt: None,
}
}
// /// Gets a defined function given its name.
// #[inline]
// pub fn get_function(&self, name: &str) -> Option<FunctionValue<'ctx>> {
// self.module.get_function(name)
// }
/// Get a defined function given its name.
#[inline]
pub fn get_function(&self, name: &str) -> Option<FunctionValue<'ctx>> {
self.module.get_function(name)
}
// /// Returns the `FunctionValue` representing the function being compiled.
// #[inline]
// pub fn current_fn(&self) -> FunctionValue<'ctx> {
// self.current_fn_opt.unwrap()
// }
/// Return the `FunctionValue` representing the function being compiled.
#[inline]
pub fn current_fn(&self) -> FunctionValue<'ctx> {
self.current_fn_opt.unwrap()
}
}

View File

@ -17,6 +17,9 @@
use inkwell::values::PointerValue;
use std::collections::HashMap;
/// Scopes in **Serene** are simply represented by hashmaps. Each
/// Scope optionally has a parent scope that lookups fallback to
/// if the lookup key is missing from the current scope.
pub struct Scope<'a> {
parent: Option<Box<Scope<'a>>>,
symbol_table: HashMap<String, PointerValue<'a>>,
@ -35,8 +38,18 @@ impl<'a> Scope<'a> {
}
}
/// Lookup the given `key` in the scope and if it is not in the current
/// scope look it up in the `parent` scope.
pub fn lookup(&self, key: &str) -> Option<PointerValue> {
self.symbol_table.get(key).map(|x| *x)
let v = self.symbol_table.get(key);
if let None = v {
return match &self.parent {
Some(x) => x.lookup(key),
None => None,
};
}
v.map(|x| *x)
}
pub fn insert(&mut self, key: &str, val: PointerValue<'a>) {