Browse Source

enforce return on last line of non void function

Kajetan Johannes Hammerle 3 years ago
parent
commit
6658049233
1 changed files with 7 additions and 0 deletions
  1. 7 0
      Compiler.c

+ 7 - 0
Compiler.c

@@ -27,6 +27,7 @@ static Functions functionQueue;
 
 static int returns[RETURN_BUFFER];
 static int returnIndex = 0;
+static bool hasReturn = false;
 static DataType returnType = DT_VOID;
 
 static int breaks[BREAK_BUFFER];
@@ -581,6 +582,7 @@ static void cReturn() {
     if(returnIndex >= RETURN_BUFFER) {
         cError("too much returns in function");
     }
+    hasReturn = true;
     if(returnType == DT_VOID) {
         cConsumeToken(T_SEMICOLON);
         cAddReturn(OP_RETURN);
@@ -762,6 +764,7 @@ static void cContinue() {
 }
 
 static void cLine(Token t) {
+    hasReturn = false;
     cAddOperation(OP_LINE);
     cAddInt16(line);
     switch(t) {
@@ -840,7 +843,11 @@ static void cInnerFunction(Function* f) {
     cConsumeToken(T_OPEN_CURVED_BRACKET);
     int p = cReserve(f->size);
     returnIndex = 0;
+    hasReturn = false;
     cConsumeScope();
+    if(returnType != DT_VOID && !hasReturn) {
+        cError("missing return");
+    }
     cFree(p, vars.maxAddress);
     cLinkReturns(vars.maxAddress);
 }