]> gerrit.simantics Code Review - simantics/platform.git/blob
7b4cae6bdf25ea2af7fceb6f6b3686fb3282de33
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions.list;\r
2 \r
3 import gnu.trove.map.hash.TObjectIntHashMap;\r
4 import gnu.trove.set.hash.THashSet;\r
5 import gnu.trove.set.hash.TIntHashSet;\r
6 \r
7 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;\r
8 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;\r
9 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;\r
10 import org.simantics.scl.compiler.elaboration.expressions.Case;\r
11 import org.simantics.scl.compiler.elaboration.expressions.EVariable;\r
12 import org.simantics.scl.compiler.elaboration.expressions.Expression;\r
13 import org.simantics.scl.compiler.elaboration.expressions.Variable;\r
14 import org.simantics.scl.compiler.elaboration.expressions.VariableProcedure;\r
15 import org.simantics.scl.compiler.errors.Locations;\r
16 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;\r
17 import org.simantics.scl.compiler.types.TMetaVar;\r
18 import org.simantics.scl.compiler.types.Type;\r
19 import org.simantics.scl.compiler.types.Types;\r
20 import org.simantics.scl.compiler.types.kinds.Kinds;\r
21 \r
22 public class ListGenerator extends ListQualifier {\r
23     public Expression pattern;\r
24     public Expression value;\r
25     \r
26     public ListGenerator(Expression pattern, Expression value) {\r
27         this.pattern = pattern;\r
28         this.value = value;\r
29     }\r
30     \r
31     @Override\r
32     public void checkType(TypingContext context) {\r
33         TMetaVar componentType = Types.metaVar(Kinds.STAR);\r
34         value.checkType(context, Types.apply(Types.LIST, componentType));\r
35         pattern.checkTypeAsPattern(context, componentType);\r
36     }\r
37 \r
38     @Override\r
39     public void collectRefs(TObjectIntHashMap<Object> allRefs,\r
40             TIntHashSet refs) {\r
41         value.collectRefs(allRefs, refs);\r
42     }\r
43 \r
44     @Override\r
45     public void collectVars(TObjectIntHashMap<Variable> allVars,\r
46             TIntHashSet vars) {\r
47         value.collectVars(allVars, vars);\r
48     }\r
49 \r
50     @Override\r
51     public void collectFreeVariables(THashSet<Variable> vars) {\r
52         value.collectFreeVariables(vars);\r
53         pattern.collectFreeVariables(vars);\r
54     }\r
55 \r
56     @Override\r
57     public CompiledQualifier compile(SimplificationContext context) {\r
58         if(pattern instanceof EVariable)\r
59             return new CompiledQualifier(value, pattern);\r
60         else {\r
61             THashSet<Variable> variables = pattern.getFreeVariables();\r
62             Variable[] variableArray = variables.toArray(new Variable[variables.size()]);\r
63             Expression[] variableExps = new Expression[variableArray.length];\r
64             for(int i=0;i<variableArray.length;++i)\r
65                 variableExps[i] = new EVariable(variableArray[i]);\r
66             Expression newPattern = context.tuple(variableExps);\r
67             \r
68             EVariable blank = context.blank();\r
69             blank.getVariable().setType(pattern.getType());\r
70             \r
71             return new CompiledQualifier(\r
72                     context.concatMap(\r
73                             context.lambda(\r
74                                     new Case(pattern, context.singletonList(newPattern.copy())),\r
75                                     new Case(blank, context.emptyList(newPattern.getType()))\r
76                                     ), \r
77                             value\r
78                             ), \r
79                     newPattern);\r
80         }\r
81     }\r
82 \r
83     @Override\r
84     public void resolve(TranslationContext context) {\r
85         value = value.resolve(context);\r
86         pattern = pattern.resolveAsPattern(context);\r
87     }\r
88     \r
89     @Override\r
90     public void decorate(ExpressionDecorator decorator) {\r
91         value = value.decorate(decorator);\r
92         pattern = pattern.decorate(decorator);\r
93     }\r
94     \r
95     @Override\r
96     public void collectEffects(THashSet<Type> effects) {\r
97         pattern.collectEffects(effects);\r
98         value.collectEffects(effects);\r
99     }\r
100     \r
101     @Override\r
102     public void setLocationDeep(long loc) {\r
103         if(location == Locations.NO_LOCATION) {\r
104             location = loc;\r
105             pattern.setLocationDeep(loc);\r
106             value.setLocationDeep(loc);\r
107         }\r
108     }\r
109     \r
110     @Override\r
111     public void accept(ListQualifierVisitor visitor) {\r
112         visitor.visit(this);\r
113     }\r
114     \r
115     @Override\r
116     public void forVariables(VariableProcedure procedure) {\r
117         value.forVariables(procedure);\r
118     }\r
119     \r
120     @Override\r
121     public ListQualifier accept(ListQualifierTransformer transformer) {\r
122         return transformer.transform(this);\r
123     }\r
124 }\r