コード例 #1
0
ファイル: BooleanExpr.cs プロジェクト: BowsiePup/Ion
        public override LLVMValueRef Emit(PipeContext <LLVMBuilderRef> context)
        {
            // Create a new primitive boolean type instance.
            PrimitiveType type = PrimitiveTypeFactory.Boolean();

            // Resolve the value.
            LLVMValueRef valueRef = Resolvers.Literal(this.tokenType, this.value, type);

            // Return the emitted value.
            return(valueRef);
        }
コード例 #2
0
ファイル: PrimaryExprParser.cs プロジェクト: BowsiePup/Ion
        public Expr Parse(ParserContext context)
        {
            // Capture current token type.
            TokenType currentTokenType = context.Stream.Current.Type;

            // Pipe operation.
            if (currentTokenType == TokenType.SymbolColon)
            {
                return(new PipeParser().Parse(context));
            }
            // Numeric expression.
            else if (TokenIdentifier.IsNumeric(currentTokenType))
            {
                return(new NumericExprParser().Parse(context));
            }
            // Identifier expression.
            else if (currentTokenType == TokenType.Identifier)
            {
                return(new IdentifierExprParser().Parse(context));
            }
            // Parentheses expression.
            else if (currentTokenType == TokenType.SymbolParenthesesL)
            {
                return(new ParenthesesExprParser().Parse(context));
            }
            // String expression.
            else if (currentTokenType == TokenType.LiteralString)
            {
                return(new StringExprParser().Parse(context));
            }
            // Boolean expression.
            else if (TokenIdentifier.IsBoolean(currentTokenType))
            {
                return(new BooleanExprParser().Parse(context));
            }
            // Struct expression.
            else if (currentTokenType == TokenType.KeywordNew)
            {
                return(new StructExprParser().Parse(context));
            }
            // Array expression.
            else if (currentTokenType == TokenType.SymbolBracketL)
            {
                // TODO: Type is hard-coded for debugging purposes, not yet supported auto-type (might need infering?).
                return(new ArrayExprParser(PrimitiveTypeFactory.Int32()).Parse(context));
            }

            // At this point, return null.
            return(null);
        }
コード例 #3
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);
        }
コード例 #4
0
ファイル: IfExpr.cs プロジェクト: BowsiePup/Ion
        // TODO: Action and alternative blocks not being handled, for debugging purposes.
        public override LLVMValueRef Emit(PipeContext <LLVMBuilderRef> context)
        {
            // Emit the condition value.
            LLVMValueRef conditionValue = this.Condition.Emit(context);

            // Create a zero-value double for the boolean comparison.
            LLVMValueRef zero = LLVM.ConstReal(PrimitiveTypeFactory.Double().Emit(), 0.0);

            // TODO: Hard-coded name.
            // Build the comparison, condition will be convered to a boolean for a 'ONE' (non-equal) comparison.
            LLVMValueRef comparison = LLVM.BuildFCmp(context.Target, LLVMRealPredicate.LLVMRealONE, conditionValue, zero, "ifcond");

            // Retrieve the parent function from the builder.
            LLVMValueRef function = LLVM.GetBasicBlockParent(LLVM.GetInsertBlock(context.Target));

            LLVMBasicBlockRef action = LLVM.AppendBasicBlock(function, "then");

            // TODO: Debugging, Ret void for action.
            LLVM.PositionBuilderAtEnd(context.Target, action);
            LLVM.BuildRetVoid(context.Target);

            LLVMBasicBlockRef otherwise = LLVM.AppendBasicBlock(function, "else");

            // TODO: Debugging, ret void for otherwise.
            LLVM.PositionBuilderAtEnd(context.Target, otherwise);
            LLVM.BuildRetVoid(context.Target);

            LLVMBasicBlockRef merge = LLVM.AppendBasicBlock(function, "ifcont");

            // TODO: Debugging, ret void for merge.
            LLVM.PositionBuilderAtEnd(context.Target, merge);
            LLVM.BuildRetVoid(context.Target);

            // Build the if construct.
            LLVMValueRef @if = LLVM.BuildCondBr(context.Target, comparison, action, otherwise);

            // TODO: Complete implementation, based off: https://github.com/microsoft/LLVMSharp/blob/master/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenVisitor.cs#L214
            // ...

            // TODO: Debugging, not complete.
            LLVM.PositionBuilderAtEnd(context.Target, action);

            // Return the resulting LLVM value reference for further use if applicable.
            return(@if);
        }
コード例 #5
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);
        }