Skip to content

Commit 12c583f

Browse files
authored
Перегрузка функций +semver:feature (#151)
* #61 - черновик иерархии ключей символов * #61 - использование ключа символа в домене * Revert "#61 - использование ключа символа в домене" This reverts commit fff91c7. * #61 - Symbol ctor param id -> name * #61 - новая иерархия ключей символов * #61 - use SymbolId in ISymbol * #61 - use symbolId in symbolTable * #61 - symbolId static analysis refactoring * fix AsString - add int support * #61 - added xxx test * #61 - codegen fix * #61 - overload tests
1 parent 7384664 commit 12c583f

File tree

34 files changed

+202
-98
lines changed

34 files changed

+202
-98
lines changed

ExtendedJavaScriptSubset.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{04AB
6666
samples\vec2d.js = samples\vec2d.js
6767
samples\cycled.js = samples\cycled.js
6868
samples\scope.js = samples\scope.js
69+
samples\xxx.js = samples\xxx.js
70+
samples\overload_object.js = samples\overload_object.js
71+
samples\overload.js = samples\overload.js
6972
EndProjectSection
7073
EndProject
7174
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Src", "Src", "{FB8F6EE1-1942-46D6-954E-9A1647BBDF10}"

samples/overload.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function func(x:number){
2+
>>>x
3+
}
4+
5+
function func(x:number, y:number){
6+
return x + y
7+
}
8+
9+
func(123)
10+
11+
>>> func(-1,5)

samples/overload_object.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function func(obj:TestObj, x:number){
2+
>>>x
3+
}
4+
5+
function func(obj:TestObj, x:number, y:number){
6+
return x + y
7+
}
8+
9+
type TestObj = {}
10+
11+
let tObj:TestObj = {}
12+
13+
tObj.func(123)
14+
15+
>>> tObj.func(-1,5)

samples/xxx.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type x = number
2+
let x:x
3+
function x(x:x) {
4+
>>>x
5+
}
6+
7+
x(x)

src/Application/HydraScript.Application.CodeGeneration/Visitors/ExpressionInstructionProvider.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ public AddressedInstructions Visit(CallExpression visitable)
262262
return [];
263263

264264
var methodCall = !visitable.Empty();
265-
string functionId;
266265
AddressedInstructions result = [];
267266

268267
if (methodCall)
@@ -271,12 +270,6 @@ public AddressedInstructions Visit(CallExpression visitable)
271270
var lastMemberInstruction = (DotRead)memberInstructions[memberInstructions.End];
272271
memberInstructions.Remove(lastMemberInstruction);
273272
result.AddRange(memberInstructions);
274-
275-
functionId = lastMemberInstruction.Property;
276-
}
277-
else
278-
{
279-
functionId = visitable.Id;
280273
}
281274

282275
if (methodCall)
@@ -298,7 +291,7 @@ public AddressedInstructions Visit(CallExpression visitable)
298291
}
299292

300293
result.Add(new CallFunction(
301-
new FunctionInfo(functionId),
294+
new FunctionInfo(visitable.ComputedFunctionAddress),
302295
visitable.HasReturnValue));
303296

304297
return result;

src/Application/HydraScript.Application.CodeGeneration/Visitors/InstructionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public AddressedInstructions Visit(FunctionDeclaration visitable)
118118
if (!visitable.Statements.Any())
119119
return [];
120120

121-
var functionInfo = new FunctionInfo(visitable.Name);
121+
var functionInfo = new FunctionInfo(visitable.ComputedFunctionAddress);
122122

123123
var result = new AddressedInstructions
124124
{

src/Application/HydraScript.Application.StaticAnalysis/Exceptions/SymbolIsNotCallable.cs

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using HydraScript.Domain.IR.Impl.SymbolIds;
12
using HydraScript.Domain.IR.Impl.Symbols;
23
using HydraScript.Domain.IR.Types;
34

@@ -7,5 +8,5 @@ public interface IMethodStorage
78
{
89
public void BindMethod(ObjectType objectType, FunctionSymbol method);
910

10-
public IReadOnlyDictionary<string, FunctionSymbol> GetAvailableMethods(ObjectType objectType);
11+
public IReadOnlyDictionary<FunctionSymbolId, FunctionSymbol> GetAvailableMethods(ObjectType objectType);
1112
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Declarations.AfterTypesAreLoaded;
2+
using HydraScript.Domain.IR.Impl.SymbolIds;
23
using HydraScript.Domain.IR.Impl.Symbols;
34

45
namespace HydraScript.Application.StaticAnalysis.Impl;
56

67
internal class FunctionWithUndefinedReturnStorage : IFunctionWithUndefinedReturnStorage
78
{
8-
private readonly OrderedDictionary<string, FunctionDeclaration> _declarations = [];
9+
private readonly OrderedDictionary<FunctionSymbolId, FunctionDeclaration> _declarations = [];
910

1011
public void Save(FunctionSymbol symbol, FunctionDeclaration declaration)
1112
{
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
using HydraScript.Domain.IR.Impl.SymbolIds;
12
using HydraScript.Domain.IR.Impl.Symbols;
23
using HydraScript.Domain.IR.Types;
34

45
namespace HydraScript.Application.StaticAnalysis.Impl;
56

67
internal class MethodStorage : IMethodStorage
78
{
8-
private readonly Dictionary<ObjectType, Dictionary<string, FunctionSymbol>> _bindings = [];
9+
private readonly Dictionary<ObjectType, Dictionary<FunctionSymbolId, FunctionSymbol>> _bindings = [];
910

1011
public void BindMethod(ObjectType objectType, FunctionSymbol method)
1112
{
1213
objectType.AddMethod(method.Id);
1314
if (!_bindings.ContainsKey(objectType))
14-
_bindings[objectType] = new Dictionary<string, FunctionSymbol>();
15+
_bindings[objectType] = new Dictionary<FunctionSymbolId, FunctionSymbol>();
1516
_bindings[objectType][method.Id] = method;
1617
}
1718

18-
public IReadOnlyDictionary<string, FunctionSymbol> GetAvailableMethods(ObjectType objectType) =>
19-
_bindings.GetValueOrDefault(objectType, new Dictionary<string, FunctionSymbol>());
19+
public IReadOnlyDictionary<FunctionSymbolId, FunctionSymbol> GetAvailableMethods(ObjectType objectType) =>
20+
_bindings.GetValueOrDefault(objectType, new Dictionary<FunctionSymbolId, FunctionSymbol>());
2021
}

0 commit comments

Comments
 (0)