-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move classes in Macro.kt to their own files
- Loading branch information
Showing
6 changed files
with
122 additions
and
82 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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/amazon/ion/impl/macro/ParameterFactory.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,17 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.ion.impl.macro | ||
|
||
import com.amazon.ion.impl.macro.Macro.* | ||
|
||
/** | ||
* Convenience functions for concisely creating [Macro.Parameter]s. | ||
*/ | ||
object ParameterFactory { | ||
@JvmStatic | ||
fun zeroToManyTagged(name: String) = Parameter(name, ParameterEncoding.Tagged, ParameterCardinality.ZeroOrMore) | ||
@JvmStatic | ||
fun exactlyOneTagged(name: String) = Parameter(name, ParameterEncoding.Tagged, ParameterCardinality.ExactlyOne) | ||
@JvmStatic | ||
fun exactlyOneFlexInt(name: String) = Parameter(name, ParameterEncoding.CompactInt, ParameterCardinality.ExactlyOne) | ||
} |
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,57 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.ion.impl.macro | ||
|
||
import com.amazon.ion.impl.macro.ParameterFactory.exactlyOneFlexInt | ||
import com.amazon.ion.impl.macro.ParameterFactory.exactlyOneTagged | ||
import com.amazon.ion.impl.macro.ParameterFactory.zeroToManyTagged | ||
|
||
/** | ||
* Macros that are built in, rather than being defined by a template. | ||
*/ | ||
enum class SystemMacro(val id: Int, val macroName: String, override val signature: List<Macro.Parameter>) : Macro { | ||
None(0, "none", emptyList()), | ||
Values(1, "values", listOf(zeroToManyTagged("values"))), | ||
Annotate(2, "annotate", listOf(zeroToManyTagged("ann"), exactlyOneTagged("value"))), | ||
MakeString(3, "make_string", listOf(zeroToManyTagged("text"))), | ||
MakeSymbol(4, "make_symbol", listOf(zeroToManyTagged("text"))), | ||
MakeDecimal(6, "make_decimal", listOf(exactlyOneFlexInt("coefficient"), exactlyOneFlexInt("exponent"))), | ||
|
||
// TODO: Other system macros | ||
|
||
// Technically not system macros, but special forms. However, it's easier to model them as if they are macros in TDL. | ||
// We give them an ID of -1 to distinguish that they are not addressable outside TDL. | ||
IfNone(-1, "IfNone", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
IfSome(-1, "IfSome", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
IfSingle(-1, "IfSingle", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
IfMulti(-1, "IfMulti", listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))), | ||
; | ||
|
||
override val dependencies: List<Macro> | ||
get() = emptyList() | ||
|
||
companion object { | ||
|
||
private val MACROS_BY_NAME: Map<String, SystemMacro> = SystemMacro.entries.associateBy { it.macroName } | ||
|
||
// TODO: Once all of the macros are implemented, replace this with an array as in SystemSymbols_1_1 | ||
private val MACROS_BY_ID: Map<Int, SystemMacro> = SystemMacro.entries | ||
.filterNot { it.id < 0 } | ||
.associateBy { it.id } | ||
|
||
@JvmStatic | ||
fun size() = MACROS_BY_ID.size | ||
|
||
/** Gets a [SystemMacro] by its address in the system table */ | ||
@JvmStatic | ||
operator fun get(id: Int): SystemMacro? = MACROS_BY_ID[id] | ||
|
||
/** Gets, by name, a [SystemMacro] with an address in the system table (i.e. that can be invoked as E-Expressions) */ | ||
@JvmStatic | ||
operator fun get(name: String): SystemMacro? = MACROS_BY_NAME[name]?.takeUnless { it.id < 0 } | ||
|
||
/** Gets a [SystemMacro] by name, including those which are not in the system table (i.e. special forms) */ | ||
@JvmStatic | ||
fun getMacroOrSpecialForm(name: String): SystemMacro? = MACROS_BY_NAME[name] | ||
} | ||
} |
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,32 @@ | ||
package com.amazon.ion.impl.macro | ||
|
||
/** | ||
* Represents a template macro. A template macro is defined by a signature, and a list of template expressions. | ||
* A template macro only gains a name and/or ID when it is added to a macro table. | ||
*/ | ||
data class TemplateMacro(override val signature: List<Macro.Parameter>, val body: List<Expression.TemplateBodyExpression>) : | ||
Macro { | ||
// TODO: Consider rewriting the body of the macro if we discover that there are any macros invoked using only | ||
// constants as arguments—either at compile time or lazily. | ||
// For example, the body of: (macro foo (x) (values (make_string "foo" "bar") x)) | ||
// could be rewritten as: (values "foobar" x) | ||
|
||
private val cachedHashCode by lazy { signature.hashCode() * 31 + body.hashCode() } | ||
override fun hashCode(): Int = cachedHashCode | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is TemplateMacro) return false | ||
// Check the hashCode as a quick check before we dive into the actual data. | ||
if (cachedHashCode != other.cachedHashCode) return false | ||
if (signature != other.signature) return false | ||
if (body != other.body) return false | ||
return true | ||
} | ||
|
||
override val dependencies: List<Macro> by lazy { | ||
body.filterIsInstance<Expression.MacroInvocation>() | ||
.map { it.macro } | ||
.distinct() | ||
} | ||
} |
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
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