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%2Ftop%2FExpressionEvaluator.java;h=a5b9a65417e79e095acc83149f49d40ffa47690d;hp=d87beec65a7380361a2c45961fa41c2c886b8c18;hb=1ec0193a5a5b8f368b03adb24acd762838ddf8ea;hpb=a8758de5bc19e5adb3f618d3038743a164f09912 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/top/ExpressionEvaluator.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/top/ExpressionEvaluator.java index d87beec65..a5b9a6541 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/top/ExpressionEvaluator.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/top/ExpressionEvaluator.java @@ -27,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; @@ -77,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) { @@ -115,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. */ @@ -175,6 +182,16 @@ 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(); @@ -219,7 +236,7 @@ public class ExpressionEvaluator { ArrayList lvTypes = new ArrayList(); if(expression instanceof EBlock) { EBlock block = (EBlock)expression; - if(localStorage != null && !(block.getStatements().getLast() instanceof GuardStatement)) { + if(localStorage != null && !(block.getLast() instanceof GuardStatement)) { THashSet localVariables = new THashSet(); ListIterator it = block.getStatements().listIterator(); while(it.hasNext()) { @@ -243,9 +260,11 @@ 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]))); } @@ -253,7 +272,7 @@ public class ExpressionEvaluator { { TranslationContext context = new TranslationContext(compilationContext, localEnvironment); expression = expression.resolve(context); - if(!errorLog.isEmpty()) + if(!errorLog.hasNoErrors()) throw new SCLExpressionCompilationException(errorLog.getErrors()); } @@ -278,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 = @@ -288,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(); @@ -319,7 +341,7 @@ public class ExpressionEvaluator { 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)