mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Move Scope into its own file
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
collections::HashMap,
|
||||
collections::{BTreeMap, HashSet},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use swc_common::Spanned;
|
||||
@@ -12,27 +10,11 @@ use crate::{
|
||||
asm::{Builtin, Register, Value},
|
||||
constants::CONSTANTS,
|
||||
name_allocator::{PointerAllocator, RegAllocator},
|
||||
scope::{init_std_scope, NameId, OwnerId, Scope, ScopeTrait},
|
||||
};
|
||||
|
||||
use super::diagnostic::{Diagnostic, DiagnosticLevel};
|
||||
|
||||
#[derive(Hash, PartialEq, Eq, Clone, Debug)]
|
||||
pub enum NameId {
|
||||
Span(swc_common::Span),
|
||||
Builtin(Builtin),
|
||||
Constant(&'static str),
|
||||
}
|
||||
|
||||
impl Spanned for NameId {
|
||||
fn span(&self) -> swc_common::Span {
|
||||
match self {
|
||||
NameId::Span(span) => *span,
|
||||
NameId::Builtin(_) => swc_common::DUMMY_SP,
|
||||
NameId::Constant(_) => swc_common::DUMMY_SP,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Make use of these in the next phase of the compiler, remove the
|
||||
// allow(dead_code) attributes
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -2073,94 +2055,6 @@ impl ScopeAnalysis {
|
||||
}
|
||||
}
|
||||
|
||||
struct ScopeData {
|
||||
pub owner_id: OwnerId,
|
||||
pub name_map: HashMap<swc_atoms::JsWord, NameId>,
|
||||
pub parent: Option<Rc<RefCell<ScopeData>>>,
|
||||
}
|
||||
|
||||
type Scope = Rc<RefCell<ScopeData>>;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||
pub enum OwnerId {
|
||||
Span(swc_common::Span),
|
||||
Module,
|
||||
}
|
||||
|
||||
trait ScopeTrait {
|
||||
fn get(&self, name: &swc_atoms::JsWord) -> Option<NameId>;
|
||||
fn set(
|
||||
&self,
|
||||
name: &swc_atoms::JsWord,
|
||||
name_id: NameId,
|
||||
span: swc_common::Span,
|
||||
diagnostics: &mut Vec<Diagnostic>,
|
||||
);
|
||||
fn nest(&self, name_owner_location: Option<OwnerId>) -> Rc<RefCell<ScopeData>>;
|
||||
}
|
||||
|
||||
impl ScopeTrait for Scope {
|
||||
fn get(&self, name: &swc_atoms::JsWord) -> Option<NameId> {
|
||||
match self.borrow().name_map.get(name) {
|
||||
Some(mapped_name) => Some(mapped_name.clone()),
|
||||
None => match &self.borrow().parent {
|
||||
Some(parent) => parent.get(name),
|
||||
None => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn set(
|
||||
&self,
|
||||
name: &swc_atoms::JsWord,
|
||||
name_id: NameId,
|
||||
span: swc_common::Span,
|
||||
diagnostics: &mut Vec<Diagnostic>,
|
||||
) {
|
||||
let old_mapping = self.borrow_mut().name_map.insert(name.clone(), name_id);
|
||||
|
||||
if old_mapping.is_some() {
|
||||
diagnostics.push(Diagnostic {
|
||||
level: DiagnosticLevel::Error,
|
||||
message: "Scope overwrite occurred (TODO: being permissive about this)".to_string(),
|
||||
span,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn nest(&self, name_owner_location: Option<OwnerId>) -> Rc<RefCell<ScopeData>> {
|
||||
return Rc::new(RefCell::new(ScopeData {
|
||||
owner_id: name_owner_location.unwrap_or(self.borrow().owner_id.clone()),
|
||||
name_map: Default::default(),
|
||||
parent: Some(self.clone()),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
fn init_std_scope() -> Scope {
|
||||
let mut name_map = HashMap::new();
|
||||
|
||||
for name in BUILTIN_NAMES {
|
||||
name_map.insert(
|
||||
swc_atoms::JsWord::from(name),
|
||||
NameId::Builtin(Builtin {
|
||||
name: name.to_string(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
for (name, _) in CONSTANTS {
|
||||
name_map.insert(swc_atoms::JsWord::from(name), NameId::Constant(name));
|
||||
}
|
||||
|
||||
Rc::new(RefCell::new(ScopeData {
|
||||
owner_id: OwnerId::Module,
|
||||
name_map,
|
||||
parent: None,
|
||||
}))
|
||||
.nest(None)
|
||||
}
|
||||
|
||||
fn is_declare(decl: &swc_ecma_ast::Decl) -> bool {
|
||||
match decl {
|
||||
swc_ecma_ast::Decl::Class(class_decl) => class_decl.declare,
|
||||
|
||||
Reference in New Issue
Block a user