mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:27:44 +00:00
LibJS: Add basic support for (scoped) variables
It's now possible to assign expressions to variables. The variables are put into the current scope of the interpreter. Variable lookup follows the scope chain, ending in the global object.
This commit is contained in:
parent
ac3c19b91c
commit
1382dbc5e1
5 changed files with 224 additions and 19 deletions
|
@ -26,6 +26,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap.h>
|
||||
|
@ -34,6 +35,7 @@ namespace JS {
|
|||
|
||||
struct ScopeFrame {
|
||||
const ScopeNode& scope_node;
|
||||
HashMap<String, Value> variables;
|
||||
};
|
||||
|
||||
class Interpreter {
|
||||
|
@ -50,6 +52,10 @@ public:
|
|||
|
||||
void do_return();
|
||||
|
||||
Value get_variable(const String& name);
|
||||
void set_variable(String name, Value);
|
||||
void declare_variable(String name);
|
||||
|
||||
private:
|
||||
void enter_scope(const ScopeNode&);
|
||||
void exit_scope(const ScopeNode&);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue