Skip to content

Commit d28a67f

Browse files
authored
Merge pull request #27 from Traben-0/main
2 parents 3dec89f + 806cb96 commit d28a67f

1 file changed

Lines changed: 29 additions & 18 deletions

File tree

src/main/kotlin/org/polyfrost/intelliprocessor/utils/PreprocessorConditions.kt

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,33 +120,44 @@ class PreprocessorConditions private constructor(
120120
return null
121121
}
122122

123-
private val BOOLEAN_SPLITTER = Regex("""\s*(&&|\|\||[^&|]+)\s*""")
124-
125123
private fun evaluateBooleanConditions(conditions: String, currentVersion: PreprocessorVersion): Boolean? {
126124
// Multiple conditions separated by && or ||
127125
if (conditions.contains("||") || conditions.contains("&&")) {
128-
val tokens = BOOLEAN_SPLITTER.findAll(conditions).map { it.groupValues[1] }.toList()
129-
var result = evaluateCondition(tokens[0], currentVersion)
130-
?: return logAndNull("Could not evaluate condition: ${tokens[0]}")
131-
var i = 1
132-
while (i < tokens.size) {
133-
val op = tokens[i]
134-
val next = evaluateCondition(tokens[i + 1], currentVersion)
135-
?: return logAndNull("Could not evaluate condition: ${tokens[i + 1]}")
136-
result = when (op) {
137-
"&&" -> result && next
138-
"||" -> result || next
139-
else -> return logAndNull("op wasn't && or || in: $conditions") // Shouldn't occur
140-
}
141-
i += 2
142-
}
143-
return result
126+
return evaluateOrConditions(conditions, currentVersion)
144127
}
145128

146129
// Single condition
147130
return evaluateCondition(conditions, currentVersion)
148131
}
149132

133+
private fun evaluateOrConditions(conditionRaw: String, currentVersion: PreprocessorVersion): Boolean? {
134+
val conditions = conditionRaw.split("||")
135+
if (conditions.size == 1) return evaluateAndConditions(conditionRaw, currentVersion)
136+
137+
var i = 0
138+
while (i < conditions.size) {
139+
val or = evaluateAndConditions(conditions[i], currentVersion)
140+
?: return logAndNull("Could not evaluate OR sub condition: ${conditions[i]}")
141+
if (or) return true
142+
i++
143+
}
144+
return false
145+
}
146+
147+
private fun evaluateAndConditions(conditionRaw: String, currentVersion: PreprocessorVersion): Boolean? {
148+
val conditions = conditionRaw.split("&&")
149+
if (conditions.size == 1) return evaluateCondition(conditionRaw, currentVersion)
150+
151+
var i = 0
152+
while (i < conditions.size) {
153+
val and = evaluateCondition(conditions[i], currentVersion)
154+
?: return logAndNull("Could not evaluate AND sub condition: ${conditions[i]}")
155+
if (!and) return false
156+
i++
157+
}
158+
return true
159+
}
160+
150161
private fun evaluateCondition(conditionRaw: String, currentVersion: PreprocessorVersion): Boolean? {
151162
val condition = conditionRaw.trim()
152163
if (!condition.startsWith("MC")) {

0 commit comments

Comments
 (0)