-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds data model for macro definitions
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
src/main/java/com/amazon/ion/impl/macro/TemplateExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |