Commit 11f21a84 authored by Sarah Grebing's avatar Sarah Grebing

Parser-Erweiterung closable and derivable

parent f6054373
Pipeline #12191 failed with stage
in 1 minute and 38 seconds
...@@ -76,8 +76,11 @@ literals : ...@@ -76,8 +76,11 @@ literals :
</pre>*/ </pre>*/
matchPattern matchPattern
: MATCH (pattern=expression :
(USING LBRACKET argList RBRACKET)?) | DERIVABLE TERM_LITERAL | IFCLOSED MATCH (
derivable=DERIVABLE derivableExpression=expression
| (pattern=expression (USING LBRACKET argList RBRACKET)?)
)
; ;
scriptVar scriptVar
...@@ -98,9 +101,17 @@ casesStmt ...@@ -98,9 +101,17 @@ casesStmt
; ;
casesList casesList
: CASE expression COLON? INDENT stmtList DEDENT // : simpleCase
// | closableCase
: CASE (MATCH ISCLOSED | expression) COLON? INDENT stmtList DEDENT
; ;
/*simpleCase
: CASE expression COLON? INDENT stmtList DEDENT
;
closableCase
: CASE MATCH ISCLOSED COLON? INDENT stmtList DEDENT
;
*/
forEachStmt forEachStmt
: FOREACH INDENT stmtList DEDENT : FOREACH INDENT stmtList DEDENT
; ;
...@@ -131,6 +142,8 @@ MULTI_LINE_COMMENT : '/*' (MULTI_LINE_COMMENT|.)*? '*/' -> channel(HIDDEN); ...@@ -131,6 +142,8 @@ MULTI_LINE_COMMENT : '/*' (MULTI_LINE_COMMENT|.)*? '*/' -> channel(HIDDEN);
CASES: 'cases'; CASES: 'cases';
CASE: 'case'; CASE: 'case';
ISCLOSED: 'isCloseable';
DERIVABLE : 'derivable';
DEFAULT: 'default'; DEFAULT: 'default';
ASSIGN : ':='; ASSIGN : ':=';
LBRACKET: '['; LBRACKET: '[';
...@@ -147,8 +160,6 @@ BOOL: 'bool' ; ...@@ -147,8 +160,6 @@ BOOL: 'bool' ;
TERMTYPE : 'term' ;*/ TERMTYPE : 'term' ;*/
FOREACH : 'foreach' ; FOREACH : 'foreach' ;
THEONLY : 'theonly' ; THEONLY : 'theonly' ;
ISCLOSED: 'isClosable';
DERIVABLE : 'derivable';
INDENT : '{' ; INDENT : '{' ;
DEDENT : '}' ; DEDENT : '}' ;
SEMICOLON : ';' ; SEMICOLON : ';' ;
......
...@@ -21,7 +21,7 @@ public class WelcomePane extends AnchorPane { ...@@ -21,7 +21,7 @@ public class WelcomePane extends AnchorPane {
proofScriptDebugger.getWelcomePaneDock().close(); proofScriptDebugger.getWelcomePaneDock().close();
proofScriptDebugger.showActiveInspector(null); proofScriptDebugger.showActiveInspector(null);
proofScriptDebugger.openScript( proofScriptDebugger.openScript(
new File("src/test/resources/edu/kit/formal/interpreter/contraposition/wo_branching.kps") new File("src/test/resources/edu/kit/formal/interpreter/contraposition/w_branching.kps")
); );
proofScriptDebugger.openKeyFile( proofScriptDebugger.openKeyFile(
......
...@@ -54,7 +54,7 @@ public class Evaluator<T> extends DefaultASTVisitor<Value> implements ScopeObser ...@@ -54,7 +54,7 @@ public class Evaluator<T> extends DefaultASTVisitor<Value> implements ScopeObser
} }
/** /**
* TODO Connect with KeY * Visit a match expression and evaluate expression using matcher
* *
* @param match * @param match
* @return * @return
...@@ -64,16 +64,18 @@ public class Evaluator<T> extends DefaultASTVisitor<Value> implements ScopeObser ...@@ -64,16 +64,18 @@ public class Evaluator<T> extends DefaultASTVisitor<Value> implements ScopeObser
if (match.getSignature() != null && !match.getSignature().isEmpty()) { if (match.getSignature() != null && !match.getSignature().isEmpty()) {
throw new IllegalStateException("not supported"); throw new IllegalStateException("not supported");
} }
Value pattern = (Value) match.getPattern().accept(this);
List<VariableAssignment> va = null; List<VariableAssignment> va = null;
if (pattern.getType() == Type.STRING) { Value pattern = (Value) match.getPattern().accept(this);
va = matcher.matchLabel(goal, (String) pattern.getData()); if (match.isDerivable()) {
} else if (pattern.getType() == Type.TERM) { } else {
va = matcher.matchSeq(goal, (String) pattern.getData(), match.getSignature()); if (pattern.getType() == Type.STRING) {
va = matcher.matchLabel(goal, (String) pattern.getData());
} else if (pattern.getType() == Type.TERM) {
va = matcher.matchSeq(goal, (String) pattern.getData(), match.getSignature());
}
} }
return va != null && va.size() > 0 ? Value.TRUE : Value.FALSE; return va != null && va.size() > 0 ? Value.TRUE : Value.FALSE;
} }
......
...@@ -168,15 +168,19 @@ public class Interpreter<T> extends DefaultASTVisitor<Void> ...@@ -168,15 +168,19 @@ public class Interpreter<T> extends DefaultASTVisitor<Void>
List<GoalNode<T>> goalsAfterCases = new ArrayList<>(); List<GoalNode<T>> goalsAfterCases = new ArrayList<>();
//copy the list of goal nodes for keeping track of goals //copy the list of goal nodes for keeping track of goals
Set<GoalNode<T>> remainingGoalsSet = new HashSet<>(allGoalsBeforeCases); Set<GoalNode<T>> remainingGoalsSet = new HashSet<>(allGoalsBeforeCases);
//TODO
//handle cases //handle cases
List<CaseStatement> cases = casesStatement.getCases(); List<CaseStatement> cases = casesStatement.getCases();
for (CaseStatement aCase : cases) { for (CaseStatement aCase : cases) {
Map<GoalNode<T>, VariableAssignment> matchedGoals = if (aCase.isClosedStmt) {
matchGoal(remainingGoalsSet, aCase); System.out.println("IsClosableStmt not implemented yet");
if (matchedGoals != null) { } else {
remainingGoalsSet.removeAll(matchedGoals.keySet()); Map<GoalNode<T>, VariableAssignment> matchedGoals =
goalsAfterCases.addAll(executeCase(aCase.getBody(), matchedGoals)); matchGoal(remainingGoalsSet, (SimpleCaseStatement) aCase);
if (matchedGoals != null) {
remainingGoalsSet.removeAll(matchedGoals.keySet());
goalsAfterCases.addAll(executeCase(aCase.getBody(), matchedGoals));
}
} }
} }
...@@ -219,11 +223,12 @@ public class Interpreter<T> extends DefaultASTVisitor<Void> ...@@ -219,11 +223,12 @@ public class Interpreter<T> extends DefaultASTVisitor<Void>
* @param aCase * @param aCase
* @return * @return
*/ */
private Map<GoalNode<T>, VariableAssignment> matchGoal(Set<GoalNode<T>> allGoalsBeforeCases, CaseStatement aCase) { private Map<GoalNode<T>, VariableAssignment> matchGoal(Set<GoalNode<T>> allGoalsBeforeCases, SimpleCaseStatement aCase) {
HashMap<GoalNode<T>, VariableAssignment> matchedGoals = new HashMap<>(); HashMap<GoalNode<T>, VariableAssignment> matchedGoals = new HashMap<>();
Expression matchExpression = aCase.getGuard(); Expression matchExpression = aCase.getGuard();
for (GoalNode<T> goal : allGoalsBeforeCases) { for (GoalNode<T> goal : allGoalsBeforeCases) {
VariableAssignment va = evaluateMatchInGoal(matchExpression, goal); VariableAssignment va = evaluateMatchInGoal(matchExpression, goal);
if (va != null) { if (va != null) {
......
...@@ -13,4 +13,5 @@ import java.util.List; ...@@ -13,4 +13,5 @@ import java.util.List;
public interface MatcherApi<T> { public interface MatcherApi<T> {
List<VariableAssignment> matchLabel(GoalNode<T> currentState, String label); List<VariableAssignment> matchLabel(GoalNode<T> currentState, String label);
List<VariableAssignment> matchSeq(GoalNode<T> currentState, String data, Signature sig); List<VariableAssignment> matchSeq(GoalNode<T> currentState, String data, Signature sig);
} }
...@@ -221,7 +221,7 @@ public class Debugger { ...@@ -221,7 +221,7 @@ public class Debugger {
} }
@Override @Override
public Void visit(CaseStatement caseStatement) { public Void visit(SimpleCaseStatement caseStatement) {
suffix(caseStatement); suffix(caseStatement);
System.out.println("case " + Facade.prettyPrint(caseStatement.getGuard())); System.out.println("case " + Facade.prettyPrint(caseStatement.getGuard()));
return super.visit(caseStatement); return super.visit(caseStatement);
......
...@@ -110,11 +110,26 @@ public class ASTChanger extends DefaultASTVisitor<ASTNode> { ...@@ -110,11 +110,26 @@ public class ASTChanger extends DefaultASTVisitor<ASTNode> {
return casesStatement; return casesStatement;
} }
@Override
public IsClosableCase visit(IsClosableCase isClosableCase) {
isClosableCase.getBody().accept(this);
return isClosableCase;
}
@Override
public SimpleCaseStatement visit(SimpleCaseStatement simpleCaseStatement) {
simpleCaseStatement.getGuard().accept(this);
simpleCaseStatement.getBody().accept(this);
return simpleCaseStatement;
}
@Override @Override
public CaseStatement visit(CaseStatement caseStatement) { public CaseStatement visit(CaseStatement caseStatement) {
caseStatement.getGuard().accept(this); //caseStatement.getGuard().accept(this);
caseStatement.getBody().accept(this); //caseStatement.getBody().accept(this);
caseStatement.accept(this);
return caseStatement; return caseStatement;
} }
@Override @Override
......
...@@ -104,8 +104,8 @@ public interface ASTTraversal<T> extends Visitor<T> { ...@@ -104,8 +104,8 @@ public interface ASTTraversal<T> extends Visitor<T> {
@Override @Override
default T visit(CaseStatement caseStatement) { default T visit(CaseStatement caseStatement) {
caseStatement.getGuard().accept(this); //caseStatement.getBody().accept(this);
caseStatement.getBody().accept(this); caseStatement.accept(this);
return null; return null;
} }
...@@ -158,4 +158,17 @@ public interface ASTTraversal<T> extends Visitor<T> { ...@@ -158,4 +158,17 @@ public interface ASTTraversal<T> extends Visitor<T> {
e.getExpression().accept(this); e.getExpression().accept(this);
return null; return null;
} }
@Override
default T visit(IsClosableCase isClosableCase) {
isClosableCase.getBody().accept(this);
return null;
}
@Override
default T visit(SimpleCaseStatement simpleCaseStatement) {
simpleCaseStatement.getGuard().accept(this);
simpleCaseStatement.getBody().accept(this);
return null;
}
} }
...@@ -128,5 +128,15 @@ public class DefaultASTVisitor<T> implements Visitor<T> { ...@@ -128,5 +128,15 @@ public class DefaultASTVisitor<T> implements Visitor<T> {
public T visit(UnaryExpression unaryExpression) { public T visit(UnaryExpression unaryExpression) {
return defaultVisit(unaryExpression); return defaultVisit(unaryExpression);
} }
@Override
public T visit(IsClosableCase isClosableCase) {
return defaultVisit(isClosableCase);
}
@Override
public T visit(SimpleCaseStatement simpleCaseStatement) {
return defaultVisit(simpleCaseStatement);
}
} }
...@@ -38,15 +38,13 @@ import java.util.Map; ...@@ -38,15 +38,13 @@ import java.util.Map;
*/ */
public class PrettyPrinter extends DefaultASTVisitor<Void> { public class PrettyPrinter extends DefaultASTVisitor<Void> {
private final StringBuilder s = new StringBuilder();
@Getter @Getter
@Setter @Setter
private int maxWidth = 80; private int maxWidth = 80;
@Getter @Getter
@Setter @Setter
private boolean unicode = true; private boolean unicode = true;
private final StringBuilder s = new StringBuilder();
private int indentation = 0; private int indentation = 0;
@Override @Override
...@@ -168,7 +166,7 @@ public class PrettyPrinter extends DefaultASTVisitor<Void> { ...@@ -168,7 +166,7 @@ public class PrettyPrinter extends DefaultASTVisitor<Void> {
} }
@Override @Override
public Void visit(CaseStatement caseStatement) { public Void visit(SimpleCaseStatement caseStatement) {
s.append("case "); s.append("case ");
caseStatement.getGuard().accept(this); caseStatement.getGuard().accept(this);
s.append(" {"); s.append(" {");
......
...@@ -249,9 +249,16 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -249,9 +249,16 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
public Object visitMatchPattern(ScriptLanguageParser.MatchPatternContext ctx) { public Object visitMatchPattern(ScriptLanguageParser.MatchPatternContext ctx) {
MatchExpression match = new MatchExpression(); MatchExpression match = new MatchExpression();
match.setRuleContext(ctx); match.setRuleContext(ctx);
if (ctx.argList() != null)
match.setSignature((Signature) ctx.argList().accept(this)); if (ctx.derivable != null) {
match.setPattern((Expression) ctx.pattern.accept(this)); match.setDerivable(true);
match.setDerivableTerm((Expression) ctx.derivableExpression.accept(this));
} else {
if (ctx.argList() != null)
match.setSignature((Signature) ctx.argList().accept(this));
match.setPattern((Expression) ctx.pattern.accept(this));
}
return match; return match;
} }
...@@ -283,12 +290,46 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -283,12 +290,46 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
@Override @Override
public Object visitCasesList(ScriptLanguageParser.CasesListContext ctx) { public Object visitCasesList(ScriptLanguageParser.CasesListContext ctx) {
CaseStatement caseStatement = new CaseStatement();
if (ctx.ISCLOSED() != null) {
IsClosableCase isClosableCase = new IsClosableCase();
isClosableCase.setRuleContext(ctx);
isClosableCase.setBody((Statements) ctx.stmtList().accept(this));
return isClosableCase;
} else {
SimpleCaseStatement caseStatement = new SimpleCaseStatement();
caseStatement.setRuleContext(ctx);
caseStatement.setGuard((Expression) ctx.expression().accept(this));
caseStatement.setBody((Statements) ctx.stmtList().accept(this));
return caseStatement;
}
/* CaseStatement caseStatement = new CaseStatement();
caseStatement.setRuleContext(ctx);
caseStatement.setGuard((Expression) ctx.expression().accept(this));
caseStatement.setBody((Statements) ctx.stmtList().accept(this));
return caseStatement;*/
}
/*
@Override
public Object visitSimpleCase(ScriptLanguageParser.SimpleCaseContext ctx) {
SimpleCaseStatement caseStatement = new SimpleCaseStatement();
caseStatement.setRuleContext(ctx); caseStatement.setRuleContext(ctx);
caseStatement.setGuard((Expression) ctx.expression().accept(this)); caseStatement.setGuard((Expression) ctx.expression().accept(this));
caseStatement.setBody((Statements) ctx.stmtList().accept(this)); caseStatement.setBody((Statements) ctx.stmtList().accept(this));
return caseStatement; return caseStatement;
}
@Override
public Object visitClosableCase(ScriptLanguageParser.ClosableCaseContext ctx) {
IsClosableCase isClosableCase = new IsClosableCase();
isClosableCase.setRuleContext(ctx);
isClosableCase.setBody((Statements) ctx.stmtList().accept(this));
return isClosableCase;
} }
*/
@Override @Override
public Object visitForEachStmt(ScriptLanguageParser.ForEachStmtContext ctx) { public Object visitForEachStmt(ScriptLanguageParser.ForEachStmtContext ctx) {
......
...@@ -53,7 +53,7 @@ public interface Visitor<T> { ...@@ -53,7 +53,7 @@ public interface Visitor<T> {
T visit(CasesStatement cases); T visit(CasesStatement cases);
T visit(CaseStatement case_); //T visit(CaseStatement case_);
T visit(CallStatement call); T visit(CallStatement call);
...@@ -70,4 +70,9 @@ public interface Visitor<T> { ...@@ -70,4 +70,9 @@ public interface Visitor<T> {
T visit(UnaryExpression e); T visit(UnaryExpression e);
T visit(IsClosableCase isClosableCase);
T visit(SimpleCaseStatement simpleCaseStatement);
T visit(CaseStatement caseStatement);
} }
...@@ -38,8 +38,8 @@ import lombok.NoArgsConstructor; ...@@ -38,8 +38,8 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class CaseStatement extends Statement<ScriptLanguageParser.CasesListContext> { public class CaseStatement extends Statement<ScriptLanguageParser.CasesListContext> {
private Expression guard; public boolean isClosedStmt;
private Statements body; protected Statements body;
/** /**
* {@inheritDoc} * {@inheritDoc}
...@@ -52,6 +52,6 @@ public class CaseStatement extends Statement<ScriptLanguageParser.CasesListConte ...@@ -52,6 +52,6 @@ public class CaseStatement extends Statement<ScriptLanguageParser.CasesListConte
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override public CaseStatement copy() { @Override public CaseStatement copy() {
return new CaseStatement(guard.copy(), body.copy()); return new CaseStatement(body.copy(), isClosedStmt);
} }
} }
package edu.kit.formal.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.Visitor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Object representing a "match isClosable{ Commands}" block
*/
@Data
@NoArgsConstructor
public class IsClosableCase extends CaseStatement {
private boolean isClosedStmt = true;
public IsClosableCase(Statements body) {
this.body = body;
}
/**
* {@inheritDoc}
*/
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
/**
* {@inheritDoc}
*/
@Override
public IsClosableCase copy() {
return new IsClosableCase(body.copy());
}
}
...@@ -27,6 +27,8 @@ import edu.kit.formal.proofscriptparser.NotWelldefinedException; ...@@ -27,6 +27,8 @@ import edu.kit.formal.proofscriptparser.NotWelldefinedException;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser; import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formal.proofscriptparser.Visitor; import edu.kit.formal.proofscriptparser.Visitor;
import lombok.Data; import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/** /**
* A match expression contains an argument and a uses clause. * A match expression contains an argument and a uses clause.
...@@ -38,6 +40,12 @@ import lombok.Data; ...@@ -38,6 +40,12 @@ import lombok.Data;
public class MatchExpression extends Expression<ScriptLanguageParser.MatchPatternContext> { public class MatchExpression extends Expression<ScriptLanguageParser.MatchPatternContext> {
private Signature signature = new Signature(); private Signature signature = new Signature();
private Expression pattern; private Expression pattern;
@Getter
@Setter
private boolean isDerivable;
@Getter
@Setter
private Expression derivableTerm;
/** /**
* {@inheritDoc} * {@inheritDoc}
......
package edu.kit.formal.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.Visitor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Created by sarah on 7/17/17.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SimpleCaseStatement extends CaseStatement {
private Expression guard;
private boolean isClosedStmt = false;
public SimpleCaseStatement(Expression guard, Statements body) {
this.guard = guard;
this.body = body;
}
/**
* {@inheritDoc}
*/
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
/**
* {@inheritDoc}
*/
@Override
public SimpleCaseStatement copy() {
return new SimpleCaseStatement(guard.copy(), body.copy());
}
}
script test(){
impRight;
impRight;
cases{
case match isCloseable:{
notLeft;
notRight;
replace_known_left occ='2';
concrete_impl_1;
close;
}
default:{
auto;
}
}