Rename "Local" to "Name" when referring to variables during compilation

* Variables were previously named "Local"s because they described a
  local variable - however, this is kind of a misnomer because it
  handles globals as well. This has been changed pretty much everywhere
  when appropriate (hopefully).
* Renamed obj/names.rs to obj/reserved.rs, freeing up the "names" module
  for the new Names handle
* Renamed obj/locals.rs to obj/names.rs, see above

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-09-18 13:57:51 -07:00
parent 7228732128
commit 0d6f68216b
14 changed files with 114 additions and 111 deletions

View File

@@ -9,10 +9,10 @@ use std::collections::{BTreeMap, HashMap};
#[derive(Default)]
pub struct Compile {
const_data: ConstData,
globals: BTreeMap<Sym, Local>,
locals: Vec<BTreeMap<Sym, Local>>,
globals: BTreeMap<Sym, Name>,
scope: Vec<BTreeMap<Sym, Name>>,
names: Vec<Sym>,
next_local: Local,
next_local: Name,
}
impl Compile {
@@ -54,8 +54,8 @@ impl Compile {
///
/// This will search up the locals stack until the given name is found, ultimately ending with
/// a global name lookup.
pub fn lookup_scope(&mut self, sym: Sym) -> Option<Local> {
self.locals
pub fn lookup_scope(&mut self, sym: Sym) -> Option<Name> {
self.scope
.iter()
.rev()
.filter_map(|locals| locals.get(&sym))
@@ -65,7 +65,7 @@ impl Compile {
}
/// Looks up a variable name, or creates a global name if it doesn't exist.
pub fn lookup_scope_or_create_global(&mut self, sym: Sym) -> Local {
pub fn lookup_scope_or_create_global(&mut self, sym: Sym) -> Name {
if let Some(local) = self.lookup_scope(sym) {
local
} else {
@@ -74,8 +74,8 @@ impl Compile {
}
/// Creates a new local variable if it does not exist in the current local scope.
pub(crate) fn create_local(&mut self, sym: Sym) -> Local {
let locals = self.locals.last_mut().expect("scope");
pub(crate) fn create_local(&mut self, sym: Sym) -> Name {
let locals = self.scope.last_mut().expect("scope");
if let Some(local) = locals.get(&sym) {
*local
} else {
@@ -90,7 +90,7 @@ impl Compile {
}
/// Creates a new global variable if it does not exist in the current global scope.
pub fn create_global(&mut self, sym: Sym) -> Local {
pub fn create_global(&mut self, sym: Sym) -> Name {
if let Some(global) = self.globals.get(&sym) {
*global
} else {
@@ -105,12 +105,12 @@ impl Compile {
/// Pushes an empty scope layer of local variables.
pub(crate) fn push_scope_layer(&mut self) {
self.locals.push(Default::default());
self.scope.push(Default::default());
}
/// Pops a scope layer of local variables, if any are available.
pub(crate) fn pop_scope_layer(&mut self) -> Option<BTreeMap<Sym, Local>> {
self.locals.pop()
pub(crate) fn pop_scope_layer(&mut self) -> Option<BTreeMap<Sym, Name>> {
self.scope.pop()
}
/// Collects local variables for the given AST body non-recursively.