Skip to content

Commit

Permalink
Adds data model for macro definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Nov 22, 2023
1 parent 02802d1 commit b950171
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/amazon/ion/impl/macro/Macro.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.amazon.ion.impl.macro

import java.math.BigDecimal

/**
* Marker interface for Macros
*/
sealed interface Macro

/**
* Represents a template macro. A template macro is defined by a name, a signature, and a list of template expressions.
*/
data class TemplateMacro(val name: String, val f: BigDecimal, val signature: MacroSignature, val body: List<TemplateExpression>): Macro

/**
* Macros that are built in, rather than being defined by a template.
*/
enum class SystemMacro: Macro {
Stream, // A stream is technically not a macro, but we can implement it as a macro that is the identity function.
Annotate,
MakeString,
// TODO: Other system macros
}
9 changes: 9 additions & 0 deletions src/main/java/com/amazon/ion/impl/macro/MacroRef.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.amazon.ion.impl.macro

/**
* A reference to a particular macro, either by name or by template id.
*/
sealed interface MacroRef {
@JvmInline value class ByName(val name: String): MacroRef
@JvmInline value class ById(val id: Long): MacroRef
}
16 changes: 16 additions & 0 deletions src/main/java/com/amazon/ion/impl/macro/MacroSignature.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.amazon.ion.impl.macro

/**
* Represents the signature of a macro.
*/
@JvmInline value class MacroSignature(val parameters: List<Parameter>) {

data class Parameter(val variableName: String, val type: Encoding, val grouped: Boolean)

enum class Encoding(val ionTextName: String?) {
Tagged(null),
Any("any"),
Int8("int8"),
// TODO: List all of the possible tagless encodings
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/amazon/ion/impl/macro/TemplateExpression.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.amazon.ion.impl.macro

import com.amazon.ion.SymbolToken
import com.amazon.ion.impl.macro.ionelement.api.IonElement

/**
* Represents an expression in the body of a template.
*/
sealed interface TemplateExpression {
// TODO: Special Forms (if_void, for, ...)?

/**
* A value that is taken literally. I.e. there can be no variable substitutions or macros inside here. Usually,
* these will just be scalars, but it is possible for it to be a container type.
*
* We cannot use [`IonValue`](com.amazon.ion.IonValue) for this because `IonValue` requires references to parent
* containers and to an IonSystem which makes it impractical for reading and writing macros definitions.
* For now, we are using [IonElement], but that is not a good permanent solution. It is copy/pasted into this repo
* to avoid a circular dependency, and the dependencies are shaded, so it is not suitable for a public API.
*/
@JvmInline value class LiteralValue(val value: IonElement): TemplateExpression

/**
* An Ion List that could contain variables or macro invocations.
*/
data class ListValue(val annotations: List<SymbolToken>, val expressionRange: IntRange): TemplateExpression

/**
* An Ion SExp that could contain variables or macro invocations.
*/
data class SExpValue(val annotations: List<SymbolToken>, val expressionRange: IntRange): TemplateExpression

/**
* An Ion Struct that could contain variables or macro invocations.
*/
data class StructValue(val annotations: List<SymbolToken>, val expressionRange: IntRange, val templateStructIndex: Map<SymbolToken, IntArray>)

/**
* A reference to a variable that needs to be expanded.
*/
@JvmInline value class Variable(val signatureIndex: Int): TemplateExpression

/**
* A macro invocation that needs to be expanded.
*/
data class MacroInvocation(val macro: MacroRef, val argumentExpressionsRange: IntRange): TemplateExpression
}

0 comments on commit b950171

Please sign in to comment.