Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Dec 17, 2024
1 parent d4a7f6c commit 474be3f
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/amazon/ion/impl/macro/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ sealed interface Expression {
*
* TODO: See if we can get rid of this by e.g. using nulls during macro compilation.
*/
data object Placeholder : TemplateBodyExpression, EExpressionBodyExpression
object Placeholder : TemplateBodyExpression, EExpressionBodyExpression

/**
* A group of expressions that form the argument for one macro parameter.
Expand Down Expand Up @@ -235,7 +235,7 @@ sealed interface Expression {
/**
* A reference to a variable that needs to be expanded.
*/
data class VariableRef @JvmOverloads constructor(val signatureIndex: Int, val parameter: Macro.Parameter? = null) : TemplateBodyExpression
data class VariableRef(val signatureIndex: Int) : TemplateBodyExpression

sealed interface InvokableExpression : HasStartAndEnd, Expression {
val macro: Macro
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal interface DataModelDsl : ValuesDsl {
@ExpressionBuilderDslMarker
internal interface TemplateDsl : ValuesDsl {
fun macro(macro: Macro, arguments: InvocationBody.() -> Unit)
fun variable(signatureIndex: Int, parameter: Macro.Parameter? = null)
fun variable(signatureIndex: Int)
fun list(content: TemplateDsl.() -> Unit)
fun sexp(content: TemplateDsl.() -> Unit)
fun struct(content: Fields.() -> Unit)
Expand Down Expand Up @@ -186,7 +186,7 @@ internal sealed class ExpressionBuilderDsl : ValuesDsl, ValuesDsl.Fields {
override fun list(content: TemplateDsl.() -> Unit) = containerWithAnnotations(content, ::ListValue)
override fun sexp(content: TemplateDsl.() -> Unit) = containerWithAnnotations(content, ::SExpValue)
override fun struct(content: TemplateDsl.Fields.() -> Unit) = containerWithAnnotations(content, ::newStruct)
override fun variable(signatureIndex: Int, parameter: Macro.Parameter?) { expressions.add(VariableRef(signatureIndex, parameter)) }
override fun variable(signatureIndex: Int) { expressions.add(VariableRef(signatureIndex)) }
override fun macro(macro: Macro, arguments: TemplateDsl.InvocationBody.() -> Unit) = container(arguments) { start, end -> MacroInvocation(macro, start, end) }
override fun expressionGroup(content: TemplateDsl.() -> Unit) = container(content, ::ExpressionGroup)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amazon/ion/impl/macro/MacroCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ internal class MacroCompiler(
confirmNoAnnotations("on variable reference '$name'")
val index = signature.indexOfFirst { it.variableName == name }
confirm(index >= 0) { "variable '$name' is not recognized" }
expressions[placeholderIndex] = VariableRef(index, parameter = signature[index])
expressions[placeholderIndex] = VariableRef(index)
confirm(!nextValue()) { "Variable expansion should contain only the variable name." }
stepOutOfContainer()
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amazon/ion/impl/macro/MacroEvaluator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class MacroEvaluator {
* Evaluate the macro expansion until the next [DataModelExpression] can be returned.
* Returns null if at the end of a container or at the end of the expansion.
*/
fun expandNext(): ExpansionOutputExpression? {
fun expandNext(): DataModelExpression? {
currentExpr = null
while (currentExpr == null && !containerStack.isEmpty()) {
val currentContainer = containerStack.peek()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@ class MacroEvaluatorAsIonReader(
private fun queueNext() {
queuedValueExpression = null
while (queuedValueExpression == null) {
val nextCandidate = evaluator.expandNext()
when (nextCandidate) {
when (val nextCandidate = evaluator.expandNext()) {
null -> {
queuedFieldName = null
return
}
is Expression.FieldName -> queuedFieldName = nextCandidate
is Expression.DataModelValue -> queuedValueExpression = nextCandidate
Expression.EndOfExpansion -> {
queuedFieldName = null
return
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/amazon/ion/impl/macro/SystemMacro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ enum class SystemMacro(
sexp {
symbol(IMPORT)
symbol(theModule)
variable(0, exactlyOneTagged("catalog_key"))
variable(0)
// This is equivalent to `(.default (%version) 1)`, but eliminates a layer of indirection.
macro(IfNone) {
variable(1, zeroOrOneTagged("version"))
variable(1)
int(1)
variable(1, zeroOrOneTagged("version"))
variable(1)
}
}
sexp {
Expand Down

0 comments on commit 474be3f

Please sign in to comment.