]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ExpressionAugmentation.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / elaboration / constraints / ExpressionAugmentation.java
1 package org.simantics.scl.compiler.internal.elaboration.constraints;
2
3 import java.util.ArrayList;
4
5 import org.simantics.scl.compiler.elaboration.expressions.ELambda;
6 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLambda;
7 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLet;
8 import org.simantics.scl.compiler.elaboration.expressions.Expression;
9 import org.simantics.scl.compiler.types.Type;
10 import org.simantics.scl.compiler.types.Types;
11
12 public class ExpressionAugmentation {
13
14     private static Expression augmentSolvedAux(ArrayList<Constraint> solved, Expression expression) {
15         long loc = expression.getLocation();
16         for(int i=solved.size()-1;i>=0;--i) {
17             Constraint c = solved.get(i);
18             Type type = expression.getType();
19             expression = new ESimpleLet(loc, c.evidence, c.generate(loc), expression);
20             expression.setType(type);
21         }
22         return expression;
23     }
24     
25     public static Expression augmentSolved(
26             ArrayList<Constraint> solved, Expression expression) {
27         // Decompose complex lambda to simple lambdas and matching
28         if(expression instanceof ELambda)
29             expression = expression.decomposeMatching();
30         
31         // Inject generation of solved constraints inside the lambdas 
32         if(expression instanceof ESimpleLambda) {
33             ESimpleLambda cur = (ESimpleLambda)expression;
34             while(cur.value instanceof ESimpleLambda)
35                 cur = (ESimpleLambda)cur.value;
36             cur.value = augmentSolvedAux(solved, cur.value);
37         }
38         else
39             expression = augmentSolvedAux(solved, expression);
40         
41         return expression;
42     }
43     
44     public static Expression augmentUnsolved(
45             ArrayList<Constraint> unsolved, Expression expression) {
46         long loc = expression.getLocation();
47         
48         for(int i=unsolved.size()-1;i>=0;--i) {
49             Constraint c = unsolved.get(i);
50             Type type = expression.getType();
51             expression = new ESimpleLambda(loc, c.evidence, expression);
52             expression.setType(Types.constrained(c.constraint, type));
53         }
54         return expression;
55     }
56 }