Skip to content

Commit

Permalink
Adds macro evaluator implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Aug 16, 2024
1 parent b235531 commit 577d8eb
Show file tree
Hide file tree
Showing 4 changed files with 955 additions and 6 deletions.
19 changes: 19 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,19 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.macro

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)

Check warning on line 17 in src/main/java/com/amazon/ion/impl/macro/Environment.kt

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/ion/impl/macro/Environment.kt#L17

Added line #L17 was not covered by tests
}
}
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 577d8eb

Please sign in to comment.