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
07b21d7b
Commit
07b21d7b
authored
Jul 09, 2013
by
Sven Robertz
Browse files
cleanup
parent
a6e3a38d
Changes
1
Hide whitespace changes
Inline
Side-by-side
examples/dynamic/TestLabcommGen.java
View file @
07b21d7b
...
...
@@ -27,8 +27,7 @@ import beaver.Parser.Exception;
public
class
TestLabcommGen
{
private
static
final
String
FOO
=
"foo"
;
private
static
final
String
BAR
=
"bar"
;
private
static
final
String
SAMPLE_NAME_FOO
=
"foo"
;
static
class
HandlerSrc
{
private
String
sampleName
;
...
...
@@ -57,14 +56,12 @@ public class TestLabcommGen {
String
labcommStr
=
readLabcommDecl
(
args
[
0
]);
String
srcStr
=
readHandlerDecl
(
args
[
1
]);
/*Map<sample name, handler code>*/
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);}");
generateHandlers
(
srcStr
,
handlers
);
System
.
out
.
println
(
"=======*******========="
);
System
.
out
.
println
(
"*** Generated handler source:"
);
handlers
.
keySet
();
for
(
String
n
:
handlers
.
keySet
())
{
...
...
@@ -81,14 +78,14 @@ public class TestLabcommGen {
String
tmpFile
=
args
[
2
];
System
.
out
.
println
(
"*** Testing writing and reading file "
+
tmpFile
);
encodeTest
(
irc
,
tmpFile
);
decodeTest
(
irc
,
tmpFile
);
encodeTest
(
irc
,
SAMPLE_NAME_FOO
,
tmpFile
);
decodeTest
(
irc
,
SAMPLE_NAME_FOO
,
tmpFile
);
}
}
public
static
void
generateHandlers
(
String
srcStr
,
HashMap
<
String
,
String
>
handlers
)
{
int
pos
=
0
;
while
(
pos
<
srcStr
.
length
())
{
System
.
out
.
println
(
"--------"
);
//
System.out.println("--------");
int
nameEnd
=
srcStr
.
indexOf
(
':'
,
pos
);
if
(
nameEnd
<
0
)
break
;
...
...
@@ -157,14 +154,99 @@ public class TestLabcommGen {
return
labcommStr
;
}
private
static
void
decodeTest
(
InRAMCompiler
irc
,
String
tmpFile
)
{
public
static
InRAMCompiler
generateCode
(
String
lcDecl
,
HashMap
<
String
,
String
>
handlers
)
{
Program
ast
=
null
;
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
);
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
if
(
ast
!=
null
)
{
irc
=
handleAst
(
ast
,
handlers
);
}
else
{
System
.
err
.
println
(
"compilation failed"
);
}
return
irc
;
}
/** 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
{
lcAST
.
J_gen
(
genCode
,
"labcomm.generated"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
"Generated labcomm code:"
);
InRAMCompiler
irc
=
new
InRAMCompilerJavax
(
"labcomm.generated"
,
null
);
Iterator
<
String
>
i
=
genCode
.
keySet
().
iterator
();
while
(
i
.
hasNext
()){
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_"
+
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
(
sampleName
,
src
);
irc
.
compile
(
"gen_"
+
sampleName
+
"Handler"
,
sb
.
toString
());
}
catch
(
IllegalArgumentException
e
)
{
e
.
printStackTrace
();
}
catch
(
SecurityException
e
)
{
e
.
printStackTrace
();
}
catch
(
ClassNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
catch
(
InvocationTargetException
e
)
{
e
.
printStackTrace
();
}
catch
(
NoSuchMethodException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
"================================"
);
}
return
irc
;
}
/** test method
*/
private
static
void
decodeTest
(
InRAMCompiler
irc
,
String
sampleName
,
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"
);
Class
fc
=
irc
.
load
(
sampleName
);
Class
hc
=
irc
.
load
(
"gen_"
+
sampleName
+
"Handler"
);
Class
hi
=
irc
.
load
(
sampleName
+
"$Handler"
);
Object
h
=
hc
.
newInstance
();
...
...
@@ -178,12 +260,11 @@ public class TestLabcommGen {
}
}
private
static
void
encodeTest
(
InRAMCompiler
irc
,
String
tmpFile
)
{
Class
<?>
hc
;
/** test encoding
*/
private
static
void
encodeTest
(
InRAMCompiler
irc
,
String
sampleName
,
String
tmpFile
)
{
try
{
hc
=
irc
.
load
(
"gen_"
+
FOO
+
"Handler"
);
Object
h
=
hc
.
newInstance
();
Class
fc
=
irc
.
load
(
FOO
);
Class
fc
=
irc
.
load
(
sampleName
);
Object
f
=
fc
.
newInstance
();
Field
x
=
fc
.
getDeclaredField
(
"x"
);
Field
y
=
fc
.
getDeclaredField
(
"y"
);
...
...
@@ -202,50 +283,17 @@ public class TestLabcommGen {
out
.
close
();
}
catch
(
Throwable
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
}
public
static
InRAMCompiler
generateCode
(
String
lcDecl
,
HashMap
<
String
,
String
>
handlers
)
{
Program
ast
=
null
;
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
);
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
if
(
ast
!=
null
)
{
irc
=
handleAst
(
ast
,
handlers
);
}
else
{
System
.
err
.
println
(
"compilation failed"
);
}
return
irc
;
}
/* dummy test creating instances of sample and handler, and calling handle*/
/** 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_"
+
SAMPLE_NAME_
FOO
+
"Handler"
);
Object
h
=
hc
.
newInstance
();
Class
fc
=
irc
.
load
(
FOO
);
Class
fc
=
irc
.
load
(
SAMPLE_NAME_
FOO
);
Object
f
=
fc
.
newInstance
();
Field
x
=
fc
.
getDeclaredField
(
"x"
);
Field
y
=
fc
.
getDeclaredField
(
"y"
);
...
...
@@ -255,7 +303,7 @@ public class TestLabcommGen {
z
.
setInt
(
f
,
12
);
Method
m
;
try
{
m
=
hc
.
getDeclaredMethod
(
"handle_"
+
FOO
,
fc
);
m
=
hc
.
getDeclaredMethod
(
"handle_"
+
SAMPLE_NAME_
FOO
,
fc
);
m
.
invoke
(
h
,
f
);
}
catch
(
SecurityException
e
)
{
// TODO Auto-generated catch block
...
...
@@ -287,54 +335,4 @@ public class TestLabcommGen {
e
.
printStackTrace
();
}
}
/** 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
{
lcAST
.
J_gen
(
genCode
,
"labcomm.generated"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
"Generated labcomm code:"
);
InRAMCompiler
irc
=
new
InRAMCompilerJavax
(
"labcomm.generated"
,
null
);
Iterator
<
String
>
i
=
genCode
.
keySet
().
iterator
();
while
(
i
.
hasNext
()){
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_"
+
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
(
sampleName
,
src
);
irc
.
compile
(
"gen_"
+
sampleName
+
"Handler"
,
sb
.
toString
());
}
catch
(
IllegalArgumentException
e
)
{
e
.
printStackTrace
();
}
catch
(
SecurityException
e
)
{
e
.
printStackTrace
();
}
catch
(
ClassNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
catch
(
InvocationTargetException
e
)
{
e
.
printStackTrace
();
}
catch
(
NoSuchMethodException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
"================================"
);
}
return
irc
;
}
}
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