Skip to content

Commit c554f26

Browse files
committed
[native] Inside of class.
fusionlanguage#168
1 parent ed46fe3 commit c554f26

File tree

9 files changed

+161
-16
lines changed

9 files changed

+161
-16
lines changed

AST.fu

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,8 @@ public abstract class FuScope : FuSymbol
373373
return null;
374374
}
375375

376-
public void Add!(FuSymbol# symbol)
376+
protected void AddToList!(FuSymbol# symbol)
377377
{
378-
this.Dict[symbol.Name] = symbol;
379378
symbol.Next = null;
380379
symbol.Parent = this;
381380
if (this.First == null)
@@ -385,6 +384,12 @@ public abstract class FuScope : FuSymbol
385384
this.Last = symbol;
386385
}
387386

387+
public void Add!(FuSymbol# symbol)
388+
{
389+
this.Dict[symbol.Name] = symbol;
390+
AddToList(symbol);
391+
}
392+
388393
public bool Encloses(FuSymbol symbol)
389394
{
390395
for (FuScope? scope = symbol.Parent; scope != null; scope = scope.Parent) {
@@ -955,7 +960,7 @@ class FuLock : FuStatement
955960
public override void AcceptStatement(FuVisitor! visitor) { visitor.VisitLock(this); }
956961
}
957962

958-
class FuNative : FuStatement
963+
class FuNative : FuSymbol
959964
{
960965
internal string() Content;
961966
public override int GetLocLength() => 6;
@@ -1343,6 +1348,7 @@ public class FuClass : FuContainerType
13431348
internal FuSymbolReference() BaseClass;
13441349
internal FuMethodBase#? Constructor;
13451350
internal List<FuConst!>() ConstArrays;
1351+
List<FuNative#>() Natives; // FIXME: avoid by moving the ownership from the dictionary to the linked list?
13461352
public bool HasBaseClass() => this.BaseClass.Name.Length > 0;
13471353
public bool AddsVirtualMethods()
13481354
{
@@ -1360,11 +1366,18 @@ public class FuClass : FuContainerType
13601366
{
13611367
Add(FuMethod.New(this, FuVisibility.Public, FuCallType.Normal, type, id, name, isMutator, param0, param1, param2, param3));
13621368
}
1369+
13631370
public void AddStaticMethod!(FuType# type, FuId id, string name, FuVar#? param0, FuVar#? param1 = null, FuVar#? param2 = null)
13641371
{
13651372
Add(FuMethod.New(this, FuVisibility.Public, FuCallType.Static, type, id, name, false, param0, param1, param2));
13661373
}
13671374

1375+
public void AddNative!(FuNative# nat)
1376+
{
1377+
AddToList(nat);
1378+
this.Natives.Add(nat);
1379+
}
1380+
13681381
public bool IsSameOrBaseOf(FuClass derived)
13691382
{
13701383
while (derived != this) {

GenBase.fu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,9 @@ public abstract class GenBase : FuVisitor
20912091
this.SwitchesWithGoto.Clear();
20922092
this.CurrentTemporaries.Clear();
20932093
break;
2094+
case FuNative nat:
2095+
VisitNative(nat);
2096+
break;
20942097
default:
20952098
assert false;
20962099
}

GenC.fu

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3201,10 +3201,17 @@ public class GenC : GenCCpp
32013201
WriteLine(" base;");
32023202
}
32033203
for (FuSymbol? symbol = klass.First; symbol != null; symbol = symbol.Next) {
3204-
if (symbol is FuField field) {
3204+
switch (symbol) {
3205+
case FuField field:
32053206
WriteDoc(field.Documentation);
32063207
WriteTypeAndName(field);
32073208
WriteCharLine(';');
3209+
break;
3210+
case FuNative nat:
3211+
VisitNative(nat);
3212+
break;
3213+
default:
3214+
break;
32083215
}
32093216
}
32103217
this.Indent--;

Parser.fu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,9 @@ public class FuParser : FuLexer
997997
visibility = FuVisibility.Public;
998998
NextToken();
999999
break;
1000+
case FuToken.Native:
1001+
klass.AddNative(ParseNative());
1002+
continue;
10001003
default:
10011004
visibility = FuVisibility.Private;
10021005
break;

libfut.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,9 +1235,8 @@ std::shared_ptr<FuSymbol> FuScope::tryLookup(std::string_view name, bool global)
12351235
return nullptr;
12361236
}
12371237

1238-
void FuScope::add(std::shared_ptr<FuSymbol> symbol)
1238+
void FuScope::addToList(std::shared_ptr<FuSymbol> symbol)
12391239
{
1240-
this->dict[symbol->name] = symbol;
12411240
symbol->next = nullptr;
12421241
symbol->parent = this;
12431242
if (this->first == nullptr)
@@ -1247,6 +1246,12 @@ void FuScope::add(std::shared_ptr<FuSymbol> symbol)
12471246
this->last = symbol.get();
12481247
}
12491248

1249+
void FuScope::add(std::shared_ptr<FuSymbol> symbol)
1250+
{
1251+
this->dict[symbol->name] = symbol;
1252+
addToList(symbol);
1253+
}
1254+
12501255
bool FuScope::encloses(const FuSymbol * symbol) const
12511256
{
12521257
for (const FuScope * scope = symbol->parent; scope != nullptr; scope = scope->parent) {
@@ -2432,6 +2437,12 @@ void FuClass::addStaticMethod(std::shared_ptr<FuType> type, FuId id, std::string
24322437
add(FuMethod::new_(this, FuVisibility::public_, FuCallType::static_, type, id, name, false, param0, param1, param2));
24332438
}
24342439

2440+
void FuClass::addNative(std::shared_ptr<FuNative> nat)
2441+
{
2442+
addToList(nat);
2443+
this->natives.push_back(nat);
2444+
}
2445+
24352446
bool FuClass::isSameOrBaseOf(const FuClass * derived) const
24362447
{
24372448
while (derived != this) {
@@ -4255,6 +4266,9 @@ void FuParser::parseClass(std::shared_ptr<FuCodeDoc> doc, int line, int column,
42554266
visibility = FuVisibility::public_;
42564267
nextToken();
42574268
break;
4269+
case FuToken::native:
4270+
klass->addNative(parseNative());
4271+
continue;
42584272
default:
42594273
visibility = FuVisibility::private_;
42604274
break;
@@ -8887,6 +8901,8 @@ void GenBase::writeMembers(const FuClass * klass, bool constArrays)
88878901
this->switchesWithGoto.clear();
88888902
this->currentTemporaries.clear();
88898903
}
8904+
else if (const FuNative *nat = dynamic_cast<const FuNative *>(symbol))
8905+
visitNative(nat);
88908906
else
88918907
std::abort();
88928908
}
@@ -12590,6 +12606,8 @@ void GenC::writeClassInternal(const FuClass * klass)
1259012606
writeTypeAndName(field);
1259112607
writeCharLine(';');
1259212608
}
12609+
else if (const FuNative *nat = dynamic_cast<const FuNative *>(symbol))
12610+
visitNative(nat);
1259312611
}
1259412612
this->indent--;
1259512613
writeLine("};");

libfut.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,9 +1698,8 @@ public FuSymbol TryLookup(string name, bool global)
16981698
return null;
16991699
}
17001700

1701-
public void Add(FuSymbol symbol)
1701+
protected void AddToList(FuSymbol symbol)
17021702
{
1703-
this.Dict[symbol.Name] = symbol;
17041703
symbol.Next = null;
17051704
symbol.Parent = this;
17061705
if (this.First == null)
@@ -1710,6 +1709,12 @@ public void Add(FuSymbol symbol)
17101709
this.Last = symbol;
17111710
}
17121711

1712+
public void Add(FuSymbol symbol)
1713+
{
1714+
this.Dict[symbol.Name] = symbol;
1715+
AddToList(symbol);
1716+
}
1717+
17131718
public bool Encloses(FuSymbol symbol)
17141719
{
17151720
for (FuScope scope = symbol.Parent; scope != null; scope = scope.Parent) {
@@ -2466,7 +2471,7 @@ public override void AcceptStatement(FuVisitor visitor)
24662471
}
24672472
}
24682473

2469-
class FuNative : FuStatement
2474+
class FuNative : FuSymbol
24702475
{
24712476

24722477
internal string Content;
@@ -2962,6 +2967,8 @@ public class FuClass : FuContainerType
29622967

29632968
internal readonly List<FuConst> ConstArrays = new List<FuConst>();
29642969

2970+
readonly List<FuNative> Natives = new List<FuNative>();
2971+
29652972
public bool HasBaseClass() => this.BaseClass.Name.Length > 0;
29662973

29672974
public bool AddsVirtualMethods()
@@ -2985,6 +2992,12 @@ public void AddStaticMethod(FuType type, FuId id, string name, FuVar param0, FuV
29852992
Add(FuMethod.New(this, FuVisibility.Public, FuCallType.Static, type, id, name, false, param0, param1, param2));
29862993
}
29872994

2995+
public void AddNative(FuNative nat)
2996+
{
2997+
AddToList(nat);
2998+
this.Natives.Add(nat);
2999+
}
3000+
29883001
public bool IsSameOrBaseOf(FuClass derived)
29893002
{
29903003
while (derived != this) {
@@ -4533,6 +4546,9 @@ void ParseClass(FuCodeDoc doc, int line, int column, bool isPublic, FuCallType c
45334546
visibility = FuVisibility.Public;
45344547
NextToken();
45354548
break;
4549+
case FuToken.Native:
4550+
klass.AddNative(ParseNative());
4551+
continue;
45364552
default:
45374553
visibility = FuVisibility.Private;
45384554
break;
@@ -9035,6 +9051,9 @@ protected void WriteMembers(FuClass klass, bool constArrays)
90359051
this.SwitchesWithGoto.Clear();
90369052
this.CurrentTemporaries.Clear();
90379053
break;
9054+
case FuNative nat:
9055+
VisitNative(nat);
9056+
break;
90389057
default:
90399058
throw new NotImplementedException();
90409059
}
@@ -12806,10 +12825,17 @@ protected override void WriteClassInternal(FuClass klass)
1280612825
WriteLine(" base;");
1280712826
}
1280812827
for (FuSymbol symbol = klass.First; symbol != null; symbol = symbol.Next) {
12809-
if (symbol is FuField field) {
12828+
switch (symbol) {
12829+
case FuField field:
1281012830
WriteDoc(field.Documentation);
1281112831
WriteTypeAndName(field);
1281212832
WriteCharLine(';');
12833+
break;
12834+
case FuNative nat:
12835+
VisitNative(nat);
12836+
break;
12837+
default:
12838+
break;
1281312839
}
1281412840
}
1281512841
this.Indent--;

libfut.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ class FuScope : public FuSymbol
731731
protected:
732732
FuScope() = default;
733733
std::unordered_map<std::string_view, std::shared_ptr<FuSymbol>> dict;
734+
void addToList(std::shared_ptr<FuSymbol> symbol);
734735
public:
735736
FuSymbol * first = nullptr;
736737
private:
@@ -1101,7 +1102,7 @@ class FuLock : public FuStatement
11011102
std::shared_ptr<FuStatement> body;
11021103
};
11031104

1104-
class FuNative : public FuStatement
1105+
class FuNative : public FuSymbol
11051106
{
11061107
public:
11071108
FuNative() = default;
@@ -1399,6 +1400,7 @@ class FuClass : public FuContainerType
13991400
static std::shared_ptr<FuClass> new_(FuCallType callType, FuId id, std::string_view name, int typeParameterCount = 0);
14001401
void addMethod(std::shared_ptr<FuType> type, FuId id, std::string_view name, bool isMutator, std::shared_ptr<FuVar> param0 = nullptr, std::shared_ptr<FuVar> param1 = nullptr, std::shared_ptr<FuVar> param2 = nullptr, std::shared_ptr<FuVar> param3 = nullptr);
14011402
void addStaticMethod(std::shared_ptr<FuType> type, FuId id, std::string_view name, std::shared_ptr<FuVar> param0, std::shared_ptr<FuVar> param1 = nullptr, std::shared_ptr<FuVar> param2 = nullptr);
1403+
void addNative(std::shared_ptr<FuNative> nat);
14021404
bool isSameOrBaseOf(const FuClass * derived) const;
14031405
bool hasToString() const;
14041406
bool addsToString() const;
@@ -1409,6 +1411,8 @@ class FuClass : public FuContainerType
14091411
FuSymbolReference baseClass;
14101412
std::shared_ptr<FuMethodBase> constructor;
14111413
std::vector<FuConst *> constArrays;
1414+
private:
1415+
std::vector<std::shared_ptr<FuNative>> natives;
14121416
};
14131417

14141418
class FuClassType : public FuType

libfut.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,9 +1631,8 @@ export class FuScope extends FuSymbol
16311631
return null;
16321632
}
16331633

1634-
add(symbol)
1634+
addToList(symbol)
16351635
{
1636-
this.dict[symbol.name] = symbol;
16371636
symbol.next = null;
16381637
symbol.parent = this;
16391638
if (this.first == null)
@@ -1643,6 +1642,12 @@ export class FuScope extends FuSymbol
16431642
this.#last = symbol;
16441643
}
16451644

1645+
add(symbol)
1646+
{
1647+
this.dict[symbol.name] = symbol;
1648+
this.addToList(symbol);
1649+
}
1650+
16461651
encloses(symbol)
16471652
{
16481653
for (let scope = symbol.parent; scope != null; scope = scope.parent) {
@@ -2518,7 +2523,7 @@ class FuLock extends FuStatement
25182523
}
25192524
}
25202525

2521-
class FuNative extends FuStatement
2526+
class FuNative extends FuSymbol
25222527
{
25232528
content;
25242529

@@ -3096,6 +3101,7 @@ export class FuClass extends FuContainerType
30963101
baseClass = new FuSymbolReference();
30973102
constructor_;
30983103
constArrays = [];
3104+
#natives = [];
30993105

31003106
hasBaseClass()
31013107
{
@@ -3127,6 +3133,12 @@ export class FuClass extends FuContainerType
31273133
this.add(FuMethod.new(this, FuVisibility.PUBLIC, FuCallType.STATIC, type, id, name, false, param0, param1, param2));
31283134
}
31293135

3136+
addNative(nat)
3137+
{
3138+
this.addToList(nat);
3139+
this.#natives.push(nat);
3140+
}
3141+
31303142
isSameOrBaseOf(derived)
31313143
{
31323144
while (derived != this) {
@@ -4753,6 +4765,9 @@ export class FuParser extends FuLexer
47534765
visibility = FuVisibility.PUBLIC;
47544766
this.nextToken();
47554767
break;
4768+
case FuToken.NATIVE:
4769+
klass.addNative(this.#parseNative());
4770+
continue;
47564771
default:
47574772
visibility = FuVisibility.PRIVATE;
47584773
break;
@@ -9444,6 +9459,10 @@ export class GenBase extends FuVisitor
94449459
this.switchesWithGoto.length = 0;
94459460
this.currentTemporaries.length = 0;
94469461
}
9462+
else if (symbol instanceof FuNative) {
9463+
const nat = symbol;
9464+
this.visitNative(nat);
9465+
}
94479466
else
94489467
throw new Error();
94499468
}
@@ -13276,12 +13295,16 @@ export class GenC extends GenCCpp
1327613295
this.writeLine(" base;");
1327713296
}
1327813297
for (let symbol = klass.first; symbol != null; symbol = symbol.next) {
13279-
let field;
13280-
if ((field = symbol) instanceof FuField) {
13298+
if (symbol instanceof FuField) {
13299+
const field = symbol;
1328113300
this.writeDoc(field.documentation);
1328213301
this.writeTypeAndName(field);
1328313302
this.writeCharLine(59);
1328413303
}
13304+
else if (symbol instanceof FuNative) {
13305+
const nat = symbol;
13306+
this.visitNative(nat);
13307+
}
1328513308
}
1328613309
this.indent--;
1328713310
this.writeLine("};");

0 commit comments

Comments
 (0)