]> gerrit.simantics Code Review - simantics/platform.git/blob
cbb69a34be75153b04d99a9d78ccb234676a9194
[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             recordMap.put(field.name, field);
57         Expression[] parameters = new Expression[fieldNames.length];
58         boolean error = false;
59         for(int i=0;i<fieldNames.length;++i) {
60             FieldAssignment assignment = recordMap.remove(fieldNames[i]);
61             if(assignment == null) {
62                 if(asPattern) {
63                     parameters[i] = Expressions.blank(null);
64                 }
65                 else {
66                     context.getErrorLog().log(location, "Field " + fieldNames[i] + " not defined.");
67                     error = true;
68                 }
69             }
70             else
71                 parameters[i] = asPattern
72                         ? assignment.value.resolveAsPattern(context) 
73                         : assignment.value.resolve(context);
74         }
75         if(!recordMap.isEmpty()) {
76             for(FieldAssignment field : recordMap.values())
77                 context.getErrorLog().log(field.location, "Field " + field.name + " is not defined in the constructor.");
78             error = true;
79         }
80         if(error)
81             return new EError(location);
82         else {
83             EApply result = new EApply(new EConstant(constructorValue), parameters);
84             result.setLocationDeep(location);
85             return result;
86         }
87     }
88
89     @Override
90     public void setLocationDeep(long loc) {
91         if(location == Locations.NO_LOCATION) {
92             location = loc;
93             for(FieldAssignment field : fields)
94                 field.value.setLocationDeep(loc);
95         }
96     }
97     
98     @Override
99     public Expression accept(ExpressionTransformer transformer) {
100         return transformer.transform(this);
101     }
102
103 }