Skip to content

Commit fa193a7

Browse files
committed
#201 - refactoring + tests
1 parent 881532c commit fa193a7

File tree

12 files changed

+62
-80
lines changed

12 files changed

+62
-80
lines changed

src/Application/HydraScript.Application.StaticAnalysis/IDefaultValueForTypeCalculator.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Application/HydraScript.Application.StaticAnalysis/Impl/DefaultValueForTypeCalculator.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

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

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

2524
services.AddSingleton<IAmbiguousInvocationStorage, AmbiguousInvocationStorage>();
2625

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ internal class SemanticChecker : VisitorBase<IAbstractSyntaxTreeNode, Type>,
2424
IVisitor<ReturnStatement, Type>,
2525
IVisitor<ExpressionStatement, Type>,
2626
IVisitor<IdentifierReference, Type>,
27-
IVisitor<Literal, Type>,
28-
IVisitor<ImplicitLiteral, Type>,
27+
IVisitor<AbstractLiteral, Type>,
2928
IVisitor<ArrayLiteral, ArrayType>,
3029
IVisitor<ObjectLiteral, ObjectType>,
3130
IVisitor<ConditionalExpression, Type>,
@@ -43,7 +42,6 @@ internal class SemanticChecker : VisitorBase<IAbstractSyntaxTreeNode, Type>,
4342
IVisitor<BlockStatement, Type>,
4443
IVisitor<PrintStatement, Type>
4544
{
46-
private readonly IDefaultValueForTypeCalculator _calculator;
4745
private readonly IFunctionWithUndefinedReturnStorage _functionStorage;
4846
private readonly IMethodStorage _methodStorage;
4947
private readonly ISymbolTableStorage _symbolTables;
@@ -52,15 +50,13 @@ internal class SemanticChecker : VisitorBase<IAbstractSyntaxTreeNode, Type>,
5250
private readonly IVisitor<TypeValue, Type> _typeBuilder;
5351

5452
public SemanticChecker(
55-
IDefaultValueForTypeCalculator calculator,
5653
IFunctionWithUndefinedReturnStorage functionStorage,
5754
IMethodStorage methodStorage,
5855
ISymbolTableStorage symbolTables,
5956
IComputedTypesStorage computedTypes,
6057
IAmbiguousInvocationStorage ambiguousInvocations,
6158
IVisitor<TypeValue, Type> typeBuilder)
6259
{
63-
_calculator = calculator;
6460
_functionStorage = functionStorage;
6561
_methodStorage = methodStorage;
6662
_symbolTables = symbolTables;
@@ -154,16 +150,9 @@ public Type Visit(IdentifierReference visitable)
154150
return symbol?.Type ?? throw new UnknownIdentifierReference(visitable);
155151
}
156152

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

160-
public Type Visit(ImplicitLiteral visitable)
161-
{
162-
var type = visitable.Type.Accept(_typeBuilder);
163-
visitable.ComputedDefaultValue = _calculator.GetDefaultValueForType(type);
164-
return type;
165-
}
166-
167156
public ArrayType Visit(ArrayLiteral visitable)
168157
{
169158
if (visitable.Expressions.Count == 0)

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Declarations/TypeValue.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public abstract record TypeValue : IVisitable<TypeValue>
1212
[AutoVisitable<TypeValue>]
1313
public partial record TypeIdentValue(IdentifierReference TypeId) : TypeValue
1414
{
15+
public static TypeValue String => new TypeIdentValue(new IdentifierReference("string"));
16+
public static TypeValue Number => new TypeIdentValue(new IdentifierReference("number"));
17+
public static TypeValue Boolean => new TypeIdentValue(new IdentifierReference("boolean"));
18+
public static TypeValue Null => new TypeIdentValue(new IdentifierReference("null"));
19+
public static TypeValue Undefined => new TypeIdentValue(new IdentifierReference("undefined"));
20+
1521
public override string ToString() => TypeId;
1622
}
1723

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions;
44

5-
public abstract class AbstractLiteral(TypeValue type) : PrimaryExpression
5+
[AutoVisitable<IAbstractSyntaxTreeNode>]
6+
public abstract partial class AbstractLiteral(TypeValue type) : PrimaryExpression
67
{
78
public TypeValue Type { get; } = type;
89

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryE
55
[AutoVisitable<IAbstractSyntaxTreeNode>]
66
public partial class ImplicitLiteral(TypeValue type) : AbstractLiteral(type)
77
{
8-
public object? ComputedDefaultValue { private get; set; }
8+
private readonly object? _defaultValue = type switch
9+
{
10+
TypeIdentValue { TypeId.Name: "string" } => string.Empty,
11+
TypeIdentValue { TypeId.Name: "number" } => 0,
12+
TypeIdentValue { TypeId.Name: "boolean" } => false,
13+
TypeIdentValue { TypeId.Name: "null" } or NullableTypeValue => null,
14+
ArrayTypeValue => new List<object>(),
15+
_ => new()
16+
};
917

1018
protected override string NodeRepresentation() =>
1119
$"Implicit {Type}";
1220

1321
public override ValueDto ToValueDto() =>
1422
ValueDto.ConstantDto(
15-
ComputedDefaultValue,
16-
ComputedDefaultValue is null
23+
_defaultValue,
24+
_defaultValue is null
1725
? "null"
18-
: ComputedDefaultValue.ToString()!);
26+
: _defaultValue.ToString()!);
1927
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,14 @@ public override ValueDto ToValueDto() =>
2525
ValueDto.ConstantDto(_value, _label);
2626

2727
public static Literal String(string value, string? segment = null, string? label = null) =>
28-
new(new TypeIdentValue(new IdentifierReference(name: "string")), value, segment ?? "(1, 1)-(1, 1)", label);
28+
new(TypeIdentValue.String, value, segment ?? "(1, 1)-(1, 1)", label);
2929

3030
public static Literal Number(double value, string? segment = null) =>
31-
new(new TypeIdentValue(new IdentifierReference(name: "number")), value, segment ?? "(1, 1)-(1, 1)");
31+
new(TypeIdentValue.Number, value, segment ?? "(1, 1)-(1, 1)");
3232

3333
public static Literal Boolean(bool value, string? segment = null) =>
34-
new(new TypeIdentValue(new IdentifierReference(name: "boolean")), value, segment ?? "(1, 1)-(1, 1)");
34+
new(TypeIdentValue.Boolean, value, segment ?? "(1, 1)-(1, 1)");
3535

3636
public static Literal Null(string? segment = null) =>
37-
new(
38-
new TypeIdentValue(new IdentifierReference(name: "null")),
39-
value: null,
40-
segment ?? "(1, 1)-(1, 1)",
41-
label: "null");
37+
new(TypeIdentValue.Null, value: null, segment ?? "(1, 1)-(1, 1)", label: "null");
4238
}

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/TopDownParser.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,7 @@ private void AddToDeclaration(LexicalDeclaration declaration)
412412
var identRef = new IdentifierReference(ident.Value) { Segment = ident.Segment };
413413
var assignment = new AssignmentExpression(
414414
new MemberExpression(identRef),
415-
new ImplicitLiteral(
416-
new TypeIdentValue(
417-
new IdentifierReference("undefined"))))
415+
new ImplicitLiteral(TypeIdentValue.Undefined))
418416
{ Segment = ident.Segment };
419417

420418
if (CurrentIs("Assign"))

tests/HydraScript.UnitTests/Application/ReturnAnalyzerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public void Visit_FunctionWithMissingReturn_CodePathEndedWithReturnIsFalse()
2222
// Arrange
2323
var functionDeclaration = new FunctionDeclaration(
2424
new IdentifierReference("f"),
25-
new TypeIdentValue(new IdentifierReference("undefined")),
26-
[new NamedArgument("b", new TypeIdentValue(new IdentifierReference("boolean")))],
25+
TypeIdentValue.Undefined,
26+
[new NamedArgument("b", TypeIdentValue.Boolean)],
2727
new BlockStatement([
2828
new IfStatement(
2929
new IdentifierReference("b"),

0 commit comments

Comments
 (0)