Skip to content

Commit

Permalink
Adds macro evaluator implementation (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt authored and tgregg committed Dec 13, 2024
1 parent 8ba72ba commit a4cee0d
Show file tree
Hide file tree
Showing 4 changed files with 1,000 additions and 6 deletions.
30 changes: 30 additions & 0 deletions src/main/java/com/amazon/ion/impl/macro/Environment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.macro

/**
* An `Environment` contains variable bindings for a given macro evaluation.
*
* The [arguments] is a list of expressions for the arguments that were passed to the current macro.
* It may also contain other expressions if the current macro invocation is part of a larger evaluation.
*
* The [argumentIndices] is a mapping from parameter index to the start of the corresponding expression in [arguments].
*
* The [parentEnvironment] is an environment to use if any of the expressions in this environment
* contains a variable that references something from an outer macro invocation.
*/
data class Environment private constructor(
// Any variables found here have to be looked up in [parentEnvironment]
val arguments: List<Expression>,
// TODO: Replace with IntArray
val argumentIndices: List<Int>,
val parentEnvironment: Environment?,
) {
fun createChild(arguments: List<Expression>, argumentIndices: List<Int>) = Environment(arguments, argumentIndices, this)
companion object {
@JvmStatic
val EMPTY = Environment(emptyList(), emptyList(), null)
@JvmStatic
fun create(arguments: List<Expression>, argumentIndices: List<Int>) = Environment(arguments, argumentIndices, null)
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/amazon/ion/impl/macro/Macro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.amazon.ion.impl.macro

import com.amazon.ion.impl.*
import com.amazon.ion.impl.macro.Macro.Parameter.Companion.zeroToManyTagged

/**
* A [Macro] is either a [SystemMacro] or a [TemplateMacro].
Expand All @@ -12,6 +13,11 @@ sealed interface Macro {

data class Parameter(val variableName: String, val type: ParameterEncoding, val cardinality: ParameterCardinality) {
override fun toString() = "$type::$variableName${cardinality.sigil}"

companion object {
@JvmStatic
fun zeroToManyTagged(name: String) = Parameter(name, Macro.ParameterEncoding.Tagged, Macro.ParameterCardinality.ZeroOrMore)
}
}

// TODO: See if we can DRY up ParameterEncoding and PrimitiveType
Expand Down Expand Up @@ -101,8 +107,8 @@ data class TemplateMacro(override val signature: List<Macro.Parameter>, val body
* Macros that are built in, rather than being defined by a template.
*/
enum class SystemMacro(override val signature: List<Macro.Parameter>) : Macro {
Values(listOf(Macro.Parameter("values", Macro.ParameterEncoding.Tagged, Macro.ParameterCardinality.ZeroOrMore))),
MakeString(listOf(Macro.Parameter("text", Macro.ParameterEncoding.Tagged, Macro.ParameterCardinality.ZeroOrMore))),
Values(listOf(zeroToManyTagged("values"))),
MakeString(listOf(zeroToManyTagged("text"))),
// TODO: Other system macros
;
}
Loading

0 comments on commit a4cee0d

Please sign in to comment.