Deduplicate builtin name list

This commit is contained in:
Andrew Morris
2023-03-20 08:42:09 +11:00
parent 71bedae91e
commit 8dfb174cb2
9 changed files with 91 additions and 72 deletions

View File

@@ -14,3 +14,5 @@ swc_ecma_parser = "0.102.2"
swc = "0.168.3"
swc_ecma_ast = "0.76.0"
queues = "1.0.2"
valuescript_common = { path = "../valuescript_common" }

View File

@@ -1,6 +1,8 @@
use std::collections::HashMap;
use std::str::FromStr;
use valuescript_common::BUILTIN_NAMES;
use crate::asm::{
Array, Builtin, Class, Definition, DefinitionContent, Function, Instruction, InstructionOrLabel,
Label, LabelRef, Module, Object, Pointer, Register, Value,
@@ -747,23 +749,10 @@ impl<'a> AssemblyParser<'a> {
}
fn assemble_builtin(&mut self) -> Builtin {
match self
.parse_one_of(&["$Math", "$Debug", "$String", "$Number"])
.as_str()
{
"$Math" => Builtin {
name: "Math".to_string(),
},
"$Debug" => Builtin {
name: "Debug".to_string(),
},
"$String" => Builtin {
name: "String".to_string(),
},
"$Number" => Builtin {
name: "Number".to_string(),
},
_ => panic!("Shouldn't happen"),
self.parse_exact("$");
Builtin {
name: self.parse_one_of(&BUILTIN_NAMES),
}
}

View File

@@ -2,6 +2,8 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use valuescript_common::BUILTIN_NAMES;
use crate::asm::{Builtin, Pointer, Register};
use super::function_compiler::QueuedFunction;
@@ -82,32 +84,17 @@ pub fn _init_scope() -> Scope {
pub fn init_std_scope() -> Scope {
Scope {
rc: Rc::new(RefCell::new(ScopeData {
name_map: HashMap::from([
(
"Math".to_string(),
MappedName::Builtin(Builtin {
name: "Math".to_string(),
}),
),
(
"Debug".to_string(),
MappedName::Builtin(Builtin {
name: "Debug".to_string(),
}),
),
(
"String".to_string(),
MappedName::Builtin(Builtin {
name: "String".to_string(),
}),
),
(
"Number".to_string(),
MappedName::Builtin(Builtin {
name: "Number".to_string(),
}),
),
]),
name_map: BUILTIN_NAMES
.iter()
.map(|name| {
(
name.to_string(),
MappedName::Builtin(Builtin {
name: name.to_string(),
}),
)
})
.collect(),
parent: None,
})),
}

View File

@@ -1,6 +1,7 @@
use std::{cell::RefCell, collections::HashMap, collections::HashSet, rc::Rc};
use swc_common::Spanned;
use valuescript_common::BUILTIN_NAMES;
use crate::asm::Builtin;
@@ -57,7 +58,7 @@ impl ScopeAnalysis {
let mut sa = ScopeAnalysis::default();
let scope = init_std_scope();
for builtin_name in vec!["Debug", "Math", "String", "Number"] {
for builtin_name in BUILTIN_NAMES {
let builtin = Builtin {
name: builtin_name.to_string(),
};
@@ -1614,35 +1615,20 @@ impl XScopeTrait for XScope {
}
fn init_std_scope() -> XScope {
return Rc::new(RefCell::new(XScopeData {
Rc::new(RefCell::new(XScopeData {
owner_id: OwnerId::Module,
name_map: HashMap::from([
(
swc_atoms::js_word!("Math"),
NameId::Builtin(Builtin {
name: "Math".to_string(),
}),
),
(
swc_atoms::JsWord::from("Debug"),
NameId::Builtin(Builtin {
name: "Debug".to_string(),
}),
),
(
swc_atoms::JsWord::from("String"),
NameId::Builtin(Builtin {
name: "String".to_string(),
}),
),
(
swc_atoms::JsWord::from("Number"),
NameId::Builtin(Builtin {
name: "Number".to_string(),
}),
),
]),
name_map: BUILTIN_NAMES
.iter()
.map(|name| {
(
swc_atoms::JsWord::from(*name),
NameId::Builtin(Builtin {
name: name.to_string(),
}),
)
})
.collect(),
parent: None,
}))
.nest(None);
.nest(None)
}