From a5072cb6a62cb1f54068b9200af9c78a6ef60344 Mon Sep 17 00:00:00 2001 From: Tad Hardesty Date: Fri, 8 May 2020 17:33:38 -0700 Subject: [PATCH] Add new ObjectTree var creation helpers --- src/dreammaker/objtree.rs | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/dreammaker/objtree.rs b/src/dreammaker/objtree.rs index dffae8d8..167ec35b 100644 --- a/src/dreammaker/objtree.rs +++ b/src/dreammaker/objtree.rs @@ -882,6 +882,69 @@ impl ObjectTree { node } + fn insert_var( + &mut self, + ty: NodeIndex, + name: &str, + value: VarValue, + declaration: Option, + ) -> &mut TypeVar { + // TODO: warn and merge docs for repeats + match self[ty].vars.entry(name.to_owned()) { + linked_hash_map::Entry::Vacant(slot) => { + slot.insert(TypeVar { value, declaration }) + }, + linked_hash_map::Entry::Occupied(slot) => { + let type_var = slot.into_mut(); + if let Some(declaration) = declaration { + type_var.declaration = Some(declaration); + } + type_var.value = value; + type_var + }, + } + } + + pub(crate) fn declare_var( + &mut self, + ty: NodeIndex, + name: &str, + location: Location, + docs: DocCollection, + var_type: VarType, + expression: Option, + ) -> &mut TypeVar { + let id = self.symbols.allocate(); + self.insert_var(ty, name, VarValue { + location, + expression, + docs, + constant: None, + being_evaluated: false, + }, Some(VarDeclaration { + var_type, + location, + id, + })) + } + + pub(crate) fn override_var( + &mut self, + ty: NodeIndex, + name: &str, + location: Location, + docs: DocCollection, + expression: Expression, + ) -> &mut TypeVar { + self.insert_var(ty, name, VarValue { + location, + expression: Some(expression), + docs, + constant: None, + being_evaluated: false, + }, None) + } + fn get_from_path<'a, I: Iterator>( &mut self, location: Location,