]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRuleset.java
(refs #7375) Replaced ExpressionDecorator by ExpressionTransformer
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ECHRRuleset.java
index b2d3b2235567808963b8644fa71ce2863518d80a..2f87719306901bde69636d1206a33efdea077acd 100644 (file)
-package org.simantics.scl.compiler.elaboration.expressions;\r
-\r
-import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;\r
-import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;\r
-import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;\r
-import org.simantics.scl.compiler.elaboration.contexts.TypingContext;\r
-import org.simantics.scl.compiler.environment.Environment;\r
-import org.simantics.scl.compiler.errors.Locations;\r
-import org.simantics.scl.compiler.internal.codegen.references.IVal;\r
-import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;\r
-import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;\r
-import org.simantics.scl.compiler.internal.interpreted.IExpression;\r
-import org.simantics.scl.compiler.top.ExpressionInterpretationContext;\r
-import org.simantics.scl.compiler.types.Type;\r
-import org.simantics.scl.compiler.types.exceptions.MatchException;\r
-\r
-import gnu.trove.map.hash.TObjectIntHashMap;\r
-import gnu.trove.set.hash.THashSet;\r
-import gnu.trove.set.hash.TIntHashSet;\r
-\r
-public class ECHRRuleset extends Expression {\r
-    CHRRuleset ruleset;\r
-    Expression in;\r
-    \r
-    public ECHRRuleset(CHRRuleset ruleset, Expression in) {\r
-        this.ruleset = ruleset;\r
-        this.in = in;\r
-    }\r
-    \r
-    @Override\r
-    public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {\r
-        ruleset.collectRefs(allRefs, refs);\r
-        in.collectRefs(allRefs, refs);\r
-    }\r
-    @Override\r
-    public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {\r
-        ruleset.collectVars(allVars, vars);\r
-        in.collectVars(allVars, vars);\r
-    }\r
-    @Override\r
-    public void forVariables(VariableProcedure procedure) {\r
-        ruleset.forVariables(procedure);\r
-        in.forVariables(procedure);\r
-    }\r
-    @Override\r
-    protected void updateType() throws MatchException {\r
-        setType(in.getType());\r
-    }\r
-    @Override\r
-    public IVal toVal(Environment env, CodeWriter w) {\r
-        ruleset.generateCode(w);\r
-        return in.toVal(env, w);\r
-    }\r
-    @Override\r
-    public void collectFreeVariables(THashSet<Variable> vars) {\r
-        ruleset.collectFreeVariables(vars);\r
-        in.collectFreeVariables(vars);\r
-    }\r
-    @Override\r
-    public Expression resolve(TranslationContext context) {\r
-        context.pushFrame();\r
-        context.pushCHRConstraintFrame();\r
-        ruleset.resolve(context);\r
-        in = in.resolve(context);\r
-        context.popCHRConstraintFrame(ruleset.constraints);\r
-        context.popFrame();\r
-        return this;\r
-    }\r
-    @Override\r
-    public void setLocationDeep(long loc) {\r
-        if(location == Locations.NO_LOCATION) {\r
-            this.location = loc;\r
-            ruleset.setLocationDeep(loc);\r
-            in.setLocationDeep(loc);\r
-        }\r
-    }\r
-    @Override\r
-    public Expression decorate(ExpressionDecorator decorator) {\r
-        in = in.decorate(decorator);\r
-        return this;\r
-    }\r
-    @Override\r
-    public void collectEffects(THashSet<Type> effects) {\r
-        ruleset.collectEffects(effects);\r
-        in.collectEffects(effects);\r
-    }\r
-    @Override\r
-    public void accept(ExpressionVisitor visitor) {\r
-        visitor.visit(this);\r
-    }\r
-    \r
-    @Override\r
-    public Expression inferType(TypingContext context) {\r
-        ruleset.checkType(context);\r
-        in = in.inferType(context);\r
-        return this;\r
-    }\r
-    \r
-    @Override\r
-    public Expression simplify(SimplificationContext context) {\r
-        ruleset.simplify(context);\r
-        ruleset.compile(context);\r
-        in = in.simplify(context);\r
-        return this;\r
-    }\r
-    \r
-    @Override\r
-    public Expression accept(ExpressionTransformer transformer) {\r
-        return transformer.transform(this);\r
-    }\r
-    \r
-    @Override\r
-    public IExpression toIExpression(ExpressionInterpretationContext context) {\r
-        throw new UnsupportedOperationException();\r
-    }\r
-\r
-}\r
+package org.simantics.scl.compiler.elaboration.expressions;
+
+import org.simantics.scl.compiler.compilation.CompilationContext;
+import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
+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.interpreted.IExpression;
+import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.compiler.types.exceptions.MatchException;
+
+import gnu.trove.map.hash.TObjectIntHashMap;
+import gnu.trove.set.hash.THashSet;
+import gnu.trove.set.hash.TIntHashSet;
+
+public class ECHRRuleset extends Expression {
+    CHRRuleset ruleset;
+    Expression in;
+    
+    public ECHRRuleset(CHRRuleset ruleset, Expression in) {
+        this.ruleset = ruleset;
+        this.in = in;
+    }
+    
+    @Override
+    public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
+        ruleset.collectRefs(allRefs, refs);
+        in.collectRefs(allRefs, refs);
+    }
+    @Override
+    public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
+        ruleset.collectVars(allVars, vars);
+        in.collectVars(allVars, vars);
+    }
+    @Override
+    public void forVariables(VariableProcedure procedure) {
+        ruleset.forVariables(procedure);
+        in.forVariables(procedure);
+    }
+    @Override
+    protected void updateType() throws MatchException {
+        setType(in.getType());
+    }
+    @Override
+    public IVal toVal(CompilationContext context, CodeWriter w) {
+        ruleset.generateCode(w);
+        return in.toVal(context, w);
+    }
+    @Override
+    public void collectFreeVariables(THashSet<Variable> vars) {
+        ruleset.collectFreeVariables(vars);
+        in.collectFreeVariables(vars);
+    }
+    @Override
+    public Expression resolve(TranslationContext context) {
+        if(context.currentRuleset != null) {
+            context.getErrorLog().log(location, "Current version of SCL compiler does not support nested rulesets.");
+            return this;
+        }
+        context.currentRuleset = ruleset;
+        
+        context.pushFrame();
+        context.pushCHRConstraintFrame();
+        ruleset.resolve(context);
+        in = in.resolve(context);
+        context.popCHRConstraintFrame(ruleset.constraints);
+        context.popFrame();
+        
+        context.currentRuleset = null;
+        
+        return this;
+    }
+    @Override
+    public void setLocationDeep(long loc) {
+        if(location == Locations.NO_LOCATION) {
+            this.location = loc;
+            ruleset.setLocationDeep(loc);
+            in.setLocationDeep(loc);
+        }
+    }
+    @Override
+    public void collectEffects(THashSet<Type> effects) {
+        ruleset.collectEffects(effects);
+        in.collectEffects(effects);
+    }
+    @Override
+    public void accept(ExpressionVisitor visitor) {
+        visitor.visit(this);
+    }
+    
+    @Override
+    public Expression inferType(TypingContext context) {
+        ruleset.checkType(context);
+        in = in.inferType(context);
+        return this;
+    }
+    
+    @Override
+    public Expression simplify(SimplificationContext context) {
+        ruleset.simplify(context);
+        ruleset.compile(context);
+        in = in.simplify(context);
+        return this;
+    }
+    
+    @Override
+    public Expression accept(ExpressionTransformer transformer) {
+        return transformer.transform(this);
+    }
+    
+    @Override
+    public IExpression toIExpression(ExpressionInterpretationContext context) {
+        throw new UnsupportedOperationException();
+    }
+
+}