public void setSuper(String superclass) {
this.superclass=superclass;
}
+
+ protected String getSuper() {
+ return superclass;
+ }
}
return new NodePair(fln,fln);
}
-
private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
TypeDescriptor td=con.getType();
FlatNew fn=new FlatNew(td, out_temp);
FlatNode last=null;
TempDescriptor thisarg=null;
- if (min.getExpression()==null) {
+ if (min.getExpression()!=null) {
thisarg=TempDescriptor.tempFactory("thisarg");
- NodePair np=flattenExpressionNode(min.getExpression(),temps[0]);
+ NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
first=np.getBegin();
last=np.getEnd();
}
protected Vector next;
protected Vector prev;
+ public FlatNode() {
+ next=new Vector();
+ prev=new Vector();
+ }
+
public String toString() {
throw new Error();
}
package IR.Tree;
+import IR.TypeDescriptor;
+import IR.TypeUtil;
+
public class LiteralNode extends ExpressionNode {
public final static int INTEGER=1;
public final static int NULL=6;
Object value;
- int type;
-
+ TypeDescriptor type;
+ String typestr;
+
public LiteralNode(String type, Object o) {
- this.type=parseType(type);
+ typestr=type;
value=o;
+ type=null;
}
- public Object getValue() {
- return value;
+ public TypeDescriptor getType() {
+ return type;
}
- private static int parseType(String type) {
- if (type.equals("integer"))
- return INTEGER;
- else if (type.equals("float"))
- return FLOAT;
- else if (type.equals("boolean"))
- return BOOLEAN;
- else if (type.equals("char"))
- return CHAR;
- else if (type.equals("string"))
- return STRING;
- else if (type.equals("null"))
- return NULL;
- else throw new Error();
+ public void setType(TypeDescriptor td) {
+ type=td;
}
- private String getStringType() {
- if (type==INTEGER)
- return "integer";
- else if (type==FLOAT)
- return "float";
- else if (type==BOOLEAN)
- return "boolean";
- else if (type==CHAR)
- return "char";
- else if (type==STRING)
- return "string";
- else if (type==NULL)
- return "null";
- else throw new Error();
-
+ public Object getValue() {
+ return value;
}
public String printNode(int indent) {
- if (type==NULL)
+ if (typestr.equals("null"))
return "null";
- if (type==STRING) {
+ if (typestr.equals("string")) {
return '"'+escapeString(value.toString())+'"';
}
- return "/*"+getType()+ "*/"+value.toString();
+ return "/*"+typestr+ "*/"+value.toString();
}
private static String escapeString(String st) {
String new_st="";
public class SemanticCheck {
State state;
+ TypeUtil typeutil;
- public SemanticCheck(State state) {
+ public SemanticCheck(State state, TypeUtil tu) {
this.state=state;
+ this.typeutil=tu;
}
public void semanticCheck() {
nametable.add(vd);
} else
throw new Error(vd.getSymbol()+" defined a second time");
- checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
+ if (dn.getExpression()!=null)
+ checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
}
void checkSubBlockNode(MethodDescriptor md, SymbolTable nametable, SubBlockNode sbn) {
}
void checkLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
+
}
void checkMethodInvokeNode(MethodDescriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
--- /dev/null
+package IR;
+import java.util.*;
+
+public class TypeUtil {
+ public static final String StringClass="java.lang.String";
+ State state;
+ Hashtable supertable;
+
+ public TypeUtil(State state) {
+ this.state=state;
+ createTables();
+ }
+
+ public ClassDescriptor getClass(String classname) {
+ ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
+ return cd;
+ }
+
+ private void createTables() {
+ supertable=new Hashtable();
+ Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
+ while(classit.hasNext()) {
+ ClassDescriptor cd=(ClassDescriptor)classit.next();
+ String superc=cd.getSuper();
+ if (superc!=null) {
+ ClassDescriptor cd_super=getClass(superc);
+ supertable.put(cd,cd_super);
+ }
+ }
+ }
+
+ public ClassDescriptor getSuper(ClassDescriptor cd) {
+ return (ClassDescriptor)supertable.get(cd);
+ }
+
+ public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
+ if (possiblesuper==cd2)
+ return true;
+ else
+ return isSuper(possiblesuper, cd2);
+ }
+
+ public boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
+ while(cd2!=null) {
+ cd2=getSuper(cd2);
+ if (cd2==possiblesuper)
+ return true;
+ }
+ return false;
+ }
+}
import IR.Tree.SemanticCheck;
import IR.Flat.BuildFlat;
import IR.State;
+import IR.TypeUtil;
public class Main {
public static void main(String args[]) throws Exception {
BuildIR bir=new BuildIR(state);
bir.buildtree();
- SemanticCheck sc=new SemanticCheck(state);
+ TypeUtil tu=new TypeUtil(state);
+
+ SemanticCheck sc=new SemanticCheck(state,tu);
sc.semanticCheck();
BuildFlat bf=new BuildFlat(state);