X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fexpressions%2FERecord.java;h=a859782239caac40c50cf91e34935b43aa303b7e;hb=2790c2eced1ae3e229972ede7173dc6831834760;hp=cbb69a34be75153b04d99a9d78ccb234676a9194;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERecord.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERecord.java index cbb69a34b..a85978223 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERecord.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERecord.java @@ -1,21 +1,25 @@ package org.simantics.scl.compiler.elaboration.expressions; +import java.util.Arrays; + import org.simantics.scl.compiler.constants.SCLConstructor; import org.simantics.scl.compiler.elaboration.contexts.TranslationContext; +import org.simantics.scl.compiler.elaboration.contexts.TranslationContext.ExistentialFrame; import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment; import org.simantics.scl.compiler.elaboration.modules.SCLValue; import org.simantics.scl.compiler.environment.AmbiguousNameException; import org.simantics.scl.compiler.errors.Locations; -import org.simantics.scl.compiler.internal.parsing.Token; import gnu.trove.map.hash.THashMap; public class ERecord extends ASTExpression { - Token constructor; - FieldAssignment[] fields; + public static final boolean DEBUG = false; + + public final EVar constructor; + public final FieldAssignment[] fields; - public ERecord(Token constructor, FieldAssignment[] fields) { + public ERecord(EVar constructor, FieldAssignment[] fields) { this.constructor = constructor; this.fields = fields; } @@ -33,57 +37,134 @@ public class ERecord extends ASTExpression { public Expression resolve(TranslationContext context, boolean asPattern) { SCLValue constructorValue; try { - constructorValue = context.getEnvironment().getLocalNamespace().getValue(constructor.text); + constructorValue = context.getEnvironment().getLocalNamespace().getValue(constructor.name); } catch (AmbiguousNameException e) { context.getErrorLog().log(constructor.location, e.getMessage()); return new EError(constructor.location); } if(constructorValue == null) { - context.getErrorLog().log(constructor.location, "Couldn't resolve the record constructor " + constructor.text + "."); + context.getErrorLog().log(constructor.location, "Couldn't resolve the record constructor " + constructor.name + "."); return new EError(constructor.location); } if(!(constructorValue.getValue() instanceof SCLConstructor)) { - context.getErrorLog().log(constructor.location, "Value " + constructor.text + " is not a record constructor."); + context.getErrorLog().log(constructor.location, "Value " + constructor.name + " is not a record constructor."); return new EError(constructor.location); } String[] fieldNames = ((SCLConstructor)constructorValue.getValue()).recordFieldNames; if(fieldNames == null) { - context.getErrorLog().log(constructor.location, "Value " + constructor.text + " is not a record constructor."); + context.getErrorLog().log(constructor.location, "Value " + constructor.name + " is not a record constructor."); return new EError(constructor.location); } + Expression[] parameters = translateFieldsToFunctionParameters(context, fields, fieldNames, false); + if(parameters == null) + return new EError(location); + if(asPattern) + for(int i=0;i 0) + System.out.print(", "); + System.out.print(field.name); + if(field.value != null) { + System.out.print(" = "); + System.out.print(field.value); + } + } + System.out.println("}"); + } + THashMap recordMap = new THashMap(fields.length); - for(FieldAssignment field : fields) - recordMap.put(field.name, field); - Expression[] parameters = new Expression[fieldNames.length]; boolean error = false; - for(int i=0;i bestMatchLength) { + bestMatch = fieldName; + bestMatchLength = fieldName.length(); + } } - else { - context.getErrorLog().log(location, "Field " + fieldNames[i] + " not defined."); + if(bestMatch == null) { + context.getErrorLog().log(field.location, "Invalid shorthand field " + field.name + " is defined twice."); error = true; } + field.value = new EVar(field.location, field.name); + field.name = bestMatch; + } + if(recordMap.put(field.name, field) != null) { + context.getErrorLog().log(field.location, "Field " + field.name + " is defined more than once."); + error = true; + } + } + if(error) + return null; + Expression[] parameters = new Expression[fieldNames.length]; + for(int i=0;i parameters = " + Arrays.toString(parameters)); + return parameters; } @Override @@ -91,7 +172,8 @@ public class ERecord extends ASTExpression { if(location == Locations.NO_LOCATION) { location = loc; for(FieldAssignment field : fields) - field.value.setLocationDeep(loc); + if(field.value != null) + field.value.setLocationDeep(loc); } } @@ -99,5 +181,9 @@ public class ERecord extends ASTExpression { public Expression accept(ExpressionTransformer transformer) { return transformer.transform(this); } - + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } }