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
Select Git revision

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Select Git revision
Show changes
Showing
with 643 additions and 242 deletions
......@@ -14,10 +14,9 @@ aspect Signature {
inh Decl Signature.parentDecl();
inh Decl SignatureList.parentDecl();
syn nta Signature Decl.getSignature() {
SignatureList sl = new SignatureList();
genSigLineForDecl(sl, true);
genSigLineForDecl(sl, true, this);
SignatureList fsl = new SignatureList();
flatSignature(fsl);
Signature sig = new Signature();
......@@ -100,6 +99,29 @@ aspect Signature {
return getIntBytes(getData(), version);
}
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: refactor out creation of sorted list of intentions
java.util.ArrayList<Intention> sorted = new ArrayList(data);
java.util.Collections.sort(sorted, TypeInstance.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));
}
......@@ -151,79 +173,200 @@ aspect Signature {
}
public void ASTNode.genSigLineForDecl(SignatureList list, boolean decl) {
public void ASTNode.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
throw new Error(this.getClass().getName() +
".genSigLineForDecl(SignatureList list)" +
" not declared");
}
public void TypeDecl.genSigLineForDecl(SignatureList list, boolean decl) {
public String TypeInstance.getIntentionString() {
return getIntentionString(intentions());
}
public static String TypeInstance.getIntentionString(List<Intention> intentions) {
if(intentions==null) return "";
Iterator<Intention> it = intentions.iterator();
return getIntentionString(it);
}
public static String TypeInstance.getIntentionString(Set<Intention> intentions) {
if(intentions==null) return "";
Iterator<Intention> it = intentions.iterator();
return getIntentionString(it);
}
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();
}
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 byte[] TypeInstance.getIntentionBytes() {
return getIntentionBytes(intentions());
}
public static byte[] TypeInstance.getIntentionBytes(List<Intention> intentions) {
if(intentions==null) return new byte[0];
Iterator<Intention> it = intentions.iterator();
return getIntentionBytes(it);
}
public static byte[] TypeInstance.getIntentionBytes(Set<Intention> intentions) {
if(intentions==null) return new byte[0];
Iterator<Intention> it = intentions.iterator();
return getIntentionBytes(it);
}
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;
}
syn Set<Intention> Specification.emptyIntentions() = new HashSet<Intention>();
inh Set<Intention> ASTNode.noIntentions();
eq Specification.getChild(int i).noIntentions() = emptyIntentions();
syn Set<Intention> ASTNode.intentions();
eq ASTNode.intentions() = noIntentions();
eq TypeInstance.intentions() = intentionSet();
public void TypeInstance.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
// debugAnnotations(this.getName());
// list.addString(inst.getIntentionString(), "intention string");
if(addIntentions()) {
list.addIntentions(intentionSet(), "intentions");
}
getDataType().genSigLineForDecl(list, decl, this);
}
public void TypeDecl.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
//TODO intent
if(decl){
getType().genSigLineForDecl(list, decl);
getTypeInstance().genSigLineForDecl(list, decl, this);
}else{
list.addTypeRef(this, "//TODO (from list.addTypeRef)");
}
}
public void SampleDecl.genSigLineForDecl(SignatureList list, boolean decl) {
getType().genSigLineForDecl(list, decl);
public void SampleDecl.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
//TODO intent
getTypeInstance().genSigLineForDecl(list, decl, this);
}
public void VoidType.genSigLineForDecl(SignatureList list, boolean decl) {
public void VoidType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(LABCOMM_STRUCT, "void");
list.addInt(0, null);
}
// public void SampleRefType.genSigLineForDecl(SignatureList list, boolean decl) {
// public void SampleRefType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
// list.addInt(LABCOMM_SAMPLE_REF, "sample");
// }
public void PrimType.genSigLineForDecl(SignatureList list, boolean decl) {
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) {
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) {
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);
getExp(i).genSigLineForDecl(list, false, null);
}
getType().genSigLineForDecl(list, false);
getDataType().genSigLineForDecl(list, false, null);
list.unindent();
list.add(null, "}");
}
public void StructType.genSigLineForDecl(SignatureList list, boolean decl) {
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);
getField(i).genSigLineForDecl(list, false, inst);
}
list.unindent();
list.add(null, "}");
}
public void Field.genSigLineForDecl(SignatureList list, boolean decl) {
list.addString(getName(), signatureComment());
getType().genSigLineForDecl(list, decl);
}
// public void Field.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
// //XXX make intention
// list.addString(getName(), signatureComment());
// super.genSigLineForDecl(list, decl, inst);
// //TODOintent
// //getDataType().genSigLineForDecl(list, decl, inst);
// }
public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl) {
public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(Integer.parseInt(getValue()), null);
}
public void VariableSize.genSigLineForDecl(SignatureList list, boolean decl) {
public void VariableSize.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(0, null);
}
}
......@@ -6,31 +6,49 @@ aspect TypeCheck {
// void is not allowed as a field in a struct or an array element
syn boolean Type.isNull();
eq Type.isNull() = false;
syn boolean DataType.isNull();
eq DataType.isNull() = false;
eq VoidType.isNull() = true;
eq UserType.isNull() = decl().isNull();
syn boolean TypeDecl.isNull();
eq TypeDecl.isNull() = getType().isNull();
eq TypeDecl.isNull() = getDataType().isNull();
public void ASTNode.nullTypeCheck() {}
public void Field.nullTypeCheck() {
if(getType().isNull()) {
if(getDataType().isNull()) {
error("field " + getName() + " of struct "+ declName()+ " may not be of type void");
}
}
public void ParseArrayType.nullTypeCheck() {
if(getType().isNull()) {
if(getDataType().isNull()) {
error("elements of array "+declName()+" may not be of type void");
}
}
public void ArrayType.nullTypeCheck() {
if(getType().isNull()) {
if(getDataType().isNull()) {
error("elements of array "+declName()+" may not be of type void");
}
}
}
aspect AnnotationCheck {
refine TypeCheck void ASTNode.typeCheck() {
refined(); // similar to call to super
annotationCheck();
}
public void ASTNode.annotationCheck() {}
public void TypeDecl.annotationCheck() {
Iterator<Intention> it = getTypeInstance().intentions().iterator();;
while(it.hasNext()) {
if(!it.next().getKey().equals("")) {
error("TypeDecl " + getName() + " has intentions. (Not allowed for typedefs)");
}
}
}
}
aspect User_Types {
syn String Type.getTypeName();
eq Type.getTypeName() = getClass().getName();
syn String DataType.getTypeName();
eq DataType.getTypeName() = getClass().getName();
eq PrimType.getTypeName() = getName();
eq UserType.getTypeName() = getName();
syn boolean Type.isUserType();
eq Type.isUserType() = false;
syn boolean DataType.isUserType();
eq DataType.isUserType() = false;
eq UserType.isUserType() = true;
}
......@@ -14,8 +14,8 @@ aspect Type_References {
// The dependencies on other type declarations for a Decl.
coll Set<Decl> Decl.type_dependencies() [new HashSet<Decl>()] with add;
Field contributes ((UserType)getType()).decl()
when parentDecl() != null && getType().isUserType()
Field contributes ((UserType)getDataType()).decl()
when parentDecl() != null && getDataType().isUserType()
to Decl.type_dependencies()
for parentDecl();
......@@ -24,8 +24,8 @@ aspect Type_References {
to Decl.type_dependencies()
for parentDecl();
/*
Field contributes getType().decl()
when parentDecl() != null && getType().isLeafType()
Field contributes getDataType().decl()
when parentDecl() != null && getDataType().isLeafType()
to Decl.type_dependencies()
for parentDecl();
*/
......
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;
}
}
}
aspect PrintEnv {
public abstract class PrintEnv {
protected static class Printer {
private final String indentString = " ";
private boolean newline = true; // last print ended with newline
protected PrintStream out;
private Printer printer;
/** dummy constructor motivated by the FilePrinter subclass */
protected Printer() {
this.out = null;
}
public Printer(PrintStream out) {
this.out = out;
}
public void print(PrintEnv env, String s) {
if (newline) {
newline = false;
for (int i = 0 ; i < env.getIndent() ; i++) {
out.print(indentString);
}
}
out.print(s);
}
public void println(PrintEnv env, String s) {
print(env, s);
out.println();
newline = true;
}
public void println(PrintEnv env) {
out.println();
newline = true;
}
public PrintStream getPrintStream() {
return(out);
}
public void close() throws IOException {
//do nothing
}
}
protected static class FilePrinter extends Printer {
private File file;
private IOException exception;
public FilePrinter(PrintStream out) {
super(out);
}
public FilePrinter(File f) {
file = f;
File parentFile = f.getParentFile();
if(parentFile != null) {
parentFile.mkdirs();
}
}
public void close() throws IOException {
if (out != null) {
out.close();
}
if (exception != null) {
throw exception;
}
}
public void checkOpen() {
if (out == null && exception == null) {
try {
out = new PrintStream(new FileOutputStream(file));
} catch (IOException e) {
exception = e;
}
}
}
public void print(PrintEnv env, String s) {
checkOpen();
super.print(env,s);
}
public void println(PrintEnv env, String s) {
checkOpen();
super.println(env, s);
}
}
public final int version; //labcomm version (2006 or 2014)
public final String verStr; // version suffix to append (currently _2006 and empty string)
private Printer printer;
private int indent;
private int depth;
protected PrintEnv(PrintStream out) {
this(new Printer(out));
}
protected PrintEnv(Printer printer) {
this(printer, 2014);
}
protected PrintEnv(Printer printer, int version) {
this(0, printer, version);
}
protected PrintEnv(int indent, Printer printer, int version) {
this(indent, printer, version, 0);
}
protected PrintEnv(int indent, Printer printer, int version, int depth) {
this.version = version;
this.indent = indent;
this.printer = printer;
this.verStr = LabCommVersion.versionString(version);
this.depth = depth;
}
public void close() throws IOException {
printer.close();
}
public PrintStream getPrintStream() {
return printer.getPrintStream();
}
public void indent(int amount) {
indent += amount;
}
public void indent() {
indent(1);
}
public void unindent(int amount) {
indent -= amount;
if (indent < 0) {
throw new Error("Negative indent level");
}
}
public void unindent() {
unindent(1);
}
public void print(String s) {
printer.print(this, s);
}
public void println(String s) {
printer.println(this, s);
}
public void println() {
printer.println(this, "");
}
public void incDepth() {
depth++;
}
public void decDepth() {
if(depth<=0) {
throw new RuntimeException("decDepth() called when depth = "+depth);
}
depth--;
}
public int getDepth() {
return depth;
}
public int getVersion() {
return version;
}
public int getIndent() {
return indent;
}
public Printer getPrinter() {
return printer;
}
public boolean versionHasMetaData() {
return version != 2006;
}
}
}
......@@ -5,7 +5,14 @@ aspect Version {
*/
class LabCommVersion {
public static String versionString(int version) {
return (version == 2006) ? "2006" : "";
switch(version) {
case 2006:
return "2006";
case 2014:
return "2014";
default:
throw new Error("no versionString for version "+version);
}
}
public static boolean versionHasPragma(int version) {
......
......@@ -778,22 +778,22 @@ Avro has multiple codecs (for compression of the data):
\subsection{Abstract syntax}
\begin{verbatim}
Program ::= Decl*;
Specification ::= Decl*;
abstract Decl ::= Type <Name:String>;
abstract Decl ::= DataType <Name:String>;
TypeDecl : Decl;
SampleDecl : Decl;
Field ::= Type <Name:String>;
Field ::= DataType <Name:String>;
abstract Type;
VoidType : Type;
SampleRefType : Type;
PrimType : Type ::= <Name:String> <Token:int>;
UserType : Type ::= <Name:String>;
StructType : Type ::= Field*;
ParseArrayType : Type ::= Type Dim*;
abstract ArrayType : Type ::= Type Exp*;
abstract DataType;
VoidType : DataType;
SampleRefType : DataType;
PrimType : DataType ::= <Name:String> <Token:int>;
UserType : DataType ::= <Name:String>;
StructType : DataType ::= Field*;
ParseArrayType : DataType ::= DataType Dim*;
abstract ArrayType : DataType ::= DataType Exp*;
VariableArrayType : ArrayType;
FixedArrayType : ArrayType;
......
......@@ -17,7 +17,7 @@ ifeq ($(UNAME_S),Darwin)
else
cd simple ; sh compile.sh && sh run.sh
$(MAKE) -C wiki_example test
$(MAKE) -C user_types test
$(MAKE) -C user_types all
endif
$(MAKE) -C duck_typing test
$(MAKE) -C twoway test
......
......@@ -27,7 +27,7 @@ if __name__ == '__main__':
while True:
value,decl = decoder.decode()
if value:
print decl.name, 'says', value.says
print decl.name, 'says', value
pass
pass
pass
......
......@@ -158,7 +158,7 @@ public class DynamicPart {
}
public static InRAMCompiler generateCode(String lcDecl, HashMap<String, String> handlers) {
Program ast = null;
Specification ast = null;
InputStream in = new ByteArrayInputStream(lcDecl.getBytes());
LabCommScanner scanner = new LabCommScanner(in);
LabCommParser parser = new LabCommParser();
......@@ -166,7 +166,7 @@ public class DynamicPart {
InRAMCompiler irc = null;
try {
Program p = (Program)parser.parse(scanner);
Specification p = (Specification)parser.parse(scanner);
p.errorCheck(errors);
if (errors.isEmpty()) {
ast = p;
......@@ -197,7 +197,7 @@ public class DynamicPart {
* @param handlers - a map <name, source> of handlers for the types in ast
* @return an InRAMCompiler object containing the generated clases
*/
private static InRAMCompiler handleAst(Program lcAST, HashMap<String, String> handlers) {
private static InRAMCompiler handleAst(Specification lcAST, HashMap<String, String> handlers) {
Map<String, String> genCode = new HashMap<String, String>();
try {
lcAST.J_gen(genCode, "labcomm.generated", 2014);
......
......@@ -22,7 +22,7 @@ import se.lth.control.labcomm2014.Encoder;
import se.lth.control.labcomm2014.EncoderChannel;
import AST.Parser;
import AST.Scanner;
import AST.Program;
import AST.Specification;
import beaver.Parser.Exception;
......@@ -121,7 +121,7 @@ public class TestCompiler {
}
public static InRAMCompiler generateCode(String lcDecl, HashMap<String, String> handlers) {
Program ast = null;
Specification ast = null;
InputStream in = new ByteArrayInputStream(lcDecl.getBytes());
Scanner scanner = new Scanner(in);
Parser parser = new Parser();
......@@ -129,7 +129,7 @@ public class TestCompiler {
InRAMCompiler irc = null;
try {
Program p = (Program)parser.parse(scanner);
Specification p = (Specification)parser.parse(scanner);
p.errorCheck(errors);
if (errors.isEmpty()) {
ast = p;
......@@ -207,7 +207,7 @@ public class TestCompiler {
* @param handlers - a map <name, source> of handlers for the types in ast
* @return an InRAMCompiler object containing the generated clases
*/
private static InRAMCompiler handleAst(Program lcAST, HashMap<String, String> handlers) {
private static InRAMCompiler handleAst(Specification lcAST, HashMap<String, String> handlers) {
Map<String, String> genCode = new HashMap<String, String>();
try {
lcAST.J_gen(genCode, "labcomm.generated", 2013);
......
......@@ -161,7 +161,7 @@ public class TestLabcommGen {
}
public static InRAMCompiler generateCode(String lcDecl, HashMap<String, String> handlers) {
Program ast = null;
Specification ast = null;
InputStream in = new ByteArrayInputStream(lcDecl.getBytes());
LabCommScanner scanner = new LabCommScanner(in);
LabCommParser parser = new LabCommParser();
......@@ -169,7 +169,7 @@ public class TestLabcommGen {
InRAMCompiler irc = null;
try {
Program p = (Program)parser.parse(scanner);
Specification p = (Specification)parser.parse(scanner);
p.errorCheck(errors);
if (errors.isEmpty()) {
ast = p;
......@@ -200,7 +200,7 @@ public class TestLabcommGen {
* @param handlers - a map <name, source> of handlers for the types in ast
* @return an InRAMCompiler object containing the generated clases
*/
private static InRAMCompiler handleAst(Program lcAST, HashMap<String, String> handlers) {
private static InRAMCompiler handleAst(Specification lcAST, HashMap<String, String> handlers) {
Map<String, String> genCode = new HashMap<String, String>();
try {
lcAST.J_gen(genCode, "labcomm.generated", 2014);
......@@ -284,7 +284,7 @@ public class TestLabcommGen {
* @param handlers - a map <name, source> of handlers for the types in ast
* @return an InRAMCompiler object containing the generated clases
*/
private static InRAMCompiler handleAstSeparate(Program lcAST, HashMap<String, String> handlers) {
private static InRAMCompiler handleAstSeparate(Specification lcAST, HashMap<String, String> handlers) {
Map<String, String> genCode = new HashMap<String, String>();
try {
lcAST.J_gen(genCode, "labcomm.generated", 2013);
......
......@@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace RobotCtrl
{
class Program
class Specification
{
public static string IP_ADDRESS = "127.0.0.1";
......@@ -20,7 +20,7 @@ namespace RobotCtrl
{
jointtarget val = new jointtarget { robax = new jointtarget.struct_robax(), extax = new jointtarget.struct_extax() };
TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(Program.IP_ADDRESS), Program.PORT);
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(Specification.IP_ADDRESS), Specification.PORT);
try
{
client.Connect(serverEndPoint);
......
......@@ -42,7 +42,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="LCRobot.cs" />
<Compile Include="Program.cs" />
<Compile Include="Specification.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
......