1 package org.simantics.scl.compiler.internal.deriving;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.common.datatypes.Constructor;
6 import org.simantics.scl.compiler.constants.IntegerConstant;
7 import org.simantics.scl.compiler.constants.StringConstant;
8 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
9 import org.simantics.scl.compiler.elaboration.expressions.EApply;
10 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
11 import org.simantics.scl.compiler.elaboration.expressions.EVar;
12 import org.simantics.scl.compiler.elaboration.expressions.Expression;
13 import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
14 import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;
15 import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
16 import org.simantics.scl.compiler.environment.AmbiguousNameException;
17 import org.simantics.scl.compiler.environment.Environment;
18 import org.simantics.scl.compiler.environment.Environments;
19 import org.simantics.scl.compiler.errors.ErrorLog;
20 import org.simantics.scl.compiler.internal.parsing.declarations.DDerivingInstanceAst;
21 import org.simantics.scl.compiler.internal.parsing.declarations.DInstanceAst;
22 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
23 import org.simantics.scl.compiler.internal.parsing.expressions.Expressions;
24 import org.simantics.scl.compiler.internal.parsing.translation.ProcessedDInstanceAst;
25 import org.simantics.scl.compiler.internal.parsing.translation.ValueRepository;
26 import org.simantics.scl.compiler.internal.parsing.types.TVarAst;
27 import org.simantics.scl.compiler.types.TCon;
29 class ShowDeriver implements InstanceDeriver {
31 private static final IntegerConstant CONST_10 = new IntegerConstant(10);
36 Environment environment,
37 ArrayList<ProcessedDInstanceAst> instancesAst,
38 DDerivingInstanceAst der) {
40 if(der.types.length != 1) {
41 errorLog.log(der.location, "Invalid number of parameters to " + der.name);
44 TVarAst headType = DerivingUtils.getHeadType(der.types[0]);
45 if(headType == null) {
46 errorLog.log(der.types[0].location, "Cannot derive Show instance for the type " + headType + ".");
51 con = Environments.getTypeDescriptorName(environment, headType.name);
52 } catch (AmbiguousNameException e1) {
53 errorLog.log(headType.location, e1.getMessage());
57 errorLog.log(headType.location, "Couldn't resolve " + headType.name);
60 TypeDescriptor tdesc = environment.getTypeDescriptor(con);
62 errorLog.log(headType.location, "Didn't find type constructor for " + headType.name);
65 if(tdesc instanceof TypeAlias) {
66 errorLog.log(headType.location, "Cannot derive instance for a type alias.");
69 TypeConstructor tcon = (TypeConstructor)tdesc;
71 errorLog.log(headType.location, "Cannot derive instance for open data types.");
76 DInstanceAst instanceAst = new DInstanceAst(der.location, der.context, der.name, der.types);
77 ValueRepository valueDefs = new ValueRepository();
78 for(Constructor constructor : tcon.constructors) {
79 int l = constructor.parameterTypes.length;
80 String[] par = new String[l];
81 for(int i=0;i<l;++i) {
84 Expression lhs = new EApply(
87 new EApply(new EVar(constructor.name.name), Expressions.vars(par))
90 value = new EApply(new EVar("<<"), new EVar("sb"),
91 new ELiteral(new StringConstant(constructor.name.name)));
92 for(int i=0;i<l;++i) {
93 value = new EApply(new EVar("<<"),
95 new ELiteral(new StringConstant(" "))
97 value = new EApply(new EVar("<+"),
99 new EApply(new EVar("Par"), new ELiteral(CONST_10), new EVar("v" + i))
104 DValueAst valueAst = new DValueAst(lhs, value);
105 valueAst.setLocationDeep(der.location);
106 valueDefs.add(valueAst);
107 /*valueDefs.addAnnotation("<+", new DAnnotationAst(new EVar("@private"),
108 Collections.<Expression>emptyList()));*/
109 } catch (NotPatternException e) {
110 errorLog.log(e.getExpression().location, "Not a pattern.");
113 instancesAst.add(new ProcessedDInstanceAst(instanceAst, valueDefs));