mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:27:35 +00:00
LibJS: Allow the choice of a scope of declaration for a variable (#1408)
Previously, we were assuming all declared variables were bound to a block scope, now, with the addition of declaration types, we can bind a variable to a block scope using `let`, or a function scope (the scope of the inner-most enclosing function of a `var` declaration) using `var`.
This commit is contained in:
parent
542108421e
commit
df40c85f80
6 changed files with 85 additions and 16 deletions
|
@ -332,10 +332,16 @@ private:
|
|||
NonnullOwnPtr<Expression> m_rhs;
|
||||
};
|
||||
|
||||
enum class DeclarationType {
|
||||
Var,
|
||||
Let,
|
||||
};
|
||||
|
||||
class VariableDeclaration : public ASTNode {
|
||||
public:
|
||||
VariableDeclaration(NonnullOwnPtr<Identifier> name, OwnPtr<Expression> initializer)
|
||||
: m_name(move(name))
|
||||
VariableDeclaration(NonnullOwnPtr<Identifier> name, OwnPtr<Expression> initializer, DeclarationType declaration_type)
|
||||
: m_declaration_type(declaration_type)
|
||||
, m_name(move(name))
|
||||
, m_initializer(move(initializer))
|
||||
{
|
||||
}
|
||||
|
@ -348,6 +354,7 @@ public:
|
|||
private:
|
||||
virtual const char* class_name() const override { return "VariableDeclaration"; }
|
||||
|
||||
DeclarationType m_declaration_type;
|
||||
NonnullOwnPtr<Identifier> m_name;
|
||||
OwnPtr<Expression> m_initializer;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue