Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Show changes
Showing
with 663 additions and 246 deletions
namespace se.lth.control.labcomm2014 {
using System;
using System.Collections.Generic;
public class RenamingRegistry {
public class Dispatcher : SampleDispatcher, IEquatable<Dispatcher> {
private SampleDispatcher dispatcher;
private String name;
public Dispatcher(SampleDispatcher dispatcher,
String name) {
this.dispatcher = dispatcher;
this.name = name;
}
public String getName() {
return name;
}
public byte[] getSignature() {
return dispatcher.getSignature();
}
public void decodeAndHandle(Decoder decoder,
SampleHandler handler) {
dispatcher.decodeAndHandle(decoder, handler);
}
public bool Equals(Dispatcher obj) {
Dispatcher other = obj as Dispatcher;
return (other != null &&
dispatcher == other.dispatcher &&
name.Equals(other.name));
}
public override int GetHashCode() {
return dispatcher.GetHashCode() ^ name.GetHashCode();
}
public override string ToString() {
return "RenamingRegistry.Dispatcher(" + name + ")";
}
}
private Dictionary<Dispatcher, Dispatcher> registry;
public RenamingRegistry() {
registry = new Dictionary<Dispatcher, Dispatcher>();
}
public SampleDispatcher add(SampleDispatcher dispatcher,
String newName) {
Dispatcher result;
Dispatcher tmp = new Dispatcher(dispatcher, newName);
lock(this) {
registry.TryGetValue(tmp, out result);
if (result == null) {
registry.Add(tmp, tmp);
result = tmp;
}
}
return result;
}
}
}
namespace se.lth.control.labcomm2014 {
public interface Sample {
SampleDispatcher getDispatcher();
}
}
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
public interface LabCommDispatcher {
Type getSampleClass();
public interface SampleDispatcher {
String getName();
byte[] getSignature();
void decodeAndHandle(LabCommDecoder decoder,
LabCommHandler handler);
void decodeAndHandle(Decoder decoder,
SampleHandler handler);
}
......
namespace se.lth.control.labcomm2014 {
public interface SampleHandler {
}
}
public interface SampleType {
}
using System;
namespace se.lth.control.labcomm2014 {
public class WrappingDecoder: Decoder {
private Decoder decoder;
public WrappingDecoder(Decoder decoder) {
this.decoder = decoder;
}
public virtual void runOne() {
decoder.runOne();
}
public virtual void run() {
decoder.run();
}
public virtual void register(SampleDispatcher dispatcher,
SampleHandler handler) {
decoder.register(dispatcher, handler);
}
public virtual void registerSampleRef(SampleDispatcher dispatcher) {
decoder.registerSampleRef(dispatcher);
}
public virtual bool decodeBoolean() {
return decoder.decodeBoolean();
}
public virtual byte decodeByte() {
return decoder.decodeByte();
}
public virtual short decodeShort() {
return decoder.decodeShort();
}
public virtual int decodeInt() {
return decoder.decodeInt();
}
public virtual long decodeLong() {
return decoder.decodeLong();
}
public virtual float decodeFloat() {
return decoder.decodeFloat();
}
public virtual double decodeDouble() {
return decoder.decodeDouble();
}
public virtual String decodeString() {
return decoder.decodeString();
}
public virtual int decodePacked32() {
return decoder.decodePacked32();
}
public virtual SampleDispatcher decodeSampleRef() {
return decoder.decodeSampleRef();
}
}
}
namespace se.lth.control.labcomm2014 {
using System;
public class WrappingEncoder : Encoder {
private Encoder encoder;
public WrappingEncoder(Encoder encoder) {
this.encoder = encoder;
}
public virtual void register(SampleDispatcher dispatcher) {
encoder.register(dispatcher);
}
public virtual void registerSampleRef(SampleDispatcher dispatcher) {
encoder.registerSampleRef(dispatcher);
}
public virtual void begin(SampleDispatcher dispatcher) {
encoder.begin(dispatcher);
}
public virtual void end(SampleDispatcher dispatcher) {
encoder.end(dispatcher);
}
public virtual void encodeBoolean(bool value) {
encoder.encodeBoolean(value);
}
public virtual void encodeByte(byte value) {
encoder.encodeByte(value);
}
public virtual void encodeShort(short value) {
encoder.encodeShort(value);
}
public virtual void encodeInt(int value) {
encoder.encodeInt(value);
}
public virtual void encodeLong(long value) {
encoder.encodeLong(value);
}
public virtual void encodeFloat(float value) {
encoder.encodeFloat(value);
}
public virtual void encodeDouble(double value) {
encoder.encodeDouble(value);
}
public virtual void encodeString(String value) {
encoder.encodeString(value);
}
public virtual void encodePacked32(Int64 value) {
encoder.encodePacked32(value);
}
public virtual void encodeSampleRef(SampleDispatcher value) {
encoder.encodeSampleRef(value);
}
}
}
gen
labcomm2014.jar
labcomm2006.jar
labcomm2014.jar
MODULES=Constant \
Decoder \
DecoderChannel \
DecoderRegistry \
Encoder \
EncoderChannel \
EncoderRegistry \
Reader \
Sample \
SampleDispatcher \
SampleHandler \
SampleType \
BuiltinType \
TypeDef \
TypeBinding \
ASTbuilder \
TypeDefParser \
SigTypeDef \
SigSampleDef \
ParsedTypeDef \
ParsedSampleDef \
Writer \
WriterWrapper \
DataType \
VoidType \
SignatureSymbolVisitor \
SignatureSymbol \
TypeSymbol \
SampleSymbol \
NameSymbol \
SigPrimitiveType \
SigStructType \
SigField \
SigArrayType \
SigUserType
.PHONY: all
all: labcomm2014.jar
labcomm2014.jar: gen/JAVAC
echo $@
cd gen ; jar cf ../$@ se/lth/control/labcomm2014/*.class
gen:
mkdir gen
gen/JAVAC: $(MODULES:%=se/lth/control/labcomm2014/%.java) \
Makefile | gen
javac -cp ../../compiler/labcomm2014_compiler.jar -d gen \
$(filter %.java, $^)
touch $@
.PHONY: test
test:
.PHONY: clean
clean:
rm -rf gen
.PHONY: distclean
distclean: clean
rm -rf labcomm2014.jar
package se.lth.control.labcomm;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.IOException;
public class LabCommDecoderChannel implements LabCommDecoder {
private DataInputStream in;
private LabCommDecoderRegistry registry;
public LabCommDecoderChannel(InputStream in) throws IOException {
this.in = new DataInputStream(in);
registry = new LabCommDecoderRegistry();
}
public void runOne() throws Exception {
boolean done = false;
while (!done) {
int tag = decodeInt();
switch (tag) {
case LabComm.TYPEDEF:
case LabComm.SAMPLE: {
int index = decodeInt();
String name = decodeString();
ByteArrayOutputStream signature = new ByteArrayOutputStream();
collectFlatSignature(new LabCommEncoderChannel(signature));
registry.add(index, name, signature.toByteArray());
} break;
default: {
LabCommDecoderRegistry.Entry e = registry.get(tag);
if (e == null) {
throw new IOException("Unhandled tag " + tag);
}
LabCommDispatcher d = e.getDispatcher();
if (d == null) {
throw new IOException("No dispatcher for '" + e.getName() + "'");
}
LabCommHandler h = e.getHandler();
if (h == null) {
throw new IOException("No handler for '" + e.getName() +"'");
}
d.decodeAndHandle(this, h);
done = true;
}
}
}
}
public void run() throws Exception {
while (true) {
runOne();
}
}
private void collectFlatSignature(LabCommEncoder out) throws IOException {
int type = decodeInt();
out.encodeInt(type);
switch (type) {
case LabComm.ARRAY: {
int dimensions = decodeInt();
out.encodeInt(dimensions);
for (int i = 0 ; i < dimensions ; i++) {
out.encodeInt(decodeInt());
}
collectFlatSignature(out);
} break;
case LabComm.STRUCT: {
int fields = decodeInt();
out.encodeInt(fields);
for (int i = 0 ; i < fields ; i++) {
out.encodeString(decodeString());
collectFlatSignature(out);
}
} break;
case LabComm.BOOLEAN:
case LabComm.BYTE:
case LabComm.SHORT:
case LabComm.INT:
case LabComm.LONG:
case LabComm.FLOAT:
case LabComm.DOUBLE:
case LabComm.STRING: {
} break;
default: {
throw new IOException("Unimplemented type=" + type);
}
}
out.end(null);
}
public void register(LabCommDispatcher dispatcher,
LabCommHandler handler) throws IOException {
registry.add(dispatcher, handler);
}
public boolean decodeBoolean() throws IOException {
return in.readBoolean();
}
public byte decodeByte() throws IOException {
return in.readByte();
}
public short decodeShort() throws IOException {
return in.readShort();
}
public int decodeInt() throws IOException {
return in.readInt();
}
public long decodeLong() throws IOException {
return in.readLong();
}
public float decodeFloat() throws IOException {
return in.readFloat();
}
public double decodeDouble() throws IOException {
return in.readDouble();
}
public String decodeString() throws IOException {
in.readShort(); // HACK
return in.readUTF();
}
}
package se.lth.control.labcomm;
public interface LabCommDispatcher {
public Class getSampleClass();
public String getName();
public byte[] getSignature();
public void decodeAndHandle(LabCommDecoder decoder,
LabCommHandler handler) throws Exception;
}
package se.lth.control.labcomm;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class LabCommEncoderChannel implements LabCommEncoder {
private OutputStream writer;
private ByteArrayOutputStream bytes;
private DataOutputStream data;
private LabCommEncoderRegistry registry;
public LabCommEncoderChannel(OutputStream writer) {
this.writer = writer;
bytes = new ByteArrayOutputStream();
data = new DataOutputStream(bytes);
registry = new LabCommEncoderRegistry();
}
public void register(LabCommDispatcher dispatcher) throws IOException {
int index = registry.add(dispatcher);
encodeInt(LabComm.SAMPLE);
encodeInt(index);
encodeString(dispatcher.getName());
byte[] signature = dispatcher.getSignature();
for (int i = 0 ; i < signature.length ; i++) {
encodeByte(signature[i]);
}
end(null);
}
public void begin(Class<? extends LabCommSample> c) throws IOException {
encodeInt(registry.getTag(c));
}
public void end(Class<? extends LabCommSample> c) throws IOException {
data.flush();
bytes.writeTo(writer);
bytes.reset();
}
public void encodeBoolean(boolean value) throws IOException{
data.writeBoolean(value);
}
public void encodeByte(byte value) throws IOException {
data.writeByte(value);
}
public void encodeShort(short value) throws IOException {
data.writeShort(value);
}
public void encodeInt(int value) throws IOException {
data.writeInt(value);
}
public void encodeLong(long value) throws IOException {
data.writeLong(value);
}
public void encodeFloat(float value) throws IOException {
data.writeFloat(value);
}
public void encodeDouble(double value) throws IOException {
data.writeDouble(value);
}
public void encodeString(String value) throws IOException {
data.writeShort(0); // HACK...
data.writeUTF(value);
}
}
package se.lth.control.labcomm;
public interface LabCommHandler {
}
\ No newline at end of file
package se.lth.control.labcomm;
public interface LabCommSample {
}
\ No newline at end of file
package se.lth.control.labcomm;
public interface LabCommType {
}
\ No newline at end of file
package se.lth.control.labcomm;
public interface LabCommWriter {
public void write(byte[] data);
}
package se.lth.control.labcomm2014;
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.labcomm2014.compiler.LabComm;
import se.lth.control.labcomm2014.compiler.LabCommParser;
import se.lth.control.labcomm2014.compiler.List;
import se.lth.control.labcomm2014.compiler.Specification;
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.TypeInstance;
import se.lth.control.labcomm2014.compiler.DataType;
import se.lth.control.labcomm2014.compiler.VoidType;
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;
/** A class for building a JastAdd AST from the parsed types
* created by a TypeDefParser. This class depends on the LabComm compiler.
*/
public class ASTbuilder implements SignatureSymbolVisitor {
private LinkedList<DataType> typeStack;
private LinkedList<Field> fieldStack;
public ASTbuilder() {
this.typeStack = new LinkedList<DataType>();
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(TypeSymbol s){
throw new Error("not implemented? needed?");
}
public void visit(SampleSymbol s){
throw new Error("not implemented? needed?");
}
public void visit(NameSymbol s){
throw new Error("not implemented? needed?");
}
public void visit(SigPrimitiveType t){
typeStack.push(new PrimType(t.getName(), t.getTag()));
}
// SampleRefs are currently represented as primitive types,
// see comment in TypeDefParser
// public void visit(TypeDefParser.SampleRefType t){
// typeStack.push(new SampleRefType());
// }
public void visit(SigStructType t){
if(t.isVoid()) {
typeStack.push(new VoidType());
} else {
List<Field> tmpF = new List<Field>();
for( SigField f : t.getFields()) {
f.accept(this);
tmpF.add(fieldStack.pop());
}
typeStack.push(new StructType(tmpF));
}
}
public void visit(SigField t){
t.getType().accept(this);
fieldStack.push(new Field(new TypeInstance(typeStack.pop(),t.getName())));
}
public void visit(SigArrayType t){
boolean isFixed = true;
List<Exp> dim = new List<Exp>();
for(int i : t.getIdx()) {
if(i == 0) {
dim.add(new VariableSize());
isFixed = false;
} else {
dim.add(new IntegerLiteral(Integer.toString(i)));
}
}
t.getType().accept(this);
if(isFixed) {
typeStack.push(new FixedArrayType(typeStack.pop(), new Dim(dim)));
} else {
typeStack.push(new VariableArrayType(typeStack.pop(), new Dim(dim)));
}
}
public void visit(SigUserType t){
typeStack.push(new UserType(t.getName()));
}
public Decl makeDecl(SigTypeDef d) {
d.getType().accept(this);
Decl result = new TypeDecl(new TypeInstance(typeStack.pop(), d.getName()));
return result;
}
private Specification createAndCheckSpecification(List<Decl> ds) {
Specification p = new Specification(ds);
LinkedList errors = new LinkedList();
p.errorCheck(errors);
if(errors.isEmpty()) {
return p;
} else {
// This should not happen
// get errors and throw exception
StringBuilder sb = new StringBuilder();
for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
String s = (String)iter.next();
sb.append(s);
}
throw new RuntimeException("Internal error: parsed labcomm declaration has errors: "+sb.toString());
}
}
public Specification makeSpecification(SigTypeDef d) {
assertStacksEmpty();
List<Decl> ds = new List<Decl>();
ds.add(makeDecl(d));
assertStacksEmpty();
return createAndCheckSpecification(ds);
}
public Decl makeDecl(SigSampleDef d) {
d.getType().accept(this);
Decl result = new SampleDecl(new TypeInstance(typeStack.pop(), d.getName()));
return result;
}
private void addAllDeps(List<Decl> ds, SigTypeDef d) {
Iterator<SigTypeDef> it = d.getDepIterator();
while(it.hasNext()){
SigTypeDef dd = it.next();
addAllDeps(ds,dd);
ds.add(makeDecl(dd));
}
}
public Specification makeSpecification(ParsedSampleDef d) {
assertStacksEmpty();
List<Decl> ds = new List<Decl>();
addAllDeps(ds, d);
ds.add(makeDecl(d));
assertStacksEmpty();
return createAndCheckSpecification(ds);
}
}
package se.lth.control.labcomm2014;
public interface BuiltinType extends SampleType{
}
package se.lth.control.labcomm;
package se.lth.control.labcomm2014;
public class LabComm {
public class Constant {
public static final String CURRENT_VERSION = "LabComm2014";
/*
* Allowed packet tags
*/
/*
* Predeclared aggregate type indices
*/
public static final int TYPEDEF = 0x01;
public static final int SAMPLE = 0x02;
public static final int VERSION = 0x01;
public static final int SAMPLE_DEF = 0x02;
public static final int SAMPLE_REF = 0x03;
public static final int TYPE_DEF = 0x04;
public static final int TYPE_BINDING = 0x05;
public static final int PRAGMA = 0x3f;
public static final int FIRST_USER_INDEX = 0x40; /* ..0xffffffff */
/*
* Predefined aggregate type indices
*/
public static final int ARRAY = 0x10;
public static final int STRUCT = 0x11;
......@@ -21,10 +35,12 @@ public class LabComm {
public static final int FLOAT = 0x25;
public static final int DOUBLE = 0x26;
public static final int STRING = 0x27;
public static final int SAMPLE = 0x28;
/*
* Start of
* Other predefined symbols
*/
public static final int FIRST_USER_INDEX = 0x80;
}
\ No newline at end of file
public static final int TYPE_BIND_SELF = 0x00;
}
package se.lth.control.labcomm2014;
import java.io.PrintStream;
import java.util.Iterator;
public abstract class DataType implements SignatureSymbol{
private final String name;
private final int typeTag;
public static String tagName(int typeTag) {
switch(typeTag) {
case Constant.BOOLEAN : return "boolean";
case Constant.BYTE : return "byte";
case Constant.SHORT : return "short";
case Constant.INT : return "int";
case Constant.LONG : return "long";
case Constant.FLOAT : return "float";
case Constant.DOUBLE : return "double";
case Constant.STRING : return "string";
case Constant.SAMPLE : return "sample";
}
throw new Error("not primitive type tag : " +Integer.toHexString(typeTag));
}
protected DataType(String name, int typeTag) {
this.name = name;
this.typeTag = typeTag;
}
// protected DataType(int typeTag) {
// this(tagName(typeTag), typeTag);
// }
//
public String getName() {
return name;
}
public int getTag() {
return typeTag;
}
public boolean isArray() {
return false;
}
public boolean isStruct(){
return false;
}
public boolean isUserType(){
return false;
}
public void addField(String name, DataType type) {
throw new Error("cannot add field to "+getClass().getSimpleName());
}
public final void print(PrintStream out) {
print(out, "");
}
public void print(PrintStream out, String indent) {
out.print(name);
}
private static void printDependencies(PrintStream out, SampleDispatcher sd, String indent) throws java.io.IOException {
Iterator<SampleDispatcher> it = sd.getDependencyIterator();
if(it.hasNext()) {
out.println(indent+"dependencies:");
}
while(it.hasNext()){
SampleDispatcher d = it.next();
printDataType(out, d, indent);
out.println(indent+"---");
}
}
public static void printDataType(PrintStream out, SampleDispatcher sd, String indent) throws java.io.IOException{
out.print(indent+"typedef ");
sd.getDataType().print(out,indent);
out.println(" "+sd.getName()+";");
printDependencies(out, sd, "...."+indent);
}
public static void printDataType(PrintStream out, SampleDispatcher sd) throws java.io.IOException{
printDataType(out, sd, "");
}
private static void printDependencies(PrintStream out, SigTypeDef td, String indent) throws java.io.IOException {
Iterator<SigTypeDef> it = td.getDepIterator();
if(it.hasNext()) {
out.println(indent+"dependencies:");
}
while(it.hasNext()){
SigTypeDef d = it.next();
printDataType(out, d, indent);
out.println(indent+"---");
}
}
public static void printDataType(PrintStream out, SigTypeDef d, String indent) throws java.io.IOException{
if(d==null) {
System.out.println("********* WARNING: printDataType(null)???");
return;
}
out.print(indent+d.defType()+" ");
d.getType().print(out,indent);
out.println(" "+d.getName()+";");
printDependencies(out, d, "...."+indent);
}
public static void printDataType(PrintStream out, SigTypeDef d) throws java.io.IOException{
printDataType(out, d, "");
}
}