Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Sven Gestegård Robertz
LabComm
Commits
5589558f
Commit
5589558f
authored
Jul 09, 2013
by
Sven Robertz
Browse files
a bit more elaborate example, now doing encode/decode
parent
26223bde
Changes
4
Hide whitespace changes
Inline
Side-by-side
examples/dynamic/README
View file @
5589558f
...
...
@@ -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.
examples/dynamic/TestLabCommCompiler.java
View file @
5589558f
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_
foo
Handler"
);
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
n
ame
=
i
.
next
();
final
String
src
=
foo
.
get
(
n
ame
);
System
.
out
.
println
(
"***"
+
n
ame
+
"\n"
+
src
);
final
String
sampleN
ame
=
i
.
next
();
final
String
src
=
genCode
.
get
(
sampleN
ame
);
System
.
out
.
println
(
"***"
+
sampleN
ame
+
"\n"
+
src
);
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"package labcomm.generated;\n"
);
sb
.
append
(
"public class gen_"
+
n
ame
+
"Handler implements "
+
n
ame
+
".Handler {\n"
);
sb
.
append
(
handlers
.
get
(
n
ame
));
sb
.
append
(
"public class gen_"
+
sampleN
ame
+
"Handler implements "
+
sampleN
ame
+
".Handler {\n"
);
sb
.
append
(
handlers
.
get
(
sampleN
ame
));
sb
.
append
(
"}\n"
);
System
.
out
.
println
(
"-------------------------------------"
);
System
.
out
.
println
(
sb
.
toString
());
try
{
irc
.
compile
(
n
ame
,
src
);
irc
.
compile
(
"gen_"
+
n
ame
+
"Handler"
,
sb
.
toString
());
irc
.
compile
(
sampleN
ame
,
src
);
irc
.
compile
(
"gen_"
+
sampleN
ame
+
"Handler"
,
sb
.
toString
());
}
catch
(
IllegalArgumentException
e
)
{
e
.
printStackTrace
();
}
catch
(
SecurityException
e
)
{
...
...
examples/dynamic/runme.sh
View file @
5589558f
...
...
@@ -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
examples/dynamic/simple.lc
View file @
5589558f
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;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment