KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > eval > ast > engine > Interpreter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.eval.ast.engine;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Stack JavaDoc;
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 JavaDoc fStack;
30     private IJavaValue fLastValue;
31     
32     /**
33      * The list of internal variables
34      */

35     private Map JavaDoc 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 JavaDoc();
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 JavaDoc();
61         fInstructionCounter= 0;
62     }
63     
64     /**
65      * Jumps to a given address
66      */

67     public void jump(int offset) {
68         fInstructionCounter+= offset;
69     }
70     
71     /**
72      * Pushes an object onto the stack
73      */

74     public void push(Object JavaDoc object) {
75         fStack.push(object);
76     }
77
78     /**
79      * Peeks at the top object of the stack
80      */

81     public Object JavaDoc peek() {
82         return fStack.peek();
83     }
84     
85     /**
86      * Pops an object off of the stack
87      */

88     public Object JavaDoc pop() {
89         return fStack.pop();
90     }
91     
92     /**
93      * Answers the context for the interpreter
94      */

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 JavaDoc 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         // XXX: exception
118
return null;
119     }
120     
121     public void setLastValue(IJavaValue value) {
122         fLastValue= value;
123     }
124     
125     /**
126      * Create a new variable in the interpreter with the given name
127      * and the given type.
128      *
129      * @param name the name of the variable to create.
130      * @param type the type of the variable to create.
131      * @return the created variable.
132      */

133     public IVariable createInternalVariable(String JavaDoc name, IJavaType referencType) {
134         IVariable var= new InterpreterVariable(name, referencType, fContext.getVM());
135         fInternalVariables.put(name, var);
136         return var;
137     }
138     
139     /**
140      * Return the variable with the given name.
141      * This method only looks in the list of internal variable (i.e. created by
142      * Interpreter#createInternalVariable(String, IJavaType))
143      *
144      * @param name the name of the variable to retrieve.
145      * @return the corresponding variable, or <code>null</code> if there is none.
146      */

147     public IVariable getInternalVariable(String JavaDoc name) {
148         return (IVariable)fInternalVariables.get(name);
149     }
150 }
151
Popular Tags