Skip to content

Commit

Permalink
Merge pull request #79 from pax-lang/wj/refactor-template-to-hashmap
Browse files Browse the repository at this point in the history
Small refactor to Manifest
  • Loading branch information
warfaj authored Dec 8, 2023
2 parents 445e0b5 + 73c8471 commit bf18037
Show file tree
Hide file tree
Showing 6 changed files with 2,904 additions and 2,866 deletions.
11 changes: 5 additions & 6 deletions pax-compiler/src/cartridge_generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,18 @@ fn generate_cartridge_render_nodes_literal(
"tried to generate render nodes literal for component, but template was undefined",
);

let implicit_root = nodes[0].borrow();
let implicit_root = nodes[&0].borrow();
let children_literal: Vec<String> = implicit_root
.child_ids
.iter()
.filter(|child_id| {
let tnd_map = rngc.active_component_definition.template.as_ref().unwrap();
let active_tnd = &tnd_map[**child_id];
let active_tnd = &tnd_map[*child_id];
active_tnd.type_id != parsing::TYPE_ID_COMMENT
})
.map(|child_id| {
let tnd_map = rngc.active_component_definition.template.as_ref().unwrap();
let active_tnd = &tnd_map[*child_id];
let active_tnd = &tnd_map[child_id];
recurse_generate_render_nodes_literal(rngc, active_tnd, host_crate_info, source_map)
})
.collect();
Expand Down Expand Up @@ -304,12 +304,11 @@ fn recurse_generate_render_nodes_literal(
.iter()
.filter(|child_id| {
let tnd_map = rngc.active_component_definition.template.as_ref().unwrap();
let active_tnd = &tnd_map[**child_id];
let active_tnd = &tnd_map[*child_id];
active_tnd.type_id != parsing::TYPE_ID_COMMENT
})
.map(|child_id| {
let active_tnd =
&rngc.active_component_definition.template.as_ref().unwrap()[*child_id];
let active_tnd = &rngc.active_component_definition.template.as_ref().unwrap()[child_id];
recurse_generate_render_nodes_literal(rngc, active_tnd, host_crate_info, source_map)
})
.collect();
Expand Down
12 changes: 6 additions & 6 deletions pax-compiler/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use pax_manifest::{
ValueDefinition,
};
use std::collections::HashMap;
use std::ops::{IndexMut, RangeFrom};
use std::ops::RangeFrom;
use std::slice::IterMut;

use crate::errors::source_map::SourceMap;
Expand Down Expand Up @@ -45,7 +45,7 @@ pub fn compile_all_expressions<'a>(

if let Some(ref mut template) = new_component_def.template {
let mut active_node_def = TemplateNodeDefinition::default();
std::mem::swap(&mut active_node_def, template.index_mut(0));
std::mem::swap(&mut active_node_def, template.get_mut(&0).unwrap());

let mut ctx = ExpressionCompilationContext {
template,
Expand All @@ -65,7 +65,7 @@ pub fn compile_all_expressions<'a>(
ctx = recurse_compile_expressions(ctx, source_map)?;
uid_track = ctx.uid_gen.next().unwrap();
all_expression_specs.extend(ctx.expression_specs.to_owned());
std::mem::swap(&mut ctx.active_node_def, template.index_mut(0));
std::mem::swap(&mut ctx.active_node_def, template.get_mut(&0).unwrap());
}

std::mem::swap(component_def, &mut new_component_def);
Expand Down Expand Up @@ -615,7 +615,7 @@ fn recurse_compile_expressions<'a>(
let mut old_active_node_def = TemplateNodeDefinition::default();

//Swap the first blank for the node with specified id
std::mem::swap(&mut active_node_def, ctx.template.get_mut(*id).unwrap());
std::mem::swap(&mut active_node_def, ctx.template.get_mut(id).unwrap());

//Swap the second blank for the current ctx.active_node_def value, so we can pass it back
//to caller when done
Expand All @@ -628,7 +628,7 @@ fn recurse_compile_expressions<'a>(
ctx = recurse_compile_expressions(ctx, source_map)?;

//Pull the (presumably mutated) active_node_def back out of ctx and attach it back into `template`
std::mem::swap(&mut ctx.active_node_def, ctx.template.get_mut(*id).unwrap());
std::mem::swap(&mut ctx.active_node_def, ctx.template.get_mut(id).unwrap());

//Put old active_node_def back in place so we can return it to caller
std::mem::swap(&mut old_active_node_def, &mut ctx.active_node_def);
Expand Down Expand Up @@ -774,7 +774,7 @@ pub struct ExpressionCompilationContext<'a> {
pub component_def: &'a ComponentDefinition,

/// Container for mutable list of TemplateNodeDefinitions,
pub template: &'a mut Vec<TemplateNodeDefinition>,
pub template: &'a mut HashMap<usize, TemplateNodeDefinition>,

/// Static stack of addressable properties, by string
/// Enables resolution of scope-nested symbolic identifiers, including shadowing
Expand Down
21 changes: 12 additions & 9 deletions pax-compiler/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn parse_template_from_component_definition_string(ctx: &mut TemplateNodeParseCo
// This IMPLICIT_ROOT placeholder node, at index 0 of the TND vec,
// is a container for the child_ids that act as "multi-roots," which enables
// templates to be authored without requiring a single top-level container
ctx.template_node_definitions.remove(0);
ctx.template_node_definitions.remove(&0);
ctx.template_node_definitions.insert(
0,
TemplateNodeDefinition {
Expand All @@ -346,7 +346,7 @@ fn parse_template_from_component_definition_string(ctx: &mut TemplateNodeParseCo
}

struct TemplateNodeParseContext {
pub template_node_definitions: Vec<TemplateNodeDefinition>,
pub template_node_definitions: HashMap<usize, TemplateNodeDefinition>,
pub pascal_identifier_to_type_id_map: HashMap<String, String>,
//each frame of the outer vec represents a list of
//children for a given node;
Expand Down Expand Up @@ -419,7 +419,7 @@ fn recurse_visit_tag_pairs_for_template(
raw_comment_string: None,
};
std::mem::swap(
ctx.template_node_definitions.get_mut(new_id).unwrap(),
ctx.template_node_definitions.get_mut(&new_id).unwrap(),
&mut template_node,
);
}
Expand All @@ -441,7 +441,7 @@ fn recurse_visit_tag_pairs_for_template(
raw_comment_string: None,
};
std::mem::swap(
ctx.template_node_definitions.get_mut(new_id).unwrap(),
ctx.template_node_definitions.get_mut(&new_id).unwrap(),
&mut template_node,
);
}
Expand Down Expand Up @@ -627,7 +627,7 @@ fn recurse_visit_tag_pairs_for_template(
};

std::mem::swap(
ctx.template_node_definitions.get_mut(new_id).unwrap(),
ctx.template_node_definitions.get_mut(&new_id).unwrap(),
&mut template_node_definition,
);
}
Expand All @@ -642,7 +642,7 @@ fn recurse_visit_tag_pairs_for_template(
raw_comment_string: Some(any_tag_pair.as_str().to_string()),
};
std::mem::swap(
ctx.template_node_definitions.get_mut(new_id).unwrap(),
ctx.template_node_definitions.get_mut(&new_id).unwrap(),
&mut template_node,
);
}
Expand Down Expand Up @@ -1036,7 +1036,7 @@ pub struct ParsingContext {

pub template_map: HashMap<String, String>,

pub template_node_definitions: Vec<TemplateNodeDefinition>,
pub template_node_definitions: HashMap<usize, TemplateNodeDefinition>,

pub type_table: TypeTable,

Expand All @@ -1051,7 +1051,7 @@ impl Default for ParsingContext {
component_definitions: HashMap::new(),
template_map: HashMap::new(),
type_table: get_primitive_type_table(),
template_node_definitions: vec![],
template_node_definitions: HashMap::new(),
import_paths: HashSet::new(),
}
}
Expand Down Expand Up @@ -1166,7 +1166,7 @@ pub fn assemble_component_definition(

let mut tpc = TemplateNodeParseContext {
pascal_identifier_to_type_id_map: template_map,
template_node_definitions: vec![],
template_node_definitions: HashMap::new(),
//each frame of the outer vec represents a list of
//children for a given node; child order matters because of z-index defaults;
//a new frame is added when descending the tree
Expand Down Expand Up @@ -1197,6 +1197,7 @@ pub fn assemble_component_definition(
settings: parse_settings_from_component_definition_string(pax),
handlers: parse_events_from_component_definition_string(pax),
module_path: modified_module_path,
next_template_id: Some(*tpc.uid_gen.peek().unwrap()),
};

(ctx, new_def)
Expand Down Expand Up @@ -1230,6 +1231,7 @@ pub fn assemble_struct_only_component_definition(
template: None,
settings: None,
handlers: None,
next_template_id: None,
};

(ctx, new_def)
Expand All @@ -1255,6 +1257,7 @@ pub fn assemble_primitive_definition(
settings: None,
module_path: modified_module_path,
handlers: None,
next_template_id: None,
}
}

Expand Down
Loading

0 comments on commit bf18037

Please sign in to comment.