1 package spoon.reflect.eval; 2 3 import java.util.List ; 4 import java.util.Map ; 5 import java.util.Stack ; 6 7 import spoon.reflect.code.CtAbstractInvocation; 8 import spoon.reflect.reference.CtExecutableReference; 9 import spoon.reflect.reference.CtVariableReference; 10 11 15 public class SymbolicEvaluationStack { 16 17 20 @Override 21 public boolean equals(Object obj) { 22 SymbolicEvaluationStack c = (SymbolicEvaluationStack) obj; 23 return frameStack.equals(c.frameStack); 24 } 25 26 private Stack <SymbolicStackFrame> frameStack = new Stack <SymbolicStackFrame>(); 27 28 31 public Stack <CtAbstractInvocation> getCallingStack() { 32 Stack <CtAbstractInvocation> s = new Stack <CtAbstractInvocation>(); 33 for (SymbolicStackFrame f : frameStack) { 34 s.add(f.getInvocation()); 35 } 36 return s; 37 } 38 39 54 public void enterFrame(CtAbstractInvocation caller, 55 SymbolicInstance target, CtExecutableReference executable, 56 List <SymbolicInstance> arguments, 57 Map <CtVariableReference, SymbolicInstance> variables) { 58 frameStack.push(new SymbolicStackFrame(caller, target, executable, 59 arguments, variables)); 60 } 61 62 65 public void exitFrame() { 66 frameStack.pop(); 67 } 68 69 72 public SymbolicInstance<?> getThis() { 73 return frameStack.peek().getThis(); 74 } 75 76 79 public SymbolicInstance getVariableValue(CtVariableReference<?> vref) { 80 if (frameStack.peek().getVariables().containsKey(vref)) { 81 return frameStack.peek().getVariables().get(vref); 82 } else { 83 throw new RuntimeException ("unknown variable '"+vref+"'"); 84 } 85 } 86 87 90 public void setVariableValue(CtVariableReference<?> vref, 91 SymbolicInstance value) { 92 if (frameStack.peek().getVariables().containsKey(vref)) { 93 frameStack.peek().getVariables().put(vref, value); 94 } else { 95 throw new RuntimeException ("unknown variable '"+vref+"'"); 96 } 97 } 98 99 102 public SymbolicEvaluationStack(SymbolicEvaluationStack stack) { 103 for (SymbolicStackFrame f : stack.frameStack) { 104 frameStack.add(new SymbolicStackFrame(f)); 105 } 106 } 107 108 111 public SymbolicEvaluationStack() { 112 } 113 114 117 @Override 118 public String toString() { 119 return "" + frameStack; 120 } 121 122 125 public void dump() { 126 System.out.println("Stack:"); 127 int i = 1; 128 for (SymbolicStackFrame f : frameStack) { 129 System.out.print(" " + (i++) + "\t"); 130 System.out.println(f.toString()); 131 } 132 } 133 134 138 public SymbolicInstance getResult() { 139 return frameStack.peek().getResult(); 140 } 141 142 146 public void setResult(SymbolicInstance result) { 147 frameStack.peek().setResult(result); 148 } 149 150 153 public Stack <SymbolicStackFrame> getFrameStack() { 154 return frameStack; 155 } 156 157 } 158 | Popular Tags |