⁠
joe: Start work on unpick
- /*
- * Minecraft Development for IntelliJ
- *
- * https://mcdev.io/
- *
- * Copyright (C) 2026 minecraft-dev
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published
- * by the Free Software Foundation, version 3.0 only.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-
- package com.demonwav.mcdev.platform.minecraft.unpick.eval
-
- import com.demonwav.mcdev.platform.minecraft.unpick.DataType
- import com.intellij.psi.PsiField
- import com.intellij.psi.PsiType
-
- /**
- * The result of evaluating an unpick expression.
- *
- * The [value] is represented as a Kotlin [Byte], [Short], [Char], [Int], [Long], [Float], [Double] or [String] for the
- * corresponding data types, or `null` for a `null` `String` or `Class` constant. A `Class` constant is represented by
- * the [PsiType] it refers to if that can be determined, or otherwise by the [PsiField] it was read from.
- *
- * If [isKnown] is false then only the type of the expression is known, not its value.
- */
- class UnpickValue(val type: DataType, val value: Any?, val isKnown: Boolean = true) {
-
- fun convertTo(target: DataType): UnpickValue {
- if (target == type || !isKnown || !type.isPrimitive || !target.isPrimitive) {
- return UnpickValue(target, value, isKnown)
- }
- return UnpickValue(target, convert(value!!, target))
- }
-
- fun asLong(): Long = when (value) {
- is Char -> value.code.toLong()
- is Number -> value.toLong()
- else -> throw IllegalStateException("Not an integral value: $value")
- }
-
- fun asDouble(): Double = when (value) {
- is Char -> value.code.toDouble()
- is Number -> value.toDouble()
- else -> throw IllegalStateException("Not a numeric value: $value")
- }
-
- /**
- * Converts the value to a string the same way Java string concatenation would.
- */
- fun toJavaString(): String? {
- if (!isKnown) {
- return null
- }
- return when (value) {
- null -> "null"
- is String -> value
- is Char -> value.toString()
- is Number -> value.toString()
- else -> null
- }
- }
-
- /**
- * A human-readable representation of the value, for use in messages.
- */
- fun toDisplayString(): String {
- return when (value) {
- null -> "null"
- is String -> "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\""
- is Char -> "'$value'"
- is Long -> "${value}L"
- is Float -> "${value}f"
- is PsiType -> value.presentableText + ".class"
- is PsiField -> value.containingClass?.name + "." + value.name
- else -> value.toString()
- }
- }
-
- override fun equals(other: Any?): Boolean {
- return other is UnpickValue && other.isKnown && isKnown && other.type == type && other.value == value
- }
-
- override fun hashCode() = 31 * type.hashCode() + value.hashCode()
-
- override fun toString() = "UnpickValue(type=$type, value=$value, isKnown=$isKnown)"
-
- companion object {
- fun unknown(type: DataType) = UnpickValue(type, null, isKnown = false)
-
- private fun convert(value: Any, target: DataType): Any {
- if (value is Float || value is Double) {
- val d = (value as Number).toDouble()
- return when (target) {
- DataType.BYTE -> d.toInt().toByte()
- DataType.SHORT -> d.toInt().toShort()
- DataType.CHAR -> d.toInt().toChar()
- DataType.INT -> d.toInt()
- DataType.LONG -> d.toLong()
- DataType.FLOAT -> if (value is Float) value else d.toFloat()
- DataType.DOUBLE -> d
- else -> value
- }
- }
- val l = when (value) {
- is Char -> value.code.toLong()
- is Number -> value.toLong()
- else -> return value
- }
- return when (target) {
- DataType.BYTE -> l.toInt().toByte()
- DataType.SHORT -> l.toInt().toShort()
- DataType.CHAR -> l.toInt().toChar()
- DataType.INT -> l.toInt()
- DataType.LONG -> l
- DataType.FLOAT -> l.toFloat()
- DataType.DOUBLE -> l.toDouble()
- else -> value
- }
- }
- }
- }