From: Hannu Niemistö Date: Fri, 20 Jan 2017 11:47:00 +0000 (+0200) Subject: Added a unit test for bug 6989 and some methods to print expressions X-Git-Tag: v1.27.0~9 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=655590362c7017aff657d1ff30e6c63f03b6dd75;p=simantics%2Fplatform.git Added a unit test for bug 6989 and some methods to print expressions refs #6989 Change-Id: Ia79a14ebf9acd9c5d1fb62dbb921db684746f316 --- diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ASTExpression.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ASTExpression.java index ac37f4a14..fbc0a7901 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ASTExpression.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ASTExpression.java @@ -54,7 +54,7 @@ public abstract class ASTExpression extends SimplifiableExpression { } @Override - final public void accept(ExpressionVisitor visitor) { + public void accept(ExpressionVisitor visitor) { throw new InternalCompilerError(getClass().getSimpleName() + " does not support accept."); } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBinary.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBinary.java index 274624aee..465c5c8ff 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBinary.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBinary.java @@ -234,5 +234,9 @@ public class EBinary extends ASTExpression { public Expression accept(ExpressionTransformer transformer) { return transformer.transform(this); } - + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java index ebe88b499..c5f30417e 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java @@ -128,4 +128,9 @@ public class EBlock extends ASTExpression { return 0; return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity(); } + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EPreLet.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EPreLet.java index c78b9f457..035a2cadc 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EPreLet.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EPreLet.java @@ -98,5 +98,9 @@ public class EPreLet extends ASTExpression { public int getSyntacticFunctionArity() { return in.getSyntacticFunctionArity(); } - + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERange.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERange.java index 3ea3d5e6c..6fbcdafec 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERange.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERange.java @@ -6,8 +6,8 @@ import org.simantics.scl.compiler.elaboration.modules.SCLValue; import org.simantics.scl.compiler.errors.Locations; public class ERange extends ASTExpression { - Expression from; - Expression to; + public Expression from; + public Expression to; public ERange(Expression from, Expression to) { this.from = from; @@ -35,5 +35,9 @@ public class ERange extends ASTExpression { public Expression accept(ExpressionTransformer transformer) { return transformer.transform(this); } - + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERecord.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERecord.java index 32999dcb4..6ef9e9e72 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERecord.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ERecord.java @@ -121,5 +121,9 @@ public class ERecord extends ASTExpression { public Expression accept(ExpressionTransformer transformer) { return transformer.transform(this); } - + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EStringLiteral.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EStringLiteral.java index fa85d8bfe..58ec41fd2 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EStringLiteral.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EStringLiteral.java @@ -8,8 +8,8 @@ import org.simantics.scl.compiler.errors.Locations; public class EStringLiteral extends ASTExpression { - String[] strings; - Expression[] expressions; + public String[] strings; + public Expression[] expressions; public EStringLiteral(String[] strings, Expression[] expressions) { this.strings = strings; @@ -50,5 +50,8 @@ public class EStringLiteral extends ASTExpression { return new EApply(new ELiteral(new StringInterpolation(strings)), expressions); } - + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EVar.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EVar.java index f1c3e31e0..a47d3e0dd 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EVar.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EVar.java @@ -2,6 +2,7 @@ package org.simantics.scl.compiler.elaboration.expressions; import java.util.ArrayList; +import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; import org.simantics.scl.compiler.elaboration.contexts.TranslationContext; import org.simantics.scl.compiler.elaboration.errors.NotPatternException; import org.simantics.scl.compiler.elaboration.expressions.lhstype.FunctionDefinitionLhs; @@ -79,4 +80,9 @@ public class EVar extends ASTExpression { public Expression accept(ExpressionTransformer transformer) { return transformer.transform(this); } + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ExpressionVisitor.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ExpressionVisitor.java index 00e5f1187..62e21f204 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ExpressionVisitor.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ExpressionVisitor.java @@ -5,7 +5,9 @@ public interface ExpressionVisitor { void visit(EApply expression); void visit(EApplyType expression); void visit(EAsPattern expression); + void visit(EBinary expression); void visit(EBind expression); + void visit(EBlock expression); void visit(ECHRRuleset echrRuleset); void visit(EConstant expression); void visit(ECoveringBranchPoint expression); @@ -25,13 +27,18 @@ public interface ExpressionVisitor { void visit(ELiteral expression); void visit(EMatch expression); void visit(EPlaceholder expression); + void visit(EPreLet expression); + void visit(ERange expression); void visit(ERealLiteral expression); + void visit(ERecord expression); void visit(ERuleset expression); void visit(ESelect expression); void visit(ESimpleLambda expression); void visit(ESimpleLet expression); + void visit(EStringLiteral expression); void visit(ETransformation expression); void visit(ETypeAnnotation expression); + void visit(EVar expression); void visit(EVariable expression); void visit(EViewPattern expression); void visit(EWhen expression); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/StandardExpressionVisitor.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/StandardExpressionVisitor.java index 6bdd7abde..401150db5 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/StandardExpressionVisitor.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/StandardExpressionVisitor.java @@ -12,12 +12,19 @@ import org.simantics.scl.compiler.elaboration.expressions.accessor.FieldAccessor import org.simantics.scl.compiler.elaboration.expressions.accessor.FieldAccessorVisitor; import org.simantics.scl.compiler.elaboration.expressions.accessor.IdAccessor; import org.simantics.scl.compiler.elaboration.expressions.accessor.StringAccessor; +import org.simantics.scl.compiler.elaboration.expressions.block.BindStatement; +import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement; +import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement; +import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement; +import org.simantics.scl.compiler.elaboration.expressions.block.Statement; +import org.simantics.scl.compiler.elaboration.expressions.block.StatementVisitor; import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment; import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator; import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard; import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifierVisitor; import org.simantics.scl.compiler.elaboration.expressions.list.ListSeq; import org.simantics.scl.compiler.elaboration.expressions.list.ListThen; +import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment; import org.simantics.scl.compiler.elaboration.query.QAlternative; import org.simantics.scl.compiler.elaboration.query.QAtom; import org.simantics.scl.compiler.elaboration.query.QConjunction; @@ -32,7 +39,7 @@ import org.simantics.scl.compiler.elaboration.query.QueryVisitor; public class StandardExpressionVisitor implements ExpressionVisitor, QueryVisitor, FieldAccessorVisitor, ListQualifierVisitor, -EquationVisitor { +EquationVisitor, StatementVisitor { @Override public void visit(EApply expression) { @@ -211,6 +218,10 @@ EquationVisitor { expression.value.accept(this); } + @Override + public void visit(EVar expression) { + } + @Override public void visit(EVariable expression) { } @@ -356,4 +367,67 @@ EquationVisitor { ruleset.in.accept(this); } + @Override + public void visit(EBinary expression) { + expression.left.accept(this); + for(EBinaryRightSide right : expression.rights) + right.right.accept(this); + } + + @Override + public void visit(EBlock expression) { + for(Statement stat : expression.statements) + stat.accept(this); + } + + @Override + public void visit(EPreLet expression) { + for(LetStatement stat : expression.assignments) { + stat.pattern.accept(this); + stat.value.accept(this); + } + expression.in.accept(this); + } + + @Override + public void visit(ERange expression) { + expression.from.accept(this); + expression.to.accept(this); + } + + @Override + public void visit(ERecord expression) { + for(FieldAssignment assignment : expression.fields) + assignment.value.accept(this); + + } + + @Override + public void visit(EStringLiteral expression) { + for(Expression exp : expression.expressions) + exp.accept(this); + } + + @Override + public void visit(BindStatement statement) { + statement.pattern.accept(this); + statement.value.accept(this); + } + + @Override + public void visit(GuardStatement statement) { + statement.value.accept(this); + } + + @Override + public void visit(LetStatement statement) { + statement.value.accept(this); + } + + @Override + public void visit(RuleStatement statement) { + statement.head.accept(this); + statement.body.accept(this); + } + } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/printing/ExpressionToStringVisitor.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/printing/ExpressionToStringVisitor.java index ac347385a..59e7d8909 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/printing/ExpressionToStringVisitor.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/printing/ExpressionToStringVisitor.java @@ -11,7 +11,9 @@ import org.simantics.scl.compiler.elaboration.expressions.Case; import org.simantics.scl.compiler.elaboration.expressions.EApply; import org.simantics.scl.compiler.elaboration.expressions.EApplyType; import org.simantics.scl.compiler.elaboration.expressions.EAsPattern; +import org.simantics.scl.compiler.elaboration.expressions.EBinary; import org.simantics.scl.compiler.elaboration.expressions.EBind; +import org.simantics.scl.compiler.elaboration.expressions.EBlock; import org.simantics.scl.compiler.elaboration.expressions.ECHRRuleset; import org.simantics.scl.compiler.elaboration.expressions.EConstant; import org.simantics.scl.compiler.elaboration.expressions.ECoveringBranchPoint; @@ -31,13 +33,18 @@ import org.simantics.scl.compiler.elaboration.expressions.EListLiteral; import org.simantics.scl.compiler.elaboration.expressions.ELiteral; import org.simantics.scl.compiler.elaboration.expressions.EMatch; import org.simantics.scl.compiler.elaboration.expressions.EPlaceholder; +import org.simantics.scl.compiler.elaboration.expressions.EPreLet; +import org.simantics.scl.compiler.elaboration.expressions.ERange; import org.simantics.scl.compiler.elaboration.expressions.ERealLiteral; +import org.simantics.scl.compiler.elaboration.expressions.ERecord; import org.simantics.scl.compiler.elaboration.expressions.ERuleset; import org.simantics.scl.compiler.elaboration.expressions.ESelect; import org.simantics.scl.compiler.elaboration.expressions.ESimpleLambda; import org.simantics.scl.compiler.elaboration.expressions.ESimpleLet; +import org.simantics.scl.compiler.elaboration.expressions.EStringLiteral; import org.simantics.scl.compiler.elaboration.expressions.ETransformation; import org.simantics.scl.compiler.elaboration.expressions.ETypeAnnotation; +import org.simantics.scl.compiler.elaboration.expressions.EVar; import org.simantics.scl.compiler.elaboration.expressions.EVariable; import org.simantics.scl.compiler.elaboration.expressions.EViewPattern; import org.simantics.scl.compiler.elaboration.expressions.EWhen; @@ -384,6 +391,11 @@ public class ExpressionToStringVisitor implements ExpressionVisitor, QueryVisito expression.getValue().accept(this); } + @Override + public void visit(EVar expression) { + b.append(expression.name); + } + @Override public void visit(EVariable expression) { show(expression.getVariable()); @@ -545,4 +557,47 @@ public class ExpressionToStringVisitor implements ExpressionVisitor, QueryVisito showPar(parameter); } } + + @Override + public void visit(EBinary expression) { + b.append(""); + } + + @Override + public void visit(EBlock expression) { + b.append(""); + } + + @Override + public void visit(EPreLet expression) { + b.append(""); + } + + @Override + public void visit(ERange expression) { + b.append('['); + expression.from.accept(this); + b.append(".."); + expression.to.accept(this); + b.append(']'); + } + + @Override + public void visit(ERecord expression) { + b.append(""); + } + + @Override + public void visit(EStringLiteral expression) { + b.append('"'); + for(int i=0;i