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%2FEViewPattern.java;h=a08ed3923570719faccb76eb06690d8d1dec24a7;hp=268ee8ea83eac77e8152dbf02d626e98f76821ad;hb=862c09c9608329f326404342d12da61792eece2c;hpb=593a8f75d9dbc363234002dc500c346afbeba040 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EViewPattern.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EViewPattern.java index 268ee8ea8..a08ed3923 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EViewPattern.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EViewPattern.java @@ -1,137 +1,137 @@ -package org.simantics.scl.compiler.elaboration.expressions; - -import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; -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.environment.Environment; -import org.simantics.scl.compiler.errors.Locations; -import org.simantics.scl.compiler.internal.codegen.references.IVal; -import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter; -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.util.MultiFunction; - -import gnu.trove.map.hash.TObjectIntHashMap; -import gnu.trove.set.hash.THashSet; -import gnu.trove.set.hash.TIntHashSet; - -public class EViewPattern extends Expression { - public Expression expression; - public Expression pattern; - - public EViewPattern(Expression expression, Expression pattern) { - this.expression = expression; - this.pattern = pattern; - } - - @Override - public void collectRefs(TObjectIntHashMap allRefs, TIntHashSet refs) { - expression.collectRefs(allRefs, refs); - pattern.collectRefs(allRefs, refs); - } - - @Override - public void collectVars(TObjectIntHashMap allVars, TIntHashSet vars) { - expression.collectVars(allVars, vars); - pattern.collectVars(allVars, vars); - } - - @Override - public void forVariables(VariableProcedure procedure) { - expression.forVariables(procedure); - pattern.forVariables(procedure); - } - - @Override - public Expression inferType(TypingContext context) { - context.setInPattern(false); - expression = expression.inferType(context); - context.setInPattern(true); - MultiFunction mfun; - try { - mfun = Types.matchFunction(expression.getType(), 1); - } catch (MatchException e) { - context.getErrorLog().log(expression.location, "Expected a function as a transformation expression."); - return new EError(location); - } - setType(mfun.parameterTypes[0]); - pattern.checkType(context, mfun.returnType); - return this; - } - - @Override - protected void updateType() throws MatchException { - MultiFunction mfun = Types.matchFunction(expression.getType(), 1); - setType(mfun.parameterTypes[0]); - } - - @Override - public IVal toVal(Environment env, CodeWriter w) { - throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked."); - } - - @Override - public void collectFreeVariables(THashSet vars) { - throw new InternalCompilerError(location, "Cannot collect free variables for a pattern."); - } - - @Override - public void removeFreeVariables(THashSet vars) { - expression.collectFreeVariables(vars); - pattern.removeFreeVariables(vars); - } - - @Override - public Expression resolve(TranslationContext context) { - context.getErrorLog().log("View pattern cannot occur only in patterns. Maybe you are missing '\\' in front of a lambda experssion?"); - return new EError(location); - } - - @Override - public Expression resolveAsPattern(TranslationContext context) { - expression = expression.resolve(context); - pattern = pattern.resolveAsPattern(context); - return this; - } - - @Override - public void setLocationDeep(long loc) { - if(location == Locations.NO_LOCATION) { - location = loc; - expression.setLocationDeep(loc); - pattern.setLocationDeep(loc); - } - } - - @Override - public Expression decorate(ExpressionDecorator decorator) { - expression = expression.decorate(decorator); - return this; - } - - @Override - public void collectEffects(THashSet effects) { - expression.collectEffects(effects); - } - - @Override - public void accept(ExpressionVisitor visitor) { - visitor.visit(this); - } - - @Override - public Expression accept(ExpressionTransformer transformer) { - return transformer.transform(this); - } - - @Override - public Expression simplify(SimplificationContext context) { - expression = expression.simplify(context); - pattern = pattern.simplify(context); - return this; - } - -} +package org.simantics.scl.compiler.elaboration.expressions; + +import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; +import org.simantics.scl.compiler.compilation.CompilationContext; +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.codegen.references.IVal; +import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter; +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.util.MultiFunction; + +import gnu.trove.map.hash.TObjectIntHashMap; +import gnu.trove.set.hash.THashSet; +import gnu.trove.set.hash.TIntHashSet; + +public class EViewPattern extends Expression { + public Expression expression; + public Expression pattern; + + public EViewPattern(Expression expression, Expression pattern) { + this.expression = expression; + this.pattern = pattern; + } + + @Override + public void collectRefs(TObjectIntHashMap allRefs, TIntHashSet refs) { + expression.collectRefs(allRefs, refs); + pattern.collectRefs(allRefs, refs); + } + + @Override + public void collectVars(TObjectIntHashMap allVars, TIntHashSet vars) { + expression.collectVars(allVars, vars); + pattern.collectVars(allVars, vars); + } + + @Override + public void forVariables(VariableProcedure procedure) { + expression.forVariables(procedure); + pattern.forVariables(procedure); + } + + @Override + public Expression inferType(TypingContext context) { + context.setInPattern(false); + expression = expression.inferType(context); + context.setInPattern(true); + MultiFunction mfun; + try { + mfun = Types.matchFunction(expression.getType(), 1); + } catch (MatchException e) { + context.getErrorLog().log(expression.location, "Expected a function as a transformation expression."); + return new EError(location); + } + setType(mfun.parameterTypes[0]); + pattern.checkType(context, mfun.returnType); + return this; + } + + @Override + protected void updateType() throws MatchException { + MultiFunction mfun = Types.matchFunction(expression.getType(), 1); + setType(mfun.parameterTypes[0]); + } + + @Override + public IVal toVal(CompilationContext context, CodeWriter w) { + throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked."); + } + + @Override + public void collectFreeVariables(THashSet vars) { + throw new InternalCompilerError(location, "Cannot collect free variables for a pattern."); + } + + @Override + public void removeFreeVariables(THashSet vars) { + expression.collectFreeVariables(vars); + pattern.removeFreeVariables(vars); + } + + @Override + public Expression resolve(TranslationContext context) { + context.getErrorLog().log("View pattern cannot occur only in patterns. Maybe you are missing '\\' in front of a lambda experssion?"); + return new EError(location); + } + + @Override + public Expression resolveAsPattern(TranslationContext context) { + expression = expression.resolve(context); + pattern = pattern.resolveAsPattern(context); + return this; + } + + @Override + public void setLocationDeep(long loc) { + if(location == Locations.NO_LOCATION) { + location = loc; + expression.setLocationDeep(loc); + pattern.setLocationDeep(loc); + } + } + + @Override + public Expression decorate(ExpressionDecorator decorator) { + expression = expression.decorate(decorator); + return this; + } + + @Override + public void collectEffects(THashSet effects) { + expression.collectEffects(effects); + } + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } + + @Override + public Expression accept(ExpressionTransformer transformer) { + return transformer.transform(this); + } + + @Override + public Expression simplify(SimplificationContext context) { + expression = expression.simplify(context); + pattern = pattern.simplify(context); + return this; + } + +}