-
Notifications
You must be signed in to change notification settings - Fork 111
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
Rewrite MacroEvaluator to support flatten
, and container-making macros
#1019
Changes from 8 commits
fea45ff
fbef69b
d4a7f6c
474be3f
1bd9701
1e8bc03
92560f5
a9fc6ea
75e012d
14f315d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,15 +231,16 @@ private void readStreamAsExpressionGroup( | |
* @param expressions receives the expressions as they are materialized. | ||
*/ | ||
protected void readValueAsExpression(boolean isImplicitRest, List<Expression.EExpressionBodyExpression> expressions) { | ||
if (isMacroInvocation()) { | ||
if (isImplicitRest && !isContainerAnExpressionGroup()) { | ||
readStreamAsExpressionGroup(expressions); | ||
return; | ||
} else if (isMacroInvocation()) { | ||
collectEExpressionArgs(expressions); // TODO avoid recursion | ||
return; | ||
} | ||
IonType type = reader.encodingType(); | ||
List<SymbolToken> annotations = getAnnotations(); | ||
if (isImplicitRest && !isContainerAnExpressionGroup()) { | ||
readStreamAsExpressionGroup(expressions); | ||
} else if (IonType.isContainer(type)) { | ||
if (IonType.isContainer(type) && !reader.isNullValue()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ Added a check for null here because in the |
||
readContainerValueAsExpression(type, annotations, expressions); | ||
} else { | ||
readScalarValueAsExpression(type, annotations, expressions); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,16 @@ | |
val parentEnvironment: Environment?, | ||
) { | ||
fun createChild(arguments: List<Expression>, argumentIndices: List<Int>) = Environment(arguments, argumentIndices, this) | ||
|
||
override fun toString() = """ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ The only change in this file was adding a custom |
||
|Environment( | ||
| argumentIndices: $argumentIndices, | ||
| argumentExpressions: [${arguments.mapIndexed { index, expression -> "\n| $index. $expression" }.joinToString() } | ||
| ], | ||
| parent: ${parentEnvironment.toString().lines().joinToString("\n| ")}, | ||
|) | ||
""".trimMargin() | ||
|
||
companion object { | ||
@JvmStatic | ||
val EMPTY = Environment(emptyList(), emptyList(), null) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,21 @@ | |
* These expressions are the only ones that may be the output from the macro evaluator. | ||
* All [DataModelExpression]s are also valid to use as [TemplateBodyExpression]s and [EExpressionBodyExpression]s. | ||
*/ | ||
sealed interface DataModelExpression : Expression, EExpressionBodyExpression, TemplateBodyExpression | ||
sealed interface DataModelExpression : Expression, EExpressionBodyExpression, TemplateBodyExpression, ExpansionOutputExpression | ||
|
||
/** Output of a macro expansion (internal to the macro evaluator) */ | ||
sealed interface ExpansionOutputExpressionOrContinue | ||
/** Output of the macro evaluator */ | ||
sealed interface ExpansionOutputExpression : ExpansionOutputExpressionOrContinue | ||
|
||
/** | ||
* Indicates to the macro evaluator that the current expansion did not produce a value this time, but it may | ||
* produce more expressions. The macro evaluator should request another expression from that macro. | ||
*/ | ||
data object ContinueExpansion : ExpansionOutputExpressionOrContinue | ||
|
||
/** Signals the end of an expansion in the macro evaluator. */ | ||
data object EndOfExpansion : ExpansionOutputExpression | ||
Comment on lines
+62
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ It was getting confusing to keep track of what |
||
|
||
/** | ||
* Interface for expressions that are _values_ in the Ion data model. | ||
|
@@ -69,6 +83,8 @@ | |
|
||
/** | ||
* A temporary placeholder that is used only while a macro or e-expression is partially compiled. | ||
* | ||
* TODO: See if we can get rid of this by e.g. using nulls during macro compilation. | ||
*/ | ||
object Placeholder : TemplateBodyExpression, EExpressionBodyExpression | ||
|
||
|
@@ -221,21 +237,25 @@ | |
*/ | ||
data class VariableRef(val signatureIndex: Int) : TemplateBodyExpression | ||
|
||
sealed interface InvokableExpression : HasStartAndEnd, Expression { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ We're able to reduce some code duplication by unifying the code paths for TDL and E-Expression macros. This interface helps to facilitate that unification. |
||
val macro: Macro | ||
} | ||
|
||
/** | ||
* A macro invocation that needs to be expanded. | ||
*/ | ||
data class MacroInvocation( | ||
val macro: Macro, | ||
override val macro: Macro, | ||
override val selfIndex: Int, | ||
override val endExclusive: Int | ||
) : TemplateBodyExpression, HasStartAndEnd | ||
) : TemplateBodyExpression, HasStartAndEnd, InvokableExpression | ||
|
||
/** | ||
* An e-expression that needs to be expanded. | ||
*/ | ||
data class EExpression( | ||
val macro: Macro, | ||
override val macro: Macro, | ||
override val selfIndex: Int, | ||
override val endExclusive: Int | ||
) : EExpressionBodyExpression, HasStartAndEnd | ||
) : EExpressionBodyExpression, HasStartAndEnd, InvokableExpression | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗺️ Had to change the order of some checks in this method so that it would correctly capture rest args when the first of the rest args was a macro invocation.