1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:37:34 +00:00

JSSpecCompiler: Allow storing NullableTrees in nodes

And use this in ElseIfBranch node.
This commit is contained in:
Dan Klishch 2023-08-18 16:23:29 -04:00 committed by Andrew Kaster
parent 4eede5282c
commit 092ed1cc8a
7 changed files with 56 additions and 18 deletions

View file

@ -8,6 +8,29 @@
namespace JSSpecCompiler {
Tree NodeSubtreePointer::get(Badge<RecursiveASTVisitor>)
{
return m_tree_ptr.visit(
[&](NullableTree* nullable_tree) -> Tree {
NullableTree copy = *nullable_tree;
return copy.release_nonnull();
},
[&](Tree* tree) -> Tree {
return *tree;
});
}
void NodeSubtreePointer::replace_subtree(Badge<RecursiveASTVisitor>, NullableTree replacement)
{
m_tree_ptr.visit(
[&](NullableTree* nullable_tree) {
*nullable_tree = replacement;
},
[&](Tree* tree) {
*tree = replacement.release_nonnull();
});
}
Vector<NodeSubtreePointer> BinaryOperation::subtrees()
{
return { { &m_left }, { &m_right } };
@ -43,8 +66,8 @@ Vector<NodeSubtreePointer> IfBranch::subtrees()
Vector<NodeSubtreePointer> ElseIfBranch::subtrees()
{
if (m_condition.has_value())
return { { &m_condition.value() }, { &m_branch } };
if (m_condition)
return { { &m_condition }, { &m_branch } };
return { { &m_branch } };
}

View file

@ -28,12 +28,16 @@ public:
{
}
Tree& get(Badge<RecursiveASTVisitor>) { return *m_tree_ptr; }
NodeSubtreePointer(NullableTree* tree_ptr)
: m_tree_ptr(tree_ptr)
{
}
void replace_subtree(Badge<RecursiveASTVisitor>, Tree tree) { *m_tree_ptr = move(tree); }
Tree get(Badge<RecursiveASTVisitor>);
void replace_subtree(Badge<RecursiveASTVisitor>, NullableTree replacement);
private:
Tree* m_tree_ptr;
Variant<Tree*, NullableTree*> m_tree_ptr;
};
// ===== Generic nodes =====
@ -258,7 +262,7 @@ protected:
class ElseIfBranch : public Node {
public:
ElseIfBranch(Optional<Tree> condition, Tree branch)
ElseIfBranch(NullableTree condition, Tree branch)
: m_condition(condition)
, m_branch(branch)
{
@ -266,7 +270,7 @@ public:
Vector<NodeSubtreePointer> subtrees() override;
Optional<Tree> m_condition;
NullableTree m_condition;
Tree m_branch;
protected:

View file

@ -91,9 +91,9 @@ void IfBranch::dump_tree(StringBuilder& builder)
void ElseIfBranch::dump_tree(StringBuilder& builder)
{
dump_node(builder, "ElseIfBranch {}", m_condition.has_value() ? "ElseIf" : "Else");
if (m_condition.has_value())
(*m_condition)->format_tree(builder);
dump_node(builder, "ElseIfBranch {}", m_condition ? "ElseIf" : "Else");
if (m_condition)
m_condition->format_tree(builder);
m_branch->format_tree(builder);
}