]> gerrit.simantics Code Review - simantics/platform.git/blob
32999dcb48877a689c980d7e7c1bc73973b46345
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import org.simantics.scl.compiler.constants.SCLConstructor;
4 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
5 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
6 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
7 import org.simantics.scl.compiler.environment.AmbiguousNameException;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.internal.parsing.Token;
10
11 import gnu.trove.map.hash.THashMap;
12
13 public class ERecord extends ASTExpression {
14
15     Token constructor;
16     FieldAssignment[] fields;
17     
18     public ERecord(Token constructor, FieldAssignment[] fields) {
19         this.constructor = constructor;
20         this.fields = fields;
21     }
22
23     @Override
24     public Expression resolve(TranslationContext context) {
25         return resolve(context, false);
26     }
27     
28     @Override
29     public Expression resolveAsPattern(TranslationContext context) {
30         return resolve(context, true);
31     }
32     
33     public Expression resolve(TranslationContext context, boolean asPattern) {
34         SCLValue constructorValue; 
35         try {
36             constructorValue = context.getEnvironment().getLocalNamespace().getValue(constructor.text);
37         } catch (AmbiguousNameException e) {
38             context.getErrorLog().log(constructor.location, e.getMessage());
39             return new EError(constructor.location);
40         }
41         if(constructorValue == null) {
42             context.getErrorLog().log(constructor.location, "Couldn't resolve the record constructor " + constructor.text + ".");
43             return new EError(constructor.location);
44         }
45         if(!(constructorValue.getValue() instanceof SCLConstructor)) {
46             context.getErrorLog().log(constructor.location, "Value " + constructor.text + " is not a record constructor.");
47             return new EError(constructor.location);
48         }
49         String[] fieldNames = ((SCLConstructor)constructorValue.getValue()).recordFieldNames;
50         if(fieldNames == null) {
51             context.getErrorLog().log(constructor.location, "Value " + constructor.text + " is not a record constructor.");
52             return new EError(constructor.location);
53         }
54         THashMap<String,FieldAssignment> recordMap = new THashMap<String,FieldAssignment>(fields.length);
55         for(FieldAssignment field : fields) {
56             if(field.value == null) {
57                 String bestMatch = null;
58                 int bestMatchLength = 0;
59                 for(int i=0;i<fieldNames.length;++i) {
60                     String fieldName = fieldNames[i];
61                     if(field.name.startsWith(fieldName) && fieldName.length() > bestMatchLength) {
62                         bestMatch = fieldName;
63                         bestMatchLength = fieldName.length();
64                     }
65                 }
66                 if(bestMatch == null) {
67                     context.getErrorLog().log(field.location, "Invalid shorthand field " + field.name + " is defined twice.");
68                     return new EError(location);
69                 }
70                 field.value = new EVar(field.location, field.name);
71                 field.name = bestMatch;
72             }
73             if(recordMap.put(field.name, field) != null) {
74                 context.getErrorLog().log(field.location, "Field " + field.name + " is defined more than once.");
75                 return new EError(location);
76             }
77         }
78         Expression[] parameters = new Expression[fieldNames.length];
79         boolean error = false;
80         for(int i=0;i<fieldNames.length;++i) {
81             FieldAssignment assignment = recordMap.remove(fieldNames[i]);
82             if(assignment == null) {
83                 if(asPattern) {
84                     parameters[i] = Expressions.blank(null);
85                 }
86                 else {
87                     context.getErrorLog().log(location, "Field " + fieldNames[i] + " not defined.");
88                     error = true;
89                 }
90             }
91             else
92                 parameters[i] = asPattern
93                         ? assignment.value.resolveAsPattern(context) 
94                         : assignment.value.resolve(context);
95         }
96         if(!recordMap.isEmpty()) {
97             for(FieldAssignment field : recordMap.values())
98                 context.getErrorLog().log(field.location, "Field " + field.name + " is not defined in the constructor.");
99             error = true;
100         }
101         if(error)
102             return new EError(location);
103         else {
104             EApply result = new EApply(new EConstant(constructorValue), parameters);
105             result.setLocationDeep(location);
106             return result;
107         }
108     }
109
110     @Override
111     public void setLocationDeep(long loc) {
112         if(location == Locations.NO_LOCATION) {
113             location = loc;
114             for(FieldAssignment field : fields)
115                 if(field.value != null)
116                     field.value.setLocationDeep(loc);
117         }
118     }
119     
120     @Override
121     public Expression accept(ExpressionTransformer transformer) {
122         return transformer.transform(this);
123     }
124
125 }