📄 manuallybuiltasts.java
字号:
package edu.berkeley.cs164.interp;
import java.util.HashMap;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ExpressionStatement;
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.TypeDeclaration;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.WhileStatement;
/**
* Class that shows how ASTs can be constructed manually.
*/
public class ManuallyBuiltASTs {
/** Return the sample program "print(42);" */
public static ASTNode createSampleProgram() {
// Create new AST object. The empty hash map argument is required
// since this is not an Eclipse plugin.
AST ast = new AST(new HashMap());
// AST for "42"
NumberLiteral n1 = ast.newNumberLiteral("42");
// AST for "print <n1>"
MethodInvocation p1 = ast.newMethodInvocation();
p1.setName(ast.newSimpleName("print"));
p1.arguments().add(n1);
ExpressionStatement s1 = ast.newExpressionStatement(p1);
// The rest is standard for any program using the interpreter. The
// code is contained in a block, which is contained in a method
// declaration, which is contained in a class declaration.
Block b1 = ast.newBlock();
b1.statements().add(s1);
MethodDeclaration m1 = ast.newMethodDeclaration();
m1.setName(ast.newSimpleName("main"));
m1.setBody(b1);
TypeDeclaration t1 = ast.newTypeDeclaration();
t1.setName(ast.newSimpleName("MainClass"));
t1.bodyDeclarations().add(m1);
return t1;
}
public static ASTNode createTestProgram1() {
// general test of constructs
// - operators +, -, *, /
// - print, assignment
// - if, while statements
AST ast = new AST(new HashMap());
Block b1 = ast.newBlock();
// e: 1 + 2
NumberLiteral n1 = ast.newNumberLiteral("1");
NumberLiteral n2 = ast.newNumberLiteral("2");
InfixExpression e = ast.newInfixExpression();
e.setLeftOperand(n1);
e.setRightOperand(n2);
e.setOperator(InfixExpression.Operator.PLUS);
// print(e)
MethodInvocation p = ast.newMethodInvocation();
p.setName(ast.newSimpleName("print"));
p.arguments().add(e);
ExpressionStatement s = ast.newExpressionStatement(p);
b1.statements().add(s);
// e: 2 - 1
n1 = ast.newNumberLiteral("2");
n2 = ast.newNumberLiteral("1");
e = ast.newInfixExpression();
e.setLeftOperand(n1);
e.setRightOperand(n2);
e.setOperator(InfixExpression.Operator.MINUS);
// print(e)
p = ast.newMethodInvocation();
p.setName(ast.newSimpleName("print"));
p.arguments().add(e);
s = ast.newExpressionStatement(p);
b1.statements().add(s);
// e: 2 * 3
n1 = ast.newNumberLiteral("2");
n2 = ast.newNumberLiteral("3");
e = ast.newInfixExpression();
e.setLeftOperand(n1);
e.setRightOperand(n2);
e.setOperator(InfixExpression.Operator.TIMES);
// print(e)
p = ast.newMethodInvocation();
p.setName(ast.newSimpleName("print"));
p.arguments().add(e);
s = ast.newExpressionStatement(p);
b1.statements().add(s);
// e: 50/25
n1 = ast.newNumberLiteral("50");
n2 = ast.newNumberLiteral("25");
e = ast.newInfixExpression();
e.setLeftOperand(n1);
e.setRightOperand(n2);
e.setOperator(InfixExpression.Operator.DIVIDE);
// print(e)
p = ast.newMethodInvocation();
p.setName(ast.newSimpleName("print"));
p.arguments().add(e);
s = ast.newExpressionStatement(p);
b1.statements().add(s);
// The rest is standard for any program using the interpreter. The
// code is contained in a block, which is contained in a method
// declaration, which is contained in a class declaration.
MethodDeclaration m1 = ast.newMethodDeclaration();
m1.setName(ast.newSimpleName("main"));
m1.setBody(b1);
TypeDeclaration t1 = ast.newTypeDeclaration();
t1.setName(ast.newSimpleName("MainClass"));
t1.bodyDeclarations().add(m1);
return t1;
}
public static ASTNode createTestProgram2() {
AST ast = new AST(new HashMap());
Block b1 = ast.newBlock();
// TypeDeclaration MainClass
// SimpleName "MainClass"
TypeDeclaration mainClass = ast.newTypeDeclaration();
mainClass.setName(ast.newSimpleName("MainClass"));
// MethodDeclaration
// PrimitiveType
// SimpleName "main"
MethodDeclaration mainMethod = ast.newMethodDeclaration();
mainMethod.setName(ast.newSimpleName("main"));
mainClass.bodyDeclarations().add(mainMethod);
// block
Block mainBlock = ast.newBlock();
mainMethod.setBody(mainBlock);
//ExpressionStatement
// Assignment
// SimpleName "f1"
// NumberLiteral "0"
addAssignmentToBlock(
ast,
mainBlock,
ast.newSimpleName("f1"),
ast.newNumberLiteral("0")
);
//ExpressionStatement
// Assignment
// SimpleName "f2"
// NumberLiteral "1"
addAssignmentToBlock(
ast,
mainBlock,
ast.newSimpleName("f2"),
ast.newNumberLiteral("1")
);
//ExpressionStatement
// Assignment
// SimpleName "count"
// NumberLiteral "10"
addAssignmentToBlock(
ast,
mainBlock,
ast.newSimpleName("count"),
ast.newNumberLiteral("10")
);
WhileStatement whileStmt = ast.newWhileStatement();
Block whileBlock = ast.newBlock();
whileStmt.setExpression(ast.newSimpleName("count"));
whileStmt.setBody(whileBlock);
//ExpressionStatement
// Assignment
// SimpleName "t"
// SimpleName "f2"
addAssignmentToBlock(
ast,
whileBlock,
ast.newSimpleName("t"),
ast.newSimpleName("f2")
);
//ExpressionStatement
// Assignment
// SimpleName "f2"
// InfixExpression
// SimpleName "f1"
// SimpleName "f2"
addAssignmentToBlock(
ast,
whileBlock,
ast.newSimpleName("f2"),
getInfixExpression(
ast,
InfixExpression.Operator.PLUS,
ast.newSimpleName("f1"),
ast.newSimpleName("f2")
)
);
/*
WhileStatement
SimpleName "count"
Block
ExpressionStatement
Assignment
SimpleName "f1"
SimpleName "t"
ExpressionStatement
Assignment
SimpleName "count"
InfixExpression
SimpleName "count"
NumberLiteral "1"
ExpressionStatement
MethodInvocation
SimpleName "print"
SimpleName "f1"
*/
return mainClass;
}
protected static void addAssignmentToBlock(
AST ast,
Block block,
Expression lhs,
Expression rhs
)
{
Assignment assignExpr = ast.newAssignment();
assignExpr.setLeftHandSide(lhs);
assignExpr.setRightHandSide(rhs);
block.statements().add(ast.newExpressionStatement(assignExpr));
}
protected static InfixExpression getInfixExpression(
AST ast,
InfixExpression.Operator op,
Expression lhs,
Expression rhs
)
{
InfixExpression exp = ast.newInfixExpression();
exp.setLeftOperand(lhs);
exp.setRightOperand(rhs);
exp.setOperator(op);
return exp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -