]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/top/ExpressionEvaluator.java
Merge "Resolve some dependency problems with SDK features"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / top / ExpressionEvaluator.java
index 39f0ac721ee2a2bd32d366a9e17123e6c9ab6e74..a5b9a65417e79e095acc83149f49d40ffa47690d 100644 (file)
@@ -8,6 +8,7 @@ import java.util.Map;
 
 import org.simantics.scl.compiler.common.names.Name;
 import org.simantics.scl.compiler.compilation.CodeGeneration;
+import org.simantics.scl.compiler.compilation.CompilationContext;
 import org.simantics.scl.compiler.constants.JavaStaticMethod;
 import org.simantics.scl.compiler.constants.SCLConstant;
 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
@@ -26,6 +27,7 @@ import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
 import org.simantics.scl.compiler.elaboration.java.Builtins;
 import org.simantics.scl.compiler.environment.Environment;
 import org.simantics.scl.compiler.environment.LocalEnvironment;
+import org.simantics.scl.compiler.errors.CompilationError;
 import org.simantics.scl.compiler.errors.ErrorLog;
 import org.simantics.scl.compiler.internal.codegen.references.IVal;
 import org.simantics.scl.compiler.internal.codegen.ssa.SSAModule;
@@ -76,6 +78,7 @@ public class ExpressionEvaluator {
     private LocalStorage localStorage;
     private boolean interpretIfPossible = true;
     private ExpressionParseMode parseMode = ExpressionParseMode.EXPRESSION;
+    private boolean validateOnly;
     
     public ExpressionEvaluator(RuntimeEnvironment runtimeEnvironment,
             String expressionText) {
@@ -114,6 +117,11 @@ public class ExpressionEvaluator {
         return this;
     }
     
+    public ExpressionEvaluator validateOnly(boolean validateOnly) {
+        this.validateOnly = validateOnly;
+        return this;
+    }
+    
     /**
      * Sets a local environment that can arbitrarily modify the resolving of the expression.
      */
@@ -174,15 +182,27 @@ public class ExpressionEvaluator {
             return "store_" + name;
         }
     }
+    
+    public CompilationError[] validate() {
+        try {
+            validateOnly = true;
+            eval();
+            return CompilationError.EMPTY_ARRAY;
+        } catch(SCLExpressionCompilationException e) {
+            return e.getErrors();
+        }
+    }
 
     public Object eval() throws SCLExpressionCompilationException {
         fillDefaults();
         
-        final ErrorLog errorLog = new ErrorLog();
+        final CompilationContext compilationContext = new CompilationContext();
+        final ErrorLog errorLog = compilationContext.errorLog;
         final Environment environment = runtimeEnvironment.getEnvironment();
+        compilationContext.environment = environment;
         
         // Parse expression
-        if(expressionText != null && !expressionText.trim().isEmpty()) {
+        if(expressionText != null) {
             try {
                 switch(parseMode) {
                 case BLOCK: {
@@ -216,7 +236,7 @@ public class ExpressionEvaluator {
         ArrayList<Type> lvTypes = new ArrayList<Type>(); 
         if(expression instanceof EBlock) {
             EBlock block = (EBlock)expression;
-            if(localStorage != null && !(block.getStatements().getLast() instanceof GuardStatement)) {
+            if(localStorage != null && !(block.getLast() instanceof GuardStatement)) {
                 THashSet<String> localVariables = new THashSet<String>();
                 ListIterator<Statement> it = block.getStatements().listIterator();
                 while(it.hasNext()) {
@@ -240,18 +260,19 @@ public class ExpressionEvaluator {
                                     Types.functionE(type, Types.PROC, Types.UNIT)),
                                     new EVar(variableName)
                             )));
+                    if(validateOnly)
+                        localStorage.store(variableName, null, type);
                 }
             }
-            if(!(block.getStatements().getLast() instanceof GuardStatement))
+            if(!(block.getLast() instanceof GuardStatement))
                 block.addStatement(new GuardStatement(new EConstant(Builtins.TUPLE_CONSTRUCTORS[0])));
         }
         
         // Elaboration
         {
-            TranslationContext context = new TranslationContext(errorLog,
-                    environment, localEnvironment);
+            TranslationContext context = new TranslationContext(compilationContext, localEnvironment);
             expression = expression.resolve(context);
-            if(!errorLog.isEmpty())
+            if(!errorLog.hasNoErrors())
                 throw new SCLExpressionCompilationException(errorLog.getErrors());
         }
         
@@ -265,7 +286,7 @@ public class ExpressionEvaluator {
         
         // Type checking
         {
-            TypingContext context = new TypingContext(errorLog, environment);
+            TypingContext context = new TypingContext(compilationContext);
 
             context.pushEffectUpperBound(expression.location, expectedEffect);
             expression = expression.checkType(context, expectedType);
@@ -276,7 +297,7 @@ public class ExpressionEvaluator {
             
             expectedType.addPolarity(Polarity.POSITIVE);
             context.solveSubsumptions(expression.location);
-            if(!errorLog.isEmpty())
+            if(!errorLog.hasNoErrors())
                 throw new SCLExpressionCompilationException(errorLog.getErrors());
             if(decorateExpression && Types.canonical(expectedEffect) != Types.NO_EFFECTS) {
                 ExpressionDecorator decorator =
@@ -286,11 +307,14 @@ public class ExpressionEvaluator {
             expression = context.solveConstraints(environment, expression);
             expressionType = expression.getType();
             
-            if(!errorLog.isEmpty())
+            if(!errorLog.hasNoErrors())
                 throw new SCLExpressionCompilationException(errorLog.getErrors());
 
             if(localEnvironment != null)
                 expression = localEnvironment.postDecorateExpression(expression);
+            
+            if(validateOnly)
+                return null;
 
             Type type = expression.getType();
             type = type.convertMetaVarsToVars();
@@ -306,17 +330,18 @@ public class ExpressionEvaluator {
         MutableClassLoader classLoader = runtimeEnvironment.getMutableClassLoader();
         String moduleName = classLoader.getFreshPackageName();
         JavaTypeTranslator javaTypeTranslator = new JavaTypeTranslator(environment);
+        compilationContext.javaTypeTranslator = javaTypeTranslator;
         JavaNamingPolicy namingPolicy = new JavaNamingPolicy(moduleName);
+        compilationContext.namingPolicy = namingPolicy;
 
         ModuleBuilder moduleBuilder = new ModuleBuilder(namingPolicy, javaTypeTranslator);
         
         // Simplify
         SimplificationContext context = 
-                new SimplificationContext(environment, errorLog, 
-                        javaTypeTranslator, DummyJavaReferenceValidator.INSTANCE);
+                new SimplificationContext(compilationContext, DummyJavaReferenceValidator.INSTANCE);
         expression = expression.simplify(context);
         
-        if(!errorLog.isEmpty())
+        if(!errorLog.hasNoErrors())
             throw new SCLExpressionCompilationException(errorLog.getErrors());
         
         if(SCLCompilerConfiguration.SHOW_EXPRESSION_BEFORE_EVALUATION)