Commit 910d46fa authored by Sarah Grebing's avatar Sarah Grebing

First sketch implementation for statelessly handling parameter evaluation. The...

First sketch implementation for statelessly handling parameter evaluation. The evaluateParameterStateLess method needs to be modified. At the moment this implementation is not working correctly.
parent 8d73503b
Pipeline #21743 passed with stages
in 3 minutes and 32 seconds
......@@ -53,7 +53,7 @@ public class InterpreterBuilder {
@Getter
private ScopeLogger logger;
@Getter
private DefaultLookup lookup = new DefaultLookup(psh, pmh, pmc, pmr, bich);
private DefaultLookup lookup = new DefaultLookup(psh, pmh, pmc, bich, pmr);
@Getter
private KeyAssignmentHook keyHooks = new KeyAssignmentHook();
......
......@@ -37,4 +37,13 @@ public class BuiltInCommandHandler implements CommandHandler<KeyData> {
public void evaluate(Interpreter<KeyData> interpreter, CallStatement call, VariableAssignment params, KeyData data) {
builtins.get(call.getCommand()).evaluate(interpreter,call,params, data);
}
@Override
public boolean isUninterpretedParams(CallStatement call) {
if(builtins.containsKey(call.getCommand())){
return builtins.get(call.getCommand()).isUninterpretedParams(call);
} else {
return false;
}
}
}
......@@ -7,6 +7,8 @@ import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment;
import edu.kit.iti.formal.psdbg.interpreter.funchdl.BuiltinCommands;
import edu.kit.iti.formal.psdbg.interpreter.funchdl.CommandHandler;
import edu.kit.iti.formal.psdbg.parser.ast.CallStatement;
import edu.kit.iti.formal.psdbg.parser.ast.Parameters;
import edu.kit.iti.formal.psdbg.parser.data.Value;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
......@@ -36,8 +38,8 @@ public class SaveCommand implements CommandHandler<KeyData>{
@Override
public void evaluate(Interpreter<KeyData> interpreter, CallStatement call, VariableAssignment params, KeyData data) {
//be careful parameters are uninterpreted
SavePoint sp = new SavePoint(call);
//Not via Parentpath -> dependency on OS
String parentpath = path.getAbsolutePath();
parentpath = parentpath.substring(0, parentpath.length() - path.getName().length());
......@@ -52,6 +54,12 @@ public class SaveCommand implements CommandHandler<KeyData>{
}
@Override
public boolean isUninterpretedParams(CallStatement call) {
return true;
}
}
......
......@@ -148,7 +148,7 @@ public class Evaluator<T> extends DefaultASTVisitor<Value> implements ScopeObser
String name = m.group().substring(1); // remove trailing '?'
Expression t = expr.getSubstitution().get(m.group());
//either evalute the substitent or find ?X in the
//either evalute the substituent or find ?X in the
String newVal = "";
if (t != null)
newVal = ((Value) t.accept(this)).getData().toString();
......
......@@ -85,7 +85,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
throw new InterpreterRuntimeException("no state on stack. call newState before interpret");
}
if(getSelectedNode() != null) {
if (getSelectedNode() != null) {
//initialize environment variables
for (VariableAssignmentHook<T> hook : variableHooks) {
VariableAssignment va = hook.getStartAssignment(getSelectedNode().getData());
......@@ -186,7 +186,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
return evaluator.eval(expr);
}
protected Evaluator<T> createEvaluator(VariableAssignment assignments, GoalNode<T> g) {
protected Evaluator<T> createEvaluator(VariableAssignment assignments, GoalNode<T> g) {
Evaluator<T> evaluator = new Evaluator<>(assignments, g);
evaluator.setMatcher(matcherApi);
return evaluator;
......@@ -562,30 +562,59 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
enterScope(call);
if (!call.getCommand().isEmpty()) //real call, can handle pseudo calls!
{
// System.out.println(stateStack.peek().hashCode());
//neuer VarScope
//enter new variable scope
VariableAssignment params = evaluateParameters(call.getParameters());
GoalNode<T> g = getSelectedNode();
g.enterScope();
VariableAssignment params;
GoalNode<T> g = null;
boolean unInterpretedParams = functionLookup.isUninterpretedParams(call);
if (!unInterpretedParams) {
params = evaluateParameters(call.getParameters());
g = getSelectedNode();
g.enterScope();
} else {
params = evaluateParametersStateLess(call.getParameters());
}
try {
functionLookup.callCommand(this, call, params);
} catch (RuntimeException e) {
System.err.println("Call command not applicable");
System.err.println("Call command " + call.getCommand() + "not applicable");
throw e;
//TODO handling of error state for each visit
//State<T> newErrorState = newState(null, null);
//newErrorState.setErrorState(true);
//pushState(newErrorState);
} finally {
g.exitScope();
// System.out.println(stateStack.peek().hashCode());
if (!unInterpretedParams) {
//TODO this may not be needed
g.exitScope();
}
}
}
exitScope(call);
return null;
}
private VariableAssignment evaluateParametersStateLess(Parameters parameters) {
VariableAssignment va = new VariableAssignment();
Evaluator<T> evaluator = createEvaluator(null, null);
parameters.entrySet().forEach(entry -> {
try {
Value val = evaluate(entry.getValue());
va.declare(entry.getKey(), val.getType());
va.assign(entry.getKey(), val);
} catch (NullPointerException npe) {
System.out.println("Caught Nullpointer in evaluation of Stateless parameters: " + entry.toString()
);
}
});
return va;
}
public VariableAssignment evaluateParameters(Parameters parameters) {
VariableAssignment va = new VariableAssignment();
parameters.entrySet().forEach(entry -> {
......@@ -669,9 +698,9 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
// TODO: quickfix
List<GoalNode<T>> currentGoals = getCurrentGoals();
if(getCurrentGoals().size() > 1) {
if(getSelectedNode() == null) {
for(GoalNode<T> goal: currentGoals) {
if (getCurrentGoals().size() > 1) {
if (getSelectedNode() == null) {
for (GoalNode<T> goal : currentGoals) {
goal.enterScope();
signature.forEach(goal::declareVariable);
......@@ -695,7 +724,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
if (selectedGoalNode != null) {
assert stateStack.peek().getGoals().contains(selectedGoalNode);
return selectedGoalNode;
}else{
} else {
throw new IllegalStateException();
}
} catch (IllegalStateException e) {
......
......@@ -49,4 +49,8 @@ public interface CommandHandler<T> {
default Stream<String> getArguments(String name) {
return Stream.of();
}
default boolean isUninterpretedParams(CallStatement call){
return false;
}
}
......@@ -16,4 +16,8 @@ public interface CommandLookup<T> {
public CommandHandler<T> getBuilder(CallStatement callStatement, T data);
String getHelp(CallStatement call);
default boolean isUninterpretedParams(CallStatement call){
return false;
}
}
......@@ -77,4 +77,16 @@ public class DefaultLookup<T> implements CommandLookup<T> {
return getBuilder(call, null).getHelp(call);
}
@Override
public boolean isUninterpretedParams(CallStatement call) {
try {
CommandHandler cmdh = getBuilder(call, null);
if (cmdh != null)
return cmdh.isUninterpretedParams(call);
return true;
} catch (NoCallHandlerException nche) {
return false;
}
}
}
script test123() {
impRight;
save 'test0';
save 'test0' force='yes';
impRight;
save 'test1';
impLeft;
......
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