Skip to content

Commit 890aa92

Browse files
authored
#122 - Прописал свой скоп литералу объекта (#123)
* #122 - Прописал свой скоп литералу объекта * #122 - тест
1 parent 7aad345 commit 890aa92

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

ExtendedJavaScriptSubset.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{04AB
6565
samples\typeresolving.js = samples\typeresolving.js
6666
samples\vec2d.js = samples\vec2d.js
6767
samples\cycled.js = samples\cycled.js
68+
samples\scope.js = samples\scope.js
6869
EndProjectSection
6970
EndProject
7071
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Src", "Src", "{FB8F6EE1-1942-46D6-954E-9A1647BBDF10}"

samples/scope.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type Obj = {
2+
x: number;
3+
}
4+
5+
let x: Obj
6+
x = {
7+
x: 1;
8+
}
9+
10+
print(x.x as string)

src/Application/HydraScript.Application.StaticAnalysis/Visitors/SymbolTableInitializer.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using HydraScript.Domain.FrontEnd.Parser;
22
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes;
33
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Declarations.AfterTypesAreLoaded;
4+
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.ComplexLiterals;
45
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Statements;
56
using HydraScript.Domain.IR.Impl;
67

@@ -9,7 +10,8 @@ namespace HydraScript.Application.StaticAnalysis.Visitors;
910
internal class SymbolTableInitializer : VisitorNoReturnBase<IAbstractSyntaxTreeNode>,
1011
IVisitor<ScriptBody>,
1112
IVisitor<FunctionDeclaration>,
12-
IVisitor<BlockStatement>
13+
IVisitor<BlockStatement>,
14+
IVisitor<ObjectLiteral>
1315
{
1416
private readonly IStandardLibraryProvider _provider;
1517
private readonly ISymbolTableStorage _symbolTables;
@@ -58,4 +60,13 @@ public VisitUnit Visit(BlockStatement visitable)
5860
visitable[i].Accept(This);
5961
return default;
6062
}
63+
64+
public VisitUnit Visit(ObjectLiteral visitable)
65+
{
66+
visitable.InitScope(scope: new Scope());
67+
_symbolTables.InitWithOpenScope(visitable.Scope);
68+
for (var i = 0; i < visitable.Count; i++)
69+
visitable[i].Accept(This);
70+
return default;
71+
}
6172
}

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/ComplexLiterals/ObjectLiteral.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,14 @@ public ObjectLiteral(IEnumerable<Property> properties)
1616
_properties.ForEach(prop => prop.Parent = this);
1717
}
1818

19+
/// <summary>Стратегия "блока" - углубление скоупа</summary>
20+
/// <param name="scope">Новый скоуп</param>
21+
public override void InitScope(Scope? scope = null)
22+
{
23+
ArgumentNullException.ThrowIfNull(scope);
24+
Scope = scope;
25+
Scope.AddOpenScope(Parent.Scope);
26+
}
27+
1928
protected override string NodeRepresentation() => "{}";
2029
}

tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@
103103
<Link>Samples\recur.js</Link>
104104
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
105105
</Content>
106+
<Content Include="..\..\samples\scope.js">
107+
<Link>Samples\scope.js</Link>
108+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
109+
</Content>
106110
<Content Include="..\..\samples\searchinll.js">
107111
<Link>Samples\searchinll.js</Link>
108112
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void Invoke_NoError_ReturnCodeIsZero(string fileName)
3535
"quicksort.js",
3636
"range.js",
3737
"recur.js",
38+
"scope.js",
3839
"searchinll.js",
3940
"settable.js",
4041
"squareroot.js",

0 commit comments

Comments
 (0)