--- /dev/null
+package org.simantics.scl.compiler.internal.elaboration.matching2;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;\r
+import org.simantics.scl.compiler.constants.Constant;\r
+import org.simantics.scl.compiler.elaboration.expressions.EApply;\r
+import org.simantics.scl.compiler.elaboration.expressions.EApplyType;\r
+import org.simantics.scl.compiler.elaboration.expressions.EAsPattern;\r
+import org.simantics.scl.compiler.elaboration.expressions.EConstant;\r
+import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;\r
+import org.simantics.scl.compiler.elaboration.expressions.ELiteral;\r
+import org.simantics.scl.compiler.elaboration.expressions.EVariable;\r
+import org.simantics.scl.compiler.elaboration.expressions.EViewPattern;\r
+import org.simantics.scl.compiler.elaboration.expressions.Expression;\r
+import org.simantics.scl.compiler.elaboration.modules.SCLValue;\r
+import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;\r
+import org.simantics.scl.compiler.environment.Environment;\r
+import org.simantics.scl.compiler.internal.codegen.continuations.Branch;\r
+import org.simantics.scl.compiler.internal.codegen.continuations.ICont;\r
+import org.simantics.scl.compiler.internal.codegen.references.IVal;\r
+import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;\r
+import org.simantics.scl.compiler.types.TCon;\r
+import org.simantics.scl.compiler.types.Types;\r
+import org.simantics.scl.compiler.types.exceptions.MatchException;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+public class PatternMatchingCompiler2 {\r
+\r
+ private static class ExpressionMatrix {\r
+ final CodeWriter w;\r
+ final IVal[] scrutinee;\r
+ final List<Row2> rows = new ArrayList<Row2>();\r
+\r
+ public ExpressionMatrix(CodeWriter w, IVal[] scrutinee) {\r
+ this.w = w;\r
+ this.scrutinee = scrutinee;\r
+ }\r
+ }\r
+\r
+ public static IVal[] replace(IVal[] vals, int columnToReplace, IVal ... substitution) {\r
+ IVal[] newVals = new IVal[vals.length-1+substitution.length];\r
+ int j=0;\r
+ for(int i=0;i<columnToReplace;++i)\r
+ newVals[j++] = vals[i];\r
+ for(int i=0;i<substitution.length;++i)\r
+ newVals[j++] = substitution[i];\r
+ for(int i=columnToReplace+1;i<vals.length;++i)\r
+ newVals[j++] = vals[i];\r
+ return newVals;\r
+ }\r
+\r
+ private static void splitByConstructors(CodeWriter w, final Environment env, IVal[] scrutinee, ICont failure, List<Row2> rows, int columnId) {\r
+ THashMap<Object, ExpressionMatrix> matrixMap = new THashMap<Object, ExpressionMatrix>();\r
+ ArrayList<Branch> branches = new ArrayList<Branch>();\r
+ ArrayList<ExpressionMatrix> matrices = new ArrayList<ExpressionMatrix>();\r
+ \r
+ /*System.out.println("---");\r
+ for(Row row : rows) {\r
+ for(Expression e : row.patterns)\r
+ System.out.print(e + " ");\r
+ System.out.println();\r
+ }*/\r
+\r
+ int i;\r
+ for(i=0;i<rows.size();++i) {\r
+ Row2 row = rows.get(i);\r
+ Expression pattern = row.patterns[columnId];\r
+ while(true) {\r
+ if(pattern instanceof EApplyType)\r
+ pattern = ((EApplyType)pattern).getExpression();\r
+ else if(pattern instanceof EAsPattern) {\r
+ EAsPattern asPattern = (EAsPattern)pattern;\r
+ pattern = asPattern.getPattern();\r
+ asPattern.getVariable().setVal(scrutinee[columnId]);\r
+ }\r
+ else\r
+ break;\r
+ row.patterns[columnId] = pattern;\r
+ }\r
+ if(pattern instanceof EVariable)\r
+ break;\r
+ else if(pattern instanceof EApply) {\r
+ EApply applyConstructor = (EApply)pattern;\r
+ Expression constructor_ = applyConstructor.getFunction();\r
+ while(constructor_ instanceof EApplyType)\r
+ constructor_ = ((EApplyType)constructor_).getExpression();\r
+ Expression[] parameters = applyConstructor.getParameters();\r
+ // TODO How type parameters are handled???\r
+ if(constructor_ instanceof EConstant) {\r
+ SCLValue constructor = ((EConstant)constructor_).getValue();\r
+ \r
+ ExpressionMatrix matrix = matrixMap.get(constructor.getName());\r
+ if(matrix == null) {\r
+ CodeWriter newW = w.createBlock(Types.getTypes(parameters));\r
+ branches.add(new Branch((Constant)constructor.getValue(), newW.getContinuation()));\r
+ matrix = new ExpressionMatrix(newW, replace(scrutinee, columnId, newW.getParameters()));\r
+ matrices.add(matrix);\r
+ matrixMap.put(constructor.getName(), matrix);\r
+ }\r
+ matrix.rows.add(row.replace(columnId, parameters));\r
+ }\r
+ else if(constructor_ instanceof ELiteral) {\r
+ Constant constructor = ((ELiteral)constructor_).getValue();\r
+ \r
+ ExpressionMatrix matrix = matrixMap.get(constructor);\r
+ if(matrix == null) {\r
+ CodeWriter newW = w.createBlock(Types.getTypes(parameters));\r
+ branches.add(new Branch(constructor, newW.getContinuation()));\r
+ matrix = new ExpressionMatrix(newW, replace(scrutinee, columnId, newW.getParameters()));\r
+ matrices.add(matrix);\r
+ matrixMap.put(constructor, matrix);\r
+ }\r
+ matrix.rows.add(row.replace(columnId, parameters));\r
+ }\r
+ }\r
+ else if(pattern instanceof EConstant) {\r
+ EConstant applyConstructor = (EConstant)pattern;\r
+ SCLValue constructor = applyConstructor.getValue();\r
+\r
+ ExpressionMatrix matrix = matrixMap.get(constructor.getName());\r
+ if(matrix == null) {\r
+ CodeWriter newW = w.createBlock();\r
+ branches.add(new Branch((Constant)constructor.getValue(), newW.getContinuation()));\r
+ matrix = new ExpressionMatrix(newW, replace(scrutinee, columnId, newW.getParameters()));\r
+ matrices.add(matrix);\r
+ matrixMap.put(constructor.getName(), matrix);\r
+ }\r
+ matrix.rows.add(row.replace(columnId, Expression.EMPTY_ARRAY));\r
+ }\r
+ else if(pattern instanceof ELiteral) {\r
+ ELiteral literal = (ELiteral)pattern;\r
+ Constant constructor = literal.getValue();\r
+\r
+ ExpressionMatrix matrix = matrixMap.get(constructor);\r
+ if(matrix == null) {\r
+ CodeWriter newW = w.createBlock();\r
+ branches.add(new Branch(constructor, newW.getContinuation()));\r
+ matrix = new ExpressionMatrix(newW, replace(scrutinee, columnId, newW.getParameters()));\r
+ matrices.add(matrix);\r
+ matrixMap.put(constructor, matrix);\r
+ }\r
+ matrix.rows.add(row.replace(columnId, Expression.EMPTY_ARRAY));\r
+ }\r
+ else if(pattern instanceof EExternalConstant) {\r
+ EExternalConstant constant = (EExternalConstant)pattern;\r
+ Constant constructor = w.getModuleWriter().getExternalConstant(constant.getValue(), constant.getType());\r
+\r
+ ExpressionMatrix matrix = matrixMap.get(constructor);\r
+ if(matrix == null) {\r
+ CodeWriter newW = w.createBlock();\r
+ branches.add(new Branch(constructor, newW.getContinuation()));\r
+ matrix = new ExpressionMatrix(newW, replace(scrutinee, columnId, newW.getParameters()));\r
+ matrices.add(matrix);\r
+ matrixMap.put(constructor, matrix);\r
+ }\r
+ matrix.rows.add(row.replace(columnId, Expression.EMPTY_ARRAY));\r
+ }\r
+ else\r
+ throw new InternalCompilerError("Cannot handle an instance of " + pattern.getClass().getSimpleName() + " in a pattern.");\r
+ }\r
+ if(i < rows.size()) {\r
+ CodeWriter newW = w.createBlock();\r
+ ICont cont = newW.getContinuation();\r
+ branches.add(new Branch(null, cont));\r
+ split(newW, env, scrutinee, failure, rows.subList(i, rows.size()));\r
+ failure = cont;\r
+ }\r
+ else {\r
+ TCon con;\r
+ try {\r
+ con = Types.getConstructor(scrutinee[columnId].getType());\r
+ } catch (MatchException e) {\r
+ throw new InternalCompilerError(e);\r
+ }\r
+ TypeConstructor cons = (TypeConstructor)env.getTypeDescriptor(con);\r
+ int maxBranchCount = cons.isOpen ? Integer.MAX_VALUE \r
+ : cons.constructors.length;\r
+ if(branches.size() < maxBranchCount)\r
+ branches.add(new Branch(null, failure));\r
+ }\r
+\r
+ for(ExpressionMatrix mx : matrices)\r
+ split(mx.w, env, mx.scrutinee, failure, mx.rows);\r
+ w.switch_(scrutinee[columnId], branches.toArray(new Branch[branches.size()]));\r
+ }\r
+\r
+ private static void splitByViewPattern(CodeWriter w, Environment env, IVal[] scrutinee, ICont failure, List<Row2> rows, int viewPatternColumn) {\r
+ Row2 firstRow = rows.get(0);\r
+ EViewPattern firstViewPattern = (EViewPattern)firstRow.patterns[viewPatternColumn];\r
+ firstRow.patterns[viewPatternColumn] = firstViewPattern.pattern;\r
+ int i;\r
+ for(i=1;i<rows.size();++i) {\r
+ Row2 row = rows.get(i);\r
+ Expression pattern = row.patterns[viewPatternColumn];\r
+ while(true) {\r
+ if(pattern instanceof EApplyType)\r
+ pattern = ((EApplyType)pattern).getExpression();\r
+ else if(pattern instanceof EAsPattern) {\r
+ EAsPattern asPattern = (EAsPattern)pattern;\r
+ pattern = asPattern.getPattern();\r
+ asPattern.getVariable().setVal(scrutinee[viewPatternColumn]);\r
+ }\r
+ else\r
+ break;\r
+ row.patterns[viewPatternColumn] = pattern;\r
+ }\r
+ if(!(pattern instanceof EViewPattern))\r
+ break;\r
+ EViewPattern otherViewPattern = (EViewPattern)pattern;\r
+ if(!otherViewPattern.expression.equalsExpression(firstViewPattern.expression))\r
+ break;\r
+ row.patterns[viewPatternColumn] = otherViewPattern.pattern;\r
+ }\r
+ \r
+ IVal[] newScrutinee = Arrays.copyOf(scrutinee, scrutinee.length);\r
+ newScrutinee[viewPatternColumn] =\r
+ w.apply(firstViewPattern.location,\r
+ firstViewPattern.expression.toVal(env, w),\r
+ scrutinee[viewPatternColumn]);\r
+ if(i == rows.size()) {\r
+ split(w, env, newScrutinee, failure, rows);\r
+ }\r
+ else {\r
+ CodeWriter cont = w.createBlock();\r
+ split(w, env, newScrutinee, cont.getContinuation(), rows.subList(0, i));\r
+ split(cont, env, scrutinee, failure, rows.subList(i, rows.size()));\r
+ }\r
+ }\r
+\r
+ public static void split(CodeWriter w, Environment env, IVal[] scrutinee, ICont failure, List<Row2> rows) {\r
+ Row2 firstRow = rows.get(0);\r
+ Expression[] patterns = firstRow.patterns;\r
+ if(scrutinee.length != patterns.length)\r
+ throw new InternalCompilerError("Scrutinee and patterns have a different length");\r
+ \r
+ // Find a non-variable pattern and split by it\r
+ int viewPatternColumn = -1;\r
+ for(int i=0;i<patterns.length;++i) {\r
+ Expression pattern = patterns[i];\r
+ if(pattern instanceof EViewPattern) {\r
+ if(viewPatternColumn == -1)\r
+ viewPatternColumn = i;\r
+ }\r
+ else if(!(pattern instanceof EVariable)) {\r
+ splitByConstructors(w, env, scrutinee, failure, rows, i);\r
+ return;\r
+ }\r
+ }\r
+ \r
+ if(viewPatternColumn >= 0) {\r
+ splitByViewPattern(w, env, scrutinee, failure, rows, viewPatternColumn);\r
+ return;\r
+ }\r
+\r
+ // The first row has only variable patterns: no matching needed\r
+ for(int i=0;i<patterns.length;++i)\r
+ ((EVariable)patterns[i]).getVariable().setVal(scrutinee[i]);\r
+ w.jump(firstRow.continuation);\r
+ }\r
+}\r