1 package org.simantics.scl.compiler.internal.elaboration.constraints;
3 import java.util.ArrayList;
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;
12 public class ExpressionAugmentation {
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);
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();
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);
39 expression = augmentSolvedAux(solved, expression);
44 public static Expression augmentUnsolved(
45 ArrayList<Constraint> unsolved, Expression expression) {
46 long loc = expression.getLocation();
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));