Beispiel #1
0
        public static ReturnNode Create(CodeBlockNode context, ExpressionNode expression, SequencePoint point)
        {
            var instance = new ReturnNode(point);

            instance.expression = expression;
            var returnType = context.GetMethod().MethodReturnType;

            if (expression != null)
            {
                if (returnType.TypeEquals(context.Parser.Void))
                {
                    ErrorCode.TypeMismatch.ReportAndThrow(instance.SequencePoint, "Cannot return a value in a void method");
                }

                if (!expression.ExpressionReturnType.IsAssignableTo(returnType))
                {
                    ErrorCode.TypeMismatch.ReportAndThrow(instance.SequencePoint, "Method returns {0}, cannot return {1}", returnType, expression.ExpressionReturnType);
                }

                if (!expression.IsGettable)
                {
                    ErrorCode.NotAnRValue.ReportAndThrow(point, "Returned expression must be gettable");
                }
            }
            else
            {
                if (!returnType.TypeEquals(context.Parser.Void))
                {
                    ErrorCode.TypeMismatch.ReportAndThrow(instance.SequencePoint, "Method returns {0}, must return a value", returnType);
                }
            }

            return(instance);
        }
        public CodeBlockNode AddStatement(IAbstractSyntaxTree lexerNode)
        {
            switch (lexerNode.Type)
            {
            case Lexer.TokenType.DeclarationNode:
                return(AddNode(SymbolDeclarationNode.Parse(this, lexerNode)));

            case Lexer.TokenType.Value:
                return(AddNode(ExpressionNode.Parse(this, lexerNode)));

            case Lexer.TokenType.ReturnNode:
                return(AddNode(ReturnNode.Parse(this, lexerNode)));

            default:
                ErrorCode.InvalidStructure.ReportAndThrow(Parser.GetSequencePoint(lexerNode), "Unexpected statement {0} in while parsing code block", lexerNode.Type);
                return(Utils.Utils.Fail <CodeBlockNode>());
            }
        }