Skip to content

Commit

Permalink
Allowing return from expression if (#1080)
Browse files Browse the repository at this point in the history
Enhancements:
- Allow a return statement in final position in an if expression (see #1060)

Notes:
- Return from a statement if short-circuits the expression and returns from the enclosing function
  • Loading branch information
degory committed Feb 21, 2024
1 parent 700e3c4 commit 0aa1a15
Show file tree
Hide file tree
Showing 21 changed files with 463 additions and 4 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.7.10",
"version": "0.7.11",
"commands": [
"ghul-compiler"
]
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": "../../../publish/ghul.exe",
"source": [
"."
]
}
Empty file.
Empty file.
45 changes: 45 additions & 0 deletions integration-tests/execution/statement-expression-if-2/run.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
test if 0: too small
test elif 0: 0 is less than five
test else 0: 0 is less than five
test if 1: too small
test elif 1: 1 is less than five
test else 1: 1 is less than five
test if 2: too small
test elif 2: 2 is less than five
test else 2: 2 is less than five
test if 3: too small
test elif 3: 3 is less than five
test else 3: 3 is less than five
test if 4: too small
test elif 4: 4 is less than five
test else 4: 4 is less than five
test if 5: 5 is less than ten
test elif 5: just right
test else 5: 5 is less than ten
test if 6: 6 is less than ten
test elif 6: just right
test else 6: 6 is less than ten
test if 7: 7 is less than ten
test elif 7: just right
test else 7: 7 is less than ten
test if 8: 8 is less than ten
test elif 8: just right
test else 8: 8 is less than ten
test if 9: 9 is less than ten
test elif 9: just right
test else 9: 9 is less than ten
test if 10: 10 is ten or more
test elif 10: 10 is too big
test else 10: too big
test if 11: 11 is ten or more
test elif 11: 11 is too big
test else 11: too big
test if 12: 12 is ten or more
test elif 12: 12 is too big
test else 12: too big
test if 13: 13 is ten or more
test elif 13: 13 is too big
test else 13: too big
test if 14: 14 is ten or more
test elif 14: 14 is too big
test else 14: too big
160 changes: 160 additions & 0 deletions integration-tests/execution/statement-expression-if-2/test.ghul
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
use IO.Std.write_line;

entry() is
for i in 0..15 do
write_line("test if " + i + ": " + test_return_if(i));
write_line("test elif " + i + ": " + test_return_elif(i));
write_line("test else " + i + ": " + test_return_else(i));

// TODO these don't work yet
/*
test_break_if(i);
test_break_elif(i);
test_break_else(i);
test_continue_if(i);
test_continue_elif(i);
test_continue_else(i);
*/
od
si

test_return_if(i: int) -> string is
let value =
if i < 5 then
return "too small";
elif i < 10 then
"less than ten";
else
"ten or more";
fi;

return "" + i + " is " + value;
si

test_return_elif(i: int) -> string is
let value =
if i < 5 then
"less than five";
elif i < 10 then
return "just right";
else
"too big";
fi;

return "" + i + " is " + value;
si

test_return_else(i: int) -> string is
let value =
if i < 5 then
"less than five";
elif i < 10 then
"less than ten";
else
return "too big";
fi;

return "" + i + " is " + value;
si

/*
test_break_if(limit: int) -> string is
let result = "";
for i in 0..10 do
result = result +
if i < limit then
"x" + i + " ";
else
break;
fi;

result = result + ".";
od

return result;
si

test_break_else(limit: int) -> string is
let result = "";
for i in 0..10 do
result = result +
if i > limit then
break;
else
"y" + i + " ";
fi;

result = result + ".";
od

return result;
si

test_break_elif(limit: int) -> string is
let result = "";
for i in 0..10 do
result = result +
if i < 3 then
"small";
elif i > limit then
break;
else
"z" + i;
fi;

result = result + ".";
od

return result;
si

test_continue_if(limit: int) -> string is
let result = "";
for i in 0..10 do
result = result +
if i < limit then
continue;
else
"x" + i + " ";
fi;

result = result + ".";
od

return result;
si

test_continue_else(limit: int) -> string is
let result = "";
for i in 0..10 do
result = result +
if i > limit then
"y" + i + " ";
else
continue;
fi;

result = result + ".";
od

return result;
si

test_continue_elif(limit: int) -> string is
let result = "";
for i in 0..10 do
result = result +
if i < 3 then
"small";
elif i > limit then
continue;
else
"z" + i;
fi;

result = result + ".";
od

return result;
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": "../../../publish/ghul.exe",
"source": [
"."
]
}
Empty file.
Empty file.
Loading

0 comments on commit 0aa1a15

Please sign in to comment.