예제 #1
0
        public Expr Parse(ParserContext context)
        {
            // Create a lambda expression.
            LambdaExpr lambda = new LambdaExpr();

            // Parse the formal arguments.
            FormalArgs args = new FormalArgsParser().Parse(context);

            // Assign the parsed arguments to the lambda.
            lambda.Args = args;

            // Create the type buffer, defaulting to void.
            ITypeEmitter type = PrimitiveTypeFactory.Void();

            // Return type is explicitly specified, parse and use it instead of the default.
            if (context.Stream.Current.Type == TokenType.SymbolColon)
            {
                // Ensure current type is symbol colon.
                context.Stream.EnsureCurrent(TokenType.SymbolColon);

                // Skip symbol colon token.
                context.Stream.Skip();

                // Parse the return type.
                type = new TypeParser().Parse(context);
            }

            // Assign the parsed type to the return type.
            lambda.ReturnType = type;

            // Ensure current token is symbol arrow.
            context.Stream.EnsureCurrent(TokenType.SymbolArrow);

            // Skip arrow symbol token.
            context.Stream.Skip();

            // Parse the body block.
            Block body = new BlockParser().Parse(context);

            // Assign the block to the lambda.
            lambda.Body = body;

            // Return the resulting expression.
            return(lambda);
        }
예제 #2
0
        public Function Parse(TokenStream stream)
        {
            // Parse the prototype from the stream, this captures the name, arguments and return type.
            Prototype prototype = new PrototypeParser().Parse(stream);

            // Create the function.
            var function = new Function();

            // Assign the function prototype to the parsed prototype.
            function.Prototype = prototype;

            // Parse the body.
            Block body = new BlockParser().Parse(stream);

            // Set the name of the body block.
            body.SetNameEntry();

            // Assign the body.
            function.Body = body;

            return(function);
        }
예제 #3
0
        public Function Parse(ParserContext context)
        {
            // Create the attribute buffer list.
            List <Attribute> attributes = new List <Attribute>();

            // Parse attribute if applicable.
            while (context.Stream.Current.Type == TokenType.SymbolBracketL)
            {
                // Invoke attribute parser.
                Attribute attribute = new AttributeParser().Parse(context);

                // Append the resulting attribute onto the buffer list.
                attributes.Add(attribute);
            }

            // Parse the prototype from the stream, this captures the name, arguments and return type.
            Prototype prototype = new PrototypeParser().Parse(context);

            // Create the function.
            Function function = new Function();

            // Assign the function attributes.
            function.Attributes = attributes.ToArray();

            // Assign the function prototype to the parsed prototype.
            function.Prototype = prototype;

            // Parse the body.
            Block body = new BlockParser().Parse(context);

            // Set the name of the body block.
            body.SetNameEntry();

            // Assign the body.
            function.Body = body;

            // Return the function.
            return(function);
        }
예제 #4
0
        public IfExpr Parse(ParserContext context)
        {
            // Ensure current token type is keyword if.
            context.Stream.EnsureCurrent(SyntaxAnalysis.TokenType.KeywordIf);

            // Skip if keyword token.
            context.Stream.Skip();

            // Invoke parentheses expression parser to parse condition.
            Expr condition = new ParenthesesExprParser().Parse(context);

            // Invoke block parser to parse if block, which is its action.
            Block action = new BlockParser().Parse(context);

            // Create the alternative action buffer.
            Block otherwise = null;

            // Capture the current token.
            Token token = context.Stream.Current;

            // If expression contains an alternative action.
            if (token.Type == SyntaxAnalysis.TokenType.KeywordElse)
            {
                // Skip else keyword token.
                context.Stream.Skip();

                // Invoke a new block parser to parse the alternative action.
                otherwise = new BlockParser().Parse(context);
            }

            // Create the if expression entity.
            IfExpr ifExpression = new IfExpr(condition, action, otherwise);

            // Return the resulting if expression entity.
            return(ifExpression);
        }