📄 checker.java
字号:
before.clear();
super.endVisit(n);
}
public void endVisit(VariableDeclarationStatement n) {
Type t = n.getType();
Iterator iter = n.fragments().iterator();
while (iter.hasNext()) {
VariableDeclarationFragment temp = (VariableDeclarationFragment)iter.next();
if (temp.getInitializer()!=null)
if (temp.getInitializer().getProperty("type")!=null){
if (!submatch(t,(Type)temp.getInitializer().getProperty("type")))
checkError("\""+temp.getName().toString()+"\""+" has the wrong type initializer",temp.getName());
}
else
if (temp.getInitializer().getProperty("size")!=null&&t.isArrayType())
if (t.getProperty("size")!=null&&((Expression)t.getProperty("size")).getProperty("value")!=null)
if (((Integer)((Expression)t.getProperty("size")).getProperty("value")).intValue()!=((Integer)temp.getInitializer().getProperty("size")).intValue())
checkError("\""+temp.getName().toString()+"\""+" has wrong array size ("+t.getProperty("size")+" compare to "+temp.getInitializer().getProperty("size")+")",temp.getName());
else {
Iterator it = ((ArrayInitializer)temp.getInitializer()).expressions().iterator();
while (it.hasNext()){
Expression e = (Expression)it.next();
if (e.getProperty("type")!=null)
if (!submatch(((ArrayType)t).getComponentType(),(Type)e.getProperty("type")))
checkError("\""+temp.getName().toString()+"\""+" initialized with wrong array element type (Element:"+e.toString()+")",temp.getName());
}
}
if (t.isArrayType()&&t.getProperty("size")==null&&temp.getInitializer()==null)
checkError("\""+temp.getName()+"\" array initial should figur the size out",temp.getName());
if (!t.isArrayType()&&temp.getInitializer() instanceof ArrayInitializer)
checkError("\""+temp.getName()+"\" no array initial need here",temp.getName());
before.remove(temp.getName());
}
dumpList(before);
super.endVisit(n);
}
public void endVisit(MethodInvocation n) {
int i;
if (n.getName().getIdentifier().equals("print")) return;
if (n.getName().getIdentifier().equals("read")){
for (i=0;i<n.arguments().size();i++){
Type tempt = (Type) ((Expression)n.arguments().get(i)).getProperty("type");
tempt.setProperty("done", new Integer(1));
if (!isInt(tempt)) checkError("read Parameters shoud be int",n);
if (tempt.getProperty("final")!=null) checkError("read Parameters shoudn't be final",n);
before.clear();
return;
}
}
SimpleName temp = ast.newSimpleName("temp");
LinkedList dacList = (LinkedList)methodTable.get(n.getName().getIdentifier());
LinkedList parList = new LinkedList();
parList.addAll(n.arguments());
if (dacList.size()!=parList.size()+1) checkError(n.toString()+" incorrect arguments size",n);
else
for (i=0;i<parList.size();i++){
temp.setProperty("type", dacList.get(i+1));
if (!match((Expression)parList.get(i),temp))
checkError("\""+n.toString()+"\" the "+i+"th parameter not match",n);
if (((Type)((Expression)parList.get(i)).getProperty("type")).getProperty("final")!=null)
checkError("\""+n.toString()+"\" the "+i+"th par shouldn't be final",n);
}
n.setProperty("type", (Type)dacList.get(0));
dumpList(before);
super.endVisit(n);
}
public void endVisit(Assignment n) {
if (n.getLeftHandSide().getProperty("type")!=null&&n.getRightHandSide().getProperty("type")!=null){
if (((Type)n.getLeftHandSide().getProperty("type")).getProperty("final")!=null)
checkError("\""+n.getLeftHandSide().toString()+"\" final lvalue error",n);
else
if (!match(n.getLeftHandSide(),n.getRightHandSide()))
checkError("type unmatch between the two side of this assignment",n);
if (((Type)n.getLeftHandSide().getProperty("type")).isPrimitiveType()
||((Type)n.getLeftHandSide().getProperty("type")).isSimpleType()){
before.remove(n.getLeftHandSide());
((Type)n.getLeftHandSide().getProperty("type")).setProperty("done", new Integer(1));
}
}
dumpList(before);
super.endVisit(n);
}
public void endVisit(InfixExpression n) {
if (n.getLeftOperand().getProperty("type")!=null&&n.getRightOperand().getProperty("type")!=null){
Type tx = (Type)n.getLeftOperand().getProperty("type");
Type ty = (Type)n.getRightOperand().getProperty("type");
if (!match(n.getLeftOperand(),n.getRightOperand())) checkError(n.toString()+" types unmatch in the expression",n);
else
{
if (isIntOP(n.getOperator())&&isInt(tx)) n.setProperty("type", tx);
else if (isBIop(n.getOperator())&&isInt(tx)) n.setProperty("type", ast.newPrimitiveType(PrimitiveType.BOOLEAN));
else if (isBBop(n.getOperator())&&!isInt(tx)) n.setProperty("type", ast.newPrimitiveType(PrimitiveType.BOOLEAN));
else checkError(n+" wrong Expression,can't be computed",n);
n.setProperty("value", compute(n));
}
super.endVisit(n);
}
}
public void endVisit(PrefixExpression n) {
Type t = (Type)n.getOperand().getProperty("type");
if (t!=null)
if (isInt(t)&&(n.getOperator()==PrefixExpression.Operator.MINUS
||n.getOperator()==PrefixExpression.Operator.PLUS))
{
n.setProperty("type", ast.newPrimitiveType(PrimitiveType.INT));
n.setProperty("value", compute(n));
}
else if (isBool(t)&&n.getOperator()==PrefixExpression.Operator.NOT)
n.setProperty("type", ast.newPrimitiveType(PrimitiveType.BOOLEAN));
else checkError(n+"wrong prefixexpression",n);
super.endVisit(n);
}
public void endVisit(ParenthesizedExpression n) {
n.setProperty("type", (Type)n.getExpression().getProperty("type"));
n.setProperty("value", n.getExpression().getProperty("value"));
super.endVisit(n);
}
public void endVisit(ArrayAccess n) {
if (!((Type)n.getArray().getProperty("type")).isArrayType()||!isInt((Type)n.getIndex().getProperty("type")))
checkError("\""+n+"\" wrong Arrayaccess(not a array type or the index is incorrect)",n);
else {
n.setProperty("type", ((ArrayType)n.getArray().getProperty("type")).getElementType());
((Type)n.getProperty("type")).setProperty("final",((Type)n.getArray().getProperty("type")).getProperty("final"));
if (((Type)n.getArray().getProperty("type")).getProperty("final")!=null&&n.getIndex().getProperty("value")!=null)
n.setProperty("value", finalvisit((Expression)((ArrayInitializer)((Type)n.getArray().getProperty("type")).getProperty("init")).expressions().get((Integer)n.getIndex().getProperty("value"))));
}
super.endVisit(n);
}
public boolean visit(SimpleName n) {
ListIterator iter = runStack.listIterator(runStack.size());
HashMap temp = curTable;
boolean hasDefined = false;
while(iter.hasPrevious()){
hasDefined = temp.containsKey(n.getIdentifier());
if (hasDefined) break;
temp = (HashMap)iter.previous();
}
Type t = (Type)temp.get(n.getIdentifier());
n.setProperty("type", t);
if (t!=null) {
if (t.getProperty("final")!=null&&t.isPrimitiveType()){
if (((Expression)t.getProperty("init")).getProperty("value")==null){
finalvisit((Expression)t.getProperty("init"));
}
n.setProperty("value",((Expression)t.getProperty("init")).getProperty("value"));
}
if (t.isArrayType()&&t.getProperty("size")!=null) {
finalvisit((Expression)t.getProperty("size"));
n.setProperty("sizevalue", ((Expression)t.getProperty("size")).getProperty("value"));
}
}
if (t!=null)
if (t.getProperty("done")==null&&!t.isArrayType()){
before.add(n);
}
return super.visit(n);
}
public void endVisit(QualifiedName n) {
Type t=(Type)n.getQualifier().getProperty("type");
if (t.isArrayType()){
n.setProperty("type", ast.newPrimitiveType(PrimitiveType.INT));
if (t.getProperty("size")!=null)
n.setProperty("value",((Expression)t.getProperty("size")).getProperty("value"));
}
else{
n.setProperty("type", ast.newPrimitiveType(PrimitiveType.INT));
n.setProperty("value", new Integer(1));
}
super.endVisit(n);
}
public void endVisit(ArrayType n) {
if ((Expression)n.getProperty("size")!=null){
finalvisit((Expression)n.getProperty("size"));
if (((Expression)n.getProperty("size")).getProperty("value")==null)
checkError(n.toString()+" not constant length for array",n);
}
super.endVisit(n);
}
public boolean visit(NumberLiteral n) {
n.setProperty("type" ,ast.newPrimitiveType(PrimitiveType.INT));
return super.visit(n);
}
public boolean visit(StringLiteral n) {
n.setProperty("type", ast.newSimpleType(ast.newSimpleName("String")));
return super.visit(n);
}
public boolean visit(BooleanLiteral n) {
n.setProperty("type", ast.newPrimitiveType(PrimitiveType.BOOLEAN));
return super.visit(n);
}
public void endVisit(ReturnStatement n) {
LinkedList dacList = (LinkedList)methodTable.get(methodTag);
if (n.getExpression()==null){
if (!submatch((Type)dacList.get(0),ast.newPrimitiveType(PrimitiveType.VOID)))
checkError("return expression needed",n);
}
else if (n.getExpression().getProperty("type")!=null){
if (!submatch((Type)n.getExpression().getProperty("type"),(Type)dacList.get(0)))
checkError("the return type is wrong",n);
}
if (n.getParent() instanceof Block) n.getParent().setProperty("return", new Boolean(true));
else n.setProperty("return", new Boolean(true));
dumpList(before);
super.endVisit(n);
}
public void endVisit(IfStatement n) {
if (!isBool((Type)n.getExpression().getProperty("type")))
checkError("the expression in the ifstatement should be boolean",n.getExpression());
if (n.getThenStatement().getProperty("return")!=null)
if (n.getElseStatement()==null)
if (n.getParent() instanceof Block) n.getParent().setProperty("return", new Boolean(true));
else n.setProperty("return", new Boolean(true));
else if (n.getElseStatement().getProperty("return")!=null)
if (n.getParent() instanceof Block) n.getParent().setProperty("return", new Boolean(true));
else n.setProperty("return", new Boolean(true));
dumpList(before);
super.endVisit(n);
}
public void endVisit(WhileStatement n) {
if (!isBool((Type)n.getExpression().getProperty("type")))
checkError("the expression in the whilestatement should be boolean",n.getExpression());
if (n.getBody().getProperty("return")!=null)
if (n.getParent() instanceof Block) n.getParent().setProperty("return", new Boolean(true));
else n.setProperty("return", new Boolean(true));
dumpList(before);
super.endVisit(n);
}
// TODO: 添加visit()方法,遍历各个AST节点,进行语义检查
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -