Skip to content

Commit 9a39eb9

Browse files
authored
Bug/method def param (#188)
* #187 - added test * #187 - fix: bind new overload to object * fix
1 parent 68cc1ab commit 9a39eb9

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed

src/Application/HydraScript.Application.StaticAnalysis/IMethodStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace HydraScript.Application.StaticAnalysis;
66

77
public interface IMethodStorage
88
{
9-
public void BindMethod(ObjectType objectType, FunctionSymbol method);
9+
public void BindMethod(ObjectType objectType, FunctionSymbol method, FunctionSymbolId overload);
1010

1111
public IReadOnlyDictionary<FunctionSymbolId, FunctionSymbol> GetAvailableMethods(ObjectType objectType);
1212

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ internal class MethodStorage : IMethodStorage
88
{
99
private readonly Dictionary<ObjectType, Dictionary<FunctionSymbolId, FunctionSymbol>> _bindings = [];
1010

11-
public void BindMethod(ObjectType objectType, FunctionSymbol method)
11+
public void BindMethod(ObjectType objectType, FunctionSymbol method, FunctionSymbolId overload)
1212
{
13-
objectType.AddMethod(method.Id);
13+
objectType.AddMethod(overload);
1414
if (!_bindings.ContainsKey(objectType))
1515
_bindings[objectType] = new Dictionary<FunctionSymbolId, FunctionSymbol>();
16-
_bindings[objectType][method.Id] = method;
16+
_bindings[objectType][overload] = method;
1717
}
1818

1919
public IReadOnlyDictionary<FunctionSymbolId, FunctionSymbol> GetAvailableMethods(ObjectType objectType) =>

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ public VisitUnit Visit(FunctionDeclaration visitable)
9999
_symbolTables[visitable.Scope].AddSymbol(arg);
100100
}
101101

102-
if (parameters is [{ Type: ObjectType objectType }, ..] &&
103-
visitable.Arguments is [{ TypeValue: TypeIdentValue }, ..])
104-
{
105-
_methodStorage.BindMethod(objectType, functionSymbol);
106-
}
102+
var isMethod =
103+
parameters is [{ Type: ObjectType }, ..] &&
104+
visitable.Arguments is [{ TypeValue: TypeIdentValue }, ..];
105+
if (isMethod)
106+
_methodStorage.BindMethod((parameters[0].Type as ObjectType)!, functionSymbol, functionSymbolId);
107107

108108
Type undefined = "undefined";
109109
if (functionSymbol.Type.Equals(undefined))
@@ -126,11 +126,12 @@ public VisitUnit Visit(FunctionDeclaration visitable)
126126

127127
var overload = new FunctionSymbolId(visitable.Name, parameters[..i].Select(x => x.Type));
128128
var existing = parentTable.FindSymbol(overload);
129-
parentTable.AddSymbol(functionSymbol, overload);
130-
if (existing is not null && existing < functionSymbol)
131-
{
132-
parentTable.AddSymbol(existing, overload);
133-
}
129+
var functionToAdd = existing is not null && existing < functionSymbol
130+
? existing
131+
: functionSymbol;
132+
parentTable.AddSymbol(functionToAdd, overload);
133+
if (isMethod)
134+
_methodStorage.BindMethod((parameters[0].Type as ObjectType)!, functionToAdd, overload);
134135

135136
if (existing is not null && !existing.Id.Equals(overload))
136137
{

src/Domain/HydraScript.Domain.IR/ISymbol.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ namespace HydraScript.Domain.IR;
33
public interface ISymbol
44
{
55
public ISymbolId<ISymbol> Id { get; }
6-
public string Name { get; }
76
public Type Type { get; }
87
}

tests/HydraScript.IntegrationTests/HydraScript.IntegrationTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
<None Update="Samples\ceil.js">
3030
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3131
</None>
32+
<None Update="Samples\counter.js">
33+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
34+
</None>
3235
<None Update="Samples\cycled.js">
3336
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3437
</None>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type Counter = {
2+
state:number;
3+
}
4+
5+
function next(counter: Counter, step = 1){
6+
counter.state = counter.state + step
7+
return counter.state
8+
}
9+
10+
function newCounter(start: number):Counter{
11+
return { state: start; }
12+
}
13+
14+
let counter = newCounter(4)
15+
>>>counter.next()
16+
>>>counter.next(2)

0 commit comments

Comments
 (0)