Skip to content

Commit 492db31

Browse files
committed
#201 - fix ImplicitLiteral.cs
1 parent 85bc0af commit 492db31

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

src/Application/HydraScript.Application.StaticAnalysis/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static IServiceCollection AddStaticAnalysis(this IServiceCollection servi
2020

2121
services.AddSingleton<IStandardLibraryProvider, StandardLibraryProvider>();
2222
services.AddSingleton<IJavaScriptTypesProvider, JavaScriptTypesProvider>();
23+
services.AddSingleton<IDefaultValueForTypeCalculator, DefaultValueForTypeCalculator>();
2324

2425
services.AddSingleton<IAmbiguousInvocationStorage, AmbiguousInvocationStorage>();
2526

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ internal class SemanticChecker : VisitorBase<IAbstractSyntaxTreeNode, Type>,
2424
IVisitor<ReturnStatement, Type>,
2525
IVisitor<ExpressionStatement, Type>,
2626
IVisitor<IdentifierReference, Type>,
27-
IVisitor<AbstractLiteral, Type>,
27+
IVisitor<Literal, Type>,
28+
IVisitor<ImplicitLiteral, Type>,
2829
IVisitor<ArrayLiteral, ArrayType>,
2930
IVisitor<ObjectLiteral, ObjectType>,
3031
IVisitor<ConditionalExpression, Type>,
@@ -42,6 +43,7 @@ internal class SemanticChecker : VisitorBase<IAbstractSyntaxTreeNode, Type>,
4243
IVisitor<BlockStatement, Type>,
4344
IVisitor<PrintStatement, Type>
4445
{
46+
private readonly IDefaultValueForTypeCalculator _calculator;
4547
private readonly IFunctionWithUndefinedReturnStorage _functionStorage;
4648
private readonly IMethodStorage _methodStorage;
4749
private readonly ISymbolTableStorage _symbolTables;
@@ -50,13 +52,15 @@ internal class SemanticChecker : VisitorBase<IAbstractSyntaxTreeNode, Type>,
5052
private readonly IVisitor<TypeValue, Type> _typeBuilder;
5153

5254
public SemanticChecker(
55+
IDefaultValueForTypeCalculator calculator,
5356
IFunctionWithUndefinedReturnStorage functionStorage,
5457
IMethodStorage methodStorage,
5558
ISymbolTableStorage symbolTables,
5659
IComputedTypesStorage computedTypes,
5760
IAmbiguousInvocationStorage ambiguousInvocations,
5861
IVisitor<TypeValue, Type> typeBuilder)
5962
{
63+
_calculator = calculator;
6064
_functionStorage = functionStorage;
6165
_methodStorage = methodStorage;
6266
_symbolTables = symbolTables;
@@ -150,9 +154,21 @@ public Type Visit(IdentifierReference visitable)
150154
return symbol?.Type ?? throw new UnknownIdentifierReference(visitable);
151155
}
152156

153-
public Type Visit(AbstractLiteral visitable) =>
157+
public Type Visit(Literal visitable) =>
154158
visitable.Type.Accept(_typeBuilder);
155159

160+
public Type Visit(ImplicitLiteral visitable)
161+
{
162+
var type = visitable.Type.Accept(_typeBuilder);
163+
if (!visitable.IsDefined)
164+
{
165+
var definedValue = _calculator.GetDefaultValueForType(type);
166+
visitable.SetValue(definedValue);
167+
}
168+
169+
return type;
170+
}
171+
156172
public ArrayType Visit(ArrayLiteral visitable)
157173
{
158174
if (visitable.Expressions.Count == 0)

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/PrimaryExpressions/ImplicitLiteral.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,29 @@ namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryE
55
[AutoVisitable<IAbstractSyntaxTreeNode>]
66
public partial class ImplicitLiteral(TypeValue type) : AbstractLiteral(type)
77
{
8-
private readonly object? _defaultValue = type switch
8+
private object? _defaultValue = type switch
99
{
1010
TypeIdentValue { TypeId.Name: "string" } => string.Empty,
1111
TypeIdentValue { TypeId.Name: "number" } => 0,
1212
TypeIdentValue { TypeId.Name: "boolean" } => false,
1313
TypeIdentValue { TypeId.Name: "null" } or NullableTypeValue => null,
1414
ArrayTypeValue => new List<object>(),
15-
_ => new()
15+
_ => new Undefined()
1616
};
1717

1818
protected override string NodeRepresentation() =>
1919
$"Implicit {Type}";
2020

21+
public void SetValue(object? value) => _defaultValue = value;
22+
23+
public bool IsDefined => _defaultValue is not Undefined;
24+
2125
public override ValueDto ToValueDto() =>
2226
ValueDto.ConstantDto(
2327
_defaultValue,
2428
_defaultValue is null
2529
? "null"
2630
: _defaultValue.ToString()!);
31+
32+
private sealed class Undefined;
2733
}

tests/HydraScript.IntegrationTests/Samples/defaultarray.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ type numbers = number[]
22

33
let a: numbers
44

5-
>>>a
5+
>>>a
6+
>>>~a
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
type x = number
22
let x:x
33
function x(x:x) {
4-
>>>x
4+
>>>x
5+
x = x + 1
56
}
67

78
x(x)

tests/HydraScript.UnitTests/Domain/FrontEnd/ImplicitLiteralTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ public void ToValueDto_Always_Expected(ImplicitLiteral implicitLiteral, object?
1313
}
1414

1515
[Fact]
16-
public void ToValueDto_Undefined_DefaultValueIsNewObject()
16+
public void ToValueDto_Undefined_DefaultValueIsUndefined()
1717
{
1818
var implicitLiteral = new ImplicitLiteral(TypeIdentValue.Undefined);
19-
var actual = implicitLiteral.ToValueDto().Value;
20-
actual.Should().BeOfType<object>();
19+
implicitLiteral.IsDefined.Should().BeFalse();
2120
}
2221

2322
public static TheoryData<ImplicitLiteral, object?> ToValueDtoData =>

0 commit comments

Comments
 (0)