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

40
Cargo.lock generated
View File

@@ -448,6 +448,12 @@ dependencies = [
"ahash",
]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@@ -1114,6 +1120,12 @@ dependencies = [
"semver 0.9.0",
]
[[package]]
name = "rustversion"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f3208ce4d8448b3f3e7d168a73f5e0c43a61e32930de3bceeccedb388b6bf06"
[[package]]
name = "ryu"
version = "1.0.9"
@@ -1328,6 +1340,25 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]]
name = "strum"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f"
[[package]]
name = "strum_macros"
version = "0.24.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]]
name = "supports-color"
version = "1.3.0"
@@ -2167,6 +2198,14 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "valuescript_common"
version = "0.1.0"
dependencies = [
"strum",
"strum_macros",
]
[[package]]
name = "valuescript_compiler"
version = "0.1.0"
@@ -2179,6 +2218,7 @@ dependencies = [
"swc_common",
"swc_ecma_ast",
"swc_ecma_parser",
"valuescript_common",
]
[[package]]

View File

@@ -3,6 +3,7 @@
members = [
"valuescript_vm",
"valuescript_compiler",
"valuescript_common",
"valuescript_program_embed",
"valuescript_wasm",
"vstc",

View File

@@ -0,0 +1,10 @@
[package]
name = "valuescript_common"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
strum = "0.24"
strum_macros = "0.24"

View File

@@ -0,0 +1 @@
pub const BUILTIN_NAMES: [&str; 4] = ["Debug", "Math", "String", "Number"];

View File

@@ -0,0 +1,3 @@
mod builtins;
pub use builtins::*;

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)
}