Skip to content
Snippets Groups Projects
Commit 0e50a779 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Added example/duck_typing

Coreccted LabComm.py
parent 184017b2
Branches
Tags
No related merge requests found
......@@ -8,8 +8,10 @@ test:
echo More to be done...
cd simple ; sh compile.sh && sh run.sh
$(MAKE) -C twoway test
$(MAKE) -C duck_typing test
.PHONY: clean distclean
clean distclean:
echo To be done...
$(MAKE) -C twoway clean
$(MAKE) -C duck_typing clean
LABCOMM_JAR=../../compiler/labComm.jar
LABCOMM=java -jar $(LABCOMM_JAR)
all: gen/animal.py
.PHONY: test
test: gen/animal.py
PYTHONPATH=../../lib/python:gen ./duck_typing.py
gen/.dir:
mkdir -p $@
gen/%.py: %.lc | gen/.dir
$(LABCOMM) --python=$@ $<
.PHONY: clean distclean
clean distclean:
rm -rf gen
typedef struct {
string says;
} animal;
sample animal cow;
sample animal dog;
sample struct {
string says;
} duck;
\ No newline at end of file
#!/usr/bin/python
import labcomm
import animal
import StringIO
class Animal:
def __init__(self):
self.says = None
if __name__ == '__main__':
buf = StringIO.StringIO()
encoder = labcomm.Encoder(labcomm.StreamWriter(buf))
encoder.add_decl(animal.cow.signature)
encoder.add_decl(animal.dog.signature)
encoder.add_decl(animal.duck.signature)
theAnimal = Animal()
theAnimal.says = 'Moo'
encoder.encode(theAnimal, animal.cow.signature)
theAnimal.says = 'Bow-Wow'
encoder.encode(theAnimal, animal.dog.signature)
theAnimal.says = 'Quack'
encoder.encode(theAnimal, animal.duck.signature)
buf.seek(0)
decoder = labcomm.Decoder(labcomm.StreamReader(buf))
try:
while True:
value,decl = decoder.decode()
if value:
print decl.name, 'says', value.says
pass
pass
pass
except EOFError:
pass
pass
......@@ -431,7 +431,7 @@ class struct:
decl.encode(encoder, obj[name])
else:
for (name, decl) in self.field:
decl.encode(encoder, obj.__getattribute__(name))
decl.encode(encoder, getattr(obj, name))
def decode_decl(self, decoder):
n_field = decoder.decode_packed32()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment