Skip to content

Commit

Permalink
feature: Add Expression#getVariableNames
Browse files Browse the repository at this point in the history
  • Loading branch information
kezz committed Aug 9, 2024
1 parent 24fb2a3 commit a2c5a17
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/kotlin/com/noxcrew/smp/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ public class Expression internal constructor(
*/
internal val rpnSortedTokens: List<Token>,
) {

/**
* Returns a set of the names of all variables in this expression.
*
* Note that if this expression has already been resolved, this set will be empty.
*
* @return the variable names
* @since 1.0
*/
public fun getVariableNames(): Set<String> =
rpnSortedTokens.mapNotNullTo(mutableSetOf()) { token ->
if (token is Value.Variable) {
token.name
} else {
null
}
}

/**
* Resolves all variables in this expression, returning a new expression that can be
* computed without exceptions.
Expand Down

0 comments on commit a2c5a17

Please sign in to comment.