com.skillw.pouvoir.util.calculate.CalcOperator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Pouvoir Show documentation
Show all versions of Pouvoir Show documentation
Bukkit Script Engine Plugin.
package com.skillw.pouvoir.util.calculate
import kotlin.math.pow
/**
* @className A
*
* @author Glom
* @date 2023/1/16 3:33 Copyright 2024 Glom.
*/
internal enum class CalcOperator(
private val symbol: Char,
val priority: Int,
val calc: (Double, Double) -> Double = { _, _ -> 0.0 },
) {
PLUS('+', 1, { a, b -> a + b }),
MINUS('-', 1, { a, b -> b - a }),
MULTIPLY('*', 2, { a, b -> a * b }),
DIVIDE('/', 2, { a, b -> b / a }),
REMAIN('%', 2, { a, b -> b % a }),
POWER('^', 3, { a, b -> b.pow(a) }),
LEFT_BRACKET('(', 3),
RIGHT_BRACKET(')', 3);
override fun toString(): String {
return symbol.toString()
}
companion object {
private val symbols = CalcOperator.values().map { it.symbol }.toHashSet()
fun Char.isCalcOperator(): Boolean = this in symbols
fun Char.toCalcOperator(): CalcOperator {
return when (this) {
'+' -> PLUS
'-' -> MINUS
'*' -> MULTIPLY
'/' -> DIVIDE
'%' -> REMAIN
'^' -> POWER
'(' -> LEFT_BRACKET
')' -> RIGHT_BRACKET
else -> error("No such Operator $this")
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy