Package net.hydromatic.morel.ast
Class FromBuilder
- java.lang.Object
-
- net.hydromatic.morel.ast.FromBuilder
-
public class FromBuilder extends Object
Builds aCore.From.Simplifies the following patterns:
- Converts "from v in list" to "list"
(only works in
buildSimplify(), notbuild()); - Removes "where true" steps;
- Removes empty "order" steps;
- Removes trivial
yield, e.g. "from v in list where condition yield v" becomes "from v in list where condition"; - Inlines
fromexpressions, e.g. "from v in (from w in list)" becomes "from w in list yield {v = w}".
- Converts "from v in list" to "list"
(only works in
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classFromBuilder.StepHandlerCalls the method to re-register a step.private static classFromBuilder.TupleTypeCategory of expression passed to "yield".
-
Field Summary
Fields Modifier and Type Field Description private List<Binding>bindingsprivate @Nullable Environmentenvprivate intremoveIfLastIndexIf non-negative, flags that particular step should be removed if it is the last step.private intremoveIfNotLastIndexIf non-negative, flags that particular step should be removed if it is not the last step.private List<Core.FromStep>stepsprivate TypeSystemtypeSystem
-
Constructor Summary
Constructors Constructor Description FromBuilder(TypeSystem typeSystem, @Nullable Environment env)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FromBuilderaddAll(Iterable<? extends Core.FromStep> steps)private FromBuilderaddStep(Core.FromStep step)List<Binding>bindings()Returns the bindings available after the most recent step.Core.Frombuild()private Core.Expbuild(boolean simplify)Core.ExpbuildSimplify()Asbuild(boolean), but also simplifies "from x in list" to "list".FromBuildergroup(SortedMap<Core.IdPat,Core.Exp> groupExps, SortedMap<Core.IdPat,Core.Aggregate> aggregates)private booleanisRename(Core.Tuple tuple)Returns whether tuple is something like "{i = j, j = x}".private booleanisTrivial(Core.Tuple tuple, @Nullable List<Binding> bindings2)Returns whether tuple is something like "{i = i, j = j}".FromBuilderorder(Iterable<Core.OrderItem> orderItems)FromBuilderscan(Core.Pat pat, Core.Exp exp)FromBuilderscan(Core.Pat pat, Core.Exp exp, Core.Exp condition)private FromBuilder.TupleTypetupleType(Core.Tuple tuple, @Nullable List<Binding> bindings2)Returns whether tuple is something like "{i = i, j = j}".FromBuilderwhere(Core.Exp condition)FromBuilderyield_(boolean uselessIfLast, @Nullable List<Binding> bindings2, Core.Exp exp)Creates a "yield" step.FromBuilderyield_(boolean uselessIfLast, Core.Exp exp)FromBuilderyield_(Core.Exp exp)
-
-
-
Field Detail
-
typeSystem
private final TypeSystem typeSystem
-
env
private final @Nullable Environment env
-
steps
private final List<Core.FromStep> steps
-
removeIfNotLastIndex
private int removeIfNotLastIndex
If non-negative, flags that particular step should be removed if it is not the last step. (For example, "yield {i = i}", which changes the result shape if the last step but is otherwise a no-op.)
-
removeIfLastIndex
private int removeIfLastIndex
If non-negative, flags that particular step should be removed if it is the last step. (For example, we flatten "from p in (from q in list)", to "from q in list yield {p = q}" but we want to remove "yield {p = q}" if it turns out to be the last step.)
-
-
Constructor Detail
-
FromBuilder
FromBuilder(TypeSystem typeSystem, @Nullable Environment env)
-
-
Method Detail
-
addStep
private FromBuilder addStep(Core.FromStep step)
-
scan
public FromBuilder scan(Core.Pat pat, Core.Exp exp)
-
scan
public FromBuilder scan(Core.Pat pat, Core.Exp exp, Core.Exp condition)
-
addAll
public FromBuilder addAll(Iterable<? extends Core.FromStep> steps)
-
where
public FromBuilder where(Core.Exp condition)
-
group
public FromBuilder group(SortedMap<Core.IdPat,Core.Exp> groupExps, SortedMap<Core.IdPat,Core.Aggregate> aggregates)
-
order
public FromBuilder order(Iterable<Core.OrderItem> orderItems)
-
yield_
public FromBuilder yield_(Core.Exp exp)
-
yield_
public FromBuilder yield_(boolean uselessIfLast, Core.Exp exp)
-
yield_
public FromBuilder yield_(boolean uselessIfLast, @Nullable List<Binding> bindings2, Core.Exp exp)
Creates a "yield" step.When copying, the
bindings2parameter is theCore.FromStep.bindingsvalue of the current Yield, so that we don't generate new variables (with different ordinals). Later steps are relying on the variables remaining the same. For example, infrom ... yield {a = b} where a > 5the
aina > 5referencesIdPat('a', 0)and we don't want yield to generate anIdPat('a', 1).- Parameters:
uselessIfLast- Whether this Yield will be useless if it is the last step. The expression{x = y}is an example of thisbindings2- Desired bindings, or nullexp- Expression to yield- Returns:
- This FromBuilder, with a Yield added to the list of steps
-
isTrivial
private boolean isTrivial(Core.Tuple tuple, @Nullable List<Binding> bindings2)
Returns whether tuple is something like "{i = i, j = j}".
-
tupleType
private FromBuilder.TupleType tupleType(Core.Tuple tuple, @Nullable List<Binding> bindings2)
Returns whether tuple is something like "{i = i, j = j}".
-
isRename
private boolean isRename(Core.Tuple tuple)
Returns whether tuple is something like "{i = j, j = x}".
-
build
private Core.Exp build(boolean simplify)
-
build
public Core.From build()
-
buildSimplify
public Core.Exp buildSimplify()
Asbuild(boolean), but also simplifies "from x in list" to "list".
-
-