From: bdemsky Date: Thu, 9 Feb 2006 23:37:52 +0000 (+0000) Subject: adding/chaning many files X-Git-Tag: preEdgeChange~991 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=86c60fe2a46a8bbf0a13ac3dbc2b4901a76bf4a2;p=IRC.git adding/chaning many files --- diff --git a/Robust/src/IR/Descriptor.java b/Robust/src/IR/Descriptor.java new file mode 100644 index 00000000..81e327c7 --- /dev/null +++ b/Robust/src/IR/Descriptor.java @@ -0,0 +1,42 @@ +package IR; + +/** + * Descriptor + * + * represents a symbol in the language (var name, function name, etc). + */ + +public abstract class Descriptor { + + protected String name; + protected String safename; + static int count=0; + int uniqueid; + + public Descriptor(String name) { + this.name = name; + this.safename = "__" + name + "__"; + this.uniqueid=count++; + } + + protected Descriptor(String name, String safename) { + this.name = name; + this.safename = safename; + this.uniqueid=count++; + } + + public String toString() { + return name; + } + + public String getSymbol() { + return name; + } + + public String getSafeSymbol() { + return safename; + } + public int getNum() { + return uniqueid; + } +} diff --git a/Robust/src/IR/FieldDescriptor.java b/Robust/src/IR/FieldDescriptor.java new file mode 100644 index 00000000..5375510c --- /dev/null +++ b/Robust/src/IR/FieldDescriptor.java @@ -0,0 +1,26 @@ +package IR; +import IR.Tree.Modifiers; + +/** + * Descriptor + * + * represents a symbol in the language (var name, function name, etc). + */ + +public class FieldDescriptor extends Descriptor { + + protected Modifiers modifier; + protected TypeDescriptor td; + + public FieldDescriptor(Modifiers m, TypeDescriptor t, String name) { + super(name); + this.modifier=m; + this.td=t; + this.safename = "__" + name + "__"; + this.uniqueid=count++; + } + + public String toString() { + return modifier.toString()+";"; + } +} diff --git a/Robust/src/IR/State.java b/Robust/src/IR/State.java index e88b87ce..cb6c22d5 100644 --- a/Robust/src/IR/State.java +++ b/Robust/src/IR/State.java @@ -1,4 +1,12 @@ -class State { +package IR; +import IR.Tree.*; +public class State { + public State(ParseNode parsetree) { + globals=new SymbolTable(); + this.parsetree=parsetree; + } + public SymbolTable globals; + public ParseNode parsetree; } diff --git a/Robust/src/IR/SymbolTable.java b/Robust/src/IR/SymbolTable.java new file mode 100644 index 00000000..7203183e --- /dev/null +++ b/Robust/src/IR/SymbolTable.java @@ -0,0 +1,140 @@ +package IR; + +import java.util.*; + +public class SymbolTable { + + private Hashtable table; + private SymbolTable parent; + + public SymbolTable() { + table = new Hashtable(); + this.parent = null; + } + + public SymbolTable(SymbolTable parent) { + table = new Hashtable(); + this.parent = parent; + } + + //public void add(String name, Descriptor d) { + //table.put(name, d); + //} + + public void add(Descriptor d) { + table.put(d.getSymbol(), d); + } + + public void add(String name, Descriptor d) { + table.put(name, d); + + } + + public void dump() { + Enumeration e = getDescriptors(); + while (e.hasMoreElements()) { + Descriptor d = (Descriptor) e.nextElement(); + System.out.println(d.getSymbol()); + } + if (parent != null) { + System.out.println("parent:"); + parent.dump(); + } + } + + public Descriptor get(String name) { + Descriptor d = (Descriptor) table.get(name); + if (d == null && parent != null) { + return parent.get(name); + } else { + return d; + } + } + + public Descriptor getFromSameScope(String name) { + return (Descriptor)table.get(name); + } + + public Enumeration getNames() { + return table.keys(); + } + + public Enumeration getDescriptors() { + return table.elements(); + } + + public Iterator descriptors() { + return table.values().iterator(); + } + + public Vector getAllDescriptors() { + Vector d; + if (parent == null) { + d = new Vector(); + } else { + d = parent.getAllDescriptors(); + } + + Enumeration e = getDescriptors(); + while(e.hasMoreElements()) { + d.addElement(e.nextElement()); + } + + return d; + } + + public boolean contains(String name) { + return (get(name) != null); + } + + + public int size() { + return table.size(); + } + + public int sizeAll() { + if (parent != null) { + return parent.sizeAll() + table.size(); + } else { + return table.size(); + } + } + + public SymbolTable getParent() { + return parent; + } + + public void setParent(SymbolTable parent) { + this.parent = parent; + } + + /** + * Adds contents of st2.table to this.table and returns a + * Vector of shared names, unless there are no shared names, + * in which case returns null. + */ + public Vector merge(SymbolTable st2) { + Vector v = new Vector(); + Enumeration names = st2.table.keys(); + + while (names.hasMoreElements()) { + Object o = names.nextElement(); + + if (table.containsKey(o)) { + v.addElement(o); + } else { + table.put(o, st2.table.get(o)); + } + } + + if (v.size() == 0) { + return null; + } else { + return v; + } + } + + public String toString() { + return "ST: " + table.toString(); + } +} diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java new file mode 100644 index 00000000..41594c4b --- /dev/null +++ b/Robust/src/IR/Tree/BuildIR.java @@ -0,0 +1,141 @@ +package IR.Tree; +import IR.*; + +public class BuildIR { + State state; + public BuildIR(State state) { + this.state=state; + } + public void buildtree() { + ParseNode pn=state.parsetree; + FileNode fn=parseFile(pn); + System.out.println(fn.printNode()); + } + + /** Parse the classes in this file */ + public FileNode parseFile(ParseNode pn) { + FileNode fn=new FileNode(); + ParseNode tpn=pn.getChild("type_declaration_list"); + if (tpn!=null) { + ParseNodeVector pnv=tpn.getChildren(); + for(int i=0;i @@ -23,6 +25,9 @@ public class Main { g = new Parse.Parser(l); ParseNode p=(ParseNode) g./*debug_*/parse().value; System.out.println(p.PPrint(4,true)); + State state=new State(p); + BuildIR bir=new BuildIR(state); + bir.buildtree(); System.exit(l.numErrors()); } } diff --git a/Robust/src/Makefile b/Robust/src/Makefile index e3edabec..a32fec3b 100644 --- a/Robust/src/Makefile +++ b/Robust/src/Makefile @@ -9,7 +9,10 @@ Lex/NumericLiteral.class Lex/Operator.class Lex/Separator.class \ Lex/StringLiteral.class Lex/Token.class Lex/TraditionalComment.class \ Lex/WhiteSpace.class IR/Tree/ParseNode.class \ IR/Tree/ParseNodeDOTVisitor.class IR/Tree/ParseNodeVector.class \ -IR/Tree/Walkable.class +IR/Tree/Walkable.class IR/State.class IR/SymbolTable.class \ +IR/Descriptor.class IR/Tree/Modifiers.class IR/Tree/FileNode.class \ +IR/Tree/ClassNode.java IR/Tree/TreeNode.class IR/Tree/BuildIR.class \ +IR/TypeDescriptor.java IR/FieldDescriptor.java all: Parse/Sym.class Parse/Parser.class $(CLASSFILES) diff --git a/Robust/src/Parse/java14.cup b/Robust/src/Parse/java14.cup index 66cd5dd3..c6c34bc8 100644 --- a/Robust/src/Parse/java14.cup +++ b/Robust/src/Parse/java14.cup @@ -221,7 +221,7 @@ start with goal; // 19.2) The Syntactic Grammar goal ::= compilation_unit:cu {: - RESULT = (new ParseNode("goal")).addChild(cu).getRoot(); + RESULT = cu; :} ; @@ -324,7 +324,7 @@ compilation_unit ::= // import_declarations_opt type_declarations_opt:tdo {: ParseNode pn=new ParseNode("compilation_unit"); - pn.addChild("types").addChild(tdo); + pn.addChild(tdo); RESULT=pn; :} ; @@ -370,7 +370,7 @@ type_declarations ::= type_declaration ::= class_declaration:cd {: - RESULT=(new ParseNode("type_declaration")).addChild("class").addChild(cd); + RESULT=cd; :} // | interface_declaration | SEMICOLON {: RESULT=new ParseNode("empty"); :} @@ -412,28 +412,19 @@ modifier ::= // 19.8.1) Class Declaration: class_declaration ::= - modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so {: - ParseNode pn=new ParseNode("class_declaration"); - pn.addChild("modifiers").addChild(mo); - pn.addChild("name").addChild(id); - pn.addChild("super").addChild(so); - RESULT=pn; - :} -//interfaces_opt + modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so //interfaces_opt class_body:body {: ParseNode pn=new ParseNode("class_declaration"); pn.addChild("modifiers").addChild(mo); pn.addChild("name").addChild(id); pn.addChild("super").addChild(so); - pn.addChild("body").addChild(body); + pn.addChild("classbody").addChild(body); RESULT=pn; :} ; super ::= EXTENDS class_type:classtype {: - ParseNode pn=new ParseNode("super"); - pn.addChild(classtype); - RESULT=pn; + RESULT=classtype; :} ; super_opt ::= @@ -474,22 +465,22 @@ class_body_declarations ::= class_body_declaration ::= class_member_declaration:member {: - RESULT=(new ParseNode("class_body_declaration")).addChild("member").addChild(member).getRoot(); + RESULT=(new ParseNode("member")).addChild(member).getRoot(); :} // | static_initializer | constructor_declaration:constructor {: - RESULT=(new ParseNode("class_body_declaration")).addChild("constructor").addChild(constructor).getRoot(); + RESULT=(new ParseNode("constructor")).addChild(constructor).getRoot(); :} | block:block {: - RESULT=(new ParseNode("class_body_declaration")).addChild("block").addChild(block).getRoot(); + RESULT=(new ParseNode("block")).addChild(block).getRoot(); :} ; class_member_declaration ::= field_declaration:field {: - RESULT=(new ParseNode("class_member_declaration")).addChild("field").addChild(field).getRoot(); + RESULT=(new ParseNode("field")).addChild(field).getRoot(); :} | method_declaration:method {: - RESULT=(new ParseNode("class_member_declaration")).addChild("method").addChild(method).getRoot(); + RESULT=(new ParseNode("method")).addChild(method).getRoot(); :} /* repeat the prod for 'class_declaration' here: */ // | modifiers_opt CLASS IDENTIFIER super_opt class_body