Commit 667d3340 authored by Sarah Grebing's avatar Sarah Grebing

further bug fixes in variable assignments, added key.core.jar and dependencies

parent d346acc2
Pipeline #10594 failed with stage
in 54 seconds
<component name="libraryTable">
<library name="Maven: antlr:antlr:2.7.7">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/antlr/antlr/2.7.7/antlr-2.7.7.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/antlr/antlr/2.7.7/antlr-2.7.7-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/antlr/antlr/2.7.7/antlr-2.7.7-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: net.java.dev.javacc:javacc:4.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/net/java/dev/javacc/javacc/4.0/javacc-4.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/net/java/dev/javacc/javacc/4.0/javacc-4.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/net/java/dev/javacc/javacc/4.0/javacc-4.0-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: org.antlr:antlr:3.5.2">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/antlr/antlr/3.5.2/antlr-3.5.2.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/antlr/antlr/3.5.2/antlr-3.5.2-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/antlr/antlr/3.5.2/antlr-3.5.2-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: org.key-project:key.core:2.7">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/key-project/key.core/2.7/key.core-2.7.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/key-project/key.core/2.7/key.core-2.7-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/key-project/key.core/2.7/key.core-2.7-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: org.key-project:recoderKey:1.0.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/key-project/recoderKey/1.0.0/recoderKey-1.0.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/key-project/recoderKey/1.0.0/recoderKey-1.0.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/key-project/recoderKey/1.0.0/recoderKey-1.0.0-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
...@@ -118,6 +118,31 @@ ...@@ -118,6 +118,31 @@
<version>1.16.16</version> <version>1.16.16</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.key-project</groupId>
<artifactId>key.core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>net.java.dev.javacc</groupId>
<artifactId>javacc</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.key-project</groupId>
<artifactId>recoderKey</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies> </dependencies>
......
...@@ -5,10 +5,12 @@ grammar ScriptLanguage; ...@@ -5,10 +5,12 @@ grammar ScriptLanguage;
: stmtList : stmtList
; ;
*/ */
start start
: (SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT)* : (script)*
; ;
script: SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT;
argList argList
: varDecl (',' varDecl)* : varDecl (',' varDecl)*
; ;
...@@ -21,10 +23,10 @@ stmtList ...@@ -21,10 +23,10 @@ stmtList
: statement* : statement*
; ;
statement statement
: //scriptDecl : //scriptDecl
varDecl assignment
| assignment
| repeatStmt | repeatStmt
| casesStmt | casesStmt
| forEachStmt | forEachStmt
...@@ -39,7 +41,8 @@ statement ...@@ -39,7 +41,8 @@ statement
; ;
*/ */
assignment assignment
: variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON : variable=ID COLON type=ID SEMICOLON
| variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON
; ;
expression expression
......
package edu.kit.formal.TestCommands;
import edu.kit.formal.interpreter.State;
/**
* Created by sarah on 5/17/17.
*/
public abstract class AbstractCommand {
public abstract State execute(State s);
}
package edu.kit.formal.TestCommands;
import edu.kit.formal.interpreter.State;
/**
* TestCommand
* Created by sarah on 5/17/17.
*/
public final class PrintCommand extends AbstractCommand {
public State execute(State s) {
System.out.println("Printing state " + s + " " + s.getSelectedGoalNode().toString());
return s;
}
}
package edu.kit.formal.TestCommands;
import edu.kit.formal.interpreter.GoalNode;
import edu.kit.formal.interpreter.State;
/**
* Created by sarah on 5/17/17.
*/
public class SplitCommand extends AbstractCommand {
@Override
public State execute(State s) {
GoalNode g = s.getSelectedGoalNode();
String seq1 = g.getSequent().concat("1");
String seq2 = g.getSequent().concat("2");
s.getGoals().remove(g);
s.getGoals().add(new GoalNode(g, seq1));
s.getGoals().add(new GoalNode(g, seq2));
return s;
}
}
...@@ -33,7 +33,9 @@ public class GoalNode { ...@@ -33,7 +33,9 @@ public class GoalNode {
} }
public String toString() { public String toString() {
return sequent; String s = "Seq: " + sequent + "\n" +
assignments.toString();
return s;
} }
/** /**
...@@ -41,7 +43,7 @@ public class GoalNode { ...@@ -41,7 +43,7 @@ public class GoalNode {
* @return value of variable if it exists * @return value of variable if it exists
*/ */
public Value lookupVarValue(String varname) { public Value lookupVarValue(String varname) {
Value v = assignments.getValue(varname); Value v = assignments.lookupVarValue(varname);
if (v != null) { if (v != null) {
return v; return v;
} else { } else {
...@@ -59,26 +61,28 @@ public class GoalNode { ...@@ -59,26 +61,28 @@ public class GoalNode {
*/ */
public Type lookUpType(String id) { public Type lookUpType(String id) {
Type t = this.getAssignments().getTypes().get(id); Type t = this.getAssignments().lookupType(id);
if (t == null) { if (t == null) {
//TODO lookup parent and outer Scope throw new RuntimeException("Variable " + id + " must be declared first");
// this.getAssignments().
} else { } else {
return t; return t;
} }
return null;
} }
/** /**
* Add a variable declaration to the type map * Add a variable declaration to the type map (TODO Default value in map?)
* TODO default value in valuemap?
*
* @param name * @param name
* @param t * @param t
*/ */
public void addVarDecl(String name, Type t) { public void addVarDecl(String name, Type t) {
getAssignments().addVariable(name, t); VariableAssignment assignments = this.getAssignments().addVarDecl(name, t);
if (assignments == null) {
throw new RuntimeException("Could not add var decl " + name);
} else {
this.assignments = assignments;
}
} }
/** /**
...@@ -89,11 +93,8 @@ public class GoalNode { ...@@ -89,11 +93,8 @@ public class GoalNode {
*/ */
public void setVarValue(String name, Value v) { public void setVarValue(String name, Value v) {
VariableAssignment assignments = getAssignments(); VariableAssignment assignments = getAssignments();
if (assignments.getTypes().containsKey(name)) { assignments.setVarValue(name, v);
assignments.setVar(name, v);
} else {
throw new RuntimeException("Variable " + name + " has to be declared first");
}
} }
/** /**
......
package edu.kit.formal.interpreter; package edu.kit.formal.interpreter;
import edu.kit.formal.TestCommands.AbstractCommand;
import edu.kit.formal.TestCommands.PrintCommand;
import edu.kit.formal.TestCommands.SplitCommand;
import edu.kit.formal.proofscriptparser.DefaultASTVisitor; import edu.kit.formal.proofscriptparser.DefaultASTVisitor;
import edu.kit.formal.proofscriptparser.ast.*; import edu.kit.formal.proofscriptparser.ast.*;
...@@ -11,13 +14,16 @@ import java.util.*; ...@@ -11,13 +14,16 @@ import java.util.*;
* @author S.Grebing * @author S.Grebing
*/ */
public class Interpreter<T> extends DefaultASTVisitor<T> { public class Interpreter<T> extends DefaultASTVisitor<T> {
//TODO later also inclulde information about source line for each state (for debugging purposes and rewind purposes) //TODO later also include information about source line for each state (for debugging purposes and rewind purposes)
public Stack<AbstractState> stateStack; public Stack<AbstractState> stateStack;
public HashMap<String, ProofScript> localCommands; public HashMap<String, ProofScript> localCommands;
public HashMap<String, AbstractCommand> commands = new HashMap<>();
public Interpreter() { public Interpreter() {
localCommands = new LinkedHashMap<>(); localCommands = new LinkedHashMap<>();
commands.put("printState", new PrintCommand());
commands.put("splitState", new SplitCommand());
} }
//starting point is a statement list //starting point is a statement list
...@@ -40,7 +46,7 @@ public class Interpreter<T> extends DefaultASTVisitor<T> { ...@@ -40,7 +46,7 @@ public class Interpreter<T> extends DefaultASTVisitor<T> {
} }
/** /**
* If new Block is entered, a new state has to be created (copy of current state) and opushed to the stack * If new Block is entered, a new state has to be created (copy of current state) and pushed to the stack
*/ */
private void enterScope() { private void enterScope() {
...@@ -63,10 +69,11 @@ public class Interpreter<T> extends DefaultASTVisitor<T> { ...@@ -63,10 +69,11 @@ public class Interpreter<T> extends DefaultASTVisitor<T> {
*/ */
@Override @Override
public T visit(ProofScript proofScript) { public T visit(ProofScript proofScript) {
//AbstractState currentState = stateStack.pop();
System.out.println("Visiting " + proofScript.getName()); System.out.println("Visiting " + proofScript.getName());
//add vars //add vars
visit(proofScript.getSignature()); visit(proofScript.getSignature());
System.out.println("Visited Signature");
Statements body = proofScript.getBody(); Statements body = proofScript.getBody();
visit(body); visit(body);
return null; return null;
...@@ -82,16 +89,20 @@ public class Interpreter<T> extends DefaultASTVisitor<T> { ...@@ -82,16 +89,20 @@ public class Interpreter<T> extends DefaultASTVisitor<T> {
System.out.println("Visiting Assignment " + assignmentStatement.toString()); System.out.println("Visiting Assignment " + assignmentStatement.toString());
AbstractState state = stateStack.pop(); AbstractState state = stateStack.pop();
GoalNode node = state.getSelectedGoalNode(); GoalNode node = state.getSelectedGoalNode();
Type t = assignmentStatement.getType();
Variable var = assignmentStatement.getLhs(); Variable var = assignmentStatement.getLhs();
Expression expr = assignmentStatement.getRhs(); Expression expr = assignmentStatement.getRhs();
if (node != null) { if (t != null) {
Type t = node.lookUpType(var.getIdentifier()); node.addVarDecl(var.getIdentifier(), t);
if (t != null) { }
if (expr != null) {
Type type = node.lookUpType(var.getIdentifier());
if (type == null) {
throw new RuntimeException("Type of Variable " + var.getIdentifier() + " is not declared yet");
} else {
Evaluator eval = new Evaluator(state.getSelectedGoalNode()); Evaluator eval = new Evaluator(state.getSelectedGoalNode());
Value v = (Value) expr.accept(eval); Value v = (Value) expr.accept(eval);
node.getAssignments().setVar(var.getIdentifier(), v); node.setVarValue(var.getIdentifier(), v);
} else {
throw new RuntimeException("Assignment problem");
} }
} }
stateStack.push(state); stateStack.push(state);
...@@ -118,9 +129,36 @@ public class Interpreter<T> extends DefaultASTVisitor<T> { ...@@ -118,9 +129,36 @@ public class Interpreter<T> extends DefaultASTVisitor<T> {
*/ */
@Override @Override
public T visit(CasesStatement casesStatement) { public T visit(CasesStatement casesStatement) {
//neuerScope State beforeCases = (State) stateStack.pop();
casesStatement.getCases(); //enterscope
List<GoalNode> allGoalsBeforeCases = beforeCases.getGoals();
for (GoalNode node : allGoalsBeforeCases) {
node.enterNewVarScope();
}
//copy the list of goal nodes for keeping track of goals
List<GoalNode> copiedList = new ArrayList<>();
for (GoalNode goalNode : allGoalsBeforeCases) {
copiedList.add(goalNode);
}
//handle cases TODO
List<CaseStatement> cases = casesStatement.getCases();
Iterator<CaseStatement> casesIter = cases.iterator();
while (casesIter.hasNext()) {
CaseStatement currentCase = casesIter.next();
currentCase.getGuard();
}
casesStatement.getDefaultCase(); casesStatement.getDefaultCase();
//exit scope
State aftercases = (State) stateStack.pop();
List<GoalNode> goalsAfterCases = aftercases.getGoals();
if (!goalsAfterCases.isEmpty()) {
for (GoalNode goalAfterCases : goalsAfterCases) {
goalAfterCases.exitNewVarScope();
}
}
return null; return null;
} }
...@@ -140,15 +178,18 @@ public class Interpreter<T> extends DefaultASTVisitor<T> { ...@@ -140,15 +178,18 @@ public class Interpreter<T> extends DefaultASTVisitor<T> {
* 1) saving the context onto the stack and creating a copy of the state and push it onto the stack * 1) saving the context onto the stack and creating a copy of the state and push it onto the stack
* 2) adding new Variable Assignments to te selected goal * 2) adding new Variable Assignments to te selected goal
* 3) adding the assigned parameters to the variable assignments * 3) adding the assigned parameters to the variable assignments
* 4) visting the body respec. letting the handler take over * 4) visiting the body respec. letting the handler take over
* 5) removing the top element form the stack * 5) removing the top element form the stack
* @param call * @param call
* @return * @return
*/ */
@Override @Override
public T visit(CallStatement call) { public T visit(CallStatement call) {
//neuer scope //neuer VarScope
State newState = stateStack.peek().copy(); State newState = (State) stateStack.pop();
//enter new variable scope
newState.getSelectedGoalNode().enterNewVarScope();
stateStack.push(newState);
Evaluator eval = new Evaluator(newState.getSelectedGoalNode()); Evaluator eval = new Evaluator(newState.getSelectedGoalNode());
String commandName = call.getCommand(); String commandName = call.getCommand();
...@@ -172,9 +213,17 @@ public class Interpreter<T> extends DefaultASTVisitor<T> { ...@@ -172,9 +213,17 @@ public class Interpreter<T> extends DefaultASTVisitor<T> {
newState.getSelectedGoalNode().enterNewVarScope(); newState.getSelectedGoalNode().enterNewVarScope();
stateStack.push(newState); stateStack.push(newState);
visit(commandScript.getBody()); visit(commandScript.getBody());
stateStack.pop().getSelectedGoalNode().exitNewVarScope(); stateStack.peek().getSelectedGoalNode().exitNewVarScope();
} else { } else {
throw new RuntimeException("Command " + commandName + " is not known"); if (commands.containsKey(commandName)) {
AbstractCommand com = commands.get(commandName);
State current = (State) stateStack.pop();
State afterCom = com.execute(current);
afterCom.getSelectedGoalNode().exitNewVarScope();
stateStack.push(afterCom);
} else {
throw new RuntimeException("Command " + commandName + " is not known");
}
} }
return null; return null;
...@@ -236,6 +285,8 @@ public class Interpreter<T> extends DefaultASTVisitor<T> { ...@@ -236,6 +285,8 @@ public class Interpreter<T> extends DefaultASTVisitor<T> {
@Override @Override
public T visit(Parameters parameters) { public T visit(Parameters parameters) {
System.out.println("Params " + parameters.toString());
return null; return null;
} }
......
...@@ -61,8 +61,6 @@ public class State extends AbstractState { ...@@ -61,8 +61,6 @@ public class State extends AbstractState {
public State copy() { public State copy() {
List<GoalNode> copiedGoals = new ArrayList<>(); List<GoalNode> copiedGoals = new ArrayList<>();
GoalNode refToSelGoal = selectedGoalNode; GoalNode refToSelGoal = selectedGoalNode;
return new State(copiedGoals, refToSelGoal); return new State(copiedGoals, refToSelGoal);
} }
......
...@@ -38,7 +38,15 @@ public class VariableAssignment { ...@@ -38,7 +38,15 @@ public class VariableAssignment {
return types; return types;
} }
public VariableAssignment copy() { public Type lookupType(String name) {
if (parent == null) {
return types.getOrDefault(name, null);
} else {
return types.getOrDefault(name, parent.lookupType(name));
}
}
/* public VariableAssignment copy() {
VariableAssignment copy; VariableAssignment copy;
if (parent != null) { if (parent != null) {
copy = new VariableAssignment(this.parent.copy()); copy = new VariableAssignment(this.parent.copy());
...@@ -51,7 +59,7 @@ public class VariableAssignment { ...@@ -51,7 +59,7 @@ public class VariableAssignment {
//deepcopy types //deepcopy types
return copy; return copy;
} }
*/
/** /**
* Lookup value of variable also in parent assignments * Lookup value of variable also in parent assignments
...@@ -59,24 +67,25 @@ public class VariableAssignment { ...@@ -59,24 +67,25 @@ public class VariableAssignment {
* @param name * @param name
* @return * @return
*/ */
//TODO throw exception
public Value getValue(String name) { public Value lookupVarValue(String name) {
if (parent == null) { if (parent == null) {
return values.getOrDefault(name, null); return values.getOrDefault(name, null);
} else { } else {
return values.getOrDefault(name, parent.getValue(name)); return values.getOrDefault(name, parent.lookupVarValue(name));
} }
} }
public VariableAssignment addVariable(String name, Type type) { public VariableAssignment addVarDecl(String name, Type type) {
this.types.put(name, type); if (lookupType(name) == null) {
return this; this.types.put(name, type);
return this;
} else {
throw new RuntimeException("Variable " + name + " is already declared with type " + type.toString());
}
} }
/*
public VariableAssignment peek(){
TODO?
}*/
//enterscope //enterscope
public VariableAssignment push() { public VariableAssignment push() {
return new VariableAssignment(this); return new VariableAssignment(this);
...@@ -87,8 +96,26 @@ public class VariableAssignment { ...@@ -87,8 +96,26 @@ public class VariableAssignment {
return getParent(); return getParent();
} }
public VariableAssignment setVar(String name, Value v) { public VariableAssignment setVarValue(String name, Value v) {
this.getValues().put(name, v); VariableAssignment temp = this;
return this; if (this.getTypes().containsKey(name)) {
this.values.put(name, v);
} else {
if (parent != null) {
parent.setVarValue(name, v);
} else {
throw new RuntimeException("Variable " + name + " needs to be declared first");
}
}
return temp;
}
@Override
public String toString() {
return "VariableAssignment{" +
"parent=" + parent +
", values=" + values +
", types=" + types +
'}';
} }
} }
...@@ -45,17 +45,23 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -45,17 +45,23 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
} }
@Override @Override
public ProofScript visitStart(ScriptLanguageParser.StartContext ctx) { public ProofScript visitScript(ScriptLanguageParser.ScriptContext ctx) {
ProofScript s = new ProofScript(); ProofScript s = new ProofScript();
s.setName(ctx.name.getText()); s.setName(ctx.name.getText());
s.setRuleContext(ctx); s.setRuleContext(ctx);
if (ctx.signature != null) if (ctx.signature != null)
s.setSignature((Signature) ctx.signature.accept(this)); s.setSignature((Signature) ctx.signature.accept(this));
s.setBody((Statements) ctx.body.accept(this)); s.setBody((Statements) ctx.body.accept(this));
scripts.add(s);
return s; return s;
} }
@Override
public List<ProofScript> visitStart(ScriptLanguageParser.StartContext ctx) {
ctx.script().forEach(s ->
scripts.add((ProofScript) s.accept(this)));
return scripts;
}
@Override @Override
public Signature visitArgList(ScriptLanguageParser.ArgListContext ctx) { public Signature visitArgList(ScriptLanguageParser.ArgListContext ctx) {
Signature signature = new Signature(); Signature signature = new Signature();
...@@ -89,6 +95,7 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -89,6 +95,7 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
return statements;