Minor refactorings related to SCL constructors
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ERecord.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.Arrays;
4
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;
12
13 import gnu.trove.map.hash.THashMap;
14
15 public class ERecord extends ASTExpression {
16
17     public static final boolean DEBUG = false;
18     
19     public final EVar constructor;
20     public final FieldAssignment[] fields;
21     
22     public ERecord(EVar constructor, FieldAssignment[] fields) {
23         this.constructor = constructor;
24         this.fields = fields;
25     }
26
27     @Override
28     public Expression resolve(TranslationContext context) {
29         return resolve(context, false);
30     }
31     
32     @Override
33     public Expression resolveAsPattern(TranslationContext context) {
34         return resolve(context, true);
35     }
36     
37     public Expression resolve(TranslationContext context, boolean asPattern) {
38         SCLValue constructorValue; 
39         try {
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);
44         }
45         if(constructorValue == null) {
46             context.getErrorLog().log(constructor.location, "Couldn't resolve the record constructor " + constructor.name + ".");
47             return new EError(constructor.location);
48         }
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);
53         }
54         Expression[] parameters = translateFieldsToFunctionParameters(context, fields, parameterNames, false);
55         if(parameters == null)
56             return new EError(location);
57         if(asPattern)
58             for(int i=0;i<parameters.length;++i) {
59                 Expression parameter = parameters[i];
60                 if(parameter == null)
61                     parameters[i] = Expressions.blank(null);
62                 else
63                     parameters[i] = parameter.resolveAsPattern(context);
64             }
65         else {
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.");
73                         error = true;
74                     }
75                     else
76                         parameters[i] = frame.createBlank(location); 
77                 }
78                 else
79                     parameters[i] = parameter.resolve(context);
80             }
81             if(error)
82                 return new EError(location);
83         }
84         EApply result = new EApply(new EConstant(constructorValue), parameters);
85         result.setLocationDeep(location);
86         return result;
87     }
88     
89     public static Expression[] translateFieldsToFunctionParameters(TranslationContext context, FieldAssignment[] fields, String[] fieldNames, boolean chrLiteral) {
90         if(DEBUG) {
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];
96                 if(i > 0)
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);
102                 }
103             }
104             System.out.println("}");
105         }
106         
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;
117                     continue;
118                 }
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();
128                     }
129                 }
130                 if(bestMatch == null) {
131                     context.getErrorLog().log(field.location, "Invalid shorthand field " + field.name + " is defined twice.");
132                     error = true;
133                 }
134                 field.value = new EVar(field.location, field.name);
135                 field.name = bestMatch;
136             }
137             if(recordMap.put(field.name, field) != null) {
138                 context.getErrorLog().log(field.location, "Field " + field.name + " is defined more than once.");
139                 error = true;
140             }
141         }
142         if(error)
143             return null;
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];
151                 if(chrLiteral)
152                     variableName = "?" + variableName;
153                 parameters[i] = new EVar(wildcardField.location, variableName);
154             }
155         }
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.");
159             return null;
160         }
161         if(DEBUG)
162             System.out.println(" => parameters = " + Arrays.toString(parameters));
163         return parameters;
164     }
165
166     @Override
167     public void setLocationDeep(long loc) {
168         if(location == Locations.NO_LOCATION) {
169             location = loc;
170             for(FieldAssignment field : fields)
171                 if(field.value != null)
172                     field.value.setLocationDeep(loc);
173         }
174     }
175     
176     @Override
177     public Expression accept(ExpressionTransformer transformer) {
178         return transformer.transform(this);
179     }
180     
181     @Override
182     public void accept(ExpressionVisitor visitor) {
183         visitor.visit(this);
184     }
185 }