mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
JSSpecCompiler: Elide nested TreeList
nodes
This commit is contained in:
parent
f30815b534
commit
14a86c8fd6
Notes:
sideshowbarker
2024-07-17 03:45:48 +09:00
Author: https://github.com/DanShaders Commit: https://github.com/SerenityOS/serenity/commit/14a86c8fd6 Pull-request: https://github.com/SerenityOS/serenity/pull/21263 Reviewed-by: https://github.com/gmta ✅
4 changed files with 23 additions and 12 deletions
|
@ -83,10 +83,22 @@ Vector<NodeSubtreePointer> IfElseIfChain::subtrees()
|
|||
return result;
|
||||
}
|
||||
|
||||
TreeList::TreeList(Vector<Tree>&& trees)
|
||||
{
|
||||
for (auto const& tree : trees) {
|
||||
if (tree->is_list()) {
|
||||
for (auto const& nested_tree : as<TreeList>(tree)->m_trees)
|
||||
m_trees.append(nested_tree);
|
||||
} else {
|
||||
m_trees.append(tree);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vector<NodeSubtreePointer> TreeList::subtrees()
|
||||
{
|
||||
Vector<NodeSubtreePointer> result;
|
||||
for (auto& expression : m_expressions)
|
||||
for (auto& expression : m_trees)
|
||||
result.append({ &expression });
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ public:
|
|||
// For expressions, order must be the same as the evaluation order.
|
||||
virtual Vector<NodeSubtreePointer> subtrees() { return {}; }
|
||||
|
||||
virtual bool is_type() { return false; }
|
||||
virtual bool is_list() const { return false; }
|
||||
|
||||
protected:
|
||||
template<typename... Parameters>
|
||||
|
@ -372,14 +372,13 @@ protected:
|
|||
|
||||
class TreeList : public Statement {
|
||||
public:
|
||||
TreeList(Vector<Tree>&& expressions_)
|
||||
: m_expressions(move(expressions_))
|
||||
{
|
||||
}
|
||||
TreeList(Vector<Tree>&& trees);
|
||||
|
||||
Vector<NodeSubtreePointer> subtrees() override;
|
||||
|
||||
Vector<Tree> m_expressions;
|
||||
bool is_list() const override { return true; }
|
||||
|
||||
Vector<Tree> m_trees;
|
||||
|
||||
protected:
|
||||
void dump_tree(StringBuilder& builder) override;
|
||||
|
|
|
@ -129,7 +129,7 @@ void IfElseIfChain::dump_tree(StringBuilder& builder)
|
|||
void TreeList::dump_tree(StringBuilder& builder)
|
||||
{
|
||||
dump_node(builder, "TreeList");
|
||||
for (auto const& expression : m_expressions)
|
||||
for (auto const& expression : m_trees)
|
||||
expression->format_tree(builder);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ RecursionDecision IfBranchMergingPass::on_entry(Tree tree)
|
|||
}
|
||||
};
|
||||
|
||||
for (auto const& node : list->m_expressions) {
|
||||
for (auto const& node : list->m_trees) {
|
||||
if (is<IfBranch>(node.ptr())) {
|
||||
merge_if_needed();
|
||||
unmerged_branches.append(node);
|
||||
|
@ -37,7 +37,7 @@ RecursionDecision IfBranchMergingPass::on_entry(Tree tree)
|
|||
}
|
||||
merge_if_needed();
|
||||
|
||||
list->m_expressions = move(result);
|
||||
list->m_trees = move(result);
|
||||
}
|
||||
return RecursionDecision::Recurse;
|
||||
}
|
||||
|
@ -75,8 +75,8 @@ Tree IfBranchMergingPass::merge_branches(Vector<Tree> const& unmerged_branches)
|
|||
// 3. Else,
|
||||
// ...
|
||||
auto substep_list = as<TreeList>(branch->m_branch);
|
||||
if (substep_list && substep_list->m_expressions.size() == 1) {
|
||||
if (auto nested_if = as<IfBranch>(substep_list->m_expressions[0]); nested_if)
|
||||
if (substep_list && substep_list->m_trees.size() == 1) {
|
||||
if (auto nested_if = as<IfBranch>(substep_list->m_trees[0]); nested_if)
|
||||
branch = make_ref_counted<ElseIfBranch>(nested_if->m_condition, nested_if->m_branch);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue