Example #1
0
        private IExpression ReadArrayAccessExpression(IParseContext context, IExpression array)
        {
            context.ReadOrPanic(TokenKind.OpenSquareBracket);
            var indexExpression = Read(context);

            context.ReadOrPanic(TokenKind.CloseSquareBracket);
            return(new ArrayAccessExpression(array, indexExpression));
        }
Example #2
0
        private IExpression ReadParanthesisExpression(IParseContext context)
        {
            context.ReadOrPanic(TokenKind.OpenParanthesis);
            var expression = Read(context);

            context.ReadOrPanic(TokenKind.CloseParanthesis);
            return(expression);
        }
Example #3
0
        private IExpression ReadTypeCastExpression(IParseContext context)
        {
            context.ReadOrPanic(TokenKind.OpenAngularBracket);
            var type = context.ReadNode <Type>();

            context.ReadOrPanic(TokenKind.CloseAngularBracket);
            return(new TypeCastExpression(type, ReadShortExpression(context)));
        }
Example #4
0
        private IExpression ReadArrayAllocationExpression(IParseContext context)
        {
            context.ReadOrPanic(TokenKind.New);
            var allocationType = context.ReadNode <Type>();

            context.ReadOrPanic(TokenKind.OpenParanthesis);
            var count = Read(context);

            context.ReadOrPanic(TokenKind.CloseParanthesis);
            return(new ArrayAllocationExpression(allocationType, count));
        }
Example #5
0
        public FunctionImport Read(IParseContext context)
        {
            context.ReadOrPanic(TokenKind.Import);
            context.ReadOrPanic(TokenKind.OpenParanthesis);
            var libraryName = context.ReadOrPanic(TokenKind.StringLiteral);

            context.ReadOrPanic(TokenKind.CloseParanthesis);
            var functionDeclaration = context.ReadNode <FunctionDeclaration>();

            return(new FunctionImport(libraryName, functionDeclaration));
        }
Example #6
0
        public Declaration Read(IParseContext context)
        {
            var type = context.ReadNode <Type>();
            var name = context.ReadOrPanic(TokenKind.Identifier);

            if (context.NextCodeToken.Kind == TokenKind.Assignment)
            {
                context.ReadOrPanic(TokenKind.Assignment);
                return(new Declaration(type, name, context.ReadNode <IExpression>()));
            }

            return(new Declaration(type, name));
        }
Example #7
0
        private FunctionCallExpression ReadFunctionCallExpression(IParseContext context, IExpression called)
        {
            context.ReadOrPanic(TokenKind.OpenParanthesis);
            var parameters = new List <IExpression>();

            while (context.NextCodeToken.Kind != TokenKind.CloseParanthesis)
            {
                parameters.Add(Read(context));
                if (context.NextCodeToken.Kind == TokenKind.Comma)
                {
                    context.ReadCodeToken();
                }
            }

            context.ReadOrPanic(TokenKind.CloseParanthesis);
            return(new FunctionCallExpression(called, parameters.ToArray()));
        }
Example #8
0
        public FunctionDeclaration Read(IParseContext context)
        {
            var returnType = context.ReadNode <Type>();
            var identifier = context.ReadOrPanic(TokenKind.Identifier);

            context.ReadOrPanic(TokenKind.OpenParanthesis);
            var parameters = new List <FunctionDeclaration.Parameter>();

            while (context.NextCodeToken.Kind != TokenKind.CloseParanthesis)
            {
                var parameterType       = context.ReadNode <Type>();
                var parameterIdentifier = context.ReadOrPanic(TokenKind.Identifier);
                if (context.NextCodeToken.Kind == TokenKind.Comma)
                {
                    context.ReadCodeToken();
                }
                parameters.Add(new FunctionDeclaration.Parameter(parameterType, parameterIdentifier));
            }

            context.ReadOrPanic(TokenKind.CloseParanthesis);
            return(new FunctionDeclaration(returnType, identifier, parameters.ToArray()));
        }
Example #9
0
        public Type Read(IParseContext context)
        {
            var type = new Type(context.ReadOrPanic(TokenKind.PrimitiveType).RawText);

            while (true)
            {
                switch (context.NextCodeToken.Kind)
                {
                case TokenKind.Pointer:
                    context.ReadCodeToken();
                    type = new Type(type, Type.WrappingType.PointerOf);
                    break;

                case TokenKind.OpenSquareBracket:
                    context.ReadCodeToken();
                    context.ReadOrPanic(TokenKind.CloseSquareBracket);
                    type = new Type(type, Type.WrappingType.ArrayOf);
                    break;

                default:
                    return(type);
                }
            }
        }