Skip to content

Commit d868436

Browse files
committed
Use static Ident for ObjectTree builtins
1 parent e954543 commit d868436

3 files changed

Lines changed: 50 additions & 55 deletions

File tree

crates/dreammaker/src/objtree.rs

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ impl ObjectTreeBuilder {
994994
&mut self,
995995
location: Location,
996996
parent: NodeIndex,
997-
child: &str,
997+
child: &Ident,
998998
len: usize,
999999
) -> NodeIndex {
10001000
if let Some(&target) = self.inner[parent].children.get(child) {
@@ -1100,24 +1100,24 @@ impl ObjectTreeBuilder {
11001100
)
11011101
}
11021102

1103-
fn get_from_path<'a, I: Iterator<Item = &'a str>>(
1103+
fn get_from_path<I: Iterator<Item = Ident>>(
11041104
&mut self,
11051105
location: Location,
11061106
mut path: I,
11071107
len: usize,
1108-
) -> Result<(NodeIndex, &'a str), DMError> {
1108+
) -> Result<(NodeIndex, Ident), DMError> {
11091109
let mut current = NodeIndex::new(0);
11101110
let mut last = match path.next() {
11111111
Some(name) => name,
11121112
None => return Err(DMError::new(location, "cannot register root path")),
11131113
};
1114-
if is_decl(last) {
1114+
if is_decl(&last) {
11151115
return Ok((current, last));
11161116
}
11171117
for each in path {
1118-
current = self.subtype_or_add(location, current, last, len);
1118+
current = self.subtype_or_add(location, current, &last, len);
11191119
last = each;
1120-
if is_decl(last) {
1120+
if is_decl(&last) {
11211121
break;
11221122
}
11231123
}
@@ -1129,33 +1129,33 @@ impl ObjectTreeBuilder {
11291129
&mut self,
11301130
location: Location,
11311131
parent: NodeIndex,
1132-
mut prev: &'a str,
1132+
mut prev: Ident,
11331133
mut rest: I,
11341134
comment: DocCollection,
11351135
suffix: VarSuffix,
11361136
) -> Result<Option<&mut TypeVar>, DMError>
11371137
where
1138-
I: Iterator<Item = &'a str>,
1138+
I: Iterator<Item = Ident>,
11391139
{
11401140
use super::ast::VarTypeFlags;
11411141
let mut is_declaration = false;
11421142
let mut flags = VarTypeFlags::default();
11431143

1144-
if is_var_decl(prev) {
1144+
if is_var_decl(&prev) {
11451145
is_declaration = true;
11461146
prev = match rest.next() {
11471147
Some(name) => name,
11481148
None => return Ok(None), // var{} block, children will be real vars
11491149
};
1150-
while let Some(flag) = VarTypeFlags::from_name(prev) {
1150+
while let Some(flag) = VarTypeFlags::from_name(&prev) {
11511151
if let Some(name) = rest.next() {
11521152
flags |= flag;
11531153
prev = name;
11541154
} else {
11551155
return Ok(None); // var/const{} block, children will be real vars
11561156
}
11571157
}
1158-
} else if is_proc_decl(prev) {
1158+
} else if is_proc_decl(&prev) {
11591159
return Err(DMError::new(location, "proc looks like a var"));
11601160
}
11611161

@@ -1174,28 +1174,26 @@ impl ObjectTreeBuilder {
11741174
let symbols = &mut self.symbols;
11751175
let node = &mut self.inner.graph[parent.index()];
11761176
// TODO: warn and merge docs for repeats
1177-
Ok(Some(
1178-
node.vars
1179-
.entry(Ident::from_nonstatic(prev))
1180-
.or_insert_with(|| TypeVar {
1181-
value: VarValue {
1177+
Ok(Some(node.vars.entry(prev.clone()).or_insert_with(|| {
1178+
TypeVar {
1179+
value: VarValue {
1180+
location,
1181+
expression: suffix.into_initializer(),
1182+
constant: None,
1183+
being_evaluated: false,
1184+
docs: comment,
1185+
},
1186+
declaration: if is_declaration {
1187+
Some(VarDeclaration {
1188+
var_type: var_type.build(),
11821189
location,
1183-
expression: suffix.into_initializer(),
1184-
constant: None,
1185-
being_evaluated: false,
1186-
docs: comment,
1187-
},
1188-
declaration: if is_declaration {
1189-
Some(VarDeclaration {
1190-
var_type: var_type.build(),
1191-
location,
1192-
id: symbols.allocate(),
1193-
})
1194-
} else {
1195-
None
1196-
},
1197-
}),
1198-
))
1190+
id: symbols.allocate(),
1191+
})
1192+
} else {
1193+
None
1194+
},
1195+
}
1196+
})))
11991197
}
12001198

12011199
// It's fine.
@@ -1205,21 +1203,18 @@ impl ObjectTreeBuilder {
12051203
context: &Context,
12061204
location: Location,
12071205
parent: NodeIndex,
1208-
name: &str,
1206+
name: &Ident,
12091207
declaration: Option<ProcDeclBuilder>,
12101208
parameters: Vec<Parameter>,
12111209
return_type: ProcReturnType,
12121210
code: Option<Block>,
12131211
body_range: Option<Range<Location>>,
12141212
) -> Result<(usize, &mut ProcValue), DMError> {
12151213
let node = &mut self.inner.graph[parent.index()];
1216-
let proc = node
1217-
.procs
1218-
.entry(Ident::from_nonstatic(name))
1219-
.or_insert_with(|| TypeProc {
1220-
value: Vec::with_capacity(1),
1221-
declaration: None,
1222-
});
1214+
let proc = node.procs.entry(name.clone()).or_insert_with(|| TypeProc {
1215+
value: Vec::with_capacity(1),
1216+
declaration: None,
1217+
});
12231218
if let Some(decl_builder) = declaration {
12241219
if let Some(ref decl) = proc.declaration {
12251220
DMError::new(
@@ -1283,24 +1278,24 @@ impl ObjectTreeBuilder {
12831278
pub(crate) fn add_builtin_type(&mut self, elems: &[&'static str]) -> &mut Type {
12841279
self.add_type(
12851280
Location::builtins(),
1286-
elems.iter().cloned(),
1281+
elems.iter().copied().map(Ident::from),
12871282
elems.len() + 1,
12881283
Default::default(),
12891284
)
12901285
.unwrap()
12911286
}
12921287

12931288
// an entry which may be anything depending on the path
1294-
fn add_type<'a, I: Iterator<Item = &'a str>>(
1289+
fn add_type<I: Iterator<Item = Ident>>(
12951290
&mut self,
12961291
location: Location,
12971292
mut path: I,
12981293
len: usize,
12991294
comment: DocCollection,
13001295
) -> Result<&mut Type, DMError> {
13011296
let (parent, child) = self.get_from_path(location, &mut path, len)?;
1302-
assert!(!is_var_decl(child) && !is_proc_decl(child));
1303-
let idx = self.subtype_or_add(location, parent, child, len);
1297+
assert!(!is_var_decl(&child) && !is_proc_decl(&child));
1298+
let idx = self.subtype_or_add(location, parent, &child, len);
13041299
self.inner[idx].docs.extend(comment);
13051300
Ok(&mut self.inner[idx])
13061301
}
@@ -1311,7 +1306,7 @@ impl ObjectTreeBuilder {
13111306
value: Option<Constant>,
13121307
) -> &mut VarValue {
13131308
let location = Location::builtins();
1314-
let mut path = elems.iter().copied();
1309+
let mut path = elems.iter().copied().map(Ident::from);
13151310
let len = elems.len() + 1;
13161311

13171312
let (parent, initial) = self.get_from_path(location, &mut path, len).unwrap();
@@ -1342,7 +1337,7 @@ impl ObjectTreeBuilder {
13421337
self.add_proc(
13431338
&Default::default(),
13441339
Location::builtins(),
1345-
elems.iter().copied(),
1340+
elems.iter().copied().map(Ident::from),
13461341
elems.len() + 1,
13471342
params
13481343
.iter()
@@ -1361,7 +1356,7 @@ impl ObjectTreeBuilder {
13611356

13621357
// an entry which is definitely a proc because an argument list is specified
13631358
#[allow(clippy::too_many_arguments)]
1364-
fn add_proc<'a, I: Iterator<Item = &'a str>>(
1359+
fn add_proc<I: Iterator<Item = Ident>>(
13651360
&mut self,
13661361
context: &Context,
13671362
location: Location,
@@ -1373,9 +1368,9 @@ impl ObjectTreeBuilder {
13731368
) -> Result<(usize, &mut ProcValue), DMError> {
13741369
let (parent, mut proc_name) = self.get_from_path(location, &mut path, len)?;
13751370
let mut declaration = None;
1376-
if let Some(kind) = ProcDeclKind::from_name(proc_name) {
1371+
if let Some(kind) = ProcDeclKind::from_name(&proc_name) {
13771372
let mut next_entry = path.next();
1378-
let flags = ProcFlags::from_name(next_entry.unwrap_or(""));
1373+
let flags = ProcFlags::from_name(next_entry.as_ref().map_or("", Ident::as_str));
13791374
if flags.is_some() {
13801375
// did something? take another step
13811376
next_entry = path.next();
@@ -1385,7 +1380,7 @@ impl ObjectTreeBuilder {
13851380
Some(name) => name,
13861381
None => return Err(DMError::new(location, "proc must have a name")),
13871382
};
1388-
} else if is_var_decl(proc_name) {
1383+
} else if is_var_decl(&proc_name) {
13891384
return Err(DMError::new(location, "var looks like a proc"));
13901385
}
13911386
if let Some(other) = path.next() {
@@ -1399,7 +1394,7 @@ impl ObjectTreeBuilder {
13991394
context,
14001395
location,
14011396
parent,
1402-
proc_name,
1397+
&proc_name,
14031398
declaration,
14041399
parameters,
14051400
ProcReturnType::default(),

crates/dreammaker/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ impl<'ctx, 'an, 'inp> Parser<'ctx, 'an, 'inp> {
11871187
&mut self,
11881188
current: NodeIndex,
11891189
proc_builder: Option<ProcDeclBuilder>,
1190-
name: &str,
1190+
name: &Ident,
11911191
entry_start: Location,
11921192
absolute: bool,
11931193
mut docs: DocCollection,

crates/dreammaker/tests/expression_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn ternary_precedence() {
2020
parse_expr("foo = 1 + 2 ? 3 + 4 : 5 + 6"),
2121
Expression::AssignOp {
2222
op: AssignOp::Assign,
23-
lhs: Box::new(Expression::from(Term::Ident("foo".to_owned()))),
23+
lhs: Box::new(Expression::from(Term::Ident("foo".into()))),
2424
rhs: Box::new(Expression::TernaryOp {
2525
cond: Box::new(Expression::BinaryOp {
2626
op: BinaryOp::Add,
@@ -51,7 +51,7 @@ fn in_after_ternary() {
5151
op: BinaryOp::In,
5252
lhs: Box::new(Expression::AssignOp {
5353
op: AssignOp::Assign,
54-
lhs: Box::new(Expression::from(Term::Ident("foo".to_owned()))),
54+
lhs: Box::new(Expression::from(Term::Ident("foo".into()))),
5555
rhs: Box::new(Expression::TernaryOp {
5656
cond: Box::new(Expression::from(Term::Int(1))),
5757
if_: Box::new(Expression::from(Term::Int(2))),
@@ -173,7 +173,7 @@ fn loaded_call_ext() {
173173
Default::default(),
174174
Term::ExternalCall {
175175
library: None,
176-
function: Box::new(Expression::from(Term::Ident("loaded_cat_meow".to_owned()))),
176+
function: Box::new(Expression::from(Term::Ident("loaded_cat_meow".into()))),
177177
args: Box::new([
178178
Expression::Base {
179179
term: Box::new(Spanned::new(Default::default(), Term::Int(1))),

0 commit comments

Comments
 (0)