diff --git a/.github/workflows/d.yml b/.github/workflows/d.yml index 3cbd14ba..b152f687 100644 --- a/.github/workflows/d.yml +++ b/.github/workflows/d.yml @@ -100,6 +100,20 @@ jobs: fi - name: Simple string run: ./tlang syntaxcheck source/tlang/testing/typecheck/simple_string.t + + - name: Simple return (good) + run: | + ./tlang syntaxcheck source/tlang/testing/return/simple_return_good.t + - name: Simple return (bad return position) + run: | + set +e + ./tlang syntaxcheck source/tlang/testing/return/simple_return_bad.t + if [ $? = 255 ] + then + exit 0 + else + exit 1 + fi typecheck: needs: [build, unittests] diff --git a/source/tlang/compiler/parsing/core.d b/source/tlang/compiler/parsing/core.d index abc082e8..099962a8 100644 --- a/source/tlang/compiler/parsing/core.d +++ b/source/tlang/compiler/parsing/core.d @@ -974,6 +974,22 @@ public final class Parser /* Parse the body (and it leaves ONLY when it gets the correct symbol, no expect needed) */ statements = parseBody(); + /* TODO: We should now run through the statements in the body and check for return */ + for(ulong i = 0; i < statements.length; i++) + { + Statement curStatement = statements[i]; + + /* If we find a return statement */ + if(cast(ReturnStmt)curStatement) + { + /* If it is not the last statement, throw an error */ + if(i != statements.length-1) + { + expect("A return statement must be the last statement of a function's body"); + } + } + } + nextToken(); } /* If no body is requested */ diff --git a/source/tlang/testing/return/simple_return_bad.t b/source/tlang/testing/return/simple_return_bad.t new file mode 100644 index 00000000..7d37fee4 --- /dev/null +++ b/source/tlang/testing/return/simple_return_bad.t @@ -0,0 +1,12 @@ +module simple_return_bad; + +int myFunction() +{ + return 1; +} + +int myFunction2() +{ + return 2; + int j = 1; +} \ No newline at end of file diff --git a/source/tlang/testing/return/simple_return_good.t b/source/tlang/testing/return/simple_return_good.t new file mode 100644 index 00000000..5f36ccc7 --- /dev/null +++ b/source/tlang/testing/return/simple_return_good.t @@ -0,0 +1,12 @@ +module simple_return_good; + +int myFunction() +{ + return 1; +} + +int myFunction2() +{ + int j = 1; + return 2; +} \ No newline at end of file