]> gerrit.simantics Code Review - simantics/platform.git/blob
302d0cf3416f4c92de753ccdb9966e3a19b59d8f
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions.block;
2
3 import org.simantics.scl.compiler.elaboration.contexts.EnvironmentalContext;
4 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
6 import org.simantics.scl.compiler.elaboration.expressions.Case;
7 import org.simantics.scl.compiler.elaboration.expressions.EMatch;
8 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLet;
9 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
10 import org.simantics.scl.compiler.elaboration.expressions.Expression;
11 import org.simantics.scl.compiler.errors.Locations;
12
13 public class LetStatement extends Statement {
14     public Expression pattern;
15     public Expression value;
16     
17     public LetStatement(Expression pattern, Expression value) {
18         this.pattern = pattern;
19         this.value = value;
20     }
21
22     @Override
23     public Expression toExpression(EnvironmentalContext context, boolean monadic, Expression in) {
24         if(pattern instanceof EVariable)
25             return new ESimpleLet(((EVariable)pattern).getVariable(), value, in);
26         else
27             //return new EPreLet(Arrays.asList(this), in);
28             return new EMatch(location, new Expression[] {value}, new Case[] {new Case(pattern, in)});
29     }
30     
31     @Override
32     public void setLocationDeep(long loc) {
33         if(location == Locations.NO_LOCATION) {
34             location = loc;
35             pattern.setLocationDeep(loc);
36             value.setLocationDeep(loc);
37         }
38     }
39     
40     @Override
41     public void resolvePattern(TranslationContext context) {
42         pattern = pattern.resolveAsPattern(context);
43     }
44     
45     @Override
46     public boolean mayBeRecursive() {
47         return pattern.isFunctionDefinitionLhs();
48     }
49     
50     @Override
51     public Statement replace(ReplaceContext context) {
52         return new LetStatement(pattern.replaceInPattern(context), value.replace(context));
53     }
54     
55     @Override
56     public void accept(StatementVisitor visitor) {
57         visitor.visit(this);
58     }
59
60     @Override
61     public StatementGroup getStatementGroup() {
62         if(pattern.isFunctionPattern())
63             return StatementGroup.LetFunction;
64         else
65             return null;
66     }
67 }