From 748e9a88e0d20b9c2bad729669239ea0a8940a86 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 10 Feb 2006 01:30:35 +0000 Subject: [PATCH] changes` --- Robust/src/IR/FieldDescriptor.java | 14 ++++-- Robust/src/IR/NameDescriptor.java | 25 ++++++++++ Robust/src/IR/State.java | 8 ++++ Robust/src/IR/Tree/BuildIR.java | 65 +++++++++++++++++++++++--- Robust/src/IR/Tree/ClassNode.java | 5 +- Robust/src/IR/Tree/ExpressionNode.java | 8 ++++ Robust/src/IR/TypeDescriptor.java | 47 +++++++++++++++++-- Robust/src/Parse/java14.cup | 4 +- 8 files changed, 160 insertions(+), 16 deletions(-) create mode 100644 Robust/src/IR/NameDescriptor.java create mode 100644 Robust/src/IR/Tree/ExpressionNode.java diff --git a/Robust/src/IR/FieldDescriptor.java b/Robust/src/IR/FieldDescriptor.java index 5375510c..367d44bd 100644 --- a/Robust/src/IR/FieldDescriptor.java +++ b/Robust/src/IR/FieldDescriptor.java @@ -1,5 +1,6 @@ package IR; import IR.Tree.Modifiers; +import IR.Tree.ExpressionNode; /** * Descriptor @@ -11,16 +12,23 @@ public class FieldDescriptor extends Descriptor { protected Modifiers modifier; protected TypeDescriptor td; + protected String identifier; + protected ExpressionNode en; - public FieldDescriptor(Modifiers m, TypeDescriptor t, String name) { - super(name); + public FieldDescriptor(Modifiers m, TypeDescriptor t, String identifier, ExpressionNode e) { + super(identifier); this.modifier=m; this.td=t; + this.identifier=identifier; + this.en=e; this.safename = "__" + name + "__"; this.uniqueid=count++; } public String toString() { - return modifier.toString()+";"; + if (en==null) + return modifier.toString()+td.toString()+" "+identifier+";"; + else + return modifier.toString()+td.toString()+" "+identifier+"="+en.toString()+";"; } } diff --git a/Robust/src/IR/NameDescriptor.java b/Robust/src/IR/NameDescriptor.java new file mode 100644 index 00000000..65b3b64e --- /dev/null +++ b/Robust/src/IR/NameDescriptor.java @@ -0,0 +1,25 @@ +package IR; + +public class NameDescriptor extends Descriptor { + String identifier; + NameDescriptor nd; + public NameDescriptor(NameDescriptor nd, String id) { + super(nd.toString()+"."+id); + identifier=id; + this.nd=nd; + } + + public NameDescriptor(String id) { + super(id); + identifier=id; + nd=null; + } + + public String toString() { + if (nd==null) + return identifier; + else + return nd+"."+identifier; + } + +} diff --git a/Robust/src/IR/State.java b/Robust/src/IR/State.java index cb6c22d5..c6c34a1e 100644 --- a/Robust/src/IR/State.java +++ b/Robust/src/IR/State.java @@ -9,4 +9,12 @@ public class State { public SymbolTable globals; public ParseNode parsetree; + public static TypeDescriptor getTypeDescriptor(int t) { + TypeDescriptor td=new TypeDescriptor(t); + return td; + } + public static TypeDescriptor getTypeDescriptor(NameDescriptor n) { + TypeDescriptor td=new TypeDescriptor(n); + return td; + } } diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index 41594c4b..84ac2528 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -61,8 +61,7 @@ public class BuildIR { ParseNode fieldnode=pn.getChild("field"); if (fieldnode!=null) { - FieldDescriptor fd=parseFieldDecl(fieldnode.getChild("field_declaration")); - cn.addField(fd); + parseFieldDecl(cn,fieldnode.getChild("field_declaration")); return; } ParseNode methodnode=pn.getChild("method"); @@ -73,12 +72,68 @@ public class BuildIR { throw new Error(); } - private FieldDescriptor parseFieldDecl(ParseNode pn) { + private TypeDescriptor parseTypeDescriptor(ParseNode pn) { + ParseNode tn=pn.getChild("type"); + String type_st=tn.getTerminal(); + if(type_st.equals("byte")) { + return state.getTypeDescriptor(TypeDescriptor.BYTE); + } else if(type_st.equals("short")) { + return state.getTypeDescriptor(TypeDescriptor.SHORT); + } else if(type_st.equals("int")) { + return state.getTypeDescriptor(TypeDescriptor.INT); + } else if(type_st.equals("long")) { + return state.getTypeDescriptor(TypeDescriptor.LONG); + } else if(type_st.equals("char")) { + return state.getTypeDescriptor(TypeDescriptor.CHAR); + } else if(type_st.equals("float")) { + return state.getTypeDescriptor(TypeDescriptor.FLOAT); + } else if(type_st.equals("double")) { + return state.getTypeDescriptor(TypeDescriptor.DOUBLE); + } else if(type_st.equals("class")) { + ParseNode nn=tn.getChild("class"); + return state.getTypeDescriptor(parseName(nn)); + } else + throw new Error(); + } + + private NameDescriptor parseName(ParseNode pn) { + ParseNode nn=pn.getChild("name"); + ParseNode base=nn.getChild("base"); + ParseNode id=nn.getChild("identifier"); + + if (base==null) + return new NameDescriptor(id.getTerminal()); + + return new NameDescriptor(parseName(base),id.getTerminal()); + + } + + private void parseFieldDecl(ClassNode cn,ParseNode pn) { ParseNode mn=pn.getChild("modifier"); Modifiers m=parseModifiersList(mn); - return new FieldDescriptor(m,null,null); + ParseNode tn=pn.getChild("type"); + TypeDescriptor t=parseTypeDescriptor(tn); + ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list"); + ParseNodeVector pnv=vn.getChildren(); + for(int i=0;i