Skip to content

Commit e2bab51

Browse files
committed
Intern identifiers that match builtins
1 parent d868436 commit e2bab51

8 files changed

Lines changed: 896 additions & 59 deletions

File tree

crates/dreammaker/src/ast.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use get_size_derive::GetSize;
1212
use phf::phf_map;
1313

1414
use crate::error::Location;
15+
use crate::intern::intern_static;
1516

1617
/// Arguments for [`Term::Pick`]
1718
pub type PickArgs = [(Option<Expression>, Expression)];
@@ -728,7 +729,24 @@ pub struct Ident {
728729

729730
impl Ident {
730731
pub fn from_nonstatic(str: &str) -> Self {
731-
str.to_owned().into()
732+
if let Some(i) = intern_static(str) {
733+
Ident {
734+
inner: Cow::borrowed(i),
735+
}
736+
} else {
737+
Ident {
738+
inner: Cow::owned(str.to_owned()),
739+
}
740+
}
741+
}
742+
743+
pub(crate) fn from_static(str: &'static str) -> Self {
744+
debug_assert!(
745+
intern_static(str).is_some(),
746+
"Missing from STATIC_INDENTS: {:?}",
747+
str
748+
);
749+
Ident { inner: str.into() }
732750
}
733751

734752
pub fn as_str(&self) -> &str {
@@ -782,19 +800,29 @@ impl<'a> PartialEq<Ident> for &'a str {
782800

783801
impl From<&'static str> for Ident {
784802
fn from(v: &'static str) -> Self {
785-
Ident { inner: v.into() }
803+
Ident {
804+
inner: Cow::borrowed(v),
805+
}
786806
}
787807
}
788808

789809
impl From<String> for Ident {
790810
fn from(v: String) -> Self {
791-
Ident { inner: v.into() }
811+
if let Some(i) = intern_static(&v) {
812+
Ident {
813+
inner: Cow::borrowed(i),
814+
}
815+
} else {
816+
Ident {
817+
inner: Cow::owned(v),
818+
}
819+
}
792820
}
793821
}
794822

795823
impl From<ProcDeclKind> for Ident {
796824
fn from(value: ProcDeclKind) -> Self {
797-
value.name().into()
825+
Ident::from_static(value.name())
798826
}
799827
}
800828

@@ -1400,7 +1428,7 @@ pub struct VarTypeBuilder {
14001428
impl VarTypeBuilder {
14011429
pub fn suffix(&mut self, suffix: &VarSuffix) {
14021430
if !suffix.list.is_empty() {
1403-
self.type_path.insert(0, "list".into());
1431+
self.type_path.insert(0, ident!("list"));
14041432
}
14051433
}
14061434

@@ -1455,7 +1483,7 @@ impl VarSuffix {
14551483
None
14561484
} else {
14571485
Some(Expression::from(Term::NewPrefab {
1458-
prefab: Box::new(Prefab::from(vec![(PathOp::Slash, "list".into())])),
1486+
prefab: Box::new(Prefab::from(vec![(PathOp::Slash, ident!("list"))])),
14591487
args: Some(args.into_boxed_slice()),
14601488
}))
14611489
}

crates/dreammaker/src/builtins.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ pub fn default_defines(defines: &mut DefineMap) {
2121

2222
// #define EXCEPTION(value) new /exception(value)
2323
defines.insert(
24-
"EXCEPTION".into(),
24+
ident!("EXCEPTION"),
2525
(
2626
location,
2727
Define::Function {
28-
params: vec!["value".into()],
28+
params: vec![ident!("value")],
2929
variadic: false,
3030
subst: vec![
31-
Ident("new".into(), true),
31+
Ident(ident!("new"), true),
3232
Punct(Punctuation::Slash),
33-
Ident("exception".into(), false),
33+
Ident(ident!("exception"), false),
3434
Punct(Punctuation::LParen),
35-
Ident("value".into(), false),
35+
Ident(ident!("value"), false),
3636
Punct(Punctuation::RParen),
3737
],
3838
docs: Default::default(),
@@ -42,30 +42,30 @@ pub fn default_defines(defines: &mut DefineMap) {
4242

4343
// #define ASSERT(expression) if (!(expression)) { CRASH("[__FILE__]:[__LINE__]:Assertion Failed: [#X]") }
4444
defines.insert(
45-
"ASSERT".into(),
45+
ident!("ASSERT"),
4646
(
4747
location,
4848
Define::Function {
49-
params: vec!["expression".into()],
49+
params: vec![ident!("expression")],
5050
variadic: false,
5151
subst: vec![
52-
Ident("if".into(), true),
52+
Ident(ident!("if"), true),
5353
Punct(Punctuation::LParen),
5454
Punct(Punctuation::Not),
5555
Punct(Punctuation::LParen),
56-
Ident("expression".into(), false),
56+
Ident(ident!("expression"), false),
5757
Punct(Punctuation::RParen),
5858
Punct(Punctuation::RParen),
5959
Punct(Punctuation::LBrace),
60-
Ident("CRASH".into(), false),
60+
Ident(ident!("CRASH"), false),
6161
Punct(Punctuation::LParen),
6262
InterpStringBegin("".to_owned()),
63-
Ident("__FILE__".into(), false),
64-
InterpStringPart(":".into()),
65-
Ident("__LINE__".into(), false),
63+
Ident(ident!("__FILE__"), false),
64+
InterpStringPart(":".to_owned()),
65+
Ident(ident!("__LINE__"), false),
6666
InterpStringPart(":Assertion Failed: ".to_owned()),
6767
Punct(Punctuation::Hash),
68-
Ident("expression".into(), false),
68+
Ident(ident!("expression"), false),
6969
InterpStringEnd("".to_owned()),
7070
Punct(Punctuation::RParen),
7171
Punct(Punctuation::RBrace),
@@ -76,14 +76,26 @@ pub fn default_defines(defines: &mut DefineMap) {
7676
);
7777

7878
// constants
79+
fn constants(defines: &mut DefineMap, values: &[(&'static str, [Token; 1])]) {
80+
for &(name, ref value) in values {
81+
let previous = defines.insert(
82+
crate::ast::Ident::from_static(name),
83+
(
84+
Location::builtins(),
85+
Define::Constant {
86+
subst: value.to_vec(),
87+
docs: Default::default(),
88+
},
89+
),
90+
);
91+
assert!(previous.is_none(), "redefined: {}", name);
92+
}
93+
}
7994
macro_rules! c {
8095
($($i:ident = $($x:expr),*;)*) => {
81-
for &(name, ref value) in &[
96+
constants(defines, &[
8297
$((stringify!($i), [$($x,)*]),)*
83-
] {
84-
let previous = defines.insert(name.into(), (location, Define::Constant { subst: value.to_vec(), docs: Default::default() }));
85-
assert!(previous.is_none(), "redefined: {}", name);
86-
}
98+
]);
8799
}
88100
}
89101
c! {
@@ -213,7 +225,6 @@ pub fn default_defines(defines: &mut DefineMap) {
213225
LINEAR_RAND = Int(2);
214226
SQUARE_RAND = Int(3);
215227

216-
217228
// json encode flags (515)
218229
JSON_PRETTY_PRINT = Int(1);
219230

@@ -227,7 +238,11 @@ pub fn default_defines(defines: &mut DefineMap) {
227238
pub fn register_builtins(tree: &mut ObjectTreeBuilder) {
228239
fn path(path: &'static [&'static str]) -> Constant {
229240
Constant::Prefab(Box::new(super::constants::Pop {
230-
path: path.iter().copied().map(Ident::from).collect::<Box<[_]>>(),
241+
path: path
242+
.iter()
243+
.copied()
244+
.map(Ident::from_static)
245+
.collect::<Box<[_]>>(),
231246
vars: Default::default(),
232247
}))
233248
}
@@ -244,7 +259,7 @@ pub fn register_builtins(tree: &mut ObjectTreeBuilder) {
244259
}
245260
macro_rules! string {
246261
($e:expr) => {
247-
Constant::String($e.into())
262+
Constant::String(ident!($e))
248263
};
249264
}
250265

crates/dreammaker/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ pub struct Location {
296296
}
297297

298298
impl Location {
299-
pub fn builtins() -> Location {
299+
pub const fn builtins() -> Location {
300300
Location {
301301
file: FILEID_BUILTINS,
302302
line: 1,

0 commit comments

Comments
 (0)