1 11 package org.eclipse.jdt.internal.debug.eval.ast.engine; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 import java.util.Stack ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.debug.core.model.IVariable; 19 import org.eclipse.jdt.debug.core.IJavaType; 20 import org.eclipse.jdt.debug.core.IJavaValue; 21 import org.eclipse.jdt.debug.core.IJavaVariable; 22 import org.eclipse.jdt.internal.debug.eval.ast.instructions.Instruction; 23 import org.eclipse.jdt.internal.debug.eval.ast.instructions.InstructionSequence; 24 25 public class Interpreter { 26 private Instruction[] fInstructions; 27 private int fInstructionCounter; 28 private IRuntimeContext fContext; 29 private Stack fStack; 30 private IJavaValue fLastValue; 31 32 35 private Map fInternalVariables; 36 37 private boolean fStopped= false; 38 39 public Interpreter(InstructionSequence instructions, IRuntimeContext context) { 40 fInstructions= instructions.getInstructions(); 41 fContext= context; 42 fInternalVariables= new HashMap (); 43 } 44 45 public void execute() throws CoreException { 46 reset(); 47 while(fInstructionCounter < fInstructions.length && !fStopped) { 48 Instruction instruction= fInstructions[fInstructionCounter++]; 49 instruction.setInterpreter(this); 50 instruction.execute(); 51 instruction.setInterpreter(null); 52 } 53 } 54 55 public void stop() { 56 fStopped= true; 57 } 58 59 private void reset() { 60 fStack= new Stack (); 61 fInstructionCounter= 0; 62 } 63 64 67 public void jump(int offset) { 68 fInstructionCounter+= offset; 69 } 70 71 74 public void push(Object object) { 75 fStack.push(object); 76 } 77 78 81 public Object peek() { 82 return fStack.peek(); 83 } 84 85 88 public Object pop() { 89 return fStack.pop(); 90 } 91 92 95 public IRuntimeContext getContext() { 96 return fContext; 97 } 98 99 public IJavaValue getResult() { 100 if (fStack == null || fStack.isEmpty()) { 101 if (fLastValue == null) { 102 return getContext().getVM().voidValue(); 103 } 104 return fLastValue; 105 } 106 Object top= fStack.peek(); 107 if (top instanceof IJavaVariable) { 108 try { 109 return (IJavaValue)((IJavaVariable)top).getValue(); 110 } catch (CoreException exception) { 111 return getContext().getVM().newValue(exception.getStatus().getMessage()); 112 } 113 } 114 if (top instanceof IJavaValue) { 115 return (IJavaValue)top; 116 } 117 return null; 119 } 120 121 public void setLastValue(IJavaValue value) { 122 fLastValue= value; 123 } 124 125 133 public IVariable createInternalVariable(String name, IJavaType referencType) { 134 IVariable var= new InterpreterVariable(name, referencType, fContext.getVM()); 135 fInternalVariables.put(name, var); 136 return var; 137 } 138 139 147 public IVariable getInternalVariable(String name) { 148 return (IVariable)fInternalVariables.get(name); 149 } 150 } 151 | Popular Tags |