Skip to content

Commit

Permalink
Improved IR for blocks of IL instructions (#1133)
Browse files Browse the repository at this point in the history
Technical:
- Better representation for IR blocks (closes #1131)
  • Loading branch information
degory authored Mar 21, 2024
1 parent fd6e084 commit b15d666
Show file tree
Hide file tree
Showing 95 changed files with 1,194 additions and 652 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.24",
"version": "0.8.25",
"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.25-alpha.2</Version>
<Version>0.8.26-alpha.43</Version>
<NoWarn>$(NoWarn);NU1507</NoWarn>
</PropertyGroup>
<ItemGroup>
Expand Down
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": "../../../bin/Release/net8.0/ghul",
"source": [
"."
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--dotnet
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fibonacci function is System.Func`2[System.Int32,System.Int32]
17 changes: 17 additions & 0 deletions integration-tests/execution/anon-function-recursion-1/test.ghul
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use IO.Std.write_line;

entry() is
test_create_function();
si

test_create_function() is
let fibonacci = (i: int) rec =>
if i < 2 then
i
else
rec(i - 1) + rec(i - 2)
fi;

write_line("fibonacci function is {fibonacci}");
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": "../../../bin/Release/net8.0/ghul",
"source": [
"."
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--dotnet
Empty file.
10 changes: 10 additions & 0 deletions integration-tests/execution/anon-function-recursion-2/run.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fibonacci(0) is 0
fibonacci(1) is 1
fibonacci(2) is 1
fibonacci(3) is 2
fibonacci(4) is 3
fibonacci(5) is 5
fibonacci(6) is 8
fibonacci(7) is 13
fibonacci(8) is 21
fibonacci(9) is 34
18 changes: 18 additions & 0 deletions integration-tests/execution/anon-function-recursion-2/test.ghul
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use IO.Std.write_line;

entry() is
test_call_function();
si

test_call_function() is
let fibonacci = (i: int) rec =>
if i < 2 then
i
else
rec(i - 1) + rec(i - 2)
fi;

for i in 0..10 do
write_line("fibonacci({i}) is {fibonacci(i)}");
od
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": [
"."
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--dotnet --keep-out-il
Empty file.
10 changes: 10 additions & 0 deletions integration-tests/execution/anon-function-recursion-3/run.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fibonacci(0) is 0
fibonacci(1) is 1
fibonacci(2) is 1
fibonacci(3) is 2
fibonacci(4) is 3
fibonacci(5) is 5
fibonacci(6) is 8
fibonacci(7) is 13
fibonacci(8) is 21
fibonacci(9) is 34
21 changes: 21 additions & 0 deletions integration-tests/execution/anon-function-recursion-3/test.ghul
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use IO.Std.write_line;

entry() is
test_call_function();
si

apply[T,R](f: (T) -> R, i: T) -> R =>
f(i);

test_call_function() is
let fibonacci = (i: int) rec =>
if i < 2 then
i
else
apply(rec, i - 1) + apply(rec, i - 2)
fi;

for i in 0..10 do
write_line("fibonacci({i}) is {fibonacci(i)}");
od
si
Empty file.
2 changes: 0 additions & 2 deletions integration-tests/execution/closure-nested-capture/test.ghul
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ namespace Test is

class Main is
entry() static is


let capture_1 = 1;
let capture_2 = 2;

Expand Down
4 changes: 1 addition & 3 deletions integration-tests/execution/control-flow/test.ghul
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
namespace Test is
namespace TestControlFlow is
use Std = IO.Std;

class Main is
entry() static is


let a = 10;
let b = 20;
let c = 30;
Expand Down
11 changes: 0 additions & 11 deletions integration-tests/execution/let-in-1/run.expected
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
simple_expression_body(5) = 10
simple_expression_body((5, 6)) = 11
fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
if_expression_condition_1(5) = 5 * 2 more than 10
if_expression_condition_2(5, 6) = 5 * 2 and 6 * 2 more than 10
generator_1().take(5).to_list() = 0, 1, 2, 3, 4
generator_fibonacci().take(5).to_list() = 0, 1, 1, 2, 3
generator_factorial().take(5).to_list() = 1, 2, 6, 24, 120
101 changes: 0 additions & 101 deletions integration-tests/execution/let-in-1/test.ghul
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ use IO.Std.write_line;
entry() is
write_line("simple_expression_body(5) = " + simple_expression_body_1(5)); // 10
write_line("simple_expression_body((5, 6)) = " + simple_expression_body_2((5, 6))); // 11
recursive_anonymous_function();

write_line("if_expression_condition_1(5) = " + if_expression_condition_1(5)); // 5 * 2 less than 10
write_line("if_expression_condition_2(5, 6) = " + if_expression_condition_2(5, 6)); // 5 * 2 less than 10

write_line("generator_1().take(5).to_list() = " + generator_incremental() | .take(5)); // [0, 1, 2, 3, 4]
write_line("generator_fibonacci().take(5).to_list() = " + generator_fibonacci() | .take(5)); // [0, 1, 1, 2, 3]
write_line("generator_factorial().take(5).to_list() = " + generator_factorial() | .take(5)); // [1, 1, 2, 6, 24]
si

simple_expression_body_1(i: int) -> int =>
Expand All @@ -20,96 +12,3 @@ simple_expression_body_1(i: int) -> int =>
simple_expression_body_2(i: (int, int)) -> int =>
let (a, b) = i
in a + b;

recursive_anonymous_function() is
let fib = (n: int) -> int rec =>
let fib = rec in
if n < 2 then
n
else
fib(n - 1) + fib(n - 2);
fi;

for (i, f) in (0..6) | .map(i => fib(i)) .index() do
write_line("fib(" + i + ") = " + f);
od
si

generator_incremental() -> Collections.Iterable[int] =>
generate(0,
(i: int) =>
let next = i + 1 in (next, i));

generator_fibonacci() -> Collections.Iterable[int] =>
generate((0, 1),
(state: (int, int)) =>
let
(a, b) = state,
next = (b, a + b)
in (next, a));

generator_factorial() -> Collections.Iterable[int] =>
generate((1, 1),
(state: (int, int)) =>
let
(i, f) = state,
next = (i + 1, f * (i + 1))
in (next, f));

if_expression_condition_1(i: int) -> string =>
if
let x = i * 2 in x < 10
then
"{i} * 2 less than 10"
else
"{i} * 2 more than 10"
fi;

if_expression_condition_2(i: int, j: int) -> string =>
if
let x = i * 2 in x < 10
then
"{i} * 2 less than 10"
elif
let y = j * 2 in y < 10
then
"{j} * 2 less than 10"
else
"{i} * 2 and {j} * 2 more than 10"
fi;


class GENERATOR[T, S]: Collections.Iterator[T], Collections.Iterable[T] is
current: T;
iterator: Collections.Iterator[T] => self;

_initial: S;
_state: S;
_generator: S -> (S, T); // given the current state, return the next state and the current value

init(initial: S, generator: S -> (S, T)) is
_initial = initial;
_generator = generator;
reset();
si

init() is
reset();
si

move_next() -> bool is
(_state, current) = _generator(_state);
return true;
si

reset() is
_state = _initial;
si

dispose() is
si
si

// generator constructor helper so we don't have to specify types
generate[T, S](initial: S, generator: S -> (S, T)) -> GENERATOR[T, S] =>
new GENERATOR[T, S](initial, generator);
23 changes: 23 additions & 0 deletions integration-tests/execution/let-in-2/.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/let-in-2/ghul.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compiler": "../../../bin/Release/net8.0/ghul",
"source": [
"."
]
}
1 change: 1 addition & 0 deletions integration-tests/execution/let-in-2/ghulflags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--dotnet
Empty file.
10 changes: 10 additions & 0 deletions integration-tests/execution/let-in-2/run.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fib(0) => 0
fib(1) => 1
fib(2) => 1
fib(3) => 2
fib(4) => 3
fib(5) => 5
fib(6) => 8
fib(7) => 13
fib(8) => 21
fib(9) => 34
Loading

0 comments on commit b15d666

Please sign in to comment.