Commit 985b0a8b authored by Alexander Weigl's avatar Alexander Weigl

simplifcations and fixes

parent 729fe9ca
Pipeline #10650 passed with stage
in 2 minutes and 7 seconds
package edu.kit.formal.interpreter;
import edu.kit.formal.proofscriptparser.DefaultASTVisitor;
import edu.kit.formal.proofscriptparser.ast.*;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
/**
* Class handling evaluation of expressions (visitor for expressions)
*
* @author S.Grebing
*/
public class Evaluator extends DefaultASTVisitor<Value> {
<<<<<<< Updated upstream
private State currentState;
private Stack<List<GoalNode>> matchedGoals = new Stack<>();
private EvaluatorABI abi;
=======
private GoalNode currentState;
private List<VariableAssignment> matchedVariables = new ArrayList<>();
@Getter
@Setter
private MatcherApi matcher;
>>>>>>> Stashed changes
public Evaluator(State s) {
this.currentState = s;
}
/**
* Evaluation of an expression.
*
* @param truth
* @return
*/
public Value eval(Expression truth) {
return (Value) truth.accept(this);
}
@Override
public Value visit(BinaryExpression e) {
Value v1 = (Value) e.getLeft().accept(this);
Value v2 = (Value) e.getRight().accept(this);
Operator op = e.getOperator();
return op.evaluate(v1, v2);
}
/**
* TODO Connect with KeY
*
* @param match
* @return
*/
@Override
public Value visit(MatchExpression match) {
<<<<<<< Updated upstream
=======
Value pattern = (Value) match.getPattern().accept(this);
>>>>>>> Stashed changes
List<VariableAssignment> va = null;
if (pattern.getType() == Type.STRING) {
va = matcher.matchLabel(currentState, (String) pattern.getData());
} else if (pattern.getType() == Type.TERM) {
va = matcher.matchSeq(currentState, (String) pattern.getData());
}
return va != null && va.size() > 0 ? Value.TRUE : Value.FALSE;
}
/**
* TODO Connect with KeY
*
* @param term
* @return
*/
@Override
public Value visit(TermLiteral term) {
return null;
}
@Override
public Value visit(StringLiteral string) {
return Value.from(string);
}
@Override
public Value visit(Variable variable) {
//get variable value
String id = variable.getIdentifier();
Value v = currentState.lookupVarValue(id);
if (v != null) {
return v;
} else {
throw new RuntimeException("Variable " + variable + " was not initialized");
}
}
@Override
public Value visit(BooleanLiteral bool) {
return bool.isValue() ? Value.TRUE : Value.FALSE;
}
@Override
public Value visit(IntegerLiteral integer) {
return Value.from(integer);
}
@Override
public Value visit(UnaryExpression e) {
Operator op = e.getOperator();
Expression expr = e.getExpression();
Value exValue = (Value) expr.accept(this);
return op.evaluate(exValue);
}
}
......@@ -4,6 +4,7 @@ import edu.kit.formal.proofscriptparser.DefaultASTVisitor;
import edu.kit.formal.proofscriptparser.ast.ASTNode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import java.util.LinkedList;
import java.util.List;
......
package edu.kit.formal.interpreter;
import de.uka.ilkd.key.api.KeYApi;
import de.uka.ilkd.key.api.ProofApi;
import de.uka.ilkd.key.api.ProofManagementApi;
import de.uka.ilkd.key.control.KeYEnvironment;
import de.uka.ilkd.key.macros.ProofMacro;
import de.uka.ilkd.key.macros.scripts.ProofScriptCommand;
import de.uka.ilkd.key.proof.Proof;
import edu.kit.formal.ScopeLogger;
import edu.kit.formal.interpreter.funchdl.*;
import edu.kit.formal.proofscriptparser.Facade;
import edu.kit.formal.proofscriptparser.Visitor;
import edu.kit.formal.proofscriptparser.ast.ProofScript;
import lombok.Getter;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* @author Alexander Weigl
* @version 1 (21.05.17)
*/
public class InterpreterBuilder {
private ProofScriptHandler psh = new ProofScriptHandler();
private MacroCommandHandler pmh = new MacroCommandHandler();
private RuleCommandHandler pmr = new RuleCommandHandler();
@Getter
private final ProofScriptHandler psh = new ProofScriptHandler();
@Getter
private final MacroCommandHandler pmh = new MacroCommandHandler();
@Getter
private final RuleCommandHandler pmr = new RuleCommandHandler();
@Getter
private ProofScriptCommandBuilder pmc = new ProofScriptCommandBuilder();
public Interpreter build(File file) throws IOException {
return build(Facade.getAST(file));
@Getter
private ProofScript entryPoint;
@Getter
private Proof proof;
@Getter
private KeYEnvironment keyEnvironment;
@Getter
private HistoryListener historyLogger;
@Getter
private ScopeLogger logger;
@Getter
private DefaultLookup lookup = new DefaultLookup(psh, pmh, pmr, pmc);
private Interpreter interpreter = new Interpreter(lookup);
public InterpreterBuilder addProofScripts(File file) throws IOException {
return addProofScripts(Facade.getAST(file));
}
private Interpreter build(List<ProofScript> ast) {
public InterpreterBuilder addProofScripts(List<ProofScript> ast) {
psh.addScripts(ast);
DefaultLookup lookup = new DefaultLookup(psh, pmh, pmr, pmc);
return new Interpreter(lookup);
return this;
}
public InterpreterBuilder scriptSearchPath(File... paths) {
public InterpreterBuilder startWith(ProofScript entryPoint) {
this.entryPoint = entryPoint;
return this;
}
public Interpreter build() {
return interpreter;
}
public InterpreterBuilder keyRules(Proof proof) {
public InterpreterBuilder scriptSearchPath(File... paths) {
psh.getSearchPath().addAll(Arrays.asList(paths));
return this;
}
//TODO
public InterpreterBuilder proof(KeYEnvironment env, Proof proof) {
this.proof = proof;
this.keyEnvironment = env;
return this;
}
public InterpreterBuilder defaultScriptCommands(List<ProofScriptCommand> commands) {
public InterpreterBuilder scriptCommands() {
return scriptCommands(KeYApi.getScriptCommandApi().getScriptCommands());
}
public InterpreterBuilder scriptCommands(Collection<ProofScriptCommand> commands) {
commands.forEach(m -> pmc.getCommands().put(m.getName(), m));
return this;
}
public InterpreterBuilder defaultKeyMacros(List<ProofMacro> macros) {
public InterpreterBuilder macros() {
return macros(KeYApi.getMacroApi().getMacros());
}
public InterpreterBuilder macros(Collection<ProofMacro> macros) {
macros.forEach(m -> pmh.getMacros().put(m.getName(), m));
return this;
}
public InterpreterBuilder register(ProofScript... script) {
......@@ -56,21 +103,27 @@ public class InterpreterBuilder {
}
public InterpreterBuilder onEntry(Visitor v) {
interpreter.getEntryListeners().add(v);
return this;
}
public InterpreterBuilder onExit(Visitor v) {
interpreter.getExitListeners().add(v);
return this;
}
public InterpreterBuilder captureHistory() {
return this;
if (historyLogger != null)
historyLogger = new HistoryListener(interpreter);
return onEntry(historyLogger);
}
public InterpreterBuilder log(String prefix) {
return this;
if (logger == null)
logger = new ScopeLogger("interpreter");
return onEntry(logger);
}
}
......@@ -9,6 +9,7 @@ import edu.kit.formal.proofscriptparser.ast.CallStatement;
import edu.kit.formal.proofscriptparser.ast.Parameters;
import edu.kit.formal.proofscriptparser.ast.StringLiteral;
import edu.kit.formal.proofscriptparser.ast.Variable;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.lang.reflect.Parameter;
......@@ -21,7 +22,7 @@ import java.util.Map;
*/
@RequiredArgsConstructor
public class MacroCommandHandler implements CommandHandler {
private final Map<String, ProofMacro> macros;
@Getter private final Map<String, ProofMacro> macros;
public MacroCommandHandler() {
macros = new HashMap<>();
......
package edu.kit.formal.interpreter.funchdl;
import de.uka.ilkd.key.api.ProofScriptCommandCall;
import de.uka.ilkd.key.macros.scripts.EngineState;
import de.uka.ilkd.key.macros.scripts.ProofScriptCommand;
import edu.kit.formal.interpreter.Interpreter;
import edu.kit.formal.interpreter.VariableAssignment;
import edu.kit.formal.proofscriptparser.ast.CallStatement;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.HashMap;
......@@ -17,7 +17,8 @@ import java.util.Map;
*/
@RequiredArgsConstructor
public class ProofScriptCommandBuilder implements CommandHandler {
private final Map<String, ProofScriptCommand> scripts;
@Getter
private final Map<String, ProofScriptCommand> commands;
public ProofScriptCommandBuilder() {
this(new HashMap<>());
......@@ -25,14 +26,14 @@ public class ProofScriptCommandBuilder implements CommandHandler {
@Override
public boolean handles(CallStatement call) {
return scripts.containsKey(call.getCommand());
return commands.containsKey(call.getCommand());
}
@Override
public void evaluate(Interpreter interpreter,
CallStatement call,
VariableAssignment params) {
ProofScriptCommand c = scripts.get(call.getCommand());
ProofScriptCommand c = commands.get(call.getCommand());
EngineState state = null;
Map<String, String> map;
//ProofScriptCommandCall cc = c.evaluateArguments(state, map);
......
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