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
Anders Blomdell
LabComm
Commits
c8bcec78
Commit
c8bcec78
authored
Jun 02, 2015
by
Anders Blomdell
Browse files
Renaming implemented for Java. Various cleanups.
parent
43d8a6f8
Changes
23
Hide whitespace changes
Inline
Side-by-side
compiler/2014/Java_CodeGen.jrag
View file @
c8bcec78
...
...
@@ -510,36 +510,36 @@ aspect Java_Class {
env.println("private static class Dispatcher implements SampleDispatcher "+genericStr+"{");
env.indent();
env.println();
env.println("public Class"+genericStr+" getSampleClass() {");
env.indent();
env.println("return " + getName() + ".class;");
env.unindent();
env.println("}");
env.println();
//
env.println("public Class"+genericStr+" getSampleClass() {");
//
env.indent();
//
env.println("return " + getName() + ".class;");
//
env.unindent();
//
env.println("}");
//
env.println();
env.println("public String getName() {");
env.indent();
env.println("return \"" + getName() + "\";");
env.unindent();
env.println("}");
env.println();
env.println("public byte getTypeDeclTag() {");
env.indent();
if(env.version == 2006) {
if(isSample) {
env.println("return Constant.SAMPLE;");
} else {
env.println("return Constant.TYPEDEF;");
}
} else {
if(isSample) {
env.println("return Constant.SAMPLE_DEF;");
} else {
env.println("return Constant.TYPE_DEF;");
}
}
env.unindent();
env.println("}");
env.println();
//
env.println("public byte getTypeDeclTag() {");
//
env.indent();
//
if(env.version == 2006) {
//
if(isSample) {
//
env.println("return Constant.SAMPLE;");
//
} else {
//
env.println("return Constant.TYPEDEF;");
//
}
//
} else {
//
if(isSample) {
//
env.println("return Constant.SAMPLE_DEF;");
//
} else {
//
env.println("return Constant.TYPE_DEF;");
//
}
//
}
//
env.unindent();
//
env.println("}");
//
env.println();
// env.println("public boolean isSample() {");
// env.indent();
// env.println("return "+isSample+";");
...
...
@@ -568,16 +568,16 @@ aspect Java_Class {
// env.unindent();
// env.println("}");
// env.println();
env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{");
env.indent();
if(!isSample || hasDependencies()) {
env.println("emitSignature(e);");
} else {
env.println("// the type has no dependencies, do nothing");
}
env.unindent();
env.println("}");
env.println();
//
env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{");
//
env.indent();
//
if(!isSample || hasDependencies()) {
//
env.println("emitSignature(e);");
//
} else {
//
env.println("// the type has no dependencies, do nothing");
//
}
//
env.unindent();
//
env.println("}");
//
env.println();
env.println("public boolean canDecodeAndHandle() {");
env.indent();
env.println("return "+isSample+";");
...
...
@@ -834,7 +834,7 @@ aspect Java_Class {
public void PrimType.Java_emitTypePrefix(Java_env env) {
switch (getToken()) {
case LABCOMM_STRING: { env.print("String"); } break;
case LABCOMM_SAMPLE: { env.print("
Class
"); } break;
case LABCOMM_SAMPLE: { env.print("
SampleDispatcher
"); } break;
default: { env.print(getName()); } break;
}
}
...
...
@@ -961,7 +961,7 @@ aspect Java_Class {
public void PrimType.Java_emitType(Java_env env) {
switch (getToken()) {
case LABCOMM_STRING: { env.print("String"); } break;
case LABCOMM_SAMPLE: { env.print("
Class
"); } break;
case LABCOMM_SAMPLE: { env.print("
SampleDispatcher
"); } break;
default: { env.print(getName()); } break;
}
}
...
...
lib/csharp/Makefile
View file @
c8bcec78
MODULES
=
Constant
\
MODULES
=
Constant
\
Decoder
\
DecoderChannel
\
DecoderRegistry
\
...
...
lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs
View file @
c8bcec78
...
...
@@ -50,9 +50,10 @@ namespace se.lth.control.labcomm2014 {
if
(
e
==
null
)
{
throw
new
IOException
(
"Unhandled tag "
+
tag
);
}
SampleDispatcher
d
=
e
.
get
Sample
Dispatcher
();
SampleDispatcher
d
=
e
.
getDispatcher
();
if
(
d
==
null
)
{
throw
new
IOException
(
"No dispatcher for '"
+
e
.
getName
()
+
"'"
+
e
.
getSignature
());
throw
new
IOException
(
"No dispatcher for '"
+
e
.
getName
()
+
"' "
+
e
.
getSignature
());
}
SampleHandler
h
=
e
.
getHandler
();
if
(
h
==
null
)
{
...
...
@@ -164,8 +165,9 @@ namespace se.lth.control.labcomm2014 {
int
index
=
(
int
)
ReadInt
(
4
);
try
{
DecoderRegistry
.
Entry
e
=
ref_registry
.
get
(
index
);
return
e
.
get
Sample
Dispatcher
();
return
e
.
getDispatcher
();
}
catch
(
NullReferenceException
)
{
// Handles both the case where index == 0 and unregistered sample
return
null
;
}
}
...
...
lib/csharp/se/lth/control/labcomm2014/DecoderRegistry.cs
View file @
c8bcec78
...
...
@@ -27,7 +27,7 @@ namespace se.lth.control.labcomm2014 {
this
.
signature
=
signature
;
}
public
SampleDispatcher
get
Sample
Dispatcher
()
{
public
SampleDispatcher
getDispatcher
()
{
return
dispatcher
;
}
...
...
lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs
View file @
c8bcec78
...
...
@@ -137,7 +137,7 @@ namespace se.lth.control.labcomm2014 {
try
{
index
=
ref_registry
.
getTag
(
value
);
}
catch
(
NullReferenceException
)
{
//
we want to return
0 for unregistered ref types
//
encode
0 for unregistered ref types
}
WriteInt
(
index
,
4
);
}
...
...
lib/csharp/se/lth/control/labcomm2014/EncoderRegistry.cs
View file @
c8bcec78
...
...
@@ -15,7 +15,7 @@ namespace se.lth.control.labcomm2014 {
this
.
index
=
index
;
}
public
SampleDispatcher
get
Sample
Dispatcher
()
{
public
SampleDispatcher
getDispatcher
()
{
return
dispatcher
;
}
...
...
lib/csharp/se/lth/control/labcomm2014/RenamingDecoder.cs
View file @
c8bcec78
...
...
@@ -4,7 +4,6 @@ namespace se.lth.control.labcomm2014 {
public
class
RenamingDecoder
:
WrappingDecoder
{
private
Decoder
decoder
;
private
RenamingRegistry
registry
;
private
Func
<
String
,
String
>
rename
;
...
...
@@ -12,20 +11,22 @@ namespace se.lth.control.labcomm2014 {
RenamingRegistry
registry
,
Func
<
String
,
String
>
rename
)
:
base
(
decoder
)
{
this
.
decoder
=
decoder
;
this
.
registry
=
registry
;
this
.
rename
=
rename
;
}
public
override
void
register
(
SampleDispatcher
dispatcher
,
SampleHandler
handler
)
{
decoder
.
register
(
registry
.
add
(
dispatcher
,
rename
(
dispatcher
.
getName
())),
handler
);
base
.
register
(
registry
.
add
(
dispatcher
,
rename
(
dispatcher
.
getName
())),
handler
);
}
public
override
void
registerSampleRef
(
SampleDispatcher
dispatcher
)
{
decoder
.
registerSampleRef
(
registry
.
add
(
dispatcher
,
rename
(
dispatcher
.
getName
())));
base
.
registerSampleRef
(
registry
.
add
(
dispatcher
,
rename
(
dispatcher
.
getName
())));
}
...
...
lib/csharp/se/lth/control/labcomm2014/RenamingEncoder.cs
View file @
c8bcec78
...
...
@@ -5,7 +5,6 @@ namespace se.lth.control.labcomm2014 {
public
class
RenamingEncoder
:
WrappingEncoder
{
private
Encoder
encoder
;
private
RenamingRegistry
registry
;
private
Func
<
String
,
String
>
rename
;
private
Dictionary
<
SampleDispatcher
,
SampleDispatcher
>
alias
;
...
...
@@ -14,7 +13,6 @@ namespace se.lth.control.labcomm2014 {
RenamingRegistry
registry
,
Func
<
String
,
String
>
rename
)
:
base
(
encoder
)
{
this
.
encoder
=
encoder
;
this
.
registry
=
registry
;
this
.
rename
=
rename
;
alias
=
new
Dictionary
<
SampleDispatcher
,
SampleDispatcher
>();
...
...
@@ -28,7 +26,6 @@ namespace se.lth.control.labcomm2014 {
alias
.
Add
(
identity
,
renamed
);
}
}
get
(
identity
);
return
renamed
;
}
...
...
@@ -40,11 +37,11 @@ namespace se.lth.control.labcomm2014 {
return
renamed
;
}
public
override
void
register
(
SampleDispatcher
identity
)
{
encoder
.
register
(
add
(
identity
));
base
.
register
(
add
(
identity
));
}
public
override
void
registerSampleRef
(
SampleDispatcher
identity
)
{
encoder
.
registerSampleRef
(
add
(
identity
));
base
.
registerSampleRef
(
add
(
identity
));
}
public
override
void
begin
(
SampleDispatcher
identity
)
{
...
...
lib/java/Makefile
View file @
c8bcec78
...
...
@@ -6,10 +6,15 @@ MODULES=Constant \
EncoderChannel
\
EncoderRegistry
\
Reader
\
RenamingDecoder
\
RenamingEncoder
\
RenamingRegistry
\
Sample
\
SampleDispatcher
\
SampleHandler
\
SampleType
\
WrappingDecoder
\
WrappingEncoder
\
Writer
\
WriterWrapper
...
...
lib/java/se/lth/control/labcomm2014/Decoder.java
View file @
c8bcec78
...
...
@@ -19,6 +19,6 @@ public interface Decoder {
public
double
decodeDouble
()
throws
IOException
;
public
String
decodeString
()
throws
IOException
;
public
int
decodePacked32
()
throws
IOException
;
public
Class
decodeSampleRef
()
throws
IOException
;
public
SampleDispatcher
decodeSampleRef
()
throws
IOException
;
}
lib/java/se/lth/control/labcomm2014/DecoderChannel.java
View file @
c8bcec78
...
...
@@ -16,42 +16,6 @@ public class DecoderChannel implements Decoder {
this
.
in
=
new
DataInputStream
(
in
);
}
private
void
processSampleDef
()
throws
IOException
{
int
index
=
decodePacked32
();
String
name
=
decodeString
();
int
signature_length
=
decodePacked32
();
byte
[]
signature
=
new
byte
[
signature_length
];
ReadBytes
(
signature
,
signature_length
);
def_registry
.
add
(
index
,
name
,
signature
);
}
private
void
processSampleRef
()
throws
IOException
{
int
index
=
decodePacked32
();
String
name
=
decodeString
();
int
signature_length
=
decodePacked32
();
byte
[]
signature
=
new
byte
[
signature_length
];
ReadBytes
(
signature
,
signature_length
);
ref_registry
.
add
(
index
,
name
,
signature
);
}
private
void
processSample
(
int
tag
)
throws
Exception
{
DecoderRegistry
.
Entry
e
=
def_registry
.
get
(
tag
);
if
(
e
==
null
)
{
throw
new
IOException
(
"Unhandled tag "
+
tag
);
}
SampleDispatcher
d
=
e
.
getDispatcher
();
if
(
d
==
null
)
{
throw
new
IOException
(
"No dispatcher for '"
+
e
.
getName
()
+
"'"
);
}
SampleHandler
h
=
e
.
getHandler
();
if
(
h
==
null
)
{
throw
new
IOException
(
"No handler for '"
+
e
.
getName
()
+
"'"
);
}
// decodeAndHandle throws Exception and not IOException because
// the user provided handler might throw anything
d
.
decodeAndHandle
(
this
,
h
);
}
public
void
runOne
()
throws
Exception
{
boolean
done
=
false
;
while
(!
done
)
{
...
...
@@ -66,15 +30,38 @@ public class DecoderChannel implements Decoder {
}
}
break
;
case
Constant
.
SAMPLE_DEF
:
{
processSampleDef
();
int
index
=
decodePacked32
();
String
name
=
decodeString
();
int
signature_length
=
decodePacked32
();
byte
[]
signature
=
new
byte
[
signature_length
];
ReadBytes
(
signature
,
signature_length
);
def_registry
.
add
(
index
,
name
,
signature
);
}
break
;
case
Constant
.
SAMPLE_REF
:
{
processSampleRef
();
int
index
=
decodePacked32
();
String
name
=
decodeString
();
int
signature_length
=
decodePacked32
();
byte
[]
signature
=
new
byte
[
signature_length
];
ReadBytes
(
signature
,
signature_length
);
ref_registry
.
add
(
index
,
name
,
signature
);
}
break
;
default
:
{
processSample
(
tag
);
done
=
true
;
DecoderRegistry
.
Entry
e
=
def_registry
.
get
(
tag
);
if
(
e
==
null
)
{
throw
new
IOException
(
"Unhandled tag "
+
tag
);
}
SampleDispatcher
d
=
e
.
getDispatcher
();
if
(
d
==
null
)
{
throw
new
IOException
(
"No dispatcher for '"
+
e
.
getName
()
+
"'"
);
}
SampleHandler
h
=
e
.
getHandler
();
if
(
h
==
null
)
{
throw
new
IOException
(
"No handler for '"
+
e
.
getName
()
+
"'"
);
}
// decodeAndHandle throws Exception and not IOException because
// the user provided handler might throw anything
d
.
decodeAndHandle
(
this
,
h
);
done
=
true
;
}
}
}
...
...
@@ -162,27 +149,17 @@ public class DecoderChannel implements Decoder {
return
(
int
)
(
res
&
0xffffffff
);
}
public
Class
decodeSampleRef
()
throws
IOException
{
public
SampleDispatcher
decodeSampleRef
()
throws
IOException
{
int
index
=
in
.
readInt
();
try
{
DecoderRegistry
.
Entry
e
=
ref_registry
.
get
(
index
);
return
e
.
getDispatcher
()
.
getSampleClass
()
;
return
e
.
getDispatcher
();
}
catch
(
NullPointerException
e
)
{
// Handles both the case where index == 0 and unregistered sample
return
null
;
}
}
/* Package visible methods for use from TypeDefParser */
String
getSampleName
(
int
idx
)
{
DecoderRegistry
.
Entry
e
=
def_registry
.
get
(
idx
);
return
e
.
getName
();
}
byte
[]
getSampleSignature
(
int
idx
)
{
DecoderRegistry
.
Entry
e
=
def_registry
.
get
(
idx
);
return
e
.
getSignature
();
}
}
lib/java/se/lth/control/labcomm2014/Encoder.java
View file @
c8bcec78
...
...
@@ -17,7 +17,7 @@ public interface Encoder {
public
void
encodeFloat
(
float
value
)
throws
IOException
;
public
void
encodeDouble
(
double
value
)
throws
IOException
;
public
void
encodeString
(
String
value
)
throws
IOException
;
public
void
encodePacked32
(
long
value
)
throws
IOException
;
public
void
encodeSampleRef
(
Class
value
)
throws
IOException
;
public
void
encodePacked32
(
int
value
)
throws
IOException
;
public
void
encodeSampleRef
(
SampleDispatcher
value
)
throws
IOException
;
}
lib/java/se/lth/control/labcomm2014/EncoderChannel.java
View file @
c8bcec78
...
...
@@ -40,7 +40,6 @@ public class EncoderChannel implements Encoder {
}
public
void
registerSampleRef
(
SampleDispatcher
dispatcher
)
throws
IOException
{
System
.
err
.
println
(
dispatcher
);
int
index
=
ref_registry
.
add
(
dispatcher
);
begin
(
Constant
.
SAMPLE_REF
);
encodePacked32
(
index
);
...
...
@@ -129,7 +128,7 @@ public class EncoderChannel implements Encoder {
}
}
public
void
encodePacked32
(
long
value
)
throws
IOException
{
public
void
encodePacked32
(
int
value
)
throws
IOException
{
byte
[]
tmp
=
new
byte
[
5
];
long
v
=
value
&
0xffffffff
;
int
i
;
...
...
@@ -142,12 +141,12 @@ public class EncoderChannel implements Encoder {
}
}
public
void
encodeSampleRef
(
Class
value
)
throws
IOException
{
public
void
encodeSampleRef
(
SampleDispatcher
value
)
throws
IOException
{
int
index
=
0
;
try
{
index
=
ref_registry
.
getTag
(
value
);
}
catch
(
NullPointerException
e
)
{
//
we want to return
0 for unregistered ref types
//
encode
0 for unregistered ref types
}
data
.
writeInt
(
index
);
}
...
...
lib/java/se/lth/control/labcomm2014/EncoderRegistry.java
View file @
c8bcec78
...
...
@@ -26,38 +26,30 @@ public class EncoderRegistry {
}
private
int
userIndex
=
Constant
.
FIRST_USER_INDEX
;
private
HashMap
<
Class
,
Entry
>
by
Class
;
private
HashMap
<
SampleDispatcher
,
Entry
>
by
Dispatcher
;
public
EncoderRegistry
()
{
by
Class
=
new
HashMap
<
Class
,
Entry
>();
by
Dispatcher
=
new
HashMap
<
SampleDispatcher
,
Entry
>();
}
public
synchronized
int
add
(
SampleDispatcher
dispatcher
)
{
Entry
e
=
by
Class
.
get
(
dispatcher
.
getSampleClass
()
);
Entry
e
=
by
Dispatcher
.
get
(
dispatcher
);
if
(
e
==
null
)
{
e
=
new
Entry
(
dispatcher
,
userIndex
);
by
Class
.
put
(
dispatcher
.
getSampleClass
()
,
e
);
by
Dispatcher
.
put
(
dispatcher
,
e
);
userIndex
++;
}
return
e
.
getIndex
();
}
public
int
getTag
(
SampleDispatcher
d
)
throws
IOException
{
return
getTag
(
d
.
getSampleClass
());
}
public
int
getTag
(
Class
<?
extends
SampleType
>
sample
)
throws
IOException
{
Entry
e
=
byClass
.
get
(
sample
);
public
synchronized
int
getTag
(
SampleDispatcher
sample
)
throws
IOException
{
Entry
e
=
byDispatcher
.
get
(
sample
);
if
(
e
==
null
)
{
throw
new
IOException
(
"'"
+
sample
.
get
Simple
Name
()
+
sample
.
getName
()
+
"' is not registered"
);
}
return
e
.
index
;
}
public
boolean
contains
(
Class
<?
extends
SampleType
>
sample
)
{
return
byClass
.
containsKey
(
sample
);
return
e
.
getIndex
();
}
}
lib/java/se/lth/control/labcomm2014/RenamingDecoder.java
0 → 100644
View file @
c8bcec78
package
se.lth.control.labcomm2014
;
import
java.io.IOException
;
import
java.util.HashMap
;
public
class
RenamingDecoder
extends
WrappingDecoder
{
public
interface
Rename
{
String
rename
(
String
name
);
}
private
RenamingRegistry
registry
;
private
Rename
rename
;
public
RenamingDecoder
(
Decoder
decoder
,
RenamingRegistry
registry
,
Rename
rename
)
throws
IOException
{
super
(
decoder
);
this
.
registry
=
registry
;
this
.
rename
=
rename
;
}
public
void
register
(
SampleDispatcher
dispatcher
,
SampleHandler
handler
)
throws
IOException
{
super
.
register
(
registry
.
add
(
dispatcher
,
rename
.
rename
(
dispatcher
.
getName
())),
handler
);
}
public
void
registerSampleRef
(
SampleDispatcher
dispatcher
)
throws
IOException
{
super
.
registerSampleRef
(
registry
.
add
(
dispatcher
,
rename
.
rename
(
dispatcher
.
getName
())));
}
}
lib/java/se/lth/control/labcomm2014/RenamingEncoder.java
0 → 100644
View file @
c8bcec78
package
se.lth.control.labcomm2014
;
import
java.io.IOException
;
import
java.util.HashMap
;
public
class
RenamingEncoder
extends
WrappingEncoder
{
public
interface
Rename
{
String
rename
(
String
name
);
}
private
RenamingRegistry
registry
;
private
Rename
rename
;
private
HashMap
<
SampleDispatcher
,
SampleDispatcher
>
alias
;
public
RenamingEncoder
(
Encoder
encoder
,
RenamingRegistry
registry
,
Rename
rename
)
throws
IOException
{
super
(
encoder
);
this
.
registry
=
registry
;
this
.
rename
=
rename
;
alias
=
new
HashMap
<
SampleDispatcher
,
SampleDispatcher
>();
}
private
synchronized
SampleDispatcher
add
(
SampleDispatcher
identity
)
{
SampleDispatcher
renamed
=
alias
.
get
(
identity
);
if
(
renamed
==
null
)
{
renamed
=
registry
.
add
(
identity
,
rename
.
rename
(
identity
.
getName
()));
alias
.
put
(
identity
,
renamed
);
}
return
renamed
;
}
private
synchronized
SampleDispatcher
get
(
SampleDispatcher
identity
)
{
return
alias
.
get
(
identity
);
}
public
void
register
(
SampleDispatcher
identity
)
throws
IOException
{
super
.
register
(
add
(
identity
));
}
public
void
registerSampleRef
(
SampleDispatcher
identity
)
throws
IOException
{
super
.
registerSampleRef
(
add
(
identity
));
}
public
void
begin
(
SampleDispatcher
identity
)
throws
IOException
{
super
.
begin
(
get
(
identity
));
}
public
void
end
(
SampleDispatcher
identity
)
throws
IOException
{
super
.
end
(
get
(
identity
));
}
}
lib/java/se/lth/control/labcomm2014/RenamingRegistry.java
0 → 100644
View file @
c8bcec78
package
se.lth.control.labcomm2014
;
import
java.util.HashMap
;
public
class
RenamingRegistry
{
public
static
class
Dispatcher
implements
SampleDispatcher
{
private
final
SampleDispatcher
dispatcher
;
private
final
String
name
;
public
Dispatcher
(
SampleDispatcher
dispatcher
,
String
name
)
{
this
.
dispatcher
=
dispatcher
;
this
.
name
=
name
;
}
public
String
getName
()
{
return
name
;
}
public
byte
[]
getSignature
()
{
return
dispatcher
.
getSignature
();
}
public
void
decodeAndHandle
(
Decoder
decoder
,
SampleHandler
handler
)
throws
Exception
{
dispatcher
.
decodeAndHandle
(
decoder
,
handler
);
}
public
boolean
equals
(
Object
obj
)
{
if
(!
(
obj
instanceof
Dispatcher
))
{
return
false
;