Clean up the scope struct to lookup in the parent as well
This commit is contained in:
parent
317726975c
commit
5c6635fd0c
|
@ -64,7 +64,7 @@ pub struct Compiler<'ctx> {
|
||||||
// /// is `abc.xyz` in this case and the otherone would be
|
// /// is `abc.xyz` in this case and the otherone would be
|
||||||
// /// `/path/to/abc/xyz.srn` file that contains the ns.
|
// /// `/path/to/abc/xyz.srn` file that contains the ns.
|
||||||
pub namespaces: HashMap<&'ctx str, Namespace<'ctx>>,
|
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>,
|
// current_ns_name: Option<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* 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::context::Context;
|
||||||
use inkwell::module::Module;
|
use inkwell::module::Module;
|
||||||
use inkwell::values::FunctionValue;
|
use inkwell::values::FunctionValue;
|
||||||
|
@ -24,28 +24,29 @@ pub struct Namespace<'ctx> {
|
||||||
/// think of modules as compilation units. Object files if you prefer.
|
/// think of modules as compilation units. Object files if you prefer.
|
||||||
/// This way we should be able to hot swap the namespaces.
|
/// This way we should be able to hot swap the namespaces.
|
||||||
pub module: Module<'ctx>,
|
pub module: Module<'ctx>,
|
||||||
scope: Scope<'ctx>,
|
//scope: Scope<'ctx>,
|
||||||
// // The option of the current function being compiled
|
|
||||||
// current_fn_opt: Option<FunctionValue<'ctx>>,
|
// The option of the current function being compiled
|
||||||
|
current_fn_opt: Option<FunctionValue<'ctx>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ctx> Namespace<'ctx> {
|
impl<'ctx> Namespace<'ctx> {
|
||||||
pub fn new(context: &'ctx Context, name: &str) -> Namespace<'ctx> {
|
pub fn new(context: &'ctx Context, name: &str) -> Namespace<'ctx> {
|
||||||
Namespace {
|
Namespace {
|
||||||
module: context.create_module(&name),
|
module: context.create_module(&name),
|
||||||
scope: Scope::new(None),
|
//scope: Scope::new(None),
|
||||||
// current_fn_opt: None,
|
current_fn_opt: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// /// Gets a defined function given its name.
|
/// Get a defined function given its name.
|
||||||
// #[inline]
|
#[inline]
|
||||||
// pub fn get_function(&self, name: &str) -> Option<FunctionValue<'ctx>> {
|
pub fn get_function(&self, name: &str) -> Option<FunctionValue<'ctx>> {
|
||||||
// self.module.get_function(name)
|
self.module.get_function(name)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// /// Returns the `FunctionValue` representing the function being compiled.
|
/// Return the `FunctionValue` representing the function being compiled.
|
||||||
// #[inline]
|
#[inline]
|
||||||
// pub fn current_fn(&self) -> FunctionValue<'ctx> {
|
pub fn current_fn(&self) -> FunctionValue<'ctx> {
|
||||||
// self.current_fn_opt.unwrap()
|
self.current_fn_opt.unwrap()
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
15
src/scope.rs
15
src/scope.rs
|
@ -17,6 +17,9 @@
|
||||||
use inkwell::values::PointerValue;
|
use inkwell::values::PointerValue;
|
||||||
use std::collections::HashMap;
|
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> {
|
pub struct Scope<'a> {
|
||||||
parent: Option<Box<Scope<'a>>>,
|
parent: Option<Box<Scope<'a>>>,
|
||||||
symbol_table: HashMap<String, PointerValue<'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> {
|
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>) {
|
pub fn insert(&mut self, key: &str, val: PointerValue<'a>) {
|
||||||
|
|
Loading…
Reference in New Issue