Skip to content

Commit

Permalink
Naming convention destructuring and public fields
Browse files Browse the repository at this point in the history
Enhancements:
- Destructure non-tuple values by field naming convention (see #1073)
- Public fields (see #1110)

Technical:
- Optionally emit boilerplate IL assembly language from a file specified in a command line argument
  • Loading branch information
degory committed Mar 12, 2024
1 parent bbdbfdb commit af2800b
Show file tree
Hide file tree
Showing 53 changed files with 524 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
"ghul.compiler": {
"version": "0.8.15",
"version": "0.8.16",
"commands": [
"ghul-compiler"
]
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.8.16-alpha.10</Version>
<Version>0.8.17-alpha.33</Version>
<NoWarn>$(NoWarn);NU1507</NoWarn>
</PropertyGroup>
<ItemGroup>
Expand Down
23 changes: 23 additions & 0 deletions integration-tests/execution/field-definition-1/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run test",
"command": "dotnet ghul-test \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "Capture test expectation",
"command": "../../../tasks/capture.sh \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Empty file.
6 changes: 6 additions & 0 deletions integration-tests/execution/field-definition-1/ghul.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"source": [
"."
]
}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
field value is 5
17 changes: 17 additions & 0 deletions integration-tests/execution/field-definition-1/test.ghul
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Test.PublicFields is
use Collections;

class TEST is
value: int field;

init(f: int) is
value = f;
si
si

entry() is
let t = new TEST(5);

IO.Std.write_line("field value is {t.value}");
si
si
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run test",
"command": "dotnet ghul-test \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "Capture test expectation",
"command": "../../../tasks/capture.sh \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"source": [
"."
]
}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
blob_let_a() = 10
blob_let_b() = 10
blob_let_c((1, 2, 3, 4)) = 10
blob_let_d() = 6
blob_let_s() = a = 1, b = AA, c = 3, d = BB
blob_assign_a() = 10
blob_assign_b() = 10
blob_assign_c((1, 2, 3, 4)) = 10
blob_assign_d() = 6
blob_assign_s() = a = 1, b = AA, c = 3, d = BB
i = 0, j = 1
i = 1, j = 2
i = 2, j = 3
i = 3, j = 4
i = 4, j = 5
i = 5, j = 6
i = 6, j = 7
i = 7, j = 8
i = 8, j = 9
i = 9, j = 10
142 changes: 142 additions & 0 deletions integration-tests/execution/naming-convention-destructure-1/test.ghul
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@

use IO.Std.write_line;

entry() is
write_line("blob_let_a() = {blob_let_a()}");
write_line("blob_let_b() = {blob_let_b()}");
write_line("blob_let_c((1, 2, 3, 4)) = {blob_let_c((1, 2, 3, 4))}");
write_line("blob_let_d() = {blob_let_d()}");
write_line("blob_let_s() = {blob_let_s()}");
write_line("blob_assign_a() = {blob_assign_a()}");
write_line("blob_assign_b() = {blob_assign_b()}");
write_line("blob_assign_c((1, 2, 3, 4)) = {blob_assign_c((1, 2, 3, 4))}");
write_line("blob_assign_d() = {blob_assign_d()}");
write_line("blob_assign_s() = {blob_assign_s()}");
blob_f();
si

// struct with fields:
struct BLOB_4[T, U, V, W] is
`0: T field;
`1: U field;
`2: V field;
`3: W field;

init(a: T, b: U, c: V, d: W) is
`0 = a;
`1 = b;
`2 = c;
`3 = d;
si
si

// class with fields
class BLOB_3[T, U, V] is
`0: T field;
`1: U field;
`2: V field;

init(a: T, b: U, c: V) is
`0 = a;
`1 = b;
`2 = c;
si
si

// struct with properties:
struct BLOB_2[T, U] is
`0: T;
`1: U;

init(a: T, b: U) is
`0 = a;
`1 = b;
si
si

blob_let_a() -> int is
let t = new BLOB_4[int,int,int,int](1, 2, 3, 4);

let (a, b, c, d) = t;

return a + b + c + d;
si

blob_let_b() -> int is
let (a, b, c, d) = new BLOB_4[int,int,int,int](1, 2, 3, 4);

return a + b + c + d;
si

blob_let_c(t: (int, int, int, int)) -> int is
let (a, b, c, d) = new BLOB_4[int,int,int,int](t.`0, t.`1, t.`2, t.`3);

return a + b + c + d;
si

blob_let_d() -> int is
let (a, (b, c), d) = new BLOB_3[int, BLOB_2[int, int], int](1, new BLOB_2[int, int](2, 3), 4);

return a + b + c + d;
si

blob_let_s() -> string is
let (a, b, c, d) = new BLOB_4[int, string, int, string](1, "AA", 3, "BB");

return "a = {a}, b = {b}, c = {c}, d = {d}";
si

blob_assign_a() -> int is
let t = new BLOB_4[int,int,int,int](1, 2, 3, 4);

let a: int, b: int, c: int, d: int;

(a, b, c, d) = t;

return a + b + c + d;
si

blob_assign_b() -> int is
let a: int, b: int, c: int, d: int;

(a, b, c, d) = new BLOB_4[int,int,int,int](1, 2, 3, 4);

return a + b + c + d;
si

blob_assign_c(t: (int, int, int, int)) -> int is
let a: int, b: int, c: int, d: int;

(a, b, c, d) = new BLOB_4[int,int,int,int](t.`0, t.`1, t.`2, t.`3);

return a + b + c + d;
si

blob_assign_d() -> int is
let a: int, b: int, c: int, d: int;

(a, (b, c), d) = new BLOB_3[int, BLOB_2[int, int], int](1, new BLOB_2[int, int](2, 3), 4);

return a + b + c + d;
si

blob_assign_s() -> string is
let a: int, b: string, c: int, d: string;

(a, b, c, d) = new BLOB_4[int, string, int, string](1, "AA", 3, "BB");

return "a = {a}, b = {b}, c = {c}, d = {d}";
si

blob_f() is
let a = new Collections.LIST[BLOB_2[int, int]]();

for i in 0..10 do
// a.add((i, i + 1));
a.add(new BLOB_2[int, int](i, i + 1));
od

for (i, j) in a do
write_line("i = {i}, j = {j}");
od
si
Empty file.
23 changes: 23 additions & 0 deletions integration-tests/parse/field-definition-1/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run test",
"command": "dotnet ghul-test \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "Capture test expectation",
"command": "../../../tasks/capture.sh \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Empty file.
1 change: 1 addition & 0 deletions integration-tests/parse/field-definition-1/fail.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

6 changes: 6 additions & 0 deletions integration-tests/parse/field-definition-1/ghul.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"source": [
"."
]
}
1 change: 1 addition & 0 deletions integration-tests/parse/field-definition-1/ghulflags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--type-check
Empty file.
7 changes: 7 additions & 0 deletions integration-tests/parse/field-definition-1/test.ghul
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Test.ParseUnary is
use Collections;

class Test is
value: int field;
si
si
Empty file.
23 changes: 23 additions & 0 deletions integration-tests/semantic/field-definition-1/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run test",
"command": "dotnet ghul-test \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "Capture test expectation",
"command": "../../../tasks/capture.sh \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
3 changes: 3 additions & 0 deletions integration-tests/semantic/field-definition-1/err.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test.ghul: 24,26..24,37: error: _value: int is not publicly readable
test.ghul: 39,9..39,19: error: value: int is not publicly assignable
test.ghul: 42,9..42,20: error: `value: int is not publicly assignable
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

6 changes: 6 additions & 0 deletions integration-tests/semantic/field-definition-1/ghul.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"source": [
"."
]
}
1 change: 1 addition & 0 deletions integration-tests/semantic/field-definition-1/ghulflags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--type-check
Empty file.
44 changes: 44 additions & 0 deletions integration-tests/semantic/field-definition-1/test.ghul
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace Test.ParseUnary is
use Collections;

class TEST is
_value: int field;
value: int field;
`value: int field;

init(`0: int, `1: int, `2: int) is
// all should be assignable here
_value = `0;
value = `1;
`value = `2;
si

get_values() -> (int, int, int) =>
(_value, value, `value);
si

test_is_not_publicly_readable() is
let test = new TEST(1, 2, 3);

// expect error
let value: int = test._value;
si

test_is_publicly_readable() is
let test = new TEST(1, 2, 3);

let value: int = test.value;

value = test.`value;
si

test_is_not_publicly_assignable() is
let test = new TEST(1, 2, 3);

// expect error
test.value = 5;

// expect error
test.`value = 5;
si
si
Empty file.
Loading

0 comments on commit af2800b

Please sign in to comment.