public static ExpressionNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode, TypeReference expectedType = null)
        {
            Contract.Ensures(Contract.Result <ExpressionNode>() != null);
            ExpressionNode ret = null;

            switch (lexerNode.Type)
            {
            case Lexer.TokenType.FullSymbol:
            case Lexer.TokenType.Symbol:
                ret = DotOperatorNode.Parse(context, lexerNode);
                break;

            case Lexer.TokenType.LiteralNode:
                ret = LiteralNode.Parse(context, lexerNode);
                break;

            case Lexer.TokenType.Value:
                ret = ExpressionNode.Parse(context, lexerNode.Children[0], expectedType);
                break;

            case Lexer.TokenType.Function:
                ret = MethodNode.Parse(context, lexerNode);
                break;

            case Lexer.TokenType.InfixNode:
                ret = InfixParser.Parse(context, lexerNode);
                break;

            case Lexer.TokenType.PostfixNode:
            case Lexer.TokenType.PrefixNode:
                ret = UnaryOperators.Parse(context, lexerNode);
                break;

            case Lexer.TokenType.ParenthesesNode:
                ret = ExpressionNode.Parse(context, lexerNode.Children[1], expectedType);
                break;

            case Lexer.TokenType.ArrayLiteral:
                ret = ArrayCreationNode.Parse(context, lexerNode);
                break;

            case Lexer.TokenType.Null:
                ret = NullNode.Parse(context, lexerNode);
                break;

            default:
                ContractsHelper.AssumeUnreachable("Unknown expression type {0}", lexerNode.Type);
                break;
            }
            if (!(expectedType == null || expectedType.IsAuto()))
            {
                var ambiguous = ret as IAmbiguousNode;
                if (ambiguous != null)
                {
                    ret = ambiguous.RemoveAmbiguity(context, expectedType);
                }
            }

            return(ret);
        }
示例#2
0
        public static ExpressionNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            var left  = ExpressionNode.Parse(context, lexerNode.Children[0]);
            var right = ExpressionNode.Parse(context, lexerNode.Children[1], left.IsGettable ? left.ExpressionReturnType : null);
            var point = context.Parser.GetSequencePoint(lexerNode);

            return(Create(context, LexerToAssignemnt[lexerNode.Children[2].Type], left, right, point));
        }
示例#3
0
        public static ExpressionNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == Lexer.TokenType.InfixNode);
            var left   = ExpressionNode.Parse(context, lexerNode.Children[0]);
            var right  = ExpressionNode.Parse(context, lexerNode.Children[1]);
            var opType = lexerNode.Children[2].Type;

            return(Create(context, Operators[opType], left, right, context.Parser.GetSequencePoint(lexerNode)));
        }
        public static WhileBlock Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == TokenType.WhileLoop);
            var point     = context.Parser.GetSequencePoint(lexerNode);
            var condition = ExpressionNode.Parse(context, lexerNode.Children[2]);
            var block     = CodeBlockNode.Parse(context, lexerNode.Children[4]);

            return(Create(context, condition, block, point));
        }
        public static ExpressionNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == Lexer.TokenType.PostfixNode);
            Contract.Requires(lexerNode.Children[1].Type == Lexer.TokenType.FunctionArgumentsList);
            var function = ExpressionNode.Parse(context, lexerNode.Children[0]);
            var args     = ParseArgList(context, lexerNode.Children[1]);
            var point    = context.Parser.GetSequencePoint(lexerNode.Children[1]);

            return(Create(context, function, args, point));
        }
示例#6
0
        public static SymbolDeclarationNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == Lexer.TokenType.DeclarationNode);
            var            info         = DeclarationInfo.Parse(context.Parser, lexerNode);
            var            name         = info.SymbolName.GetSingleSymbolOrThrow();
            var            declaredType = TypeNode.Parse(context, info.Type);
            var            point        = context.Parser.GetSequencePoint(lexerNode);
            ExpressionNode initializer  = info.Initializer == null ? null : ExpressionNode.Parse(context, info.Initializer, declaredType);

            return(Create(context, info.Modifiers, declaredType, name, initializer, point));
        }
示例#7
0
        public static ReturnNode Parse(CodeBlockNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == Lexer.TokenType.ReturnNode);
            var            point      = context.Parser.GetSequencePoint(lexerNode);
            var            returnType = context.GetMethod().MethodReturnType;
            ExpressionNode expression = null;

            if (lexerNode.Children.Count == 2)
            {
                expression = ExpressionNode.Parse(context, lexerNode.Children[1], returnType);
            }
            return(Create(context, expression, point));
        }
        public static ConditionBlockNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == TokenType.ConditionalSentence);
            Contract.Ensures(Contract.Result <ConditionBlockNode>() != null);
            var           point      = context.Parser.GetSequencePoint(lexerNode);
            var           condition  = ExpressionNode.Parse(context, lexerNode.Children[2]);
            var           trueBlock  = CodeBlockNode.Parse(context, lexerNode.Children[4]);
            CodeBlockNode falseBlock = null;

            if (lexerNode.Children.Count > 5)
            {
                falseBlock = CodeBlockNode.Parse(context, lexerNode.Children[6]);
            }
            return(Create(context, condition, trueBlock, falseBlock, point));
        }
        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>());
            }
        }
        public static ExpressionNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == Lexer.TokenType.PrefixNode);
            Contract.Requires(lexerNode.Children[1].Type == Lexer.TokenType.CastOperator);

            var type   = TypeNode.Parse(context, lexerNode.Children[1].Children[1]);
            var target = ExpressionNode.Parse(context, lexerNode.Children[0], type);

            if (!target.IsGettable)
            {
                ErrorCode.NotAnRValue.ReportAndThrow(target.SequencePoint, "The expression being cast must be gettable");
            }

            if (!target.ExpressionReturnType.IsCastableTo(type))
            {
                ErrorCode.IllegalCast.ReportAndThrow(context.Parser.GetSequencePoint(lexerNode), "Cannot cast expression of type {0} to {1}", target.ExpressionReturnType, type);
            }

            return(new CastNode(type, target, context.Parser.GetSequencePoint(lexerNode)));
        }
        public void Initialize()
        {
            if (initializer == null)
            {
                if (TypeReference.IsAuto())
                {
                    ErrorCode.MissingInit.ReportAndThrow(SequencePoint, "Type inference requires initialization");
                }
                return;
            }

            Initializer = ExpressionNode.Parse(this, initializer, TypeReference);

            if (!Initializer.IsGettable)
            {
                ErrorCode.NotAnRValue.ReportAndThrow(Initializer.SequencePoint, "Initializer must be a gettable expression");
            }

            if (TypeReference.IsAuto())
            {
                if (Initializer.ExpressionReturnType.IsTypeless())
                {
                    ErrorCode.InferrenceFromTypeless.ReportAndThrow(Initializer.SequencePoint, "Cannot infer type from a typeless expression");
                }
                TypeReference = Initializer.ExpressionReturnType;
                DeclareField();
            }
            else
            {
                if (!Initializer.ExpressionReturnType.IsAssignableTo(TypeReference))
                {
                    ErrorCode.TypeMismatch.ReportAndThrow(Initializer.SequencePoint,
                                                          "Field of type {0} initialized with {1}", TypeReference, Initializer.ExpressionReturnType);
                }
            }

            if (Parser.ProjectParser.ShouldEmit)
            {
                GetClass().TypeEmitter.AddFieldInitializer(FieldDefinition, Initializer);
            }
        }
示例#12
0
        private static ExpressionNode ParsePrefix(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            var opType = lexerNode.Children[1].Type;

            if (opType == Lexer.TokenType.CastOperator)
            {
                return(CastNode.Parse(context, lexerNode));
            }
            else
            {
                InternalUnaryOperatorType op;
                switch (opType)
                {
                case Lexer.TokenType.PlusPlus:
                    op = InternalUnaryOperatorType.PreIncrement;
                    break;

                case Lexer.TokenType.MinusMinus:
                    op = InternalUnaryOperatorType.PreDecrement;
                    break;

                case Lexer.TokenType.Not:
                    op = InternalUnaryOperatorType.LogicalNot;
                    break;

                case Lexer.TokenType.BitwiseComplement:
                    op = InternalUnaryOperatorType.BinaryNot;
                    break;

                case Lexer.TokenType.Minus:
                    op = InternalUnaryOperatorType.Negation;
                    break;

                default:
                    ContractsHelper.AssertUnreachable("Unknown prefix op {0}", opType);
                    return(null);   //unreachable
                }
                return(Create(context, ExpressionNode.Parse(context, lexerNode.Children[0]), op));
            }
        }
        public static InitializerList Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == Lexer.TokenType.InitializerList);
            Contract.Ensures(Contract.Result <InitializerList>() != null);
            var point    = context.Parser.GetSequencePoint(lexerNode);
            var instance = new InitializerList(point);
            var members  = new List <ExpressionNode>();

            foreach (var node in lexerNode.Children)
            {
                switch (node.Type)
                {
                case Lexer.TokenType.LeftCurlyBrace:
                case Lexer.TokenType.RightCurlyBrace:
                case Lexer.TokenType.Comma:
                    break;

                case Lexer.TokenType.Value:
                    members.Add(ExpressionNode.Parse(context, node));
                    break;
                }
            }

            var arrays = members.Select(m => m as ArrayCreationNode);

            //implicit = initializer list
            if (arrays.Any(a => a != null && a.IsImplicit))
            {
                if (arrays.Any(a => a == null || !a.IsImplicit))
                {
                    ErrorCode.MisshapedMatrix.ReportAndThrow(point, "An initializer list can only contain another initializer list if all members are lists of the same dimmensions and types");
                }

                var lists = arrays.Select(a => a.InitializerList);
                return(Create(context, lists, point));
            }

            return(Create(context, members, point));
        }
        public static ForEachNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            //For + LeftParenthesis + ForEachDeclaration + In + Value + RightParenthesis + CodeConstruct),
            Contract.Requires(lexerNode.Type == Lexer.TokenType.ForEachLoop);

            var instance = new ForEachNode(context, context.Parser.GetSequencePoint(lexerNode));

            var collection = ExpressionNode.Parse(context, lexerNode.Children[4]);

            if (!collection.IsGettable)
            {
                ErrorCode.NotAnRValue.ReportAndThrow(collection.SequencePoint, "collection must be a gettable expression");
            }
            if (collection.ExpressionReturnType.IsTypeless())
            {
                ErrorCode.InvalidForEachCollection.ReportAndThrow(collection.SequencePoint, "collection must not be a typeless expression");
            }
            var collectionElementType = collection.ExpressionReturnType.GetEnumerableElementType();

            if (collectionElementType == null)
            {
                ErrorCode.InvalidForEachCollection.ReportAndThrow(collection.SequencePoint, "Cannot iterate over expression type {0}", collection.ExpressionReturnType);
            }

            var declaration = LoopVariableDeclaration.Parse(context, lexerNode.Children[2], collectionElementType);

            if (!collectionElementType.IsAssignableTo(declaration.Variable.VariableType))
            {
                ErrorCode.TypeMismatch.ReportAndThrow(declaration.SequencePoint, "Cannot assign collection elements of type {0} to {1}", collectionElementType, declaration.Variable.VariableType);
            }

            instance.collection = collection;
            instance.variable   = declaration;

            instance.body = CodeBlockNode.Parse(instance, lexerNode.Children[6]);

            return(instance);
        }
示例#15
0
        public static ExpressionNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Children[1].Type == Lexer.TokenType.IndexNode);
            Contract.Ensures(Contract.Result <ExpressionNode>() != null);
            var point   = context.Parser.GetSequencePoint(lexerNode);
            var array   = ExpressionNode.Parse(context, lexerNode.Children[0]);
            var indexer = lexerNode.Children[1];

            if (IsEmptyIndexer(indexer))
            {
                var type = array as TypeNode;
                if (type == null)
                {
                    ErrorCode.TypeExpected.ReportAndThrow(point, "Type expected");
                }
                int rank = CountEmptyIndexerDims(indexer);
                return(TypeNode.Create(AssemblyRegistry.GetArrayType(type.ParsedType, rank), context, point));
            }

            var indices = ParseIndex(context, indexer);

            return(Create(context, array, indices, point));
        }
示例#16
0
        private static ExpressionNode ParseSuffix(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            var opType = lexerNode.Children[1].Type;

            switch (opType)
            {
            case Lexer.TokenType.PlusPlus:
                return(Create(context, ExpressionNode.Parse(context, lexerNode.Children[0]), InternalUnaryOperatorType.PostIncrement));

            case Lexer.TokenType.MinusMinus:
                return(Create(context, ExpressionNode.Parse(context, lexerNode.Children[0]), InternalUnaryOperatorType.PostDecrement));

            case Lexer.TokenType.FunctionArgumentsList:
                return(MethodCallNode.Parse(context, lexerNode));

            case Lexer.TokenType.IndexNode:
                return(ArrayAccessNode.Parse(context, lexerNode));

            default:
                ContractsHelper.AssertUnreachable("Unknown postfix op {0}", opType);
                return(null);   //unreachable
            }
        }
        public static ParserNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(context != null);

            CodeBlockNode  init      = null;
            ExpressionNode condition = null;
            CodeBlockNode  increment = null;

            IAbstractSyntaxTree initNode      = lexerNode.Children[2];
            IAbstractSyntaxTree conditionNode = lexerNode.Children[4];
            IAbstractSyntaxTree incrementNode = lexerNode.Children[6];
            IAbstractSyntaxTree bodyNode      = lexerNode.Children[8];

            if (initNode.Type != TokenType.Empty)
            {
                init = CodeBlockNode.Create(context, context.Parser.GetSequencePoint(initNode));
                init.AddStatement(initNode);
                //makes init scope encompass for scope
                context = init;
            }

            if (conditionNode.Type != TokenType.Empty)
            {
                condition = ExpressionNode.Parse(context, conditionNode, context.Parser.Bool);
            }

            if (incrementNode.Type != TokenType.Empty)
            {
                increment = CodeBlockNode.Create(context, context.Parser.GetSequencePoint(incrementNode));
                increment.AddStatement(incrementNode);
            }

            var body = CodeBlockNode.Parse(context, bodyNode);

            return(ForLoopNode.Create(context, init, condition, increment, body, context.Parser.GetSequencePoint(lexerNode)));
        }
        private static List <ExpressionNode> ParseArgList(ContextNode parent, IAbstractSyntaxTree lexerNode)
        {
            var args = new List <ExpressionNode>();

            foreach (var node in lexerNode.Children)
            {
                switch (node.Type)
                {
                case Lexer.TokenType.LeftParenthesis:
                case Lexer.TokenType.RightParenthesis:
                case Lexer.TokenType.Comma:
                    break;

                case Lexer.TokenType.Value:
                    args.Add(ExpressionNode.Parse(parent, node));
                    break;

                default:
                    ContractsHelper.AssertUnreachable("Unexpected node {0} in call", node.Type);
                    break;
                }
            }
            return(args);
        }
示例#19
0
        public static ExpressionNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(
                lexerNode.Type == Lexer.TokenType.InfixNode && lexerNode.Children[2].Type == Lexer.TokenType.Period ||
                lexerNode.Type == Lexer.TokenType.FullSymbol ||
                lexerNode.Type == Lexer.TokenType.Symbol);

            Contract.Ensures(Contract.Result <ExpressionNode>() != null);

            var instance = new DotOperatorNode(context);

            if (lexerNode.Type == Lexer.TokenType.Symbol)
            {
                instance.Append(new SymbolNode(lexerNode.Content, context, context.Parser.GetSequencePoint(lexerNode)));
                return(instance.builtNode);
            }

            foreach (var node in lexerNode.Children)
            {
                var point = context.Parser.GetSequencePoint(node);
                switch (node.Type)
                {
                case Lexer.TokenType.Period:
                    break;

                case Lexer.TokenType.Symbol:
                    instance.Append(new SymbolNode(node.Content, context, point));
                    break;

                default:
                    instance.Append(ExpressionNode.Parse(context, node));
                    break;
                }
            }
            return(instance.builtNode);
        }
示例#20
0
        public static IReadOnlyList <ExpressionNode> ParseIndex(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == Lexer.TokenType.IndexNode);
            Contract.Requires(!IsEmptyIndexer(lexerNode));

            return(lexerNode.Children.AsEnumerable().Where(n => n.Type == Lexer.TokenType.Value).Select(n => ExpressionNode.Parse(context, n)).ToArray());
        }