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
aa1622fb
Commit
aa1622fb
authored
May 26, 2015
by
Sven Gestegård Robertz
Browse files
hacked python to make tests run. Duck-typing needs attention
parent
0a409467
Changes
4
Hide whitespace changes
Inline
Side-by-side
compiler/2014/Python_CodeGen.jrag
View file @
aa1622fb
...
...
@@ -151,11 +151,16 @@ aspect PythonTypes {
}
public void TypeInstance.Python_genIntentions(Python_env env) {
env.print("{");
// env.print("{");
// for(Intention i : sortedIntentions()) {
// env.print("'"+i.getKey()+"':'"+new String(i.getValue())+"', ");
// }
// env.print("}");
env.print("tuple((");
for(Intention i : sortedIntentions()) {
env.print("'"+i.getKey()+"'
:
'"+new String(i.getValue())+"', ");
env.print("
(
'"+i.getKey()+"'
,
'"+new String(i.getValue())+"'
)
, ");
}
env.print("
}
");
env.print("
))
");
}
public void TypeInstance.Python_genTypedef(Python_env env) {
...
...
examples/duck_typing/duck_typing.py
View file @
aa1622fb
...
...
@@ -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
...
...
examples/user_types/example_encoder.py
View file @
aa1622fb
...
...
@@ -8,31 +8,31 @@ if __name__ == '__main__':
version
=
sys
.
argv
[
2
]
if
len
(
sys
.
argv
)
==
3
else
"LabComm2014"
encoder
=
labcomm2014
.
Encoder
(
labcomm2014
.
StreamWriter
(
open
(
sys
.
argv
[
1
],
'w'
)),
version
)
encoder
.
add_decl
(
test
.
twoLines
.
signature
)
tl
=
dict
(
l1
=
dict
(
start
=
dict
(
x
=
dict
(
val
=
1
),
y
=
dict
(
val
=
11
)
)
,
end
=
dict
(
x
=
dict
(
val
=
2
),
y
=
dict
(
val
=
22
)
)
)
,
l2
=
dict
(
start
=
dict
(
x
=
dict
(
val
=
3
),
y
=
dict
(
val
=
33
)
)
,
end
=
dict
(
x
=
dict
(
val
=
4
),
y
=
dict
(
val
=
44
)
)
)
,
f
=
dict
(
a
=
10
,
b
=
20
,
c
=
False
)
)
tl
=
{
((
''
,
'l1'
),):{
((
''
,
'start'
),):{
((
''
,
'x'
),):{((
''
,
'val'
),):
1
},
((
''
,
'y'
),):{((
''
,
'val'
),):
11
}
}
,
((
''
,
'end'
),):{
((
''
,
'x'
),):{((
''
,
'val'
),):
2
},
((
''
,
'y'
),):{((
''
,
'val'
),):
22
}
}
}
,
((
''
,
'l2'
),):{
((
''
,
'start'
),):{
((
''
,
'x'
),):{((
''
,
'val'
),):
3
},
((
''
,
'y'
),):{((
''
,
'val'
),):
33
}
}
,
((
''
,
'end'
),):{
((
''
,
'x'
),):{((
''
,
'val'
),):
4
},
((
''
,
'y'
),):{((
''
,
'val'
),):
44
}
}
}
,
((
''
,
'f'
),):{
((
''
,
'a'
),):
10
,
((
''
,
'b'
),):
20
,
((
''
,
'c'
),):
False
}
}
encoder
.
encode
(
tl
,
test
.
twoLines
.
signature
)
lib/python/labcomm2014/LabComm.py
View file @
aa1622fb
...
...
@@ -389,15 +389,25 @@ class SAMPLE(primitive):
def
__repr__
(
self
):
return
"labcomm.SAMPLE()"
# helper function
def
dict_to_sorted_tuple
(
d
):
tmpL
=
zip
(
d
.
keys
(),
d
.
values
())
tmpL
.
sort
()
return
tuple
(
tmpL
)
#
# Aggregate types
#
class
sampledef_or_sampleref_or_typedef
(
type_decl
):
def
__init__
(
self
,
intentions
=
None
,
decl
=
None
):
if
intentions
is
not
None
and
''
in
intentions
:
self
.
name
=
intentions
[
''
]
self
.
name
=
None
if
intentions
is
not
None
:
self
.
intentionDict
=
dict
(
intentions
)
if
''
in
self
.
intentionDict
:
self
.
name
=
self
.
intentionDict
[
''
]
else
:
self
.
name
=
None
self
.
intentionDict
=
{}
self
.
intentions
=
intentions
self
.
decl
=
decl
...
...
@@ -459,21 +469,25 @@ class sample_def(sampledef_or_sampleref_or_typedef):
decoder
.
add_decl
(
decl
,
index
)
def
rename
(
self
,
name
):
newIntentions
=
intention
s
.
copy
()
newIntentions
=
intention
Dict
.
copy
()
newIntentions
[
''
]
=
name
return
sample_def
(
newIntentions
,
decl
=
self
.
decl
)
return
sample_def
(
dict_to_sorted_tuple
(
newIntentions
)
,
decl
=
self
.
decl
)
class
sample_ref
(
sampledef_or_sampleref_or_typedef
):
type_index
=
i_SAMPLE_REF
type_name
=
'sample_ref'
def
__init__
(
self
,
intentions
=
None
,
decl
=
None
,
sample
=
None
):
if
intentions
is
not
None
and
''
in
intentions
:
self
.
name
=
intentions
[
''
]
print
"sampleref: name = %s"
%
self
.
name
if
intentions
is
not
None
:
self
.
intentionDict
=
dict
(
intentions
)
if
''
in
self
.
intentionDict
:
self
.
name
=
self
.
intentionDict
[
''
]
print
"sampleref: name = %s"
%
self
.
name
else
:
self
.
intentionDict
=
{}
self
.
name
=
None
self
.
decl
=
decl
self
.
intentions
=
intentions
if
sample
==
None
and
self
.
name
!=
None
and
decl
!=
None
:
self
.
sample
=
sample_def
(
intentions
,
decl
)
else
:
...
...
@@ -624,6 +638,7 @@ class array(type_decl):
class
struct
(
type_decl
):
def
__init__
(
self
,
field
):
#print "struct __init__: field = %s" % str(field)
self
.
field
=
tuple
(
field
)
def
__eq__
(
self
,
other
):
...
...
@@ -645,13 +660,24 @@ class struct(type_decl):
encoder
.
encode_type_number
(
decl
)
def
encode
(
self
,
encoder
,
obj
):
if
isinstance
(
obj
,
dict
):
try
:
# hack to get names as keys in the obj:
tmp_foo
=
zip
(
map
(
lambda
x
:
dict
(
x
)[
''
],
obj
.
keys
()),
obj
.
values
())
tmp_obj
=
dict
(
tmp_foo
)
for
(
intentions
,
decl
)
in
self
.
field
:
name
=
intentions
[
''
]
decl
.
encode
(
encoder
,
obj
[
name
])
else
:
tmp
=
dict
(
intentions
)
#print "struct.encode field intentionstmp: %s" % tmp
#print "struct.encode field obj: %s" % tmp_obj
name
=
tmp
[
''
]
decl
.
encode
(
encoder
,
tmp_obj
[
name
])
except
AttributeError
:
print
"HERE BE DRAGONS! hack to get duck-typing example to work"
for
(
intentions
,
decl
)
in
self
.
field
:
name
=
intentions
[
''
]
tmp
=
dict
(
intentions
)
#print "struct.encode field intentionstmp: %s" % tmp
#print "struct.encode field obj: %s" % tmp_obj
name
=
tmp
[
''
]
print
"trying to encode [%s] "
%
(
name
)
decl
.
encode
(
encoder
,
getattr
(
obj
,
name
))
def
decode_decl
(
self
,
decoder
):
...
...
@@ -667,14 +693,14 @@ class struct(type_decl):
if
obj
==
None
:
obj
=
decoder
.
create_object
()
for
(
intentions
,
decl
)
in
self
.
field
:
name
=
intentions
[
''
]
obj
.
__setattr__
(
name
,
decl
.
decode
(
decoder
))
#
name =
dict(
intentions
)
['']
obj
.
__setattr__
(
intentions
,
decl
.
decode
(
decoder
))
return
obj
def
new_instance
(
self
):
result
=
anonymous_object
()
for
(
intentions
,
decl
)
in
self
.
field
:
name
=
intentions
[
''
]
name
=
dict
(
intentions
)
[
''
]
result
.
__setattr__
(
name
,
decl
.
new_instance
())
return
result
...
...
@@ -682,7 +708,10 @@ class struct(type_decl):
delim
=
""
result
=
"labcomm.struct(["
for
(
intentions
,
decl
)
in
self
.
field
:
name
=
intentions
[
''
]
if
''
in
intentions
else
'(no name)'
try
:
name
=
intentions
[
''
]
except
:
name
=
'(no name)'
result
+=
"%s
\n
('%s', %s)"
%
(
delim
,
name
,
decl
)
delim
=
","
result
+=
"
\n
])"
...
...
@@ -696,12 +725,14 @@ STRUCT = struct([])
class
anonymous_object
(
dict
):
def
__setattr__
(
self
,
name
,
value
):
if
name
.
startswith
(
"_"
):
# XXX HERE BE DRAGONS! Is this used or should it be removed?
if
(
str
(
name
)).
startswith
(
"_"
):
super
(
anonymous_object
,
self
).
__setattr__
(
name
,
value
)
else
:
self
[
name
]
=
value
def
__getattr__
(
self
,
name
):
# XXX d:o
if
name
.
startswith
(
"_"
):
return
super
(
anonymous_object
,
self
).
__getattr__
(
name
)
else
:
...
...
@@ -815,7 +846,7 @@ class Encoder(Codec):
if
not
isinstance
(
ref
,
type_decl
):
# Trying to register a sample class
ref
=
ref
.
signature
decl
=
sample_ref
(
name
=
ref
.
name
,
decl
=
ref
.
decl
,
sample
=
ref
)
decl
=
sample_ref
(
intentions
=
ref
.
intentions
,
decl
=
ref
.
decl
,
sample
=
ref
)
if
index
==
0
:
self
.
writer
.
mark_begin
(
ref
,
None
)
if
super
(
Encoder
,
self
).
add_ref
(
decl
,
index
):
...
...
@@ -889,13 +920,13 @@ class Encoder(Codec):
self
.
pack
(
"%ds"
%
len
(
s
),
s
)
def
encode_intentions
(
self
,
intentions
):
keys
=
intentions
.
keys
()
keys
.
sort
();
self
.
encode_packed32
(
len
(
intentions
))
for
k
in
keys
:
v
=
intentions
[
k
]
self
.
encode_string
(
k
)
self
.
encode_string
(
v
)
try
:
for
(
k
,
v
)
in
intentions
:
self
.
encode_string
(
k
)
self
.
encode_string
(
v
)
except
:
print
"WARNING! encode_intentions: don't know what to do with %s"
%
intentions
class
Decoder
(
Codec
):
def
__init__
(
self
,
reader
,
version
=
DEFAULT_VERSION
):
...
...
@@ -1035,7 +1066,8 @@ class Decoder(Codec):
key
=
self
.
decode_string
()
val
=
self
.
decode_string
()
res
[
key
]
=
val
return
res
result
=
dict_to_sorted_tuple
(
res
)
return
result
class
signature_reader
:
def
__init__
(
self
,
signature
):
...
...
Write
Preview
Supports
Markdown
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