Commit 5589558f authored by Sven Robertz's avatar Sven Robertz
Browse files

a bit more elaborate example, now doing encode/decode

parent 26223bde
......@@ -5,4 +5,5 @@ NB! There is currently no connection between the files StaticPart.java, DynamicP
TestLabCommCompiler.java
the runme.sh script builds and runs the TestLabCommCompiler, which illustrates the
on-the-fly compilation to RAM, but is not yet connected to an actual LabComm channel
on-the-fly compilation to RAM, reading the labcomm declarations from file but with the
handlers hard coded in the test program.
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.CharBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import se.lth.control.labcomm.LabCommDecoder;
import se.lth.control.labcomm.LabCommDecoderChannel;
import se.lth.control.labcomm.LabCommEncoder;
import se.lth.control.labcomm.LabCommEncoderChannel;
import AST.LabCommParser;
import AST.LabCommScanner;
import AST.Program;
......@@ -19,59 +27,136 @@ import beaver.Parser.Exception;
public class TestLabCommCompiler {
private static final String BAR = "bar";
private static final String FOO = "foo";
/**
* @param args
*/
public static void main(String[] args) {
/* input data: */
String prg ="sample struct { int x; int y; int z;}foo; sample int bar;";
FileReader fr;
int len=0;;
CharBuffer buf = CharBuffer.allocate(1024);
try {
fr = new FileReader(args[0]);
len = fr.read(buf);
buf.rewind();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
String labcommStr = buf.toString().substring(0, len-1);
HashMap <String, String> handlers = new HashMap<String, String>();
handlers.put("foo", "public void handle_foo(foo value) {\nSystem.out.println(value.x);\nSystem.out.println(value.y);\nSystem.out.println(value.z);}");
handlers.put("bar", "public void handle_bar(int value) {System.out.println(value);}");
generateCode(prg, handlers);
handlers.put(FOO, "public void handle_"+FOO+"("+FOO+" value) {\nSystem.out.println(value.x);\nSystem.out.println(value.y);\nSystem.out.println(value.z);}");
handlers.put(BAR, "public void handle_"+BAR+"(int value) {System.out.println(value);}");
InRAMCompiler irc = generateCode(labcommStr, handlers);
if(irc != null) {
System.out.println("*** Testing instantiation and invocation of Handler ");
dummyTest(irc);
// String tmpFile = "/tmp/lcctest";
String tmpFile = args[1];
System.out.println("*** Testing writing and reading file "+tmpFile);
encodeTest(irc, tmpFile);
decodeTest(irc, tmpFile);
}
}
private static void decodeTest(InRAMCompiler irc, String tmpFile) {
try {
FileInputStream in = new FileInputStream(tmpFile);
LabCommDecoderChannel dec = new LabCommDecoderChannel(in);
Class fc = irc.load(FOO);
Class hc = irc.load("gen_"+FOO+"Handler");
Class hi = irc.load(FOO+"$Handler");
Object h = hc.newInstance();
Method reg = fc.getDeclaredMethod("register", LabCommDecoder.class, hi);
reg.invoke(fc, dec, h);
dec.runOne();
in.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
private static void encodeTest(InRAMCompiler irc, String tmpFile) {
Class<?> hc;
try {
hc = irc.load("gen_"+FOO+"Handler");
Object h = hc.newInstance();
Class fc = irc.load(FOO);
Object f = fc.newInstance();
Field x = fc.getDeclaredField("x");
Field y = fc.getDeclaredField("y");
Field z = fc.getDeclaredField("z");
x.setInt(f, 10);
y.setInt(f, 11);
z.setInt(f, 12);
FileOutputStream out = new FileOutputStream(tmpFile);
LabCommEncoderChannel enc = new LabCommEncoderChannel(out);
Method reg = fc.getDeclaredMethod("register", LabCommEncoder.class);
reg.invoke(fc, enc);
Method doEncode = fc.getDeclaredMethod("encode", LabCommEncoder.class, fc);
doEncode.invoke(fc, enc, f);
out.close();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void generateCode(String prg, HashMap<String, String> handlers) {
public static InRAMCompiler generateCode(String lcDecl, HashMap<String, String> handlers) {
Program ast = null;
InputStream in = new ByteArrayInputStream(prg.getBytes());
InputStream in = new ByteArrayInputStream(lcDecl.getBytes());
LabCommScanner scanner = new LabCommScanner(in);
LabCommParser parser = new LabCommParser();
Collection errors = new LinkedList();
InRAMCompiler irc = null;
try {
Program p = (Program)parser.parse(scanner);
p.errorCheck(errors);
if (errors.isEmpty()) {
ast = p;
} else {
System.out.println("*** Errors:");
for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
String s = (String)iter.next();
System.out.println(s);
}
}
if (errors.isEmpty()) {
ast = p;
} else {
System.out.println("*** Errors:");
for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
String s = (String)iter.next();
System.out.println(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (ast != null) {
InRAMCompiler irc = handleAst(ast, handlers);
dummyTest(irc);
irc = handleAst(ast, handlers);
} else {
System.err.println("compilation failed");
}
return irc;
}
/* dummy test creating instances of sample and handler, and calling handle*/
private static void dummyTest(InRAMCompiler irc) {
try {
Class hc = irc.load("gen_fooHandler");
Class hc = irc.load("gen_"+FOO+"Handler");
Object h = hc.newInstance();
Class fc = irc.load("foo");
Class fc = irc.load(FOO);
Object f = fc.newInstance();
Field x = fc.getDeclaredField("x");
Field y = fc.getDeclaredField("y");
......@@ -81,21 +166,49 @@ public class TestLabCommCompiler {
z.setInt(f, 12);
Method m;
try {
m = hc.getDeclaredMethod("handle_foo", fc);
m = hc.getDeclaredMethod("handle_"+FOO, fc);
m.invoke(h, f);
} catch (Throwable e) {
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Throwable e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static InRAMCompiler handleAst(Program ast, HashMap<String, String> handlers) {
Map<String, String> foo = new HashMap<String, String>();
/** generate labcomm code and compile handlers
*
* @param lcAST - the AST of the labcomm declaration
* @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) {
Map<String, String> genCode = new HashMap<String, String>();
try {
ast.J_gen(foo, "labcomm.generated");
lcAST.J_gen(genCode, "labcomm.generated");
} catch (IOException e) {
e.printStackTrace();
}
......@@ -103,21 +216,21 @@ public class TestLabCommCompiler {
InRAMCompiler irc = new InRAMCompilerJavax("labcomm.generated", null);
Iterator<String> i = foo.keySet().iterator();
Iterator<String> i = genCode.keySet().iterator();
while(i.hasNext()){
final String name = i.next();
final String src = foo.get(name);
System.out.println("***"+name+"\n"+src);
final String sampleName = i.next();
final String src = genCode.get(sampleName);
System.out.println("***"+sampleName+"\n"+src);
StringBuilder sb = new StringBuilder();
sb.append("package labcomm.generated;\n");
sb.append("public class gen_"+name+"Handler implements "+name+".Handler {\n");
sb.append(handlers.get(name));
sb.append("public class gen_"+sampleName+"Handler implements "+sampleName+".Handler {\n");
sb.append(handlers.get(sampleName));
sb.append("}\n");
System.out.println("-------------------------------------");
System.out.println(sb.toString());
try {
irc.compile(name, src);
irc.compile("gen_"+name+"Handler", sb.toString());
irc.compile(sampleName, src);
irc.compile("gen_"+sampleName+"Handler", sb.toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
......
......@@ -2,4 +2,4 @@
javac -cp .:../../compiler/labComm.jar:../../lib/java/labcomm.jar:../../lib/tools/beaver.jar:../../lib/tools/beaver-rt.jar:../../lib/tools/jastadd2.jar:../../lib/tools/JFlex.jar:../../lib/tools/proj.jar TestLabCommCompiler.java
java -cp .:../../compiler/labComm.jar:../../lib/java/labcomm.jar:../../lib/tools/beaver.jar:../../lib/tools/beaver-rt.jar:../../lib/tools/jastadd2.jar:../../lib/tools/JFlex.jar:../../lib/tools/proj.jar TestLabCommCompiler
java -cp .:../../compiler/labComm.jar:../../lib/java/labcomm.jar:../../lib/tools/beaver.jar:../../lib/tools/beaver-rt.jar:../../lib/tools/jastadd2.jar:../../lib/tools/JFlex.jar:../../lib/tools/proj.jar TestLabCommCompiler simple.lc encoded_data
typedef struct {
int a;
int b;
} TwoInts;
sample TwoInts theTwoInts;
sample TwoInts anotherTwoInts;
sample struct {
int x;
string s;
} IntString;
int y;
int z;
} foo;
sample int bar;
Markdown is supported
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