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/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 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",