Commit e0c77460 authored by Sarah Grebing's avatar Sarah Grebing

local converter for local namespaces

parent 9aba5490
Pipeline #18884 failed with stages
in 2 minutes and 12 seconds
......@@ -46,6 +46,7 @@ assignment
| variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON
;
expression
:
expression MUL expression #exprMultiplication
......@@ -56,6 +57,7 @@ expression
| expression AND expression #exprAnd
| expression OR expression #exprOr
| expression IMP expression #exprIMP
| expression USING LBRACKET argList RBRACKET #namespaceset
//| expression EQUIV expression already covered by EQ/NEQ
| expression LBRACKET substExpressionList RBRACKET #exprSubst
| ID LPAREN (expression (',' expression)*)? RPAREN #function
......
......@@ -226,4 +226,11 @@ public interface ASTTraversal<T> extends Visitor<T> {
relaxBlock.getBody().accept(this);
return null;
}
@Override
default T visit(NamespaceSetExpression nss) {
nss.getExpression().accept(this);
nss.getSignature().accept(this);
return null;
}
}
......@@ -176,5 +176,10 @@ public class DefaultASTVisitor<T> implements Visitor<T> {
public T visit(RelaxBlock relaxBlock) {
return defaultVisit(relaxBlock);
}
@Override
public T visit(NamespaceSetExpression nss) {
return defaultVisit(nss);
}
}
......@@ -244,6 +244,14 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
return ctx.matchPattern().accept(this);
}
@Override
public Object visitNamespaceset(ScriptLanguageParser.NamespacesetContext ctx) {
NamespaceSetExpression nse = new NamespaceSetExpression();
nse.setExpression((Expression) ctx.expression().accept(this));
nse.setSignature((Signature) ctx.argList().accept(this));
return nse;
}
@Override
public Object visitExprIMP(ScriptLanguageParser.ExprIMPContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), Operator.IMPLICATION);
......
......@@ -90,4 +90,5 @@ public interface Visitor<T> {
T visit(RelaxBlock relaxBlock);
T visit(NamespaceSetExpression nss);
}
package edu.kit.iti.formal.psdbg.parser.ast;
import edu.kit.iti.formal.psdbg.parser.NotWelldefinedException;
import edu.kit.iti.formal.psdbg.parser.ScriptLanguageParser;
import edu.kit.iti.formal.psdbg.parser.Visitor;
import edu.kit.iti.formal.psdbg.parser.types.Type;
import lombok.Getter;
import lombok.Setter;
public class NamespaceSetExpression extends Expression<ScriptLanguageParser.NamespacesetContext>{
@Getter @Setter
private Expression expression;
@Getter @Setter
private Signature signature = new Signature();
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override
public boolean hasMatchExpression() {
return expression.hasMatchExpression();
}
@Override
public int getPrecedence() {
return 5;
}
@Override
public NamespaceSetExpression copy() {
NamespaceSetExpression nse = new NamespaceSetExpression();
nse.expression = expression.copy();
nse.signature = signature.copy();
return nse;
}
@Override
public Type getType(Signature signature) throws NotWelldefinedException {
return expression.getType(signature);
}
}
......@@ -2,6 +2,7 @@ package edu.kit.iti.formal.psdbg.parser.data;
import edu.kit.iti.formal.psdbg.parser.ast.*;
import edu.kit.iti.formal.psdbg.parser.types.SimpleType;
import edu.kit.iti.formal.psdbg.parser.types.TermType;
import edu.kit.iti.formal.psdbg.parser.types.Type;
import edu.kit.iti.formal.psdbg.parser.types.TypeFacade;
import lombok.Getter;
......
......@@ -23,27 +23,52 @@ package edu.kit.iti.formal.psdbg.parser.types;
*/
import javax.annotation.Nullable;
/**
* Represents the possible types (including scriptVarTypes).
* <p>
* Created at 30.04.2017
*
* INT("\\term int"),
BOOL("\\term bool"),
ANY("\\term any"),
INT_ARRAY("\\term int[]"),
OBJECT("\\term Object"),
HEAP("\\term Heap"),
FIELD("\\term Field"),
LOCSET("\\term LocSet"),
FORMULA("\\formula"),
SEQ("\\term Seq");
* @author Sarah Grebing
*/
public enum SimpleType implements Type {
STRING("string"), ANY("any"),
PATTERN("pattern"),
INT("int"), BOOL("bool"), INT_ARRAY("int[]"), OBJECT("object"),
HEAP("heap"), FIELD("field"), LOCSET("locset"), NULL("null"), FORMULA("formula"), SEQ("Seq");
STRING("string", "string"),
//ANY("any", "any"),
PATTERN("pattern", null),
INT("int", "int"),
BOOL("bool", "bool"),
/*NULL("null", interpreterSort),
FORMULA("formula", interpreterSort),
SEQ("Seq", interpreterSort)*/;
private final String symbol;
private @Nullable final String interpreterSort;
SimpleType(String symbol) {
SimpleType(String symbol, @Nullable String interpreterSort) {
this.symbol = symbol;
this.interpreterSort = interpreterSort;
}
@Override
public String symbol() {
return symbol;
}
@Override
public String interpreterSort() {
return interpreterSort;
}
}
......@@ -29,4 +29,8 @@ public class TermType implements Type {
+ ">";
}
@Override
public String interpreterSort() {
return argTypes.get(0).interpreterSort();
}
}
......@@ -6,4 +6,5 @@ package edu.kit.iti.formal.psdbg.parser.types;
*/
public interface Type {
String symbol();
String interpreterSort();
}
package edu.kit.iti.formal.psdbg.interpreter;
import de.uka.ilkd.key.logic.Name;
import de.uka.ilkd.key.logic.NamespaceSet;
import de.uka.ilkd.key.logic.Term;
import de.uka.ilkd.key.logic.op.LogicVariable;
import de.uka.ilkd.key.logic.op.QuantifiableVariable;
import de.uka.ilkd.key.logic.sort.Sort;
import de.uka.ilkd.key.proof.Goal;
import edu.kit.iti.formal.psdbg.interpreter.data.GoalNode;
import edu.kit.iti.formal.psdbg.interpreter.data.KeyData;
import edu.kit.iti.formal.psdbg.interpreter.data.TermValue;
import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment;
import edu.kit.iti.formal.psdbg.parser.ast.Expression;
import edu.kit.iti.formal.psdbg.parser.ast.NamespaceSetExpression;
import edu.kit.iti.formal.psdbg.parser.ast.SubstituteExpression;
import edu.kit.iti.formal.psdbg.parser.ast.Variable;
import edu.kit.iti.formal.psdbg.parser.data.Value;
import edu.kit.iti.formal.psdbg.parser.types.SimpleType;
import edu.kit.iti.formal.psdbg.parser.types.TermType;
import edu.kit.iti.formal.psdbg.parser.types.Type;
import edu.kit.iti.formal.psdbg.parser.types.TypeFacade;
import org.key_project.util.collection.ImmutableArray;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class KeyEvaluator extends Evaluator<KeyData> {
public KeyEvaluator(VariableAssignment assignments, GoalNode<KeyData> g) {
super(assignments, g);
}
@Override
public Value visit(NamespaceSetExpression nss) {
Value term = (Value) nss.getExpression().accept(this);
if (term.getType() instanceof TermType) {
TermValue data = ((TermValue) term.getData()).copy();
nss.getSignature().forEach((v,s) -> {
Sort sort = asKeySort(s, getGoal().getData().getGoal());
data.getNs().variables().add(new LogicVariable(new Name(v.getIdentifier()), sort));
});
return new Value(term.getType(), data);
}else {
throw new IllegalStateException("Try to apply substitute on a non-term value.");
}
}
private Sort asKeySort(Type symbol, Goal g) {
NamespaceSet global = g.proof().getServices().getNamespaces();
Sort s = global.sorts().lookup(symbol.interpreterSort());
if(s!=null)
return s;
return g.getLocalNamespaces().sorts().lookup(symbol.interpreterSort());
}
@Override
public Value visit(SubstituteExpression expr) {
Value term = (Value) expr.getSub().accept(this);
if (term.getType() instanceof TermType) {
TermType tt = (TermType) term.getData();
//TODO better and new
Pattern pattern = Pattern.compile("\\?[a-zA-Z_]+");
String termstr = term.getData().toString();
Matcher m = pattern.matcher(termstr);
StringBuffer newTerm = new StringBuffer();
while (m.find()) {
String name = m.group().substring(1); // remove trailing '?'
Expression t = expr.getSubstitution().get(m.group());
//either evalute the substitent or find ?X in the
String newVal = "";
if (t != null)
newVal = ((Value) t.accept(this)).getData().toString();
else
// newVal = state.getValue(new Variable(name)).getData().toString();
m.appendReplacement(newTerm, newVal);
}
m.appendTail(newTerm);
return new Value<>(TypeFacade.ANY_TERM, newTerm.toString());
} else {
throw new IllegalStateException("Try to apply substitute on a non-term value.");
}
}
}
package edu.kit.iti.formal.psdbg.interpreter;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableBiMap;
import de.uka.ilkd.key.api.VariableAssignments;
import de.uka.ilkd.key.logic.sort.Sort;
import de.uka.ilkd.key.proof.Node;
import de.uka.ilkd.key.proof.Proof;
import edu.kit.iti.formal.psdbg.interpreter.data.GoalNode;
import edu.kit.iti.formal.psdbg.interpreter.data.KeyData;
import edu.kit.iti.formal.psdbg.interpreter.data.State;
import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment;
import edu.kit.iti.formal.psdbg.interpreter.data.*;
import edu.kit.iti.formal.psdbg.interpreter.funchdl.CommandLookup;
import edu.kit.iti.formal.psdbg.parser.Visitor;
import edu.kit.iti.formal.psdbg.parser.ast.ClosesCase;
import edu.kit.iti.formal.psdbg.parser.ast.Statements;
import edu.kit.iti.formal.psdbg.parser.ast.TermLiteral;
import edu.kit.iti.formal.psdbg.parser.ast.TryCase;
import edu.kit.iti.formal.psdbg.parser.data.Value;
import edu.kit.iti.formal.psdbg.parser.types.SimpleType;
import edu.kit.iti.formal.psdbg.parser.types.TermType;
import edu.kit.iti.formal.psdbg.parser.types.Type;
import edu.kit.iti.formal.psdbg.parser.types.TypeFacade;
import lombok.Getter;
import lombok.val;
import java.util.List;
import java.util.function.Function;
/**
* @author Alexander Weigl
......@@ -27,15 +33,17 @@ public class KeyInterpreter extends Interpreter<KeyData> {
@Getter
private static final BiMap<SimpleType, VariableAssignments.VarType> typeConversionBiMap =
new ImmutableBiMap.Builder<SimpleType, VariableAssignments.VarType>()
.put(SimpleType.ANY, VariableAssignments.VarType.ANY)
// .put(SimpleType.ANY, VariableAssignments.VarType.ANY)
.put(SimpleType.BOOL, VariableAssignments.VarType.BOOL)
//.put(SimpleType.TERM, VariableAssignments.VarType.FORMULA) //TODO: parametrisierte Terms
.put(SimpleType.INT, VariableAssignments.VarType.INT)
.put(SimpleType.STRING, VariableAssignments.VarType.OBJECT)
.put(SimpleType.INT_ARRAY, VariableAssignments.VarType.INT_ARRAY)
.put(SimpleType.SEQ, VariableAssignments.VarType.SEQ)
// .put(SimpleType.INT_ARRAY, VariableAssignments.VarType.INT_ARRAY)
// .put(SimpleType.SEQ, VariableAssignments.VarType.SEQ)
.build();
public KeyInterpreter(CommandLookup lookup) {
super(lookup);
}
......@@ -126,4 +134,17 @@ public class KeyInterpreter extends Interpreter<KeyData> {
entryListeners = backupEntryListener;
}
}
@Override
protected Evaluator<KeyData> createEvaluator(VariableAssignment assignments, GoalNode<KeyData> g) {
KeyEvaluator eval = new KeyEvaluator(assignments, g);
eval.setMatcher(getMatcherApi());
eval.setTermValueFactory(new Function<TermLiteral, Value>() {
@Override
public Value apply(TermLiteral termLiteral) {
return new Value(TypeFacade.ANY_TERM, new TermValue(termLiteral.getText()));
}
});
return eval;
}
}
......@@ -14,4 +14,9 @@ public class SortType implements Type {
public String symbol() {
return sort.toString();
}
@Override
public String interpreterSort() {
return symbol();
}
}
package edu.kit.iti.formal.psdbg.interpreter.data;
import de.uka.ilkd.key.logic.NamespaceSet;
import de.uka.ilkd.key.logic.Term;
import de.uka.ilkd.key.logic.sort.Sort;
import de.uka.ilkd.key.pp.LogicPrinter;
import lombok.Data;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@Data
public class TermValue {
@Nonnull
private Sort keySort = Sort.ANY;
private String termRepr;
@Nullable
private Term term;
/**
* Stores the local namespace, e.g. bounded variables.
*/
@Nullable
private NamespaceSet ns;
public TermValue() {
}
public TermValue(String text) {
termRepr = text;
}
public String getTermRepr(){
if(term!=null){
//TODO the better method
return LogicPrinter.quickPrintTerm(term, null, false,false);
}
else{
return termRepr;
}
}
public TermValue copy() {
TermValue tv = new TermValue();
tv.keySort = keySort;
tv.termRepr=termRepr;
tv.term=term;
tv.ns = ns.copy();
return tv;
}
}
......@@ -10,20 +10,15 @@ import java.util.Map;
* @author grebing
*/
public class ScriptCommandNotApplicableException extends InterpreterRuntimeException {
public ScriptCommandNotApplicableException(Exception e) {
super(e);
}
public ScriptCommandNotApplicableException(Exception e, RuleCommand c) {
System.out.println("Call " + c.getName() + " was not applicable");
}
public ScriptCommandNotApplicableException(Exception e, RuleCommand c, Map<String, String> params) {
public ScriptCommandNotApplicableException(Exception e, RuleCommand c, Map<String, Object> params) {
super(createMessage(c, params), e);
}
private static String createMessage(RuleCommand c, Map<String, String> params) {
private static String createMessage(RuleCommand c, Map<String, Object> params) {
StringBuilder sb = new StringBuilder();
sb.append("Call " + c.getName() + " with parameters ");
for (String s : params.keySet()) {
......
......@@ -6,6 +6,7 @@ import de.uka.ilkd.key.macros.scripts.EngineState;
import de.uka.ilkd.key.macros.scripts.ProofScriptCommand;
import de.uka.ilkd.key.proof.Goal;
import de.uka.ilkd.key.proof.Node;
import edu.kit.iti.formal.psdbg.ValueInjector;
import edu.kit.iti.formal.psdbg.interpreter.Interpreter;
import edu.kit.iti.formal.psdbg.interpreter.data.GoalNode;
import edu.kit.iti.formal.psdbg.interpreter.data.KeyData;
......@@ -14,12 +15,14 @@ import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment;
import edu.kit.iti.formal.psdbg.parser.ast.CallStatement;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.key_project.util.collection.ImmutableList;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
......@@ -63,16 +66,16 @@ public class ProofScriptCommandBuilder implements CommandHandler<KeyData> {
State<KeyData> state = interpreter.getCurrentState();
GoalNode<KeyData> expandedNode = state.getSelectedGoalNode();
KeyData kd = expandedNode.getData();
Map<String, String> map = new HashMap<>();
Map<String, Object> map = new HashMap<>();
params.asMap().forEach((k, v) -> {
// System.out.println("v.getData().toString() = " + v.getData().toString());
map.put(k.getIdentifier(), v.getData().toString());
map.put(k.getIdentifier(), v.getData());
}
);
try {
EngineState estate = new EngineState(kd.getProof());
estate.setGoal(kd.getNode());
Object cc = c.evaluateArguments(estate, map); //exception?
ValueInjector vi = ValueInjector.createDefault(kd.getNode());
Object cc = vi.inject(c, getParameterInstance(c), map);
AbstractUserInterfaceControl uiControl = new DefaultUserInterfaceControl();
c.execute(uiControl, cc, estate);
//what happens if this is empty -> meaning proof is closed
......@@ -104,6 +107,13 @@ public class ProofScriptCommandBuilder implements CommandHandler<KeyData> {
}
}
private <T> T getParameterInstance(ProofScriptCommand c) throws NoSuchMethodException, IllegalAccessException,
InstantiationException {
Method method = c.getClass().getMethod("evaluateArguments", EngineState.class, Map.class);
Class rtclazz = method.getReturnType();
return (T) rtclazz.newInstance();
}
@Override
public String getHelp(CallStatement call) {
ProofScriptCommand c = commands.get(call.getCommand());
......
......@@ -16,6 +16,7 @@ import de.uka.ilkd.key.proof.rulefilter.TacletFilter;
import de.uka.ilkd.key.rule.Rule;
import de.uka.ilkd.key.rule.Taclet;
import de.uka.ilkd.key.rule.TacletApp;
import edu.kit.iti.formal.psdbg.ValueInjector;
import edu.kit.iti.formal.psdbg.interpreter.Interpreter;
import edu.kit.iti.formal.psdbg.interpreter.data.GoalNode;
import edu.kit.iti.formal.psdbg.interpreter.data.KeyData;
......@@ -23,6 +24,7 @@ import edu.kit.iti.formal.psdbg.interpreter.data.State;
import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment;
import edu.kit.iti.formal.psdbg.interpreter.exceptions.ScriptCommandNotApplicableException;
import edu.kit.iti.formal.psdbg.parser.ast.CallStatement;
import edu.kit.iti.formal.psdbg.parser.data.Value;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.LogManager;
......@@ -103,14 +105,16 @@ public class RuleCommandHandler implements CommandHandler<KeyData> {
State<KeyData> state = interpreter.getCurrentState();
GoalNode<KeyData> expandedNode = state.getSelectedGoalNode();
KeyData kd = expandedNode.getData();
Map<String, String> map = new HashMap<>();
params.asMap().forEach((k, v) -> map.put(k.getIdentifier(), v.getData().toString()));
Map<String, Object> map = new HashMap<>();
params.asMap().forEach((k, v) -> map.put(k.getIdentifier(), v.getData()));
LOGGER.info("Execute {} with {}", call, map);
try {
map.put("#2", call.getCommand());
EngineState estate = new EngineState(kd.getProof());
estate.setGoal(kd.getNode());
RuleCommand.Parameters cc = c.evaluateArguments(estate, map); //reflection exception
RuleCommand.Parameters cc = new RuleCommand.Parameters();
ValueInjector valueInjector = ValueInjector.createDefault(kd.getNode());
cc = valueInjector.inject(c, cc, map);
AbstractUserInterfaceControl uiControl = new DefaultUserInterfaceControl();
c.execute(uiControl, cc, estate);
......
......@@ -14,6 +14,7 @@ import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -35,6 +36,9 @@ public class Evaluator<T> extends DefaultASTVisitor<Value> implements ScopeObser
private List<Visitor> entryListeners = new ArrayList<>(),
exitListeners = new ArrayList<>();
@Getter @Setter
private Function<TermLiteral, Value> termValueFactory = (TermLiteral l) -> Value.from(l);
public Evaluator(VariableAssignment assignment, GoalNode<T> node) {
state = new VariableAssignment(assignment); // unmodifiable version of assignment
goal = node;
......@@ -94,7 +98,7 @@ public class Evaluator<T> extends DefaultASTVisitor<Value> implements ScopeObser
*/
@Override
public Value visit(TermLiteral term) {
return Value.from(term);
return termValueFactory.apply(term);
}
@Override
......@@ -163,4 +167,9 @@ public class Evaluator<T> extends DefaultASTVisitor<Value> implements ScopeObser
public Value visit(FunctionCall func) {
return func.getFunction().eval(this, func);
}
@Override
public Value visit(NamespaceSetExpression nss) {
return null;
}
}
......@@ -181,12 +181,18 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
private Value evaluate(GoalNode<T> g, Expression expr) {
//enterScope(expr);
Evaluator<T> evaluator = new Evaluator<>(g.getAssignments(), g);
evaluator.setMatcher(matcherApi);
Evaluator<T> evaluator = createEvaluator(g.getAssignments(), g);
//exitScope(expr);
return evaluator.eval(expr);
}
protected Evaluator<T> createEvaluator(VariableAssignment assignments, GoalNode<T> g) {
Evaluator<T> evaluator = new Evaluator<>(assignments, g);
evaluator.setMatcher(matcherApi);
return evaluator;
}
/**
* Visiting a statement list results in visiting each statement
*
......
......@@ -15,6 +15,7 @@ import de.uka.ilkd.key.proof.Node;
import de.uka.ilkd.key.proof.Proof;
import edu.kit.iti.formal.psdbg.LabelFactory;
import edu.kit.iti.formal.psdbg.RuleCommandHelper;
import edu.kit.iti.formal.psdbg.ValueInjector;
import edu.kit.iti.formal.psdbg.gui.controls.ScriptArea;
import edu.kit.iti.formal.psdbg.gui.controls.ScriptController;
import edu.kit.iti.formal.psdbg.gui.controls.Utils;
......@@ -254,11 +255,11 @@ public class InteractiveModeController {
// KeyData kd = g.getData();
Evaluator eval = new Evaluator(expandedNode.getAssignments(), expandedNode);
Map<String, String> map = new HashMap<>();
Map<String, Object> map = new HashMap<>();
call.getParameters().forEach((variable, expression) -> {
Value exp = eval.eval(expression);
map.put(variable.getIdentifier(), exp.getData().toString());
map.put(variable.getIdentifier(), exp.getData());
});
// call.getParameters().forEach((k, v) -> map.put(k.getIdentifier(),));
......@@ -271,8 +272,9 @@ public class InteractiveModeController {
//System.out.println("on = " + map.get("on"));
//System.out.println("formula = " + map.get("formula"));
//System.out.println("occ = " + map.get("occ"));
RuleCommand.Parameters cc = c.evaluateArguments(estate, map); //reflection exception
ValueInjector valueInjector = ValueInjector.createDefault(kd.getNode());
RuleCommand.Parameters cc = new RuleCommand.Parameters();
cc = valueInjector.inject(c, cc, map);
AbstractUserInterfaceControl uiControl = new DefaultUserInterfaceControl();
c.execute(uiControl, cc, estate);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment