From 2ef19fc0dd9a1d0987ccba4a37253a824476f1b9 Mon Sep 17 00:00:00 2001 From: Stepami Date: Sun, 2 Feb 2025 19:55:07 +0300 Subject: [PATCH 1/2] =?UTF-8?q?#122=20-=20=D0=9F=D1=80=D0=BE=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BB=20=D1=81=D0=B2=D0=BE=D0=B9=20=D1=81=D0=BA?= =?UTF-8?q?=D0=BE=D0=BF=20=D0=BB=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D0=BB=D1=83?= =?UTF-8?q?=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Visitors/SymbolTableInitializer.cs | 13 ++++++++++++- .../Expressions/ComplexLiterals/ObjectLiteral.cs | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Application/HydraScript.Application.StaticAnalysis/Visitors/SymbolTableInitializer.cs b/src/Application/HydraScript.Application.StaticAnalysis/Visitors/SymbolTableInitializer.cs index 7b75c558..e3798ba7 100644 --- a/src/Application/HydraScript.Application.StaticAnalysis/Visitors/SymbolTableInitializer.cs +++ b/src/Application/HydraScript.Application.StaticAnalysis/Visitors/SymbolTableInitializer.cs @@ -1,6 +1,7 @@ using HydraScript.Domain.FrontEnd.Parser; using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes; using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Declarations.AfterTypesAreLoaded; +using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.ComplexLiterals; using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Statements; using HydraScript.Domain.IR.Impl; @@ -9,7 +10,8 @@ namespace HydraScript.Application.StaticAnalysis.Visitors; internal class SymbolTableInitializer : VisitorNoReturnBase, IVisitor, IVisitor, - IVisitor + IVisitor, + IVisitor { private readonly IStandardLibraryProvider _provider; private readonly ISymbolTableStorage _symbolTables; @@ -58,4 +60,13 @@ public VisitUnit Visit(BlockStatement visitable) visitable[i].Accept(This); return default; } + + public VisitUnit Visit(ObjectLiteral visitable) + { + visitable.InitScope(scope: new Scope()); + _symbolTables.InitWithOpenScope(visitable.Scope); + for (var i = 0; i < visitable.Count; i++) + visitable[i].Accept(This); + return default; + } } \ No newline at end of file diff --git a/src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs b/src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs index f8d01ea6..ffe946e0 100644 --- a/src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs +++ b/src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs @@ -16,5 +16,14 @@ public ObjectLiteral(IEnumerable properties) _properties.ForEach(prop => prop.Parent = this); } + /// Стратегия "блока" - углубление скоупа + /// Новый скоуп + public override void InitScope(Scope? scope = null) + { + ArgumentNullException.ThrowIfNull(scope); + Scope = scope; + Scope.AddOpenScope(Parent.Scope); + } + protected override string NodeRepresentation() => "{}"; } \ No newline at end of file From d8140c5b4a2b64b5c45941584ac7b5263917dae2 Mon Sep 17 00:00:00 2001 From: Stepami Date: Sun, 2 Feb 2025 20:25:38 +0300 Subject: [PATCH 2/2] =?UTF-8?q?#122=20-=20=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ExtendedJavaScriptSubset.sln | 1 + samples/scope.js | 10 ++++++++++ .../HydraScript.IntegrationTests.csproj | 4 ++++ .../SuccessfulProgramsTests.cs | 1 + 4 files changed, 16 insertions(+) create mode 100644 samples/scope.js diff --git a/ExtendedJavaScriptSubset.sln b/ExtendedJavaScriptSubset.sln index 2a49b3a3..342c3b8e 100644 --- a/ExtendedJavaScriptSubset.sln +++ b/ExtendedJavaScriptSubset.sln @@ -65,6 +65,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{04AB samples\typeresolving.js = samples\typeresolving.js samples\vec2d.js = samples\vec2d.js samples\cycled.js = samples\cycled.js + samples\scope.js = samples\scope.js EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Src", "Src", "{FB8F6EE1-1942-46D6-954E-9A1647BBDF10}" diff --git a/samples/scope.js b/samples/scope.js new file mode 100644 index 00000000..befd07ec --- /dev/null +++ b/samples/scope.js @@ -0,0 +1,10 @@ +type Obj = { + x: number; +} + +let x: Obj +x = { + x: 1; +} + +print(x.x as string) \ No newline at end of file diff --git a/tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj b/tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj index 28252b0f..67175bcf 100644 --- a/tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj +++ b/tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj @@ -103,6 +103,10 @@ Samples\recur.js Always + + Samples\scope.js + Always + Samples\searchinll.js Always diff --git a/tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs b/tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs index e48d7e96..d97c6e17 100644 --- a/tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs +++ b/tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs @@ -35,6 +35,7 @@ public void Invoke_NoError_ReturnCodeIsZero(string fileName) "quicksort.js", "range.js", "recur.js", + "scope.js", "searchinll.js", "settable.js", "squareroot.js",