]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EVar.java
(refs #7250) Support for record syntax for CHR relations
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EVar.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.ArrayList;
4
5 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
6 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
7 import org.simantics.scl.compiler.elaboration.expressions.lhstype.FunctionDefinitionLhs;
8 import org.simantics.scl.compiler.elaboration.expressions.lhstype.LhsType;
9 import org.simantics.scl.compiler.elaboration.expressions.lhstype.PatternMatchingLhs;
10 import org.simantics.scl.compiler.errors.Locations;
11 import org.simantics.scl.compiler.internal.parsing.Token;
12
13 public class EVar extends ASTExpression {
14     public final String name;
15
16     public EVar(Token token) {
17         this.location = token.location;
18         this.name = token.text;
19     }
20     
21     public EVar(long location, String name) {
22         this.location = location;
23         this.name = name;
24     }
25     
26     public EVar(String name) {
27         this(Locations.NO_LOCATION, name);
28     }
29    
30     @Override
31     public EVar getPatternHead() {
32         return this;
33     }
34     
35     @Override
36     public LhsType getLhsType() throws NotPatternException {
37         if(TranslationContext.isConstructorName(name))
38             return new PatternMatchingLhs();
39         else
40             return new FunctionDefinitionLhs(name);
41     }
42     
43     @Override
44     protected void collectVariableNames(PatternMatchingLhs lhsType)
45             throws NotPatternException {
46         if(!TranslationContext.isConstructorName(name))
47             lhsType.variableNames.add(name);
48     }
49
50     @Override
51     public Expression resolve(TranslationContext context) {
52         return context.resolveVariable(location, name);
53     }
54     
55     @Override
56     public void getParameters(TranslationContext translationContext,
57             ArrayList<Expression> parameters) {
58     }
59     
60     @Override
61     public Expression resolveAsPattern(TranslationContext context) {
62         return context.resolvePattern(this);
63     }
64     
65     @Override
66     public int getFunctionDefinitionPatternArity() throws NotPatternException {
67         if(TranslationContext.isConstructorName(name))
68             throw new NotPatternException(this);
69         else
70             return 0;
71     }
72     
73     @Override
74     public boolean isConstructorApplication() {
75         return TranslationContext.isConstructorName(name);
76     }
77     
78     @Override
79     public void setLocationDeep(long loc) {
80         if(location == Locations.NO_LOCATION)
81             location = loc;
82     }
83     
84     @Override
85     public Expression accept(ExpressionTransformer transformer) {
86         return transformer.transform(this);
87     }
88     
89     @Override
90     public void accept(ExpressionVisitor visitor) {
91         visitor.visit(this);
92     }
93 }