1 package org.simantics.scl.compiler.elaboration.macros;
3 import gnu.trove.map.hash.THashMap;
5 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
6 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
7 import org.simantics.scl.compiler.elaboration.expressions.EApply;
8 import org.simantics.scl.compiler.elaboration.expressions.ELambda;
9 import org.simantics.scl.compiler.elaboration.expressions.ELambdaType;
10 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLambda;
11 import org.simantics.scl.compiler.elaboration.expressions.Expression;
12 import org.simantics.scl.compiler.elaboration.expressions.Variable;
13 import org.simantics.scl.compiler.types.TVar;
14 import org.simantics.scl.compiler.types.Type;
17 * This is a macro rule that replaces an application with
18 * the definition of the function.
19 * @author Hannu Niemistö
21 public class StandardMacroRule implements MacroRule {
22 Expression baseExpression;
25 public StandardMacroRule() {
28 public void setBaseExpression(Expression baseExpression) {
29 this.baseExpression = baseExpression;
31 Expression cur = baseExpression;
33 if(cur instanceof ELambdaType) {
34 cur = ((ELambdaType)cur).value;
36 else if(cur instanceof ELambda) {
37 ELambda lambda = (ELambda)cur;
38 arity += lambda.getCases()[0].getPatterns().length;
41 else if(cur instanceof ESimpleLambda) {
42 ESimpleLambda lambda = (ESimpleLambda)cur;
52 public Expression apply(SimplificationContext context,
53 Type[] typeParameters, EApply apply) {
54 if(apply.getParameters().length < arity)
56 THashMap<TVar, Type> tvarMap = new THashMap<TVar, Type>();
57 THashMap<Variable, Expression> varMap = new THashMap<Variable, Expression>();
59 Expression baseExpr = baseExpression;
60 /*System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
61 TypeUnparsingContext tuc = new TypeUnparsingContext();
62 System.out.println("initial1 = " + baseExpr.toString(tuc));
65 int typeParameterId = 0;
66 while(typeParameterId < typeParameters.length) {
67 ELambdaType lambda = (ELambdaType)baseExpr;
68 for(TVar var : lambda.parameters)
69 tvarMap.put(var, typeParameters[typeParameterId++]);
70 baseExpr = lambda.value;
73 for(Expression parameter : apply.getParameters()) {
74 if(baseExpr instanceof ELambda)
75 baseExpr = ((ELambda)baseExpr).decomposeMatching();
76 ESimpleLambda lambda = (ESimpleLambda)baseExpr;
77 varMap.put(lambda.parameter, parameter);
78 baseExpr = lambda.value;
83 System.out.println("initial2 = " + baseExpr.toString(tuc));
85 System.out.print("tvarMap={");
87 for(Map.Entry<TVar, Type> entry : tvarMap.entrySet()) {
91 System.out.print(", ");
92 System.out.print(entry.getKey().toString(tuc));
93 System.out.print(" = ");
94 System.out.print(entry.getValue().toString(tuc));
96 System.out.println("}");
97 System.out.println("varMap = " + varMap);
99 baseExpr = baseExpr.replace(new ReplaceContext(tvarMap, varMap, null));
101 //System.out.println("final = " + baseExpr.toString(tuc));