Skip to content
Snippets Groups Projects
Commit 71869dc0 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

added incremental ASTbuilding for typedefs

parent f6f91400
No related branches found
No related tags found
No related merge requests found
......@@ -32,6 +32,8 @@ public class TDDecoder
private DecoderChannel decoder;
private TypeDefParser tdp;
private final ASTbuilder astBuilder;
private Program curAST;
public TDDecoder(InputStream in)
throws Exception
......@@ -45,6 +47,7 @@ public class TDDecoder
intAndRef.register(decoder, this);
doavoid.registerSampleRef(decoder);
this.tdp = TypeDefParser.registerTypeDefParser(decoder);
this.astBuilder = new ASTbuilder(tdp);
// TypeDef.register(decoder, this);
// TypeBinding.register(decoder, this);
......@@ -78,8 +81,7 @@ public class TDDecoder
public void onTypeDef(TypeDefParser.ParsedTypeDef d) {
if(d.isSampleDef()){
System.out.println("onTypeDef (sample): ");
ASTbuilder v = new ASTbuilder();
Program p = v.makeProgram((TypeDefParser.ParsedSampleDef) d);
Program p = astBuilder.makeProgram(d, curAST);
try {
//FileOutputStream f = new FileOutputStream("/tmp/foopp"+d.getName()+".txt");
//PrintStream out = new PrintStream(f);
......@@ -91,6 +93,7 @@ public class TDDecoder
System.err.println("Exception: " + e);
e.printStackTrace();
}
curAST = p;
}
//System.out.println(" "+d.getName()+";");
//for(byte b: d.getSignature()) {
......
......@@ -43,10 +43,12 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
private LinkedList<Type> typeStack;
private LinkedList<Field> fieldStack;
private TypeDefParser tdp;
public ASTbuilder() {
public ASTbuilder(TypeDefParser tdp) {
this.typeStack = new LinkedList<Type>();
this.fieldStack = new LinkedList<Field>();
this.tdp = tdp;
}
private void assertStacksEmpty() throws RuntimeException {
......@@ -145,33 +147,75 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
}
}
/** Create a labcomm AST for the ParsedTypeDef d.
* If d is a sampleDecl, include the typedefs it depends on.
*/
public Program makeProgram(TypeDefParser.ParsedTypeDef d) {
assertStacksEmpty();
List<Decl> ds = new List<Decl>();
return makeProgram(d, ds);
}
ds.add(makeDecl(d));
assertStacksEmpty();
return createAndCheckProgram(ds);
/** Create a labcomm AST for the ParsedTypeDef d, including
* all declarations from p.
*
* This copies the declarations in p, and creates a new AST.
*
* If d is a sampleDecl, include the typedefs it depends on.
*/
public Program makeProgram(TypeDefParser.ParsedTypeDef d,
Program p) {
if(p != null) {
return makeProgram(d, p.getDecls().fullCopy());
} else {
return makeProgram(d);
}
}
public Decl makeDecl(TypeDefParser.ParsedSampleDef d) {
d.getType().accept(this);
Decl result = new SampleDecl(typeStack.pop(), d.getName());
return result;
//TODO: The above method, adding to an arbitrary Program
// does not really allow "recreating the labcomm file"
// This method is a placeholder for
public Program makeProgram(TypeDefParser.ParsedTypeDef d,
TypeDefParser source) {
throw new Error("Not yet implemented");
}
public Program makeProgram(TypeDefParser.ParsedSampleDef d) {
private void tryAdd(Decl decl, List<Decl> ds){
boolean exists = false;
// Check if decl already exists in ds
// otherwise add it
// Maybe add a Program.lookupDecl(String Name)
// to the NameAnalysis aspect instead of doing
// it here.
Iterator<Decl> it = ds.iterator();
while(!exists && it.hasNext()) {
Decl inList = it.next();
if(inList.getName().equals(decl.getName())){
exists = true;
}
}
if(!exists) {
ds.add(decl);
}
}
protected Program makeProgram(TypeDefParser.ParsedTypeDef d,
List<Decl> ds) {
assertStacksEmpty();
List<Decl> ds = new List<Decl>();
// add dependencies for sample decls
if(d.isSampleDef()) {
Iterator<TypeDefParser.ParsedTypeDef> it = d.getDepIterator();
while(it.hasNext()){
ds.add(makeDecl(it.next()));
Decl decl = makeDecl(it.next());
tryAdd(decl,ds);
}
}
ds.add(makeDecl(d));
assertStacksEmpty();
return createAndCheckProgram(ds);
}
}
......@@ -420,6 +420,10 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
return false;
}
Iterator<ParsedTypeDef> getDepIterator() {
throw new Error("ParseTypeDef has no dependencies");
}
void setType(ParsedType type) {
this.type = type;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment