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:
@@ -9,7 +9,7 @@ pub struct CollectLocals<'c> {
|
||||
}
|
||||
|
||||
// - Python's LEGB methodology seems to be good. Look up variables in this order:
|
||||
// - Local
|
||||
// - Name
|
||||
// - Enclosing functions (i.e. inner functions)
|
||||
// - Global
|
||||
// - Builtin
|
||||
@@ -35,7 +35,7 @@ impl Visit for CollectLocals<'_> {
|
||||
fn visit_assign_stmt(&mut self, assign: &AssignStmt) -> Self::Out { DefaultAccept::default_accept(assign, self); }
|
||||
fn visit_lhs_expr(&mut self, lhs_expr: &LhsExpr) -> Self::Out {
|
||||
match lhs_expr {
|
||||
LhsExpr::Local(name) => {
|
||||
LhsExpr::Name(name) => {
|
||||
let sym = global_sym(name.to_string());
|
||||
self.compile.create_local(sym);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -264,7 +264,7 @@ impl Visit for CompileBody<'_> {
|
||||
let attr = global_sym(expr.access.to_string());
|
||||
thunk.push(Inst::SetAttr(attr));
|
||||
}
|
||||
LhsExpr::Local(local_name) => {
|
||||
LhsExpr::Name(local_name) => {
|
||||
let sym = global_sym(local_name.to_string());
|
||||
let local = self.compile.lookup_scope_or_create_global(sym);
|
||||
thunk = Inst::Pop(Some(local)).into();
|
||||
@@ -349,9 +349,9 @@ impl Visit for CompileBody<'_> {
|
||||
let thunk = match atom {
|
||||
Atom::Ident(ident) => {
|
||||
let sym = global_sym(ident.to_string());
|
||||
let local = self.compile.lookup_scope_or_create_global(sym);
|
||||
let name = self.compile.lookup_scope_or_create_global(sym);
|
||||
// get local
|
||||
Inst::PushLocal(local).into()
|
||||
Inst::LoadName(name).into()
|
||||
}
|
||||
Atom::Sym(sym) => {
|
||||
// push symbol
|
||||
|
||||
Reference in New Issue
Block a user