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

refactoring to isolate dependencies on the compiler

parent ca70032a
LCDIR=../..
LCCJAR=${LCDIR}/compiler/labcomm2014_compiler.jar # the LabComm compiler
LCLJAR=${LCDIR}/lib/java/labcomm2014.jar # the LabComm library
LCCJAR=${LCDIR}/compiler/labcomm2014_compiler.jar# the LabComm compiler
LCLJAR=${LCDIR}/lib/java/labcomm2014.jar# the LabComm library
EXECUTABLES=example_encoder example_decoder \
Encoder.class Decoder.class \
Encoder.class Decoder.class TDDecoder.class \
ExampleEncoder.exe ExampleDecoder.exe
include ${LCDIR}/lib/c/os_compat.mk
GENDIR=gen
......@@ -49,6 +50,7 @@ build :
java -jar ${LCDIR}/compiler/labcomm_compiler.jar --java=${GENDIR} --c=${GENDIR}/test.c --h=${GENDIR}/test.h --python=${GENDIR}/test.py --cs=${GENDIR}/test.cs test.lc
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:. ${GENDIR}/*.java Encoder.java Decoder.java
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR}:. TDDecoder.java
${CC} ${CFLAGS} ${LDFLAGS} -Wall -Werror -Wno-unused-function \
-I. -I${LCDIR}/lib/c/2014 -L${LCDIR}/lib/c \
......@@ -70,6 +72,9 @@ run:
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Encoder encoded_data_j
@echo "************ running Java TypeDefdecoder: *****************"
java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR} TDDecoder encoded_data_j
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_j
......
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import se.lth.control.labcomm.DecoderChannel;
import se.lth.control.labcomm.TypeDef;
import se.lth.control.labcomm.TypeDefParser;
import se.lth.control.labcomm.TypeDefVisitor;
//import se.lth.control.labcomm.TypeBinding;
import se.lth.control.labcomm2014.compiler.Program;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Vector;
import java.util.LinkedList;
import java.util.Iterator;
public class TDDecoder
implements twoLines.Handler,
// TypeDef.Handler,
// TypeBinding.Handler,
TypeDefParser.TypeDefListener,
twoInts.Handler,
theFirstInt.Handler,
theSecondInt.Handler
{
private DecoderChannel decoder;
private TypeDefParser tdp;
public TDDecoder(InputStream in)
throws Exception
{
decoder = new DecoderChannel(in);
twoInts.register(decoder, this);
twoLines.register(decoder, this);
theFirstInt.register(decoder, this);
theSecondInt.register(decoder, this);
this.tdp = TypeDefParser.registerTypeDefParser(decoder);
// TypeDef.register(decoder, this);
// TypeBinding.register(decoder, this);
tdp.addListener(this);
try {
System.out.println("Running decoder.");
decoder.run();
} catch (java.io.EOFException e) {
System.out.println("Decoder reached end of file.");
}
}
private String genPoint(point p) {
return "("+p.x.val+", "+p.y.val+")";
}
private String genLine(line l) {
return "Line from "+genPoint(l.start)+" to "+genPoint(l.end);
}
// public void handle_TypeDef(TypeDef d) throws java.io.IOException {
// System.out.println("Got TypeDef: "+d.getName()+"("+d.getIndex()+")");
// }
//
// public void handle_TypeBinding(TypeBinding d) throws java.io.IOException {
// System.out.println("Got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+"");
// }
public void onTypeDef(TypeDefParser.ParsedTypeDef d) {
if(d.isSampleDef()){
System.out.println("onTypeDef (sample): ");
TypeDefVisitor v = new TypeDefVisitor();
Program p = v.makeProgram((TypeDefParser.ParsedSampleDef) d);
LinkedList errors = new LinkedList();
p.errorCheck(errors);
if(errors.isEmpty()) {
try {
//FileOutputStream f = new FileOutputStream("/tmp/foopp"+d.getName()+".txt");
//PrintStream out = new PrintStream(f);
p.pp(System.out);
p.C_genC(System.out, new Vector(), "lcname", "prefix", 2014);
//p.J_gen(out, "testpackage", 2014);
//out.close();
} catch (Throwable e) {
System.err.println("Exception: " + e);
e.printStackTrace();
}
} else {
for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
String s = (String)iter.next();
System.out.println(s);
}
}
}
//System.out.println(" "+d.getName()+";");
//for(byte b: d.getSignature()) {
// System.out.print(Integer.toHexString(b)+" ");
//}
//System.out.println();
//try {
// tdp.parseSignature(d.getIndex());
//} catch(IOException ex) { ex.printStackTrace();}
}
public void handle_twoInts(twoInts d) throws java.io.IOException {
System.out.print("Got twoInts: ");
System.out.println(d.a +", "+d.b);
}
public void handle_theFirstInt(int d) throws java.io.IOException {
System.out.println("Got theFirstInt: "+d);
}
public void handle_theSecondInt(int d) throws java.io.IOException {
System.out.println("Got theSecondInt: "+d);
}
public void handle_twoLines(twoLines d) throws java.io.IOException {
System.out.print("Got twoLines: ");
System.out.println("Line l1: "+genLine(d.l1));
System.out.println(" Line l2: "+genLine(d.l2));
}
public static void main(String[] arg) throws Exception {
TDDecoder example = new TDDecoder(
new FileInputStream(new File(arg[0]))
);
}
}
......@@ -13,6 +13,7 @@ MODULES=Constant \
BuiltinType \
TypeDef \
TypeBinding \
TypeDefVisitor \
TypeDefParser \
Writer \
WriterWrapper
......@@ -35,7 +36,7 @@ gen:
mkdir gen
gen/JAVAC: $(MODULES:%=se/lth/control/labcomm/%.java) $(MODULES:%=se/lth/control/labcomm2006/%.java) Makefile | gen
javac -d gen $(filter %.java, $^)
javac -cp ../../compiler/labcomm_compiler.jar -d gen $(filter %.java, $^)
touch $@
......
......@@ -15,6 +15,34 @@ import se.lth.control.labcomm.DecoderChannel;
import se.lth.control.labcomm.TypeDef;
import se.lth.control.labcomm.TypeBinding;
// for BinaryScanner
// import beaver.Scanner;
// import beaver.Symbol;
// import se.lth.control.labcomm2014.compiler.LabComm;
// import se.lth.control.labcomm2014.compiler.LabCommParser;
//
// import se.lth.control.labcomm2014.compiler.List;
// import se.lth.control.labcomm2014.compiler.Program;
// import se.lth.control.labcomm2014.compiler.Decl;
// import se.lth.control.labcomm2014.compiler.TypeDecl;
// import se.lth.control.labcomm2014.compiler.SampleDecl;
// import se.lth.control.labcomm2014.compiler.Type;
// //import se.lth.control.labcomm2014.compiler.VoidType;
// //import se.lth.control.labcomm2014.compiler.SampleRefType;
// import se.lth.control.labcomm2014.compiler.PrimType;
// import se.lth.control.labcomm2014.compiler.UserType;
// import se.lth.control.labcomm2014.compiler.StructType;
// import se.lth.control.labcomm2014.compiler.Field;
// import se.lth.control.labcomm2014.compiler.ArrayType;
// import se.lth.control.labcomm2014.compiler.VariableArrayType;
// import se.lth.control.labcomm2014.compiler.FixedArrayType;
// import se.lth.control.labcomm2014.compiler.Dim;
// import se.lth.control.labcomm2014.compiler.Exp;
// import se.lth.control.labcomm2014.compiler.IntegerLiteral;
// import se.lth.control.labcomm2014.compiler.VariableSize;
//
////////////
public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
......@@ -56,6 +84,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
private HashMap<Integer,TypeDef> typeDefs;
private HashMap<Integer,Integer> typeBindings;
private HashSet<TypeDefListener> listeners;
private LinkedList<ParsedSampleDef> sampleDefs;
private Decoder decoder;
protected TypeDefParser(Decoder d) {
......@@ -63,12 +92,18 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
typeDefs = new HashMap<Integer,TypeDef>();
typeBindings = new HashMap<Integer,Integer>();
listeners = new HashSet<TypeDefListener>();
sampleDefs = new LinkedList<ParsedSampleDef>();
}
public void addListener(TypeDefListener l) {
listeners.add(l);
}
public Iterator<ParsedSampleDef> sampleDefIterator() {
return sampleDefs.iterator();
}
public void handle_TypeDef(TypeDef d) throws java.io.IOException {
//System.out.println("Got TypeDef: "+d.getName()+"("+d.getIndex()+")");
typeDefs.put(d.getIndex(), d);
......@@ -83,9 +118,14 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
typeBindings.put(d.getSampleIndex(), d.getTypeIndex());
td = getTypeDefForIndex(d.getSampleIndex());
//System.out.println("handleTypeBinding: td:"+td.getIndex()+"=="+td.getName());
//System.out.println("++++++++++++++++++++++++");
//System.out.println(symbolString());
//System.out.println("++++++++++++++++++++++++");
}
ParsedSampleDef result = parseSignature(td);
sampleDefs.add(result);
Iterator<TypeDefListener> it = listeners.iterator();
while(it.hasNext()){
notifyListener(it.next(), result);
......@@ -131,12 +171,105 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
// Sketch of result types "unparsed labcomm-file"
//
//
public abstract class ParsedType{
public LinkedList<ParsedSymbol> symbolify() {
LinkedList<ParsedSymbol> result = new LinkedList<ParsedSymbol>();
Iterator<ParsedSampleDef> sdi = sampleDefIterator();
while(sdi.hasNext()) {
ParsedSampleDef sd = sdi.next();
result.add(new SampleSymbol());
result.add(sd.getType());
result.add(new NameSymbol(sd.getName()));
Iterator<ParsedTypeDef> di = sd.getDepIterator();
while(di.hasNext()) {
ParsedTypeDef d = di.next();
result.add(new TypeSymbol());
result.add(d.getType());
result.add(new NameSymbol(d.getName()));
}
}
return result;
}
public String symbolString() {
Iterator<ParsedSymbol> i = symbolify().iterator();
StringBuilder sb = new StringBuilder();
while(i.hasNext()) {
sb.append(i.next().toString());
}
return sb.toString();
}
public interface ParsedSymbolVisitor {
void visit(TypeSymbol s);
void visit(SampleSymbol s);
void visit(NameSymbol s);
void visit(PrimitiveType t);
void visit(ParsedStructType t);
void visit(ParsedField t);
void visit(ArrayType t);
void visit(ParsedUserType t);
}
public abstract class ParsedSymbol{
public abstract void accept(ParsedSymbolVisitor v);
}
public class TypeSymbol extends ParsedSymbol {
public String toString() {
return "typedef ";
}
public void accept(ParsedSymbolVisitor v){
v.visit(this);
}
}
public class SampleSymbol extends ParsedSymbol {
public String toString() {
return "sample ";
}
public void accept(ParsedSymbolVisitor v){
v.visit(this);
}
}
public class NameSymbol extends ParsedSymbol {
private String name;
public NameSymbol(String name) {
this.name = name;
}
public String toString() {
return name;
}
public void accept(ParsedSymbolVisitor v){
v.visit(this);
}
}
public abstract class ParsedType extends ParsedSymbol{
//public abstract Type makeNode();
}
public class PrimitiveType extends ParsedType {
private final String name;
private int tag;
String getName() {
return name;
}
int getTag() {
return tag;
}
PrimitiveType(int tag) {
this.tag = tag;
switch(tag) {
case Constant.BOOLEAN:
this.name = "boolean";
......@@ -167,45 +300,69 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
}
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
public String toString() {
return name;}
}
public class StructType extends ParsedType {
private Field fields[];
public class ParsedStructType extends ParsedType {
private ParsedField fields[];
ParsedStructType(int nParsedFields) {
this.fields = new ParsedField[nParsedFields];
}
StructType(int nFields) {
this.fields = new Field[nFields];
public ParsedField[] getFields() {
return fields;
}
void setField(int idx, Field f) {
void setParsedField(int idx, ParsedField f) {
fields[idx] = f;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("struct {\n");
for(Field f : fields) {
for(ParsedField f : fields) {
sb.append(f.toString());
sb.append(";\n");
}
sb.append("}");
return sb.toString();
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
}
public class Field {
public class ParsedField extends ParsedSymbol{
private ParsedType type;
private String name;
Field(String name, ParsedType type) {
ParsedField(String name, ParsedType type) {
this.name = name;
this.type = type;
}
public ParsedType getType() {
return type;
}
public String getName() {
return name;
}
public String toString() {
return type.toString() + " " + name;
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
}
public class ArrayType extends ParsedType {
......@@ -217,6 +374,14 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
this.type = elementType;
}
public ParsedType getType() {
return type;
}
public int[] getIdx() {
return idx;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(type.toString());
......@@ -225,17 +390,29 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
}
return sb.toString();
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
}
public class UserType extends ParsedType {
public class ParsedUserType extends ParsedType {
private String name;
public String getName() {
return name;
}
public String toString() {
return name;
}
UserType(String name) {
ParsedUserType(String name) {
this.name = name;
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
}
public class ParsedTypeDef{
......@@ -291,6 +468,10 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
return other.idx == idx && other.name.equals(name);
}
}
public void accept(ParsedSymbolVisitor v) {
type.accept(v);
}
}
public class ParsedSampleDef extends ParsedTypeDef{
......@@ -312,6 +493,10 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
private HashSet<ParsedTypeDef> getDependencies() {
return deps;
}
Iterator<ParsedTypeDef> getDepIterator() {
return deps.iterator();
}
}
private class ParserState {
......@@ -450,26 +635,26 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
return result;
}
private StructType parseStruct(ParserState in) throws IOException {
private ParsedStructType parseStruct(ParserState in) throws IOException {
//System.out.println("struct");
int numFields = in.decodePacked32();
StructType result = new StructType(numFields);
for(int i=0; i<numFields; i++) {
result.setField(i, parseField(in));
int numParsedFields = in.decodePacked32();
ParsedStructType result = new ParsedStructType(numParsedFields);
for(int i=0; i<numParsedFields; i++) {
result.setParsedField(i, parseParsedField(in));
}
return result;
}
private Field parseField(ParserState in) throws IOException {
private ParsedField parseParsedField(ParserState in) throws IOException {
String name = in.decodeString();
return new Field(name, parseType(in));
return new ParsedField(name, parseType(in));
}
private ParsedType lookupType(int tag, ParserState in) {
ParsedType result;
if(tag >= Constant.FIRST_USER_INDEX) {
TypeDef td = typeDefs.get(tag);
result = new UserType(td.getName());
result = new ParsedUserType(td.getName());
in.pushType(tag);
} else {
result = new PrimitiveType(tag);
......
package se.lth.control.labcomm;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.EOFException;
import se.lth.control.labcomm.TypeDef;
import se.lth.control.labcomm.TypeDefParser;
// for BinaryScanner
import beaver.Scanner;
import beaver.Symbol;
import se.lth.control.labcomm2014.compiler.LabComm;
import se.lth.control.labcomm2014.compiler.LabCommParser;
import se.lth.control.labcomm2014.compiler.List;
import se.lth.control.labcomm2014.compiler.Program;
import se.lth.control.labcomm2014.compiler.Decl;
import se.lth.control.labcomm2014.compiler.TypeDecl;
import se.lth.control.labcomm2014.compiler.SampleDecl;
import se.lth.control.labcomm2014.compiler.Type;
//import se.lth.control.labcomm2014.compiler.VoidType;
//import se.lth.control.labcomm2014.compiler.SampleRefType;
import se.lth.control.labcomm2014.compiler.PrimType;
import se.lth.control.labcomm2014.compiler.UserType;
import se.lth.control.labcomm2014.compiler.StructType;
import se.lth.control.labcomm2014.compiler.Field;
import se.lth.control.labcomm2014.compiler.ArrayType;
import se.lth.control.labcomm2014.compiler.VariableArrayType;
import se.lth.control.labcomm2014.compiler.FixedArrayType;
import se.lth.control.labcomm2014.compiler.Dim;
import se.lth.control.labcomm2014.compiler.Exp;
import se.lth.control.labcomm2014.compiler.IntegerLiteral;
import se.lth.control.labcomm2014.compiler.VariableSize;
////////////
public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
///// tree building
//
//
private LinkedList<Type> typeStack;
private LinkedList<Field> fieldStack;
public TypeDefVisitor() {
this.typeStack = new LinkedList<Type>();
this.fieldStack = new LinkedList<Field>();
}
public void visit(TypeDefParser.TypeSymbol s){
throw new Error("not implemented? needed?");
}
public void visit(TypeDefParser.SampleSymbol s){