private void Visit(ReturnStmt returnStmt) { //var expectedType = ReturnTypeStack.Peek(); var expectedType = FunctionStack.Peek().Type; if (returnStmt.ReturnExpression != null && expectedType.BasicType == ExprType.Void) { AddError("Can't return a value in a procedure", returnStmt); return; } if (returnStmt.ReturnExpression == null) { if (IsNotVoid(expectedType)) { AddError("Return statement can't be empty in a function", returnStmt); } return; } Visit((dynamic)returnStmt.ReturnExpression); var returnType = TypeStack.Pop(); if (!TypeChecker.TypeCheckAssign(expectedType, returnType)) { AddError(string.Format("Can't return a value of type {0} in a function of type {1}", returnType, expectedType), returnStmt); } }
private void Visit(ReturnStmt returnStmt) { if (returnStmt.ReturnExpression != null) { Visit((dynamic)returnStmt.ReturnExpression); } CurrentMethodIL.Emit(OpCodes.Ret); }
private Statement ParseReturnStmt() { Match(TokenType.KwReturn); ReturnStmt statement = new ReturnStmt(AcceptedToken); if (MatchPeek(0, Expression.FirstSet)) { statement.ReturnExpression = ParseExpression(); } return(statement); }