Skip to content

Commit e048021

Browse files
committed
type decl fix
1 parent 55f5473 commit e048021

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@ namespace HydraScript.Application.StaticAnalysis.Impl;
44

55
internal class DefaultValueForTypeCalculator : IDefaultValueForTypeCalculator
66
{
7-
private readonly Type _boolean = "boolean";
8-
private readonly Type _number = "number";
9-
private readonly Type _string = "string";
10-
private readonly Type _void = "void";
11-
private readonly Type _null = new NullType();
12-
137
public object? GetDefaultValueForType(Type type)
148
{
159
if (type is NullableType)
1610
return null;
17-
if (type.Equals(_boolean))
11+
if (type.Equals("boolean"))
1812
return false;
19-
if (type.Equals(_number))
13+
if (type.Equals("number"))
2014
return 0;
21-
if (type.Equals(_string))
15+
if (type.Equals("string"))
2216
return string.Empty;
23-
if (type.Equals(_void))
17+
if (type.Equals("void"))
2418
return new object();
25-
if (type.Equals(_null))
19+
if (type.Equals(new NullType()))
2620
return null;
2721
if (type is ArrayType)
2822
return new List<object>();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ public VisitUnit Visit(FunctionDeclaration visitable)
101101
if (parameters is [ObjectType methodOwner, ..] && visitable.Arguments is [{ TypeValue: TypeIdentValue }, ..])
102102
_methodStorage.BindMethod(methodOwner, functionSymbol, functionSymbolId);
103103

104-
Type undefined = "undefined";
105-
if (functionSymbol.Type.Equals(undefined))
104+
if (functionSymbol.Type.Equals("undefined"))
106105
{
107106
if (visitable.HasReturnStatement)
108107
_functionStorage.Save(functionSymbol, visitable);

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ public Type Visit(ScriptBody visitable)
9595
public Type Visit(WhileStatement visitable)
9696
{
9797
var condType = visitable.Condition.Accept(This);
98-
Type boolean = "boolean";
99-
if (!condType.Equals(boolean))
98+
if (!condType.Equals("boolean"))
10099
throw new NotBooleanTestExpression(visitable.Segment, condType);
101100

102101
visitable.Statement.Accept(This);
@@ -107,8 +106,7 @@ public Type Visit(WhileStatement visitable)
107106
public Type Visit(IfStatement visitable)
108107
{
109108
var testType = visitable.Test.Accept(This);
110-
Type boolean = "boolean";
111-
if (!testType.Equals(boolean))
109+
if (!testType.Equals("boolean"))
112110
throw new NotBooleanTestExpression(visitable.Segment, testType);
113111

114112
visitable.Then.Accept(This);
@@ -219,8 +217,7 @@ public ObjectType Visit(ObjectLiteral visitable)
219217
public Type Visit(ConditionalExpression visitable)
220218
{
221219
var tType = visitable.Test.Accept(This);
222-
Type boolean = "boolean";
223-
if (!tType.Equals(boolean))
220+
if (!tType.Equals("boolean"))
224221
throw new NotBooleanTestExpression(visitable.Test.Segment, tType);
225222

226223
var cType = visitable.Consequent.Accept(This);
@@ -296,7 +293,7 @@ public Type Visit(UnaryExpression visitable)
296293

297294
public Type Visit(LexicalDeclaration visitable)
298295
{
299-
Type undefined = "undefined", @void = "void";
296+
Type undefined = "undefined";
300297

301298
for (var i = 0; i < visitable.Assignments.Count; i++)
302299
{
@@ -306,7 +303,7 @@ public Type Visit(LexicalDeclaration visitable)
306303

307304
if (sourceType.Equals(undefined))
308305
throw new CannotDefineType(assignment.Source.Segment);
309-
if (sourceType.Equals(@void))
306+
if (sourceType.Equals("void"))
310307
throw new CannotAssignVoid(assignment.Source.Segment);
311308
if (!registeredSymbol.Type.Equals(undefined) && !registeredSymbol.Type.Equals(sourceType))
312309
throw new IncompatibleTypesOfOperands(
@@ -384,9 +381,8 @@ public Type Visit(IndexAccess visitable)
384381
if (prevType is not ArrayType arrayType)
385382
throw new NonAccessibleType(prevType);
386383

387-
Type number = "number";
388384
var indexType = visitable.Index.Accept(This);
389-
if (!indexType.Equals(number))
385+
if (!indexType.Equals("number"))
390386
throw new ArrayAccessException(visitable.Segment, indexType);
391387

392388
var elemType = arrayType.Type;
@@ -437,10 +433,9 @@ public ObjectType Visit(WithExpression visitable)
437433

438434
public Type Visit(CastAsExpression visitable)
439435
{
440-
Type undefined = "undefined";
441436
var from = visitable.Expression.Accept(This);
442437

443-
if (from.Equals(undefined))
438+
if (from.Equals("undefined"))
444439
throw new CannotDefineType(visitable.Expression.Segment);
445440

446441
var to = visitable.Cast.Accept(_typeBuilder);
@@ -495,8 +490,7 @@ public Type Visit(CallExpression visitable)
495490
throw new WrongTypeOfArgument(expr.Segment, expectedType, actualType);
496491
});
497492

498-
Type undefined = "undefined";
499-
if (functionSymbol.Type.Equals(undefined))
493+
if (functionSymbol.Type.Equals("undefined"))
500494
{
501495
var declaration = _functionStorage.Get(functionSymbol);
502496
functionReturnType = declaration.Accept(This);

0 commit comments

Comments
 (0)