-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expressions as Countable #13218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SomeTroglodyte
wants to merge
11
commits into
yairm210:master
Choose a base branch
from
SomeTroglodyte:Expressions1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Expressions as Countable #13218
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cb6f247
Redefine ICountable.matches, getDeprecationAnnotation is not part of …
SomeTroglodyte c9f27b8
Add a way to mark not-yet-finished Countables
SomeTroglodyte 6d95ea3
Add an empty framework for @AutumnPizazz's Expression engine
SomeTroglodyte eee7dd0
Make "which does not fit parameter type" constant for pattern match r…
SomeTroglodyte adfa8bf
New expression evaluator engine
SomeTroglodyte dd46c40
Fix countable tests: correct expected failures
SomeTroglodyte 9b6161f
All syntax errors MUST indicate position.
yairm210 4ad2cb7
Better positions + documentation
yairm210 b5bf57a
Get rid of functions that modders *should not* be using
yairm210 d52c277
Fix tests given the new changes
yairm210 442600c
Add modder-visible expression parsing errors
yairm210 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
core/src/com/unciv/models/ruleset/unique/expressions/Expressions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.unciv.models.ruleset.unique.expressions | ||
|
||
import com.unciv.models.ruleset.Ruleset | ||
import com.unciv.models.ruleset.unique.ICountable | ||
import com.unciv.models.ruleset.unique.StateForConditionals | ||
import com.unciv.models.ruleset.unique.UniqueType | ||
import kotlin.math.roundToInt | ||
|
||
class Expressions : ICountable { | ||
override fun matches(parameterText: String, ruleset: Ruleset?) = | ||
parse(parameterText, ruleset).severity | ||
|
||
override fun eval(parameterText: String, stateForConditionals: StateForConditionals): Int? { | ||
val node = parse(parameterText, null).node ?: return null | ||
return node.eval(stateForConditionals).roundToInt() | ||
} | ||
|
||
override fun getErrorSeverity(parameterText: String, ruleset: Ruleset) = | ||
when(parse(parameterText, ruleset).severity) { | ||
ICountable.MatchResult.No -> UniqueType.UniqueParameterErrorSeverity.RulesetInvariant | ||
ICountable.MatchResult.Maybe -> UniqueType.UniqueParameterErrorSeverity.PossibleFilteringUnique | ||
ICountable.MatchResult.Yes -> null | ||
} | ||
|
||
private data class ParseResult(val severity: ICountable.MatchResult, val node: Node?) | ||
|
||
companion object { | ||
private val cache: MutableMap<String, ParseResult> = mutableMapOf() | ||
|
||
private fun parse(parameterText: String, ruleset: Ruleset?): ParseResult = cache.getOrPut(parameterText) { | ||
try { | ||
val node = Parser.parse(parameterText, ruleset) | ||
ParseResult(ICountable.MatchResult.Yes, node) | ||
} catch (ex: Parser.ParsingError) { | ||
ParseResult(ex.severity, null) | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
core/src/com/unciv/models/ruleset/unique/expressions/Node.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.unciv.models.ruleset.unique.expressions | ||
|
||
import com.unciv.models.ruleset.unique.Countables | ||
import com.unciv.models.ruleset.unique.StateForConditionals | ||
|
||
internal sealed interface Node { | ||
fun eval(context: StateForConditionals): Double | ||
|
||
// All elements below are not members, they're nested for namespace notation and common visibility | ||
// All toString() are for debugging only | ||
|
||
interface Constant : Node, Tokenizer.Token { | ||
val value: Double | ||
override fun eval(context: StateForConditionals) = value | ||
} | ||
|
||
class NumericConstant(override val value: Double) : Constant { | ||
override fun toString() = value.toString() | ||
} | ||
|
||
class UnaryOperation(private val operator: Operator.Unary, private val operand: Node): Node { | ||
override fun eval(context: StateForConditionals): Double = operator.implementation(operand.eval(context)) | ||
override fun toString() = "($operator $operand)" | ||
} | ||
|
||
class BinaryOperation(private val operator: Operator.Binary, private val left: Node, private val right: Node): Node { | ||
override fun eval(context: StateForConditionals): Double = operator.implementation(left.eval(context), right.eval(context)) | ||
override fun toString() = "($left $operator $right)" | ||
} | ||
|
||
class Countable(private val countable: Countables, private val parameterText: String): Node, Tokenizer.Token { | ||
override fun eval(context: StateForConditionals): Double = countable.eval(parameterText, context)?.toDouble() ?: 0.0 | ||
override fun toString() = "[Countables.${countable.name}: $parameterText]" | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please just call this 'from' without operator overloading.
When you're young it's nice to experiment, it's fine to be operator-curious, but I think we're both past that phase :)