📄 interpreter.java
字号:
package edu.berkeley.cs164.interp;
import edu.berkeley.cs164.util.*;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.WhileStatement;
import org.eclipse.jdt.internal.corext.dom.GenericVisitor;
/** <p>
* AST interpreter. An instance of this class executes a program
* in AST form. Since the interpreter is a class, it can be easily
* embedded in any Java program.
* </p><p>
* As given, this class implements a tiny subset of the language. You
* will complete this class as part of the assignment.
*/
public class Interpreter extends GenericVisitor {
private IntValue value;
// Represents a program value. Since integers are the
// the only data type, all values are integers.
private static class IntValue {
private int value;
public IntValue(int val) {
value = val;
}
private int get() {
return value;
}
private void set(int v) {
value = v;
}
public String toString() {
return Integer.toString(value);
}
}
protected void setValue(IntValue value) {
this.value = value;
}
protected IntValue getValue() {
return value;
}
/** Run a program. */
public void run(ASTNode program) {
program.accept(this);
}
// Evaluation Strategy:
//
// Evaluate expressions by visiting nodes. The visit method
// for a node type WhateverNode has these tasks:
//
// 1. Carry out actions of the node. This may require
// visiting child nodes.
// 2. If the node evaluates to a value, store it in
// the instance variable value.
// 3. Always return false.
// TypeDeclaration is the AST node for the class declaration. In this project,
// there should always be exactly one method.
public boolean visit(TypeDeclaration e) {
List l = e.bodyDeclarations();
assert l.size() == 1;
ASTNode ch = (ASTNode) l.get(0);
ch.accept(this);
return false;
}
public boolean visit(MethodDeclaration e) {
e.getBody().accept(this);
return false;
}
public boolean visit(Block b) {
List l = b.statements();
for (Iterator iter = l.iterator(); iter.hasNext();) {
ASTNode s = (ASTNode) iter.next();
s.accept(this);
}
return false;
}
public boolean visit(NumberLiteral e) {
setValue(new IntValue(Integer.parseInt(e.getToken())));
return false;
}
// Since Java does not have a print statement, we use MethodInvocation for
// the print statement. In our files, there should always be exactly one
// argument.
public boolean visit(MethodInvocation e) {
// Evaluate argument expression. The result goes in this.value.
List args = e.arguments();
assert args.size() == 1;
ASTNode child = (ASTNode) args.get(0);
child.accept(this);
// Print argument.
System.out.println(getValue());
// Clear out value to ensure it is not erroneously used later.
setValue(null);
return false;
}
public boolean visit(ExpressionStatement s) {
s.getExpression().accept(this);
return false;
}
public boolean visit(Assignment e) {
// TODO Implement this node type
throw new RuntimeException("Evaluation not implemented for this node.");
}
public boolean visit(InfixExpression e) {
// TODO Implement this node type
throw new RuntimeException("Evaluation not implemented for this node.");
}
public boolean visit(SimpleName e) {
// TODO Implement this node type
// In this method, use the following warning message format!
//
// Util.warningMsg("variable '" + e.getIdentifier()+ "' has not been assigned");
throw new RuntimeException("Evaluation not implemented for this node.");
}
public boolean visit(IfStatement s) {
// TODO Implement this node type
throw new RuntimeException("Evaluation not implemented for this node.");
}
public boolean visit(WhileStatement s) {
// TODO Implement this node type
throw new RuntimeException("Evaluation not implemented for this node.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -