1 package org.simantics.scl.compiler.elaboration.chr.translation;
3 import java.util.ArrayList;
4 import java.util.Arrays;
6 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
7 import org.simantics.scl.compiler.elaboration.chr.CHRLiteral;
8 import org.simantics.scl.compiler.elaboration.chr.CHRQuery;
9 import org.simantics.scl.compiler.elaboration.chr.CHRRule;
10 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
11 import org.simantics.scl.compiler.elaboration.chr.relations.SpecialCHRRelation;
12 import org.simantics.scl.compiler.elaboration.chr.relations.UnresolvedCHRRelation;
13 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
14 import org.simantics.scl.compiler.elaboration.expressions.EApply;
15 import org.simantics.scl.compiler.elaboration.expressions.EBinary;
16 import org.simantics.scl.compiler.elaboration.expressions.ERecord;
17 import org.simantics.scl.compiler.elaboration.expressions.EVar;
18 import org.simantics.scl.compiler.elaboration.expressions.Expression;
19 import org.simantics.scl.compiler.elaboration.expressions.block.CHRStatement;
20 import org.simantics.scl.compiler.elaboration.expressions.block.ConstraintStatement;
21 import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment;
22 import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator;
23 import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard;
24 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
25 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
26 import org.simantics.scl.compiler.environment.AmbiguousNameException;
27 import org.simantics.scl.compiler.environment.Environments;
28 import org.simantics.scl.compiler.errors.Locations;
29 import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
30 import org.simantics.scl.compiler.internal.parsing.types.TypeAst;
32 public class CHRTranslation {
34 private static CHRLiteral convertExpression(boolean isHead, Expression expression) {
36 return new CHRLiteral(expression.location, SpecialCHRRelation.CHECK, new Expression[] {expression}, false, false);
38 return new CHRLiteral(expression.location, SpecialCHRRelation.EXECUTE, new Expression[] {expression}, false, false);
41 private static CHRLiteral convertConstraint(boolean remove, boolean negated, Expression expression) {
42 long location = expression.location;
43 Expression[] parameters;
44 FieldAssignment[] fields = null;
45 if(expression instanceof EApply) {
46 EApply apply = (EApply)expression;
47 parameters = apply.parameters;
48 expression = apply.function;
50 else if(expression instanceof ERecord) {
51 ERecord record = (ERecord)expression;
53 fields = record.fields;
54 expression = record.constructor;
56 else // if(expression instanceof EVar)
57 parameters = Expression.EMPTY_ARRAY;
58 EVar var = (EVar)expression; // this should succeed because of isConstraint test
59 CHRLiteral literal = new CHRLiteral(location, new UnresolvedCHRRelation(var.location, var.name),
60 parameters, remove, negated);
61 literal.fields = fields;
65 private static CHRLiteral convertListQualifier(TranslationContext context, boolean isHead, ListQualifier qualifier) {
66 if(qualifier instanceof ListGuard) {
67 Expression originalExpression = ((ListGuard)qualifier).condition;
68 if(originalExpression instanceof EVar && ((EVar)originalExpression).name.equals("True"))
70 Expression currentExpression = originalExpression;
71 boolean remove = false;
72 boolean negated = false;
73 if(currentExpression instanceof EBinary) {
74 EBinary binary = (EBinary)currentExpression;
75 if(binary.negation==null || !binary.rights.isEmpty())
76 return convertExpression(isHead, originalExpression);
77 currentExpression = binary.left;
80 else if(currentExpression instanceof EApply) {
81 EApply apply = (EApply)currentExpression;
82 if(apply.function instanceof EVar && ((EVar)apply.function).name.equals("not")) {
84 if(apply.parameters.length == 1)
85 currentExpression = apply.parameters[0];
87 currentExpression = new EApply(
88 Locations.combine(apply.parameters[0].location, apply.parameters[apply.parameters.length-1].location),
90 Arrays.copyOfRange(apply.parameters, 1, apply.parameters.length));
94 if(isConstraint(context, currentExpression))
95 return convertConstraint(remove, negated, currentExpression);
97 return convertExpression(isHead, originalExpression);
99 else if(qualifier instanceof ListAssignment) {
100 ListAssignment assignment = (ListAssignment)qualifier;
101 return new CHRLiteral(assignment.location, SpecialCHRRelation.EQUALS, new Expression[] {
102 assignment.pattern, assignment.value
105 else if(qualifier instanceof ListGenerator) {
106 ListGenerator generator = (ListGenerator)qualifier;
107 return new CHRLiteral(generator.location, SpecialCHRRelation.MEMBER, new Expression[] {
108 generator.pattern, generator.value
112 context.getErrorLog().log(qualifier.location, "Invalid CHR literal.");
117 private static boolean isConstraint(TranslationContext context, Expression expression) {
118 if(expression instanceof EApply)
119 expression = ((EApply)expression).function;
120 else if(expression instanceof ERecord)
121 expression = ((ERecord)expression).constructor;
122 if(!(expression instanceof EVar))
124 String name = ((EVar)expression).name;
125 if(TranslationContext.isConstructorName(name))
128 return Environments.getRelation(context.getEnvironment(), name) != null;
129 } catch (AmbiguousNameException e) {
134 public static CHRRule convertCHRStatement(TranslationContext context, CHRStatement statement) {
135 ArrayList<CHRLiteral> head = new ArrayList<CHRLiteral>(statement.head.length);
136 for(ListQualifier qualifier : statement.head) {
137 CHRLiteral literal = convertListQualifier(context, true, qualifier);
141 ArrayList<CHRLiteral> body = new ArrayList<CHRLiteral>(statement.body.length);
142 for(ListQualifier qualifier : statement.body) {
143 CHRLiteral literal = convertListQualifier(context, false, qualifier);
147 return new CHRRule(statement.location,
148 new CHRQuery(head.toArray(new CHRLiteral[head.size()])),
149 new CHRQuery(body.toArray(new CHRLiteral[body.size()])),
153 public static CHRConstraint convertConstraintStatement(TranslationContext context, ConstraintStatement statement) {
154 CHRConstraint constraint = new CHRConstraint(statement.location, statement.name.text, TypeAst.toTypes(context, statement.parameterTypes));
155 for(DAnnotationAst annotation : statement.annotations)
156 applyConstraintAnnotation(context, constraint, annotation);
157 constraint.fieldNames = statement.fieldNames;
161 private static void applyConstraintAnnotation(TranslationContext context, CHRConstraint constraint, DAnnotationAst annotation) {
162 context.getErrorLog().log(annotation.location, "Invalid constraint annotation");