X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fexpressions%2FELambda.java;h=b01b771894a831b16d193d208317b746eede221e;hp=abf4c89da4f282a79425f3a08eba5d03357e9d21;hb=4bb82aede02d798846196cc4f51e09d9f9161fef;hpb=eecd74faded034bd067094b42bbac0d286d8d9fa diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELambda.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELambda.java old mode 100755 new mode 100644 index abf4c89da..b01b77189 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELambda.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELambda.java @@ -1,215 +1,183 @@ -package org.simantics.scl.compiler.elaboration.expressions; - -import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext; -import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext; -import org.simantics.scl.compiler.elaboration.contexts.TranslationContext; -import org.simantics.scl.compiler.elaboration.contexts.TypingContext; -import org.simantics.scl.compiler.errors.Locations; -import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator; -import org.simantics.scl.compiler.types.Type; -import org.simantics.scl.compiler.types.Types; -import org.simantics.scl.compiler.types.exceptions.MatchException; -import org.simantics.scl.compiler.types.exceptions.UnificationException; -import org.simantics.scl.compiler.types.kinds.Kinds; -import org.simantics.scl.compiler.types.util.MultiFunction; - -import gnu.trove.map.hash.TObjectIntHashMap; -import gnu.trove.set.hash.THashSet; -import gnu.trove.set.hash.TIntHashSet; - -public class ELambda extends SimplifiableExpression { - public Case[] cases; - Type effect = Types.NO_EFFECTS; - - public ELambda(Case[] cases) { - this.cases = cases; - } - - public ELambda(Case case_) { - this(new Case[] {case_}); - } - - public ELambda(long loc, Case ... cases) { - super(loc); - this.cases = cases; - } - - public ELambda(long loc, Expression pat, Expression exp) { - this(loc, new Case(new Expression[] {pat}, exp)); - } - - public void collectRefs(TObjectIntHashMap allRefs, TIntHashSet refs) { - for(Case case_ : cases) - case_.collectRefs(allRefs, refs); - } - - @Override - public void collectVars(TObjectIntHashMap allVars, - TIntHashSet vars) { - for(Case case_ : cases) - case_.collectVars(allVars, vars); - } - - public Expression decomposeMatching() { - Expression[] patterns = cases[0].patterns; - int arity = patterns.length; - - // Simple cases - if(cases.length == 1 && - !(cases[0].value instanceof GuardedExpressionGroup)) { - boolean noMatchingNeeded = true; - for(int i=0;i=0;--i) { - Variable var = ((EVariable)patterns[i]).getVariable(); - decomposed = new ESimpleLambda(getLocation(), var, effect, decomposed); - effect = Types.NO_EFFECTS; - } - return decomposed; - } - } - - // Complex case - Variable[] vars = new Variable[arity]; - Expression[] scrutinee = new Expression[arity]; - for(int i=0;i=0;--i) { - decomposed = new ESimpleLambda(getLocation(), vars[i], curEffect, decomposed); - curEffect = Types.NO_EFFECTS; - } - return decomposed; - } - - @Override - protected void updateType() throws MatchException { - setType(Types.functionE(Types.getTypes(cases[0].patterns), effect, cases[0].value.getType())); - } - - @Override - public Expression simplify(SimplificationContext context) { - return decomposeMatching().simplify(context); - } - - @Override - public void collectFreeVariables(THashSet vars) { - for(Case case_ : cases) - case_.collectFreeVariables(vars); - } - - @Override - public Expression resolve(TranslationContext context) { - for(Case case_ : cases) - case_.resolve(context); - return this; - } - - @Override - public void setLocationDeep(long loc) { - if(location == Locations.NO_LOCATION) { - location = loc; - for(Case case_ : cases) - case_.setLocationDeep(loc); - } - } - - @Override - public Expression replace(ReplaceContext context) { - Case[] newCases = new Case[cases.length]; - for(int i=0;i effects) { - for(Case case_ : cases) { - for(Expression pattern : case_.patterns) - pattern.collectEffects(effects); - case_.value.collectEffects(effects); - } - } - - @Override - public void accept(ExpressionVisitor visitor) { - visitor.visit(this); - } - - @Override - public void forVariables(VariableProcedure procedure) { - for(Case case_ : cases) - case_.forVariables(procedure); - } - - @Override - public Expression accept(ExpressionTransformer transformer) { - return transformer.transform(this); - } - -} +package org.simantics.scl.compiler.elaboration.expressions; + +import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext; +import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext; +import org.simantics.scl.compiler.elaboration.contexts.TranslationContext; +import org.simantics.scl.compiler.elaboration.contexts.TypingContext; +import org.simantics.scl.compiler.errors.Locations; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.Types; +import org.simantics.scl.compiler.types.exceptions.MatchException; +import org.simantics.scl.compiler.types.exceptions.UnificationException; +import org.simantics.scl.compiler.types.kinds.Kinds; +import org.simantics.scl.compiler.types.util.MultiFunction; + +public class ELambda extends SimplifiableExpression { + public Case[] cases; + Type effect = Types.NO_EFFECTS; + + public ELambda(Case[] cases) { + this.cases = cases; + } + + public ELambda(Case case_) { + this(new Case[] {case_}); + } + + public ELambda(long loc, Case ... cases) { + super(loc); + this.cases = cases; + } + + public ELambda(long loc, Type effect, Case ... cases) { + super(loc); + this.cases = cases; + this.effect = effect; + } + + public ELambda(long loc, Expression pat, Expression exp) { + this(loc, new Case(new Expression[] {pat}, exp)); + } + + public Expression decomposeMatching() { + Expression[] patterns = cases[0].patterns; + int arity = patterns.length; + + // Simple cases + if(cases.length == 1 && + !(cases[0].value instanceof GuardedExpressionGroup)) { + boolean noMatchingNeeded = true; + for(int i=0;i=0;--i) { + Variable var = ((EVariable)patterns[i]).getVariable(); + decomposed = new ESimpleLambda(getLocation(), var, effect, decomposed); + effect = Types.NO_EFFECTS; + } + return decomposed; + } + } + + // Complex case + Variable[] vars = new Variable[arity]; + Expression[] scrutinee = new Expression[arity]; + for(int i=0;i=0;--i) { + decomposed = new ESimpleLambda(getLocation(), vars[i], curEffect, decomposed); + curEffect = Types.NO_EFFECTS; + } + return decomposed; + } + + @Override + protected void updateType() throws MatchException { + setType(Types.functionE(Types.getTypes(cases[0].patterns), effect, cases[0].value.getType())); + } + + @Override + public Expression simplify(SimplificationContext context) { + return decomposeMatching().simplify(context); + } + + @Override + public Expression resolve(TranslationContext context) { + for(Case case_ : cases) + case_.resolve(context); + return this; + } + + @Override + public void setLocationDeep(long loc) { + if(location == Locations.NO_LOCATION) { + location = loc; + for(Case case_ : cases) + case_.setLocationDeep(loc); + } + } + + @Override + public Expression replace(ReplaceContext context) { + Case[] newCases = new Case[cases.length]; + for(int i=0;i