From 0e50a7794aaf3d5e23961e77c4d77604aabc9afe Mon Sep 17 00:00:00 2001 From: Anders Blomdell <anders.blomdell@control.lth.se> Date: Mon, 24 Jun 2013 10:40:59 +0200 Subject: [PATCH] Added example/duck_typing Coreccted LabComm.py --- examples/Makefile | 2 ++ examples/duck_typing/Makefile | 20 ++++++++++++++++ examples/duck_typing/animal.lc | 9 ++++++++ examples/duck_typing/duck_typing.py | 36 +++++++++++++++++++++++++++++ lib/python/labcomm/LabComm.py | 2 +- 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 examples/duck_typing/Makefile create mode 100644 examples/duck_typing/animal.lc create mode 100755 examples/duck_typing/duck_typing.py diff --git a/examples/Makefile b/examples/Makefile index 30ecece..cdbef3d 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -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 diff --git a/examples/duck_typing/Makefile b/examples/duck_typing/Makefile new file mode 100644 index 0000000..cd5ec76 --- /dev/null +++ b/examples/duck_typing/Makefile @@ -0,0 +1,20 @@ +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 + diff --git a/examples/duck_typing/animal.lc b/examples/duck_typing/animal.lc new file mode 100644 index 0000000..3272ae9 --- /dev/null +++ b/examples/duck_typing/animal.lc @@ -0,0 +1,9 @@ +typedef struct { + string says; +} animal; + +sample animal cow; +sample animal dog; +sample struct { + string says; +} duck; \ No newline at end of file diff --git a/examples/duck_typing/duck_typing.py b/examples/duck_typing/duck_typing.py new file mode 100755 index 0000000..3138e47 --- /dev/null +++ b/examples/duck_typing/duck_typing.py @@ -0,0 +1,36 @@ +#!/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 diff --git a/lib/python/labcomm/LabComm.py b/lib/python/labcomm/LabComm.py index 604b901..2a66397 100644 --- a/lib/python/labcomm/LabComm.py +++ b/lib/python/labcomm/LabComm.py @@ -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() -- GitLab