1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.Arrays;
5 import org.simantics.scl.compiler.constants.SCLConstructor;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext.ExistentialFrame;
8 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
9 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
10 import org.simantics.scl.compiler.environment.AmbiguousNameException;
11 import org.simantics.scl.compiler.errors.Locations;
13 import gnu.trove.map.hash.THashMap;
15 public class ERecord extends ASTExpression {
17 public static final boolean DEBUG = false;
19 public final EVar constructor;
20 public final FieldAssignment[] fields;
22 public ERecord(EVar constructor, FieldAssignment[] fields) {
23 this.constructor = constructor;
28 public Expression resolve(TranslationContext context) {
29 return resolve(context, false);
33 public Expression resolveAsPattern(TranslationContext context) {
34 return resolve(context, true);
37 public Expression resolve(TranslationContext context, boolean asPattern) {
38 SCLValue constructorValue;
40 constructorValue = context.resolveRecordConstructor(location, constructor.name);
41 } catch (AmbiguousNameException e) {
42 context.getErrorLog().log(constructor.location, e.getMessage());
43 return new EError(constructor.location);
45 if(constructorValue == null) {
46 context.getErrorLog().log(constructor.location, "Couldn't resolve the record constructor " + constructor.name + ".");
47 return new EError(constructor.location);
49 String[] parameterNames = constructorValue.parameterNames;
50 if(parameterNames == null) {
51 context.getErrorLog().log(constructor.location, "Value " + constructor.name + " is not a record constructor.");
52 return new EError(constructor.location);
54 Expression[] parameters = translateFieldsToFunctionParameters(context, fields, parameterNames, false);
55 if(parameters == null)
56 return new EError(location);
58 for(int i=0;i<parameters.length;++i) {
59 Expression parameter = parameters[i];
61 parameters[i] = Expressions.blank(null);
63 parameters[i] = parameter.resolveAsPattern(context);
66 boolean error = false;
67 for(int i=0;i<parameters.length;++i) {
68 Expression parameter = parameters[i];
69 if(parameter == null) {
70 ExistentialFrame frame = context.getCurrentExistentialFrame();
71 if(frame == null || frame.disallowNewExistentials) {
72 context.getErrorLog().log(location, "Field " + parameterNames[i] + " not defined.");
76 parameters[i] = frame.createBlank(location);
79 parameters[i] = parameter.resolve(context);
82 return new EError(location);
84 EApply result = new EApply(new EConstant(constructorValue), parameters);
85 result.setLocationDeep(location);
89 public static Expression[] translateFieldsToFunctionParameters(TranslationContext context, FieldAssignment[] fields, String[] fieldNames, boolean chrLiteral) {
91 System.out.println("translateFieldsToFunctionParameters");
92 System.out.println(" fieldNames = " + Arrays.toString(fieldNames));
93 System.out.print(" fields = {");
94 for(int i=0;i<fields.length;++i) {
95 FieldAssignment field = fields[i];
97 System.out.print(", ");
98 System.out.print(field.name);
99 if(field.value != null) {
100 System.out.print(" = ");
101 System.out.print(field.value);
104 System.out.println("}");
107 THashMap<String,FieldAssignment> recordMap = new THashMap<String,FieldAssignment>(fields.length);
108 boolean error = false;
109 FieldAssignment wildcardField = null;
110 for(FieldAssignment field : fields) {
111 if(field.value == null) {
112 String actualName = field.name;
113 if(actualName.equals(FieldAssignment.WILDCARD)) {
114 if(wildcardField != null)
115 context.getErrorLog().log(field.location, "The record has more than one wildcard.");
116 wildcardField = field;
119 if(actualName.charAt(0) == '?')
120 actualName = actualName.substring(1);
121 String bestMatch = null;
122 int bestMatchLength = 0;
123 for(int i=0;i<fieldNames.length;++i) {
124 String fieldName = fieldNames[i];
125 if(actualName.startsWith(fieldName) && fieldName.length() > bestMatchLength) {
126 bestMatch = fieldName;
127 bestMatchLength = fieldName.length();
130 if(bestMatch == null) {
131 context.getErrorLog().log(field.location, "Invalid shorthand field " + field.name + " is defined twice.");
134 field.value = new EVar(field.location, field.name);
135 field.name = bestMatch;
137 if(recordMap.put(field.name, field) != null) {
138 context.getErrorLog().log(field.location, "Field " + field.name + " is defined more than once.");
144 Expression[] parameters = new Expression[fieldNames.length];
145 for(int i=0;i<fieldNames.length;++i) {
146 FieldAssignment assignment = recordMap.remove(fieldNames[i]);
147 if(assignment != null)
148 parameters[i] = assignment.value;
149 else if(wildcardField != null) {
150 String variableName = fieldNames[i];
152 variableName = "?" + variableName;
153 parameters[i] = new EVar(wildcardField.location, variableName);
156 if(!recordMap.isEmpty()) {
157 for(FieldAssignment field : recordMap.values())
158 context.getErrorLog().log(field.location, "Field " + field.name + " is not defined in the constructor.");
162 System.out.println(" => parameters = " + Arrays.toString(parameters));
167 public void setLocationDeep(long loc) {
168 if(location == Locations.NO_LOCATION) {
170 for(FieldAssignment field : fields)
171 if(field.value != null)
172 field.value.setLocationDeep(loc);
177 public Expression accept(ExpressionTransformer transformer) {
178 return transformer.transform(this);
182 public void accept(ExpressionVisitor visitor) {