public class SugarExpansionVisitor extends AbstractGoloIrVisitor
Modify the IR to transform some nodes, in order to expand syntactic sugar, such as case and match statements, foreach loops, list comprehension, and so on.
Constructor and Description |
---|
SugarExpansionVisitor() |
Modifier and Type | Method and Description |
---|---|
void |
visitBinaryOperation(BinaryOperation op) |
void |
visitBlock(Block block)
Generate the local function corresponding to this closure.
|
void |
visitCaseStatement(CaseStatement caseStatement)
Case expansion.
|
void |
visitClosureReference(ClosureReference closure) |
void |
visitCollectionComprehension(CollectionComprehension collection)
Collection comprehension expansion.
|
void |
visitCollectionLiteral(CollectionLiteral collection)
Literal expansion.
|
void |
visitConstantStatement(ConstantStatement constantStatement)
Converts a literal function reference into a call to
Predefined.fun . |
void |
visitDestructuringAssignment(DestructuringAssignment assignment)
Destructuring assignment expansion.
|
void |
visitForEachLoopStatement(ForEachLoopStatement foreachStatement)
ForEach expansion.
|
void |
visitFunction(GoloFunction function) |
void |
visitFunctionInvocation(FunctionInvocation fun) |
void |
visitMatchExpression(MatchExpression matchExpression)
Match expansion.
|
void |
visitMethodInvocation(MethodInvocation invoke) |
void |
visitModule(GoloModule module) |
void |
visitReferenceLookup(ReferenceLookup ref) |
void |
visitReturnStatement(ReturnStatement ret)
Special returned values.
|
void |
visitStruct(Struct struct)
Add struct factories if they don't already exist.
|
void |
visitTryCatchFinally(TryCatchFinally tryCatchFinally) |
void |
visitUnaryOperation(UnaryOperation op) |
visitAssignmentStatement, visitAugmentation, visitConditionalBranching, visitDecorator, visitLocalReference, visitLoopBreakFlowStatement, visitLoopStatement, visitMacroInvocation, visitMember, visitModuleImport, visitNamedArgument, visitNamedAugmentation, visitNoop, visitThrowStatement, visitToplevelElements, visitUnion, visitUnionValue, visitWhenClause
public SugarExpansionVisitor()
public void visitModule(GoloModule module)
visitModule
in interface GoloIrVisitor
visitModule
in class AbstractGoloIrVisitor
public void visitBlock(Block block)
visitBlock
in interface GoloIrVisitor
visitBlock
in class AbstractGoloIrVisitor
public void visitClosureReference(ClosureReference closure)
visitClosureReference
in interface GoloIrVisitor
visitClosureReference
in class AbstractGoloIrVisitor
public void visitFunction(GoloFunction function)
visitFunction
in interface GoloIrVisitor
visitFunction
in class AbstractGoloIrVisitor
public void visitCaseStatement(CaseStatement caseStatement)
Convert a CaseStatement
node into a imbrication of ConditionalBranching
, and
replace it in the parent node.
For instance:
case {
when cond1 { block1 }
when cond2 { block2 }
otherwise { block3 }
}
is converted into the equivalent of
if cond1 { block1 }
else if cond2 { block2 }
else { block3 }
visitCaseStatement
in interface GoloIrVisitor
visitCaseStatement
in class AbstractGoloIrVisitor
public void visitMatchExpression(MatchExpression matchExpression)
Convert a MatchExpression
node into a block containing a CaseStatement
.
For instance
match {
when cond1 then val1
when cond2 then val2
otherwise val3
}
is converted into the equivalent of
tmp = null
case {
when cond1 { tmp = val1 }
when cond2 { tmp = val2 }
otherwise { tmp = val3 }
}
tmp
visitMatchExpression
in interface GoloIrVisitor
visitMatchExpression
in class AbstractGoloIrVisitor
public void visitReturnStatement(ReturnStatement ret)
Deal with special return cases:
MatchExpression
, convert it to a
CaseStatement
containing ReturnStatement
instead of assignments.
For instance
return match { when cond1 then val1 when cond2 then val2 otherwise val3 }is converted into the equivalent of
case { when cond1 { return val1 } when cond2 { return val2 } otherwise { return val3 } }This will allows for better optimisations (e.g. TCE).
visitReturnStatement
in interface GoloIrVisitor
visitReturnStatement
in class AbstractGoloIrVisitor
public void visitCollectionLiteral(CollectionLiteral collection)
Converts a collection literal into a call to gololang.Predefined.<type>
.
visitCollectionLiteral
in interface GoloIrVisitor
visitCollectionLiteral
in class AbstractGoloIrVisitor
public void visitConstantStatement(ConstantStatement constantStatement)
Predefined.fun
.visitConstantStatement
in interface GoloIrVisitor
visitConstantStatement
in class AbstractGoloIrVisitor
public void visitCollectionComprehension(CollectionComprehension collection)
Convert a list comprehension expression into a block initialising a collection from nested loops defined in the comprehension. For instance
list[ f(x, y) foreach x in col1 foreach y in col2 ]
is converted to the equivalent of
let collection = list[]
foreach x in col1 {
foreach y in col2 {
collection: add(f(x, y))
}
}
visitCollectionComprehension
in interface GoloIrVisitor
visitCollectionComprehension
in class AbstractGoloIrVisitor
public void visitForEachLoopStatement(ForEachLoopStatement foreachStatement)
Convert a ForEachLoopStatement
into a loop using the associated iterator.
For instance:
foreach x in expr {
block
}
is converted to:
for (__$$_iterator_0 = expr: iterator(), __$$_iterator_0: hasNext(),) {
let x = __$$_iterator_0: next()
block
}
visitForEachLoopStatement
in interface GoloIrVisitor
visitForEachLoopStatement
in class AbstractGoloIrVisitor
public void visitDestructuringAssignment(DestructuringAssignment assignment)
visitDestructuringAssignment
in interface GoloIrVisitor
visitDestructuringAssignment
in class AbstractGoloIrVisitor
public void visitStruct(Struct struct)
visitStruct
in interface GoloIrVisitor
visitStruct
in class AbstractGoloIrVisitor
public void visitBinaryOperation(BinaryOperation op)
visitBinaryOperation
in interface GoloIrVisitor
visitBinaryOperation
in class AbstractGoloIrVisitor
public void visitUnaryOperation(UnaryOperation op)
visitUnaryOperation
in interface GoloIrVisitor
visitUnaryOperation
in class AbstractGoloIrVisitor
public void visitReferenceLookup(ReferenceLookup ref)
visitReferenceLookup
in interface GoloIrVisitor
visitReferenceLookup
in class AbstractGoloIrVisitor
public void visitFunctionInvocation(FunctionInvocation fun)
visitFunctionInvocation
in interface GoloIrVisitor
visitFunctionInvocation
in class AbstractGoloIrVisitor
public void visitMethodInvocation(MethodInvocation invoke)
visitMethodInvocation
in interface GoloIrVisitor
visitMethodInvocation
in class AbstractGoloIrVisitor
public void visitTryCatchFinally(TryCatchFinally tryCatchFinally)
visitTryCatchFinally
in interface GoloIrVisitor
visitTryCatchFinally
in class AbstractGoloIrVisitor