1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
4 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
7 import org.simantics.scl.compiler.errors.Locations;
8 import org.simantics.scl.compiler.internal.parsing.Symbol;
9 import org.simantics.scl.compiler.types.Type;
11 public class Case extends Symbol {
12 public Expression[] patterns;
13 public Expression value;
17 public Case(Expression[] patterns, Expression value) {
18 this.patterns = patterns;
22 public Case(Expression pattern, Expression value) {
23 this(new Expression[] {pattern}, value);
26 public void setLhs(long lhs) {
30 public long getLhs() {
34 public void resolve(TranslationContext context) {
36 for(int i=0;i<patterns.length;++i)
37 patterns[i] = patterns[i].resolveAsPattern(context);
38 value = value.resolve(context);
42 public void simplify(SimplificationContext context) {
43 for(int i=0;i<patterns.length;++i)
44 patterns[i] = patterns[i].simplify(context);
45 value = value.simplify(context);
48 public void setLocationDeep(long loc) {
49 if(location == Locations.NO_LOCATION) {
51 for(Expression pattern : patterns)
52 pattern.setLocationDeep(loc);
53 value.setLocationDeep(loc);
57 public Case replace(ReplaceContext context) {
58 Expression[] newPatterns = new Expression[patterns.length];
59 for(int i=0;i<patterns.length;++i)
60 newPatterns[i] = patterns[i].replaceInPattern(context);
61 Expression newValue = value.replace(context);
62 Case result = new Case(newPatterns, newValue);
67 public Expression[] getPatterns() {
71 public void checkType(TypingContext context, Type[] parameterTypes,
73 if(patterns.length != parameterTypes.length) {
74 context.getErrorLog().log(location, "This case has different arity ("+patterns.length+
75 ") than than the first case (+"+parameterTypes.length+"+).");
78 for(int i=0;i<patterns.length;++i)
79 patterns[i] = patterns[i].checkTypeAsPattern(context, parameterTypes[i]);
80 value = value.checkType(context, requiredType);
83 public void checkIgnoredType(TypingContext context, Type[] parameterTypes) {
84 if(patterns.length != parameterTypes.length) {
85 context.getErrorLog().log(location, "This case has different arity ("+patterns.length+
86 ") than than the first case (+"+parameterTypes.length+"+).");
89 for(int i=0;i<patterns.length;++i)
90 patterns[i] = patterns[i].checkTypeAsPattern(context, parameterTypes[i]);
91 value = value.checkIgnoredType(context);