Ejemplo n.º 1
0
        public static LLVMValueRef Literal(TokenType tokenType, string value, PrimitiveType type)
        {
            // Token value is an integer.
            if (tokenType == TokenType.LiteralInteger)
            {
                return(LLVM.ConstInt(type.Emit(), ulong.Parse(value), false));
            }
            // Token value is a decimal.
            else if (tokenType == TokenType.LiteralDecimal)
            {
                return(LLVM.ConstReal(type.Emit(), double.Parse(value)));
            }
            // Token value is a string.
            else if (tokenType == TokenType.LiteralString)
            {
                return(LLVM.ConstString(value, (uint)value.Length, false));
            }
            // Token value is a boolean.
            else if (TokenIdentifier.IsBoolean(tokenType))
            {
                // Create the boolean value.
                uint boolValue;

                // True.
                if (tokenType == TokenType.KeywordTrue)
                {
                    boolValue = 1;
                }
                // False.
                else if (tokenType == TokenType.KeywordFalse)
                {
                    boolValue = 0;
                }
                // Ensure token type is a boolean representative.
                else
                {
                    throw new Exception($"Unexpected boolean value; Expected true or false token type but got {tokenType}");
                }

                // Create and return the resulting constant.
                return(LLVM.ConstInt(type.Emit(), boolValue, false));
            }

            // At this point, type is unsupported.
            throw new Exception("Cannot resolve unsupported type");
        }