Skip to content

Commit 3af3943

Browse files
committed
[fields] Accept public fields.
Close #191
1 parent c738643 commit 3af3943

File tree

8 files changed

+56
-27
lines changed

8 files changed

+56
-27
lines changed

GenC.fu

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,6 +3936,16 @@ public class GenC : GenCCpp
39363936
WriteLine("extern \"C\" {");
39373937
WriteLine("#endif");
39383938
WriteTypedefs(program, true);
3939+
foreach (FuClass klass in program.Classes) {
3940+
if (!klass.IsPublic)
3941+
continue;
3942+
for (FuSymbol? member = klass.First; member != null; member = member.Next) {
3943+
if (member is FuField field && field.Visibility == FuVisibility.Public) {
3944+
WriteClass(klass, program);
3945+
break;
3946+
}
3947+
}
3948+
}
39393949
CloseStringWriter();
39403950
WriteNewLine();
39413951
WriteLine("#ifdef __cplusplus");

Parser.fu

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,6 @@ public class FuParser : FuLexer
10691069
}
10701070

10711071
// field
1072-
if (visibility == FuVisibility.Public)
1073-
ReportFormerError(line, column, "public".Length, "Field cannot be public");
10741072
if (callType != FuCallType.Normal)
10751073
ReportCallTypeError(callTypeLine, callTypeColumn, "Field", callType);
10761074
if (type == this.Host.Program.System.VoidType)

doc/reference.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -636,20 +636,6 @@ class Point
636636
}
637637
```
638638

639-
Fields _cannot be_ public. Instead, define _getter/setter_ methods:
640-
641-
```csharp
642-
public class Image
643-
{
644-
int Width;
645-
public int GetWidth() { return Width; }
646-
public int SetWidth!(int value) { Width = value; }
647-
648-
int Height;
649-
public int GetHeight() => Height; // syntax sugar
650-
}
651-
```
652-
653639
Fields _cannot be_ static. Shared state poses problems with lifetime
654640
and multithreading.
655641

libfut.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4390,8 +4390,6 @@ void FuParser::parseClass(std::shared_ptr<FuCodeDoc> doc, int line, int column,
43904390
this->foundName = method.get();
43914391
continue;
43924392
}
4393-
if (visibility == FuVisibility::public_)
4394-
reportFormerError(line, column, 6, "Field cannot be public");
43954393
if (callType != FuCallType::normal)
43964394
reportCallTypeError(callTypeLine, callTypeColumn, "Field", callType);
43974395
if (type == this->host->program->system->voidType)
@@ -13471,6 +13469,17 @@ void GenC::writeProgram(const FuProgram * program, std::string_view outputFile,
1347113469
writeLine("extern \"C\" {");
1347213470
writeLine("#endif");
1347313471
writeTypedefs(program, true);
13472+
for (const FuClass * klass : program->classes) {
13473+
if (!klass->isPublic)
13474+
continue;
13475+
for (const FuSymbol * member = klass->first; member != nullptr; member = member->next) {
13476+
const FuField * field;
13477+
if ((field = dynamic_cast<const FuField *>(member)) && field->visibility == FuVisibility::public_) {
13478+
writeClass(klass, program);
13479+
break;
13480+
}
13481+
}
13482+
}
1347413483
closeStringWriter();
1347513484
writeNewLine();
1347613485
writeLine("#ifdef __cplusplus");

libfut.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4654,8 +4654,6 @@ void ParseClass(FuCodeDoc doc, int line, int column, bool isPublic, FuCallType c
46544654
this.FoundName = method;
46554655
continue;
46564656
}
4657-
if (visibility == FuVisibility.Public)
4658-
ReportFormerError(line, column, 6, "Field cannot be public");
46594657
if (callType != FuCallType.Normal)
46604658
ReportCallTypeError(callTypeLine, callTypeColumn, "Field", callType);
46614659
if (type == this.Host.Program.System.VoidType)
@@ -13686,6 +13684,16 @@ public override void WriteProgram(FuProgram program, string outputFile, string n
1368613684
WriteLine("extern \"C\" {");
1368713685
WriteLine("#endif");
1368813686
WriteTypedefs(program, true);
13687+
foreach (FuClass klass in program.Classes) {
13688+
if (!klass.IsPublic)
13689+
continue;
13690+
for (FuSymbol member = klass.First; member != null; member = member.Next) {
13691+
if (member is FuField field && field.Visibility == FuVisibility.Public) {
13692+
WriteClass(klass, program);
13693+
break;
13694+
}
13695+
}
13696+
}
1368913697
CloseStringWriter();
1369013698
WriteNewLine();
1369113699
WriteLine("#ifdef __cplusplus");

libfut.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4878,8 +4878,6 @@ export class FuParser extends FuLexer
48784878
this.#foundName = method;
48794879
continue;
48804880
}
4881-
if (visibility == FuVisibility.PUBLIC)
4882-
this.#reportFormerError(line, column, 6, "Field cannot be public");
48834881
if (callType != FuCallType.NORMAL)
48844882
this.#reportCallTypeError(callTypeLine, callTypeColumn, "Field", callType);
48854883
if (type == this.host.program.system.voidType)
@@ -14181,6 +14179,17 @@ export class GenC extends GenCCpp
1418114179
this.writeLine("extern \"C\" {");
1418214180
this.writeLine("#endif");
1418314181
this.writeTypedefs(program, true);
14182+
for (const klass of program.classes) {
14183+
if (!klass.isPublic)
14184+
continue;
14185+
for (let member = klass.first; member != null; member = member.next) {
14186+
let field;
14187+
if ((field = member) instanceof FuField && field.visibility == FuVisibility.PUBLIC) {
14188+
this.writeClass(klass, program);
14189+
break;
14190+
}
14191+
}
14192+
}
1418414193
this.closeStringWriter();
1418514194
this.writeNewLine();
1418614195
this.writeLine("#ifdef __cplusplus");

test/ObjectFieldPublic.fu

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Foo
2+
{
3+
public int Bar;
4+
}
5+
6+
public static class Test
7+
{
8+
public static bool Run()
9+
{
10+
Foo() o;
11+
o.Bar = 42;
12+
return o.Bar == 42;
13+
}
14+
}

test/error/FieldPublic.fu

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

0 commit comments

Comments
 (0)