Commit cc8f66ad authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

rename and cleanup

parent 64a8c45d
...@@ -6,7 +6,7 @@ import java.io.IOException; ...@@ -6,7 +6,7 @@ import java.io.IOException;
import se.lth.control.labcomm.DecoderChannel; import se.lth.control.labcomm.DecoderChannel;
import se.lth.control.labcomm.TypeDef; import se.lth.control.labcomm.TypeDef;
import se.lth.control.labcomm.TypeDefParser; import se.lth.control.labcomm.TypeDefParser;
import se.lth.control.labcomm.TypeDefVisitor; import se.lth.control.labcomm.ASTbuilder;
//import se.lth.control.labcomm.TypeBinding; //import se.lth.control.labcomm.TypeBinding;
import se.lth.control.labcomm2014.compiler.Program; import se.lth.control.labcomm2014.compiler.Program;
...@@ -78,27 +78,18 @@ public class TDDecoder ...@@ -78,27 +78,18 @@ public class TDDecoder
public void onTypeDef(TypeDefParser.ParsedTypeDef d) { public void onTypeDef(TypeDefParser.ParsedTypeDef d) {
if(d.isSampleDef()){ if(d.isSampleDef()){
System.out.println("onTypeDef (sample): "); System.out.println("onTypeDef (sample): ");
TypeDefVisitor v = new TypeDefVisitor(); ASTbuilder v = new ASTbuilder();
Program p = v.makeProgram((TypeDefParser.ParsedSampleDef) d); Program p = v.makeProgram((TypeDefParser.ParsedSampleDef) d);
LinkedList errors = new LinkedList(); try {
p.errorCheck(errors);
if(errors.isEmpty()) {
try {
//FileOutputStream f = new FileOutputStream("/tmp/foopp"+d.getName()+".txt"); //FileOutputStream f = new FileOutputStream("/tmp/foopp"+d.getName()+".txt");
//PrintStream out = new PrintStream(f); //PrintStream out = new PrintStream(f);
p.pp(System.out); p.pp(System.out);
//p.C_genC(System.out, new Vector(), "lcname", "prefix", 2014); //p.C_genC(System.out, new Vector(), "lcname", "prefix", 2014);
//p.J_gen(out, "testpackage", 2014); //p.J_gen(out, "testpackage", 2014);
//out.close(); //out.close();
} catch (Throwable e) { } catch (Throwable e) {
System.err.println("Exception: " + e); System.err.println("Exception: " + e);
e.printStackTrace(); e.printStackTrace();
}
} else {
for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
String s = (String)iter.next();
System.out.println(s);
}
} }
} }
//System.out.println(" "+d.getName()+";"); //System.out.println(" "+d.getName()+";");
......
...@@ -13,7 +13,7 @@ MODULES=Constant \ ...@@ -13,7 +13,7 @@ MODULES=Constant \
BuiltinType \ BuiltinType \
TypeDef \ TypeDef \
TypeBinding \ TypeBinding \
TypeDefVisitor \ ASTbuilder \
TypeDefParser \ TypeDefParser \
Writer \ Writer \
WriterWrapper WriterWrapper
......
...@@ -40,7 +40,7 @@ import se.lth.control.labcomm2014.compiler.VariableSize; ...@@ -40,7 +40,7 @@ import se.lth.control.labcomm2014.compiler.VariableSize;
/** A class for building a JastAdd AST from the parsed types /** A class for building a JastAdd AST from the parsed types
* created by a TypeDefParser. This class depends on the LabComm compiler. * created by a TypeDefParser. This class depends on the LabComm compiler.
*/ */
public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor { public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
///// tree building ///// tree building
// //
...@@ -48,11 +48,20 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor { ...@@ -48,11 +48,20 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
private LinkedList<Type> typeStack; private LinkedList<Type> typeStack;
private LinkedList<Field> fieldStack; private LinkedList<Field> fieldStack;
public TypeDefVisitor() { public ASTbuilder() {
this.typeStack = new LinkedList<Type>(); this.typeStack = new LinkedList<Type>();
this.fieldStack = new LinkedList<Field>(); this.fieldStack = new LinkedList<Field>();
} }
private void assertStacksEmpty() throws RuntimeException {
if(!typeStack.isEmpty()) {
throw new RuntimeException("Error: type stack not empty");
}
if(!fieldStack.isEmpty()) {
throw new RuntimeException("Error: field stack not empty");
}
}
public void visit(TypeDefParser.TypeSymbol s){ public void visit(TypeDefParser.TypeSymbol s){
throw new Error("not implemented? needed?"); throw new Error("not implemented? needed?");
...@@ -119,12 +128,31 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor { ...@@ -119,12 +128,31 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
Decl result = new TypeDecl(typeStack.pop(), d.getName()); Decl result = new TypeDecl(typeStack.pop(), d.getName());
return result; return result;
} }
private Program createAndCheckProgram(List<Decl> ds) {
Program p = new Program(ds);
LinkedList errors = new LinkedList();
p.errorCheck(errors);
if(errors.isEmpty()) {
return p;
} else {
//XXX temporary debug printout
for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
String s = (String)iter.next();
System.err.println(s);
}
// This should not happen
throw new RuntimeException("Internal error: parsed labcomm declaration has errors");
}
}
public Program makeProgram(TypeDefParser.ParsedTypeDef d) { public Program makeProgram(TypeDefParser.ParsedTypeDef d) {
assertStacksEmpty();
List<Decl> ds = new List<Decl>(); List<Decl> ds = new List<Decl>();
ds.add(makeDecl(d)); ds.add(makeDecl(d));
return new Program(ds); assertStacksEmpty();
return createAndCheckProgram(ds);
} }
public Decl makeDecl(TypeDefParser.ParsedSampleDef d) { public Decl makeDecl(TypeDefParser.ParsedSampleDef d) {
...@@ -133,6 +161,7 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor { ...@@ -133,6 +161,7 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
return result; return result;
} }
public Program makeProgram(TypeDefParser.ParsedSampleDef d) { public Program makeProgram(TypeDefParser.ParsedSampleDef d) {
assertStacksEmpty();
List<Decl> ds = new List<Decl>(); List<Decl> ds = new List<Decl>();
Iterator<TypeDefParser.ParsedTypeDef> it = d.getDepIterator(); Iterator<TypeDefParser.ParsedTypeDef> it = d.getDepIterator();
...@@ -142,7 +171,8 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor { ...@@ -142,7 +171,8 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
ds.add(makeDecl(d)); ds.add(makeDecl(d));
return new Program(ds); assertStacksEmpty();
return createAndCheckProgram(ds);
} }
} }
package se.lth.control.labcomm2006; package se.lth.control.labcomm2006;
public class TypeDefVisitor { public class ASTbuilder {
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment