From 7014aa3ab355eee64858d076f649080a8e0f265c Mon Sep 17 00:00:00 2001 From: Sven Gestegard Robertz <sven.robertz@cs.lth.se> Date: Tue, 19 May 2015 16:11:08 +0200 Subject: [PATCH] changed intention encoding to be struct {byte key[_] , byte val[_]}[_] instead of just a string --- compiler/2014/Annotations.jrag | 76 +++ compiler/2014/LabComm.ast | 4 +- compiler/2014/LabCommParser.parser | 4 +- compiler/2014/Signature.jrag | 597 +++++++++--------- compiler/2014/Utilities.jadd | 39 ++ examples/user_types/test.lc | 8 +- .../control/labcomm2014/TypeDefParser.java | 29 +- 7 files changed, 466 insertions(+), 291 deletions(-) create mode 100644 compiler/2014/Utilities.jadd diff --git a/compiler/2014/Annotations.jrag b/compiler/2014/Annotations.jrag index 5141043..3533f8e 100644 --- a/compiler/2014/Annotations.jrag +++ b/compiler/2014/Annotations.jrag @@ -16,3 +16,79 @@ aspect Annotations { } +aspect SigAnnotations { + + inh Decl TypeInstance.parentDecl(); + + coll Set Decl.allAnnotations() [new HashSet()] with add; + TypeInstance contributes getAnnotationString() + to Decl.allAnnotations() + for parentDecl(); + + public DocString.DocString(byte[] bs) { + super("DOCSTRING", bs); + } + + public DocString.DocString(String s) { + super("DOCSTRING", s.getBytes()); + } + + public String Intention.toString() { + return "("+getKey() + ":"+new String(getValue())+")"; + } + public String DocString.toString() { + return "\""+new String(getValue())+"\""; + } + + syn boolean ASTNode.isTypeInstance() = false; + eq TypeInstance.isTypeInstance() = true; + + /// TESTING + syn String Decl.getAnnotationString() { + StringBuilder sb = new StringBuilder(); + Iterator<String> iti = allAnnotations().iterator(); + while(iti.hasNext()) { + //Annotation i = iti.next(); + //sb.append("("+i.getKey()+" : "+i.getValue()+") "); + String i = iti.next(); + sb.append(i); + } + return sb.toString(); + } + + + syn int TypeInstance.fooHash() { + List<Annotation> ints = getAnnotationList(); + int result=0; + for(Annotation i : ints) { + if(i.isIntention()) { + result += i.toString().hashCode(); + } + } + return result; + } + + syn String TypeInstance.getAnnotationString() { + StringBuilder sb = new StringBuilder(); + List<Annotation> ints = getAnnotationList(); + for(Annotation i : ints) { + sb.append(i.toString()); + } + return sb.toString(); + } + + public void Decl.debugAnnotations() { + getTypeInstance().debugAnnotations(getName()); + } + + public void TypeInstance.debugAnnotations(String context) { + if(hasAnnotations()){ + System.out.println(context+".annotations: " + fooHash() + " : " + getAnnotationString()); + } else { + //System.out.println(context + " : " + fooHash() + " : " + ": NO ANNOTATIONS "); + } + } + // TESTING END + +} + diff --git a/compiler/2014/LabComm.ast b/compiler/2014/LabComm.ast index d76868c..f2c73dd 100644 --- a/compiler/2014/LabComm.ast +++ b/compiler/2014/LabComm.ast @@ -4,7 +4,7 @@ abstract Decl ::= TypeInstance /Signature/; TypeInstance ::= DataType <Name:String> Annotation*; -Annotation ::= <Key:String> <Value:String>; +Annotation ::= <Key:String> <Value:byte[]>; Intention : Annotation; DocString : Annotation; @@ -22,7 +22,7 @@ abstract DataSignatureLine : SignatureLine; ByteArraySignatureLine : DataSignatureLine ::= <Data:byte[]>; IntSignatureLine : DataSignatureLine ::= <Data:int>; StringSignatureLine : DataSignatureLine ::= <Data:String>; -IntentionSignatureLine : DataSignatureLine ::= <Data:java.util.Map> ; +IntentionSignatureLine : DataSignatureLine ::= Intention* ; TypeRefSignatureLine : SignatureLine ::= Decl; Field : TypeInstance; diff --git a/compiler/2014/LabCommParser.parser b/compiler/2014/LabCommParser.parser index b5f76be..73f8080 100644 --- a/compiler/2014/LabCommParser.parser +++ b/compiler/2014/LabCommParser.parser @@ -68,8 +68,8 @@ String key = IDENTIFIER; String stringliteral = IDENTIFIER | QUOTEDSTRING; Annotation annotation = intention.i | docstring.d; -Annotation intention = LPAREN key.k COLON stringliteral.v RPAREN {: return new Intention(k,v); :}; -Annotation docstring = QUOTEDSTRING.s {: return new DocString(s.substring(1,s.length()-1)); :}; +Annotation intention = LPAREN key.k COLON stringliteral.v RPAREN {: return new Intention(k,v.getBytes()); :}; +Annotation docstring = QUOTEDSTRING.s {: return new DocString(s.substring(1,s.length()-1).getBytes()); :}; TypeInstance type_instance = annotations.a type.t IDENTIFIER {: return new TypeInstance(t, IDENTIFIER, a); :} diff --git a/compiler/2014/Signature.jrag b/compiler/2014/Signature.jrag index c996f9c..7928697 100644 --- a/compiler/2014/Signature.jrag +++ b/compiler/2014/Signature.jrag @@ -1,342 +1,375 @@ import java.util.*; -aspect SigAnnotations { +aspect Signature { - inh Decl TypeInstance.parentDecl(); + syn boolean Decl.isSampleDecl(); + eq TypeDecl.isSampleDecl() = false; + eq SampleDecl.isSampleDecl() = true; - coll Set Decl.allAnnotations() [new HashSet()] with add; - TypeInstance contributes getAnnotationString() - to Decl.allAnnotations() - for parentDecl(); + syn boolean Decl.sendOnlyFlatSignatures(Java_env env) = (env.version==2006); - public DocString.DocString(String s) { - super("DOCSTRING", s); - } + eq Decl.getSignature().parentDecl() = this; + eq Signature.getSignatureList().parentDecl() = parentDecl(); + + inh Decl Signature.parentDecl(); + inh Decl SignatureList.parentDecl(); - public String Intention.toString() { - return("("+getKey()+" : "+getValue()+") "); + syn nta Signature Decl.getSignature() { + SignatureList sl = new SignatureList(); + genSigLineForDecl(sl, true, this); + SignatureList fsl = new SignatureList(); + flatSignature(fsl); + Signature sig = new Signature(); + sig.setSignatureList(sl); + sig.setFlatSignatureList(fsl); + setSignature(sig); + return sig; } - public String DocString.toString() { - return "\""+getValue()+"\""; + public String SignatureLine.getIndentString() { + StringBuffer result = new StringBuffer(); + int indent = getIndent(); + for (int i = 0 ; i < indent ; i++) { + result.append(" "); + } + return result.toString(); } - syn boolean ASTNode.isTypeInstance() = false; - eq TypeInstance.isTypeInstance() = true; - - /// TESTING - syn String Decl.getAnnotationString() { - StringBuilder sb = new StringBuilder(); - Iterator<String> iti = allAnnotations().iterator(); - while(iti.hasNext()) { - //Annotation i = iti.next(); - //sb.append("("+i.getKey()+" : "+i.getValue()+") "); - String i = iti.next(); - sb.append(i); - } - return sb.toString(); - } - - - syn int TypeInstance.fooHash() { - List<Annotation> ints = getAnnotationList(); - int result=0; - for(Annotation i : ints) { - if(i.isIntention()) { - result += i.toString().hashCode(); - } - } - return result; - } - - syn String TypeInstance.getAnnotationString() { - StringBuilder sb = new StringBuilder(); - List<Annotation> ints = getAnnotationList(); - for(Annotation i : ints) { - sb.append(i.toString()); - } - return sb.toString(); - } - - public void Decl.debugAnnotations() { - getTypeInstance().debugAnnotations(getName()); - } - - public void TypeInstance.debugAnnotations(String context) { - if(hasAnnotations()){ - System.out.println(context+".annotations: " + fooHash() + " : " + getAnnotationString()); - } else { - System.out.println(context + " : " + fooHash() + " : " + ": NO ANNOTATIONS "); - } - } - // TESTING END + syn byte[] SignatureLine.getData(int version) = null; + // return new byte[0]; + + private Decl TypeRefSignatureLine.decl; + public TypeRefSignatureLine.TypeRefSignatureLine(int indent, Decl decl, String comment) { + super(indent, comment); + this.decl = decl; + } + public void SignatureList.addTypeRef(Decl type, String comment) { + addSignatureLine(new TypeRefSignatureLine(indent, type, comment)); + } + + public ByteArraySignatureLine.ByteArraySignatureLine(int indent, byte[] data, String comment) { + super(indent, comment); + setData(data); + } + + public IntSignatureLine.IntSignatureLine(int indent, int data, String comment) { + super(indent, comment); + setData(data); + } + + public void SignatureList.add(byte[] data, String comment) { + addSignatureLine(new ByteArraySignatureLine(indent, data, comment)); + } + + public void SignatureList.addInt(int data, String comment) { + addSignatureLine(new IntSignatureLine(indent, data, comment)); + } + + protected byte[] DataSignatureLine.getIntBytes(int value, int version) { + byte data[]; + switch(version) { + case 2006: // Use old encoding with 32 bit integers + data = new byte[4]; + for (int i = 0 ; i < 4 ; i++) { + data[3 - i] = (byte)((value >> (8 * i)) & 0xff); + } + //add(data, comment); + break; + case 2014: // Use new encoding with varints + byte[] tmp = new byte[5]; + long v = value & 0xffffffff; + int i, j; + for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) { + tmp[i] = (byte)(v & 0x7f); + } + byte[] packed = new byte[i]; + for (i = i - 1, j = 0 ; i >= 0 ; i--, j++) { + packed[j] = (byte)(tmp[i] | (i!=0?0x80:0x00)); + } + //add(packed, comment); + data = packed; + break; + default: + throw new RuntimeException("Unsupported version = "+version+". This should never happen."); + } + return data; + } + + eq IntSignatureLine.getData(int version) { + return getIntBytes(getData(), version); + } + + private static Comparator SignatureList.intentionComp = + new Comparator<Intention>() { + public int compare(Intention i1, Intention i2) { + return i1.getKey().compareTo(i2.getKey()); + } + }; + + public void SignatureList.addIntentions(Set<Intention> data, String comment) { + //addString(TypeInstance.getIntentionString(data), comment); + //create IntenionSignatureLine + IntentionSignatureLine line = new IntentionSignatureLine(indent, comment, new List()); + //TODO: create sorted list of intentions + + java.util.ArrayList<Intention> sorted = new ArrayList(data); + java.util.Collections.sort(sorted, intentionComp); + for(Intention i : sorted) { + line.addIntention(i); + } + + addSignatureLine(line); + } + + eq IntentionSignatureLine.getData(int version) { + //String tmpString = TypeInstance.getIntentionString(getIntentions()); + + byte[] bs = TypeInstance.getIntentionBytes(getIntentions()); + return bs; + } + + + public void SignatureList.addString(String data, String comment) { + addSignatureLine(new StringSignatureLine(indent, comment, data)); + } + eq StringSignatureLine.getData(int version) { + byte[] lenBytes = getIntBytes(getData().length(), version); + byte[] data = new byte[lenBytes.length+getData().length()]; + + // first add the encoded length + for (int i = 0 ; i < lenBytes.length ; i++) { + data[i] = lenBytes[i]; + } + // and then the actual chars + for (int i = 0 ; i < getData().length() ; i++) { + int idx = lenBytes.length + i; + data[idx] = (byte)(getData().charAt(i) & 0xff); + } + return data; + } + + public int SignatureList.size() { + return getNumSignatureLine(); + } + + public String SignatureList.getIndent(int i) { + StringBuffer result = new StringBuffer(); + int indent = getSignatureLine(i).getIndent(); + for (i = 0 ; i < indent ; i++) { + result.append(" "); + } + return result.toString(); + } -} + public byte[] SignatureList.getData(int i, int version) { + return getSignatureLine(i).getData(version); + } -aspect Signature { + public String SignatureList.getComment(int i) { + return getSignatureLine(i).getComment(); + } - syn boolean Decl.isSampleDecl(); - eq TypeDecl.isSampleDecl() = false; - eq SampleDecl.isSampleDecl() = true; - - syn boolean Decl.sendOnlyFlatSignatures(Java_env env) = (env.version==2006); - - eq Decl.getSignature().parentDecl() = this; - eq Signature.getSignatureList().parentDecl() = parentDecl(); - - inh Decl Signature.parentDecl(); - inh Decl SignatureList.parentDecl(); - - syn nta Signature Decl.getSignature() { - SignatureList sl = new SignatureList(); - genSigLineForDecl(sl, true, this); - SignatureList fsl = new SignatureList(); - flatSignature(fsl); - Signature sig = new Signature(); - sig.setSignatureList(sl); - sig.setFlatSignatureList(fsl); - setSignature(sig); - return sig; - } - - public String SignatureLine.getIndentString() { - StringBuffer result = new StringBuffer(); - int indent = getIndent(); - for (int i = 0 ; i < indent ; i++) { - result.append(" "); - } - return result.toString(); - } + private int SignatureList.indent; - syn byte[] SignatureLine.getData(int version) = null; - // return new byte[0]; + public void SignatureList.indent() { + indent++; + } - private Decl TypeRefSignatureLine.decl; - public TypeRefSignatureLine.TypeRefSignatureLine(int indent, Decl decl, String comment) { - super(indent, comment); - this.decl = decl; - } - public void SignatureList.addTypeRef(Decl type, String comment) { - addSignatureLine(new TypeRefSignatureLine(indent, type, comment)); - } + public void SignatureList.unindent() { + indent--; + } - public ByteArraySignatureLine.ByteArraySignatureLine(int indent, byte[] data, String comment) { - super(indent, comment); - setData(data); - } - public IntSignatureLine.IntSignatureLine(int indent, int data, String comment) { - super(indent, comment); - setData(data); + public void ASTNode.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + throw new Error(this.getClass().getName() + + ".genSigLineForDecl(SignatureList list)" + + " not declared"); } - public void SignatureList.add(byte[] data, String comment) { - addSignatureLine(new ByteArraySignatureLine(indent, data, comment)); + public String TypeInstance.getIntentionString() { + return getIntentionString(intentions()); } - public void SignatureList.addInt(int data, String comment) { - addSignatureLine(new IntSignatureLine(indent, data, comment)); - } + public static String TypeInstance.getIntentionString(List<Intention> intentions) { + if(intentions==null) return ""; + Iterator<Intention> it = intentions.iterator(); + return getIntentionString(it); - protected byte[] DataSignatureLine.getIntBytes(int value, int version) { - byte data[]; - switch(version) { - case 2006: // Use old encoding with 32 bit integers - data = new byte[4]; - for (int i = 0 ; i < 4 ; i++) { - data[3 - i] = (byte)((value >> (8 * i)) & 0xff); - } - //add(data, comment); - break; - case 2014: // Use new encoding with varints - byte[] tmp = new byte[5]; - long v = value & 0xffffffff; - int i, j; - for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) { - tmp[i] = (byte)(v & 0x7f); - } - byte[] packed = new byte[i]; - for (i = i - 1, j = 0 ; i >= 0 ; i--, j++) { - packed[j] = (byte)(tmp[i] | (i!=0?0x80:0x00)); - } - //add(packed, comment); - data = packed; - break; - default: - throw new RuntimeException("Unsupported version = "+version+". This should never happen."); - } - return data; } - - eq IntSignatureLine.getData(int version) { - return getIntBytes(getData(), version); + public static String TypeInstance.getIntentionString(Set<Intention> intentions) { + if(intentions==null) return ""; + Iterator<Intention> it = intentions.iterator(); + return getIntentionString(it); } - public void SignatureList.addIntentions(Set<Intention> data, String comment) { - addString(TypeInstance.getIntentionString(data), comment); + public static String TypeInstance.getIntentionString(Iterator<Intention> it) { + StringBuilder sb = new StringBuilder(); + while(it.hasNext()) { + Intention i = it.next(); + sb.append(i.toString()); + } + return sb.toString(); } - public void SignatureList.addString(String data, String comment) { - addSignatureLine(new StringSignatureLine(indent, comment, data)); - } - eq StringSignatureLine.getData(int version) { - byte[] lenBytes = getIntBytes(getData().length(), version); - byte[] data = new byte[lenBytes.length+getData().length()]; - - // first add the encoded length - for (int i = 0 ; i < lenBytes.length ; i++) { - data[i] = lenBytes[i]; - } - // and then the actual chars - for (int i = 0 ; i < getData().length() ; i++) { - int idx = lenBytes.length + i; - data[idx] = (byte)(getData().charAt(i) & 0xff); - } - return data; + syn byte[] Intention.keyBytes() = getKey().getBytes(); + syn byte[] Intention.valBytes() = getValue(); + + syn byte[] Intention.toByteArray() { + byte[] k = keyBytes(); + byte[] v = valBytes(); + int klen = Utilities.size_packed32(k.length); + int vlen = Utilities.size_packed32(v.length); + int tlen = k.length + v.length + Utilities.size_packed32(klen) + Utilities.size_packed32(vlen); + //int size = Utilities.size_packed32(tlen)+tlen; + + byte result[] = new byte[tlen]; + + int pos=0; + +// pos = Utilities.encodePacked32(tlen, result, pos, Utilities.size_packed32(tlen)); + pos = Utilities.encodePacked32(k.length, result, pos, klen); + for(byte kb : k) { + result[pos++] = kb; + } + pos = Utilities.encodePacked32(v.length, result, pos, vlen); + for(byte vb : v) { + result[pos++] = vb; + } + return result; } - public int SignatureList.size() { - return getNumSignatureLine(); - } - public String SignatureList.getIndent(int i) { - StringBuffer result = new StringBuffer(); - int indent = getSignatureLine(i).getIndent(); - for (i = 0 ; i < indent ; i++) { - result.append(" "); - } - return result.toString(); + public byte[] TypeInstance.getIntentionBytes() { + return getIntentionBytes(intentions()); } - public byte[] SignatureList.getData(int i, int version) { - return getSignatureLine(i).getData(version); - } + public static byte[] TypeInstance.getIntentionBytes(List<Intention> intentions) { + if(intentions==null) return new byte[0]; - public String SignatureList.getComment(int i) { - return getSignatureLine(i).getComment(); + Iterator<Intention> it = intentions.iterator(); + return getIntentionBytes(it); } - private int SignatureList.indent; + public static byte[] TypeInstance.getIntentionBytes(Set<Intention> intentions) { + if(intentions==null) return new byte[0]; - public void SignatureList.indent() { - indent++; + Iterator<Intention> it = intentions.iterator(); + return getIntentionBytes(it); } - public void SignatureList.unindent() { - indent--; + public static byte[] TypeInstance.getIntentionBytes(Iterator<Intention> it) { + java.util.ArrayList<byte[]> tmp = new java.util.ArrayList<byte[]>(); + int tmpLen=0; + int numIntentions=0; + + while(it.hasNext()) { + Intention i = it.next(); + byte[] bs = i.toByteArray(); + tmp.add(bs); + tmpLen+=bs.length; + numIntentions++; + } + + byte result[] = new byte[tmpLen + Utilities.size_packed32(numIntentions)]; + + int pos = 0; + pos = Utilities.encodePacked32(numIntentions, result, 0, Utilities.size_packed32(numIntentions)); + for(byte[] bs : tmp) { + for(byte b : bs) { + result[pos++] = b; + } + } + return result; } - public void ASTNode.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - throw new Error(this.getClass().getName() + - ".genSigLineForDecl(SignatureList list)" + - " not declared"); - } + syn Set<Intention> Specification.emptyIntentions() = new HashSet<Intention>(); + + inh Set<Intention> ASTNode.noIntentions(); + eq Specification.getChild(int i).noIntentions() = emptyIntentions(); - public String TypeInstance.getIntentionString() { - return getIntentionString(intentions()); - } + syn Set<Intention> ASTNode.intentions(); - public static String TypeInstance.getIntentionString(Set<Intention> intentions) { - if(intentions==null) return ""; + eq ASTNode.intentions() = noIntentions(); + eq TypeInstance.intentions() = intentionSet(); - StringBuilder sb = new StringBuilder(); - Iterator<Intention> it = intentions.iterator(); - while(it.hasNext()) { - Intention i = it.next(); - sb.append(i.toString()); + public void TypeInstance.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { +// debugAnnotations(this.getName()); +// list.addString(inst.getIntentionString(), "intention string"); + getDataType().genSigLineForDecl(list, decl, this); } - return sb.toString(); - } - syn Set<Intention> Specification.emptyIntentions() = new HashSet<Intention>(); + public void TypeDecl.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + //TODO intent + if(decl){ + getTypeInstance().genSigLineForDecl(list, decl, this); + }else{ + list.addTypeRef(this, "//TODO (from list.addTypeRef)"); + } + } - inh Set<Intention> ASTNode.noIntentions(); - eq Specification.getChild(int i).noIntentions() = emptyIntentions(); + public void SampleDecl.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + //TODO intent + getTypeInstance().genSigLineForDecl(list, decl, this); + } - syn Set<Intention> ASTNode.intentions(); + public void VoidType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + list.addInt(LABCOMM_STRUCT, "void"); + list.addInt(0, null); + } - eq ASTNode.intentions() = noIntentions(); - eq TypeInstance.intentions() = intentionSet(); +// public void SampleRefType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { +// list.addInt(LABCOMM_SAMPLE_REF, "sample"); +// } + public void PrimType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + list.addInt(getToken(), null); + } - public void TypeInstance.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { -// debugAnnotations(this.getName()); -// list.addString(inst.getIntentionString(), "intention string"); - getDataType().genSigLineForDecl(list, decl, this); - } + /* For UserType, the decl parameter is ignored, as a UserType + * will always be a TypeRef + */ + public void UserType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + TypeDecl thet = lookupType(getName()); + list.addTypeRef(thet, null); + } - public void TypeDecl.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - //TODO intent - if(decl){ - getTypeInstance().genSigLineForDecl(list, decl, this); - }else{ - list.addTypeRef(this, "//TODO (from list.addTypeRef)"); + public void ArrayType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + list.addInt(LABCOMM_ARRAY, signatureComment()); + list.indent(); + list.addInt(getNumExp(), null); + for (int i = 0 ; i < getNumExp() ; i++) { + getExp(i).genSigLineForDecl(list, false, null); + } + getDataType().genSigLineForDecl(list, false, null); + list.unindent(); + list.add(null, "}"); } - } - public void SampleDecl.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - //TODO intent - getTypeInstance().genSigLineForDecl(list, decl, this); - } + public void StructType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + list.addInt(LABCOMM_STRUCT, "struct { " + getNumField() + " fields"); + list.indent(); + list.addInt(getNumField(), null); + for (int i = 0 ; i < getNumField() ; i++) { + getField(i).genSigLineForDecl(list, false, inst); + } + list.unindent(); + list.add(null, "}"); + } - public void VoidType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - list.addInt(LABCOMM_STRUCT, "void"); - list.addInt(0, null); - } + public void Field.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + list.addString(getName(), signatureComment()); + super.genSigLineForDecl(list, decl, inst); + //TODOintent + //getDataType().genSigLineForDecl(list, decl, inst); + } -// public void SampleRefType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { -// list.addInt(LABCOMM_SAMPLE_REF, "sample"); -// } - public void PrimType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - list.addInt(getToken(), null); - } - - /* For UserType, the decl parameter is ignored, as a UserType - * will always be a TypeRef - */ - public void UserType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - - TypeDecl thet = lookupType(getName()); - list.addTypeRef(thet, null); - } - - public void ArrayType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - list.addInt(LABCOMM_ARRAY, signatureComment()); - list.indent(); - list.addInt(getNumExp(), null); - for (int i = 0 ; i < getNumExp() ; i++) { - getExp(i).genSigLineForDecl(list, false, null); + public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + list.addInt(Integer.parseInt(getValue()), null); } - getDataType().genSigLineForDecl(list, false, null); - list.unindent(); - list.add(null, "}"); - } - - public void StructType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - list.addInt(LABCOMM_STRUCT, "struct { " + getNumField() + " fields"); - list.indent(); - list.addInt(getNumField(), null); - for (int i = 0 ; i < getNumField() ; i++) { - getField(i).genSigLineForDecl(list, false, inst); + + public void VariableSize.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { + list.addInt(0, null); } - list.unindent(); - list.add(null, "}"); - } - - public void Field.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - list.addString(getName(), signatureComment()); - super.genSigLineForDecl(list, decl, inst); - //TODOintent - //getDataType().genSigLineForDecl(list, decl, inst); - } - - public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - list.addInt(Integer.parseInt(getValue()), null); - } - - public void VariableSize.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { - list.addInt(0, null); - } } diff --git a/compiler/2014/Utilities.jadd b/compiler/2014/Utilities.jadd new file mode 100644 index 0000000..f7b7d98 --- /dev/null +++ b/compiler/2014/Utilities.jadd @@ -0,0 +1,39 @@ +aspect Encoding { + + public class Utilities { + /* Size of packed32 variable */ + public static int size_packed32(long data) + { + long d = data & 0xffffffff; + int result = 0; + int i; + + for (i = 0 ; i == 0 || d != 0; i++, d = (d >>> 7)) { + result++; + } + return result; + } + + public static int encodePacked32(long value, byte[] buf, int start, int len) { + int pos = start; + byte[] tmp = new byte[5]; + long v = value & 0xffffffff; + int i; + + for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) { + tmp[i] = (byte)(v & 0x7f); + } + + if(i != len) { + throw new Error("wrong length, was: "+i+", expected "+len); + } + + for (i = i - 1 ; i >= 0 ; i--) { + buf[pos++] = (byte)(tmp[i] | (i!=0?0x80:0x00)); + } + + return pos; + } + } +} + diff --git a/examples/user_types/test.lc b/examples/user_types/test.lc index 860e2fa..fdc9d54 100644 --- a/examples/user_types/test.lc +++ b/examples/user_types/test.lc @@ -8,7 +8,7 @@ typedef void avoid; sample (function:"a trigger")(foo:bar) avoid doavoid; sample (a:b) "A struct: an int and a ref." struct { - (c:d)(e:f) int x; + (e:f)(c:d) int x; sample reference; } intAndRef; @@ -29,9 +29,9 @@ typedef struct { } foo; sample struct { - line l1; - line l2; - foo f; + (name:l1)line l1; + (name:l2)line l2; + (b:"kalle anka")(c:hejdu)(a:"kalle anka")foo f; } twoLines; sample struct { diff --git a/lib/java/se/lth/control/labcomm2014/TypeDefParser.java b/lib/java/se/lth/control/labcomm2014/TypeDefParser.java index 0b3e84c..e063cb4 100644 --- a/lib/java/se/lth/control/labcomm2014/TypeDefParser.java +++ b/lib/java/se/lth/control/labcomm2014/TypeDefParser.java @@ -547,6 +547,33 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { String getCurrentName() { return current.getName(); } + + String decodeIntentions() throws IOException { + int n = decodePacked32() & 0xffffffff; + if(n==0) return ""; + + StringBuilder sb = new StringBuilder(); + for(int i=0; i<n;i++) { + sb.append("("); + int klen = decodePacked32() & 0xffffffff; + byte[] kchars = new byte[klen]; + for(int k=0; k<klen; k++) { + kchars[k] = in.readByte(); + } + sb.append(new String(kchars)); + + sb.append(":"); + + int vlen = decodePacked32() & 0xffffffff; + byte[] vchars = new byte[vlen]; + for(int j=0; j<vlen; j++) { + vchars[j] = in.readByte(); + } + sb.append(new String(vchars)); + sb.append(")"); + } + return sb.toString(); + } String decodeString() throws IOException { int len = decodePacked32() & 0xffffffff; @@ -661,7 +688,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { private ParsedType parseType(ParserState in, boolean parseIntentions) throws IOException { if(parseIntentions) { - String intentions = in.decodeString(); + String intentions = in.decodeIntentions(); if(intentions.length()>0) { System.out.println("parseType intentions ("+intentions); } else { -- GitLab