From d4f6b24c28cc7d527ae177f93bcd0ab200ce0095 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 31 Aug 2024 20:05:41 +0300 Subject: [PATCH] feat: implement "has" function for scopes --- .../jscript/compilation/scope/FunctionScope.java | 8 ++++++++ .../topchetoeu/jscript/compilation/scope/GlobalScope.java | 5 +++++ .../topchetoeu/jscript/compilation/scope/LocalScope.java | 8 ++++++++ .../me/topchetoeu/jscript/compilation/scope/Scope.java | 1 + 4 files changed, 22 insertions(+) diff --git a/src/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java b/src/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java index 3318f17..72f7468 100644 --- a/src/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java +++ b/src/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java @@ -39,6 +39,14 @@ public class FunctionScope extends Scope { return childVar; } + @Override public boolean has(String name) { + if (locals.has(name)) return true; + if (captures.has(name)) return true; + if (parent != null) return parent.has(name); + + return false; + } + public int localsCount() { return locals.size(); } diff --git a/src/java/me/topchetoeu/jscript/compilation/scope/GlobalScope.java b/src/java/me/topchetoeu/jscript/compilation/scope/GlobalScope.java index 75ba7d0..1f7e8a3 100644 --- a/src/java/me/topchetoeu/jscript/compilation/scope/GlobalScope.java +++ b/src/java/me/topchetoeu/jscript/compilation/scope/GlobalScope.java @@ -15,4 +15,9 @@ public final class GlobalScope extends Scope { @Override public int offset() { return 0; } + @Override public boolean has(String name) { + return false; + } + + public GlobalScope() { super(); } } diff --git a/src/java/me/topchetoeu/jscript/compilation/scope/LocalScope.java b/src/java/me/topchetoeu/jscript/compilation/scope/LocalScope.java index 0ee6630..9244fb6 100644 --- a/src/java/me/topchetoeu/jscript/compilation/scope/LocalScope.java +++ b/src/java/me/topchetoeu/jscript/compilation/scope/LocalScope.java @@ -29,6 +29,13 @@ public class LocalScope extends Scope { return null; } + @Override public boolean has(String name) { + if (locals.has(name)) return true; + if (parent != null) return parent.has(name); + + return false; + } + @Override public boolean end() { if (!super.end()) return false; @@ -40,6 +47,7 @@ public class LocalScope extends Scope { return () -> locals.iterator(); } + public LocalScope(Scope parent) { super(parent); } diff --git a/src/java/me/topchetoeu/jscript/compilation/scope/Scope.java b/src/java/me/topchetoeu/jscript/compilation/scope/Scope.java index 6631abd..51cf155 100644 --- a/src/java/me/topchetoeu/jscript/compilation/scope/Scope.java +++ b/src/java/me/topchetoeu/jscript/compilation/scope/Scope.java @@ -35,6 +35,7 @@ public abstract class Scope { * which could break the semantics of a capture */ public abstract VariableDescriptor get(String name, boolean capture); + public abstract boolean has(String name); /** * Gets the index offset from this scope to its children */