示例#1
0
文件: Function.cs 项目: BowsiePup/Ion
        /// <summary>
        /// Creates a prototype for this function, overriding
        /// any existing prototype property value. Creates arguments.
        /// </summary>
        public Prototype CreatePrototype()
        {
            // Default the return type to void.
            ITypeEmitter returnType = PrimitiveTypeFactory.Void();

            // Create a new prototype instance.
            this.Prototype = new Prototype(NameRegister.GetAnonymous(), null, returnType);

            // Create formal arguments after assigning prototype to avoid infinite loop.
            FormalArgs args = this.CreateArgs();

            // Assign the formal arguments.
            this.Prototype.Args = args;

            // Return the prototype.
            return(this.Prototype);
        }
示例#2
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);
        }