Skip to content
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

Adds macro evaluator implementation #924

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment here.

// 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
Loading