Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Show changes
Showing
with 757 additions and 304 deletions
class StreamReader:
def __init__(self, stream):
self.stream = stream
pass
def read(self, count):
result = self.stream.read(count)
if len(result) == 0:
raise EOFError()
return result
def mark(self, value, decl):
pass
pass
class StreamWriter:
def __init__(self, stream):
self.stream = stream
pass
def write(self, data):
self.stream.write(data)
pass
def mark_begin(self, decl, value):
pass
def mark_end(self, decl, value):
self.stream.flush()
pass
pass
__all__ = [ 'LabComm' ]
import LabComm
from StreamReader import StreamReader
from StreamWriter import StreamWriter
Decoder = LabComm.Decoder
Encoder = LabComm.Encoder
sample = LabComm.sample_def
sample_def = LabComm.sample_def
sample_ref = LabComm.sample_ref
typedef = LabComm.typedef
array = LabComm.array
struct = LabComm.struct
primitive = LabComm.primitive
BOOLEAN = LabComm.BOOLEAN
BYTE = LabComm.BYTE
SHORT = LabComm.SHORT
INTEGER = LabComm.INTEGER
LONG = LabComm.LONG
FLOAT = LabComm.FLOAT
DOUBLE = LabComm.DOUBLE
STRING = LabComm.STRING
SAMPLE = LabComm.SAMPLE
decl_from_signature = LabComm.decl_from_signature
labcomm2014-*.src.rpm
rpmbuild
.PHONY: all
all:
.PHONY: test
test:
.PHONY: clean
clean:
rm -rf rpmbuild
rm -f *~
.PHONY: distclean
distclean: clean
rm -f labcomm-*.src.rpm
.PHONY: srpm
srpm:
./make_srpm
#!/bin/sh
spec() {
cat << 'EOF'
Name: labcomm__SUFFIX__
Version: __VERSION__
Release: 1
Summary: LabComm communication protocol
License: GPLv3
# https://gitlab.control.lth.se/anders_blomdell/labcomm/repository/archive.tar.gz?__COMMIT__
Source0: labcomm.__DESCRIBE__.tar.gz
BuildRequires: gcc
BuildRequires: ant
BuildRequires: java
BuildRequires: mono-core
%description
LabComm communication protocol
%package devel
Summary: LabComm communication protocol
Requires: %{name} = %{version}-%{release}
%description devel
LabComm communication protocol
%
%prep
%setup -q -c -a 0
%build
pwd
make
%install
#
# C
#
install -d ${RPM_BUILD_ROOT}/%{_libdir}
install lib/c/liblabcomm2014.a ${RPM_BUILD_ROOT}/%{_libdir}/
install lib/c/liblabcomm2014.so.1 ${RPM_BUILD_ROOT}/%{_libdir}/liblabcomm2014.so.__VERSION__
ln -s liblabcomm2014.so.__VERSION__ ${RPM_BUILD_ROOT}/%{_libdir}/liblabcomm2014.so
install -d ${RPM_BUILD_ROOT}/%{_includedir}/labcomm
install lib/c/2014/*h ${RPM_BUILD_ROOT}/%{_includedir}/labcomm
#
# java
#
install -d ${RPM_BUILD_ROOT}/usr/lib
install -m u=r,g=r,o=r compiler/labcomm2014_compiler.jar ${RPM_BUILD_ROOT}/usr/lib
install -m u=r,g=r,o=r lib/java/labcomm2014.jar ${RPM_BUILD_ROOT}/usr/lib
install -d ${RPM_BUILD_ROOT}/%{_bindir}
install -m u=rx,g=rx,o=rx \
compiler/labcomm2014 ${RPM_BUILD_ROOT}/%{_bindir}/labcomm2014
ls -l ${RPM_BUILD_ROOT}/%{_bindir}
#
# C#
#
install -d ${RPM_BUILD_ROOT}/usr/lib
install -m u=r,g=r,o=r lib/csharp/labcomm2014.dll ${RPM_BUILD_ROOT}/usr/lib
#
# Python
#
install -d ${RPM_BUILD_ROOT}/%{python_sitelib}/labcomm2014
install lib/python/labcomm2014/* ${RPM_BUILD_ROOT}/%{python_sitelib}/labcomm2014
%files
%defattr (-, root, root)
%exclude /usr/lib/debug
%exclude /usr/lib/labcomm2014_compiler.jar
/usr/lib/*
%{_libdir}/*
%files devel
%defattr (-, root, root)
/usr/lib/labcomm2014_compiler.jar
%{_includedir}/labcomm/*
%{_bindir}/*
EOF
}
# Create a suitable directory for rpmbuild
rm -rf rpmbuild
mkdir -p rpmbuild/BUILD
mkdir -p rpmbuild/SPECS
mkdir -p rpmbuild/RPMS
mkdir -p rpmbuild/SRPMS
mkdir -p rpmbuild/SOURCES
rm -rf rpmbuild/SOURCES/*
# Create spec and .tar.gz
DESCRIBE=$(git describe | sed -e 's/^v\(.*\)/\1/')
SUFFIX=$(echo ${DESCRIBE} | sed -e 's/^\([^.]*\)[.].*$/\1/g')
VERSION=$(echo ${DESCRIBE} | sed -e 's/^[^.]*[.]\(.*\)/\1/g;s/-/./g')
COMMIT=$(git rev-parse HEAD)
(
spec | \
sed -e "s/__SUFFIX__/${SUFFIX}/g" | \
sed -e "s/__VERSION__/${VERSION}/g" | \
sed -e "s/__DESCRIBE__/${DESCRIBE}/g" | \
sed -e "s/__COMMIT__/${COMMIT}/g" \
) > rpmbuild/SPECS/labcomm.spec
(
cd $(git rev-parse --show-toplevel)
git archive --format tar HEAD
) > rpmbuild/SOURCES/labcomm.${DESCRIBE}.tar.gz
rpmbuild --define "_topdir $(pwd)/rpmbuild" \
-bs rpmbuild/SPECS/labcomm.spec 2>&1 | sed -e 's|rpmbuild/SRPMS/||g'
mv rpmbuild/SRPMS/* .
gen
TESTS=basic simple nested ref
LABCOMM_JAR=../compiler/labcomm2014_compiler.jar
LABCOMM=java -jar $(LABCOMM_JAR)
MONO_PATH=$(shell pwd)/../lib/csharp
PYTHONPATH=$(shell pwd)/../lib/python
include ../lib/c/os_compat.mk
all:
test: $(TESTS:%=test_%) $(TESTS:%=test_renaming_%) compiler_errors
# PYTHONPATH=../lib/python \
# ./test_encoder_decoder.py --labcomm="$(LABCOMM)" basic.lc
.PHONY: clean distclean
clean distclean:
rm -rf gen
.PHONY: test_%
test_%: gen/%/signatures.py \
gen/%/c_relay \
gen/%/cs_relay.exe \
gen/%/java_relay.class \
gen/%/java_code
PYTHONPATH=$(PYTHONPATH) MONO_PATH=$(MONO_PATH) \
./test_encoder_decoder.py \
--signatures=gen/$*/signatures.py \
--test tee gen/$*/testdata \
--test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
gen/$*/c_relay /dev/stdin /dev/stdout \
--test mono gen/$*/cs_relay.exe /dev/stdin /dev/stdout \
--test java \\-cp gen/$*:../lib/java/labcomm2014.jar \
java_relay /dev/stdin /dev/stdout
.PHONY: test_renaming_%
test_renaming_%: gen/%/signatures.py \
gen/%/c_renaming_relay \
gen/%/cs_renaming_relay.exe \
gen/%/java_relay.class \
gen/%/java_code
PYTHONPATH=$(PYTHONPATH) MONO_PATH=$(MONO_PATH) \
./test_renaming_encoder_decoder.py \
--signatures=gen/$*/signatures.py \
--test tee gen/$*/testdata.renamed \
--test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
gen/$*/c_renaming_relay /dev/stdin /dev/stdout \
--test mono gen/$*/cs_renaming_relay.exe \
/dev/stdin /dev/stdout
echo \
--test java \\-cp gen/$*:../lib/java/labcomm2014.jar \
java_relay /dev/stdin /dev/stdout
# test cases for compiler error checking
.PHONY: compiler_errors testErrorsOK testErrorsNOK
compiler_errors: testErrorsOK testErrorsNOK
# tests that should succeed
testErrorsOK: $(wildcard errors/correct/*.lc)
./test_errors.py --labcomm="$(LABCOMM)" --testOK $^
# tests that should fail
testErrorsNOK: $(wildcard errors/incorrect/*.lc)
./test_errors.py --labcomm="$(LABCOMM)" --testNOK $^
.PRECIOUS: gen/%/.dir
gen/%/.dir:
mkdir -p gen/$*
touch $@
.PRECIOUS: gen/%/typeinfo
gen/%/typeinfo: %.lc Makefile | gen/%/.dir
$(LABCOMM) --typeinfo=$@ $<
.PRECIOUS: gen/%/signatures.py
gen/%/signatures.py: %.lc Makefile | gen/%/.dir
$(LABCOMM) --python=$@ $<
# C relay test rules
.PRECIOUS: gen/%/c_code.h gen/%/c_code.c
gen/%/c_code.h gen/%/c_code.c: %.lc Makefile | gen/%/.dir
$(LABCOMM) --c=gen/$*/c_code.c --h=gen/$*/c_code.h $<
.PRECIOUS: gen/%/c_relay.c
gen/%/c_relay.c: gen/%/typeinfo relay_gen_c.py Makefile
./relay_gen_c.py $< > $@
.PRECIOUS: gen/%/c_relay
gen/%/c_relay: gen/%/c_relay.c gen/%/c_code.c Makefile
$(CC) $(CFLAGS) -o $@ $< -I../lib/c/2014 -I. -L../lib/c \
gen/$*/c_code.c -llabcomm2014
.PRECIOUS: gen/%/c_renaming_relay.c
gen/%/c_renaming_relay.c: gen/%/typeinfo relay_gen_c.py Makefile
./relay_gen_c.py --renaming $< > $@
.PRECIOUS: gen/%/c_renaming_relay
gen/%/c_renaming_relay: gen/%/c_renaming_relay.c gen/%/c_code.c Makefile
$(CC) $(CFLAGS) -o $@ $< -I../lib/c/2014 -I. -L../lib/c \
gen/$*/c_code.c -llabcomm2014
# C# relay test rules
.PRECIOUS: gen/%/cs_code.cs
gen/%/cs_code.cs: %.lc Makefile | gen/%/.dir
$(LABCOMM) --cs=$@ $<
.PRECIOUS: gen/%/cs_relay.cs
gen/%/cs_relay.cs: gen/%/typeinfo relay_gen_cs.py Makefile
./relay_gen_cs.py $< > $@
.PRECIOUS: gen/%/cs_relay.exe
gen/%/cs_relay.exe: gen/%/cs_relay.cs gen/%/cs_code.cs Makefile
mcs -out:$@ $(filter %.cs, $^) -lib:../lib/csharp/ -r:labcomm2014
.PRECIOUS: gen/%/cs_renaming_relay.cs
gen/%/cs_renaming_relay.cs: gen/%/typeinfo relay_gen_cs.py Makefile
./relay_gen_cs.py --renaming $< > $@
.PRECIOUS: gen/%/cs_renaming_relay.exe
gen/%/cs_renaming_relay.exe: gen/%/cs_renaming_relay.cs \
gen/%/cs_code.cs Makefile
mcs -out:$@ $(filter %.cs, $^) -lib:../lib/csharp/ -r:labcomm2014
# Java relay test rules
.PRECIOUS: gen/%/java_code
gen/%/java_code: %.lc | gen/%/.dir
mkdir -p $@
$(LABCOMM) --java=$@ $<
.PRECIOUS: gen/%/java_relay.java
gen/%/java_relay.java: gen/%/typeinfo relay_gen_java.py Makefile
./relay_gen_java.py $< > $@
.PRECIOUS: gen/%/java_relay.class
gen/%/java_relay.class: gen/%/java_relay.java gen/%/java_code Makefile
javac -d gen/$* -cp ../lib/java/labcomm2014.jar:gen/$*/java_code $<
sample void s_void;
sample byte s_byte;
sample short s_short;
sample int s_int;
sample long s_long;
sample float s_float;
sample double s_double;
sample boolean s_boolean;
sample string s_string;
def gen(f, signatures):
print """
#include <labcomm.h>
#include <labcomm_fd_reader_writer.h>
#include "%(f)s.h"
""" % { 'f': f }
for s in [s.name for s in signatures]:
t = "%s_%s" % (f, s)
print """
void handle_%(t)s(%(t)s *v, void *context)
{
struct labcomm_encoder *e = context;
labcomm_encode_%(t)s(e, v);
}
""" % {'t':t}
print """
int main(int argc, char *argv[]) {
struct labcomm_decoder *d;
struct labcomm_encoder *e;
"""
#!/usr/bin/python
import os
import re
import subprocess
import sys
def gen(classname, namespace, signatures):
yield 'using System;'
yield 'using System.IO;'
yield 'using se.lth.control.labcomm;'
yield ''
yield 'class %s :' % classname
for s in signatures[0:-1]:
yield '%s%s.Handler,' % (namespace, s.name)
yield '%s%s.Handler' % (namespace, signatures[-1].name)
yield '{'
yield ''
yield ' LabCommEncoderChannel encoder;'
for s in signatures:
yield ''
if s.type == 'void':
yield ' void %s%s.Handler.handle() {' % ( namespace, s.name)
else:
yield ' void %s%s.Handler.handle(%s data) {' % (
namespace, s.name, s.type)
yield ' Console.Error.WriteLine("%s%s");' % (namespace, s.name)
if s.type == 'void':
yield ' %s%s.encode(encoder);' % (namespace, s.name)
else:
yield ' %s%s.encode(encoder, data);' % (namespace, s.name)
yield ' }'
yield ''
yield ' public %s(String InName, String OutName) {' % (classname)
yield ' FileStream InFile = new FileStream(InName,'
yield ' FileMode.Open, '
yield ' FileAccess.Read);'
yield ' LabCommDecoderChannel d = new LabCommDecoderChannel(InFile);'
yield ' FileStream OutFile = new FileStream(OutName, '
yield ' FileMode.OpenOrCreate, '
yield ' FileAccess.Write);'
yield ' encoder = new LabCommEncoderChannel(OutFile);'
yield ''
for s in signatures:
yield ' %s%s.register(d, this);' % (namespace, s.name)
yield ''
for s in signatures:
yield ' %s%s.register(encoder);' % (namespace, s.name)
yield ''
yield ' try {'
yield ' d.run();'
yield ' } catch (EndOfStreamException) {'
yield ' }'
yield ''
yield ' }'
yield ''
yield ' static void Main(String[] arg) {'
yield ' new %s(arg[0], arg[1]);' % (classname)
yield ' }'
yield ''
yield '}'
class Signature:
def __init__(self, name, type):
self.name = name
self.type = type
def test_program(target, typeinfo):
f = open(typeinfo)
signature = []
for l in f:
m = re.match('C#,sample,([^,]+),(.*)', l)
if m:
signature.append(Signature(m.group(1), m.group(2)))
f.close()
f = open(target, "w")
for l in gen("x", "", signature):
f.write(l + "\n")
f.close()
if __name__ == "__main__":
#os.system("labcomm --typeinfo=/dev/stdout %s" % sys.argv[1])
typeinfo = subprocess.Popen(["labcomm",
"--typeinfo=/dev/stdout %s" % sys.argv[1]],
stdout=subprocess.PIPE)
signature = []
for l in typeinfo.stdout:
m = re.match('C#,sample,([^,]+),(.*)', l)
if m:
signature.append(Signature(m.group(1), m.group(2)))
for l in gen("x", "", signature):
print l
typedef struct {
string topic;
} pubsub;
sample pubsub subscribe;
sample pubsub unsubscribe;
sample pubsub publish;
sample pubsub unpublish;
typedef void avoid;
sample avoid doavoid;
// examples of errors: void may not be used
// in structs or arrays
//
// sample struct {
// int a;
// avoid error;
//} foo;
//
//sample void error2[2] ;
//sample avoid error3[_];
typedef void avoid;
// examples of errors: void may not be used
// in structs or arrays
//
sample struct {
int a;
avoid error_1;
} foo;
sample void error_2[2] ;
sample avoid error_3[_];
typedef int int_array_ss[1][_, 3, _][5];
sample struct { int aa; boolean bb; int_array_ss ias; } struct_array_ss[1][_][_];
sample int s_array_array_array[1][1, 1][1, 1, 1];
sample struct {
int b[1][1, 1];
int c[1][_, 1, _][_,_];
} struct_array_ss[1];
typedef int int_t;
typedef int int_array_t[1][2][3,4];
typedef struct { int a; } struct_t;
......@@ -18,3 +20,6 @@ sample int_array_t int_array_t_s;
sample struct_t struct_t_s;
sample struct_array_t struct_array_t_s;
sample struct_struct_t struct_struct_t_s;
sample string string_array[_];
sample struct { string s[_]; } string_struct_array;
sample sample s1;
sample sample s2;
sample sample s3;
sample sample sample_list[4];
#!/usr/bin/python
import argparse
import re
import sys
def split_match(pattern, multiline):
def match(s):
m = re.match(pattern, s)
if m:
return m.group(1)
pass
return filter(lambda s: s != None, map(match, multiline.split('\n')))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate C test relay.')
parser.add_argument('--renaming', action='store_true')
parser.add_argument('typeinfo', help='typeinfo file')
options = parser.parse_args(sys.argv[1:])
f = open(options.typeinfo)
sample = []
for l in map(lambda s: s.strip(), f):
lang,kind,func,arg,stype = l[1:].split(l[0])
if lang == 'C' and kind == 'sample':
sample.append((func, arg, stype))
pass
pass
result = []
result.extend(split_match('^[^|]*\|(.*)$', """
|// generated by relay_gen_c.py
|#include <sys/types.h>
|#include <sys/stat.h>
|#include <fcntl.h>
|#include <stdio.h>
|#include <errno.h>
|#include <labcomm2014.h>
|#include <labcomm2014_default_error_handler.h>
|#include <labcomm2014_default_memory.h>
|#include <labcomm2014_default_scheduler.h>
|#include <labcomm2014_fd_reader.h>
|#include <labcomm2014_fd_writer.h>
"""))
if options.renaming:
result.extend(split_match('^[^|]*\|(.*)$', """
|#include "labcomm2014_renaming.h"
|#include "labcomm2014_renaming_encoder.h"
|#include "labcomm2014_renaming_decoder.h"
"""))
result.extend(split_match('^[^|]*\|(.*)$', """
|#include "c_code.h"
"""))
for func,arg,stype in sample:
result.extend(split_match('^[^|]*\|(.*)$', """
|void handle_%(func)s(%(arg)s *v, void *context)
|{
| struct labcomm2014_encoder *e = context;
| labcomm2014_encode_%(func)s(e%(valargstr)s);
|}""" % { 'func': func, 'arg': arg, 'valargstr': '' if stype == "void" else', v' }))
pass
result.extend(split_match('^[^|]*\|(.*)$', """
|int main(int argc, char *argv[]) {
| struct labcomm2014_encoder *e, *e_e;
| struct labcomm2014_decoder *d, *d_d;
"""))
if options.renaming:
result.extend(split_match('^[^|]*\|(.*)$', """
| struct labcomm2014_renaming_registry *registry;
| struct labcomm2014_encoder *e_p, *e_s;
| struct labcomm2014_decoder *d_p, *d_s;
"""))
result.extend(split_match('^[^|]*\|(.*)$', """
| int in, out, result;
|
| if (argc < 3) { return 1; }
| in = open(argv[1], O_RDONLY);
| if (in < 0) { return 1; }
| out = open(argv[2], O_WRONLY);
| if (out < 0) { return 1; }
| e_e = labcomm2014_encoder_new(labcomm2014_fd_writer_new(
| labcomm2014_default_memory, out, 1),
| labcomm2014_default_error_handler,
| labcomm2014_default_memory,
| labcomm2014_default_scheduler);
| d_d = labcomm2014_decoder_new(labcomm2014_fd_reader_new(
| labcomm2014_default_memory, in, 1),
| labcomm2014_default_error_handler,
| labcomm2014_default_memory,
| labcomm2014_default_scheduler);
"""))
if not options.renaming:
result.extend(split_match('^[^|]*\|(.*)$', """
| e = e_e;
| d = d_d;
"""))
else:
result.extend(split_match('^[^|]*\|(.*)$', """
| registry = labcomm2014_renaming_registry_new(
| labcomm2014_default_error_handler,
| labcomm2014_default_memory,
| labcomm2014_default_scheduler);
| e_p = labcomm2014_renaming_encoder_new(e_e,
| registry,
| labcomm2014_renaming_prefix,
| "prefix:");
| e_s = labcomm2014_renaming_encoder_new(e_p,
| registry,
| labcomm2014_renaming_suffix,
| ":suffix");
| e = e_s;
| d_p = labcomm2014_renaming_decoder_new(d_d,
| registry,
| labcomm2014_renaming_prefix,
| "prefix:");
| d_s = labcomm2014_renaming_decoder_new(d_p,
| registry,
| labcomm2014_renaming_suffix,
| ":suffix");
| d = d_s;
"""))
for func,arg,stype in sample:
result.extend(split_match('^[^|]*\|(.*)$', """
| labcomm2014_encoder_register_%(func)s(e);
| labcomm2014_encoder_sample_ref_register(e, labcomm2014_signature_%(func)s);
| labcomm2014_decoder_register_%(func)s(d, handle_%(func)s, e);
| labcomm2014_decoder_sample_ref_register(d, labcomm2014_signature_%(func)s);
""" % { 'func': func, 'arg': arg }))
result.extend(split_match('^[^|]*\|(.*)$', """
| while ((result = labcomm2014_decoder_decode_one(d)) > 0) {};
"""))
if options.renaming:
result.extend(split_match('^[^|]*\|(.*)$', """
| labcomm2014_decoder_free(d_s);
| labcomm2014_decoder_free(d_p);
| labcomm2014_encoder_free(e_s);
| labcomm2014_encoder_free(e_p);
| labcomm2014_renaming_registry_free(registry);
"""))
result.extend(split_match('^[^|]*\|(.*)$', """
| labcomm2014_decoder_free(d_d);
| labcomm2014_encoder_free(e_e);
| if (result == 0) return 0;
| if (result == -EPIPE) return 0;
| fprintf(stderr, "Failed with %d", result);
| return 1;
|}
"""))
print "\n".join(result)
pass
#!/usr/bin/python
import argparse
import re
import sys
import random
def split_match(pattern, multiline):
def match(s):
m = re.match(pattern, s)
if m:
return m.group(1)
pass
return filter(lambda s: s != None, map(match, multiline.split('\n')))
def shuffle(l):
result = list(l)
random.shuffle(result)
return result
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate C test relay.')
parser.add_argument('--renaming', action='store_true')
parser.add_argument('typeinfo', help='typeinfo file')
options = parser.parse_args(sys.argv[1:])
f = open(options.typeinfo)
sample = []
for l in map(lambda s: s.strip(), f):
lang,kind,func,arg,dummy = l[1:].split(l[0])
if lang == 'C#' and kind == 'sample':
sample.append((func, arg))
pass
pass
result = []
result.extend(split_match('^[^|]*\|(.*)$', """
|using System;
|using System.IO;
|using se.lth.control.labcomm2014;
|
|public class cs_relay :
"""))
for func,arg in sample[0:-1]:
result.append(' %s.Handler,' % func)
pass
result.append(' %s.Handler' % sample[-1][0])
result.extend(split_match('^[^|]*\|(.*)$', """
|{
| Encoder encoder;
| Decoder decoder;
|
"""))
for func,arg in sample:
if arg == 'void':
result.extend(split_match('^[^|]*\|(.*)$', """
| void %(func)s.Handler.handle() {
| %(func)s.encode(encoder);
| }
""" % { 'func': func}))
pass
else:
result.extend(split_match('^[^|]*\|(.*)$', """
| void %(func)s.Handler.handle(%(arg)s data) {
| %(func)s.encode(encoder, data);
| }
""" % { 'func': func, 'arg': arg}))
pass
pass
result.extend(split_match('^[^|]*\|(.*)$', """
| public cs_relay(String InName, String OutName) {
| FileStream InFile = new FileStream(InName,
| FileMode.Open,
| FileAccess.Read);
| decoder = new DecoderChannel(InFile);
| FileStream OutFile = new FileStream(OutName,
| FileMode.OpenOrCreate,
| FileAccess.Write);
| encoder = new EncoderChannel(OutFile);
"""))
if options.renaming:
result.extend(split_match('^[^|]*\|(.*)$', """
| RenamingRegistry registry = new RenamingRegistry();
| decoder = new RenamingDecoder(
| decoder, registry, s => "prefix:" + s + ":suffix");
| encoder = new RenamingEncoder(
| encoder, registry, s => "prefix:" + s + ":suffix");
"""))
for func,arg in shuffle(sample):
result.append(' %s.register(decoder, this);' % func)
pass
for func,arg in shuffle(sample):
result.append(' %s.registerSampleRef(decoder);' % func)
pass
for func,arg in shuffle(sample):
result.append(' %s.register(encoder);' % func)
pass
for func,arg in shuffle(sample):
result.append(' %s.registerSampleRef(encoder);' % func)
pass
result.extend(split_match('^[^|]*\|(.*)$', """
| try {
| decoder.run();
| } catch (EndOfStreamException) {
| }
| }
| static void Main(String[] arg) {
| new cs_relay(arg[0], arg[1]);
| }
|}
"""))
print "\n".join(result)
pass
#!/usr/bin/python
import re
import sys
import random
def split_match(pattern, multiline):
def match(s):
m = re.match(pattern, s)
if m:
return m.group(1)
pass
return filter(lambda s: s != None, map(match, multiline.split('\n')))
def shuffle(l):
result = list(l)
random.shuffle(result)
return result
if __name__ == '__main__':
f = open(sys.argv[1])
sample = []
for l in map(lambda s: s.strip(), f):
lang,kind,func,arg,dummy = l[1:].split(l[0])
if lang == 'Java' and kind == 'sample':
sample.append((func, arg))
pass
pass
result = []
result.extend(split_match('^[^|]*\|(.*)$', """
|import java.io.FileInputStream;
|import java.io.FileOutputStream;
|import java.io.IOException;
|import se.lth.control.labcomm2014.DecoderChannel;
|import se.lth.control.labcomm2014.EncoderChannel;
|import se.lth.control.labcomm2014.Sample;
|
|public class java_relay implements
"""))
for func,arg in sample[0:-1]:
result.append(' %s.Handler,' % func)
pass
result.append(' %s.Handler' % sample[-1][0])
result.extend(split_match('^[^|]*\|(.*)$', """
|{
| EncoderChannel encoder;
"""))
for func,arg in sample:
if arg == 'void':
result.extend(split_match('^[^|]*\|(.*)$', """
| public void handle_%(func)s() throws IOException {
| %(func)s.encode(encoder);
| }
""" % { 'func': func}))
pass
else:
result.extend(split_match('^[^|]*\|(.*)$', """
| public void handle_%(func)s(%(arg)s data) throws IOException {
| %(func)s.encode(encoder, data);
| }
""" % { 'func': func, 'arg': arg}))
pass
pass
result.extend(split_match('^[^|]*\|(.*)$', """
| public java_relay(String InName, String OutName) throws Exception {
| FileInputStream InFile = new FileInputStream(InName);
| DecoderChannel decoder = new DecoderChannel(InFile);
| FileOutputStream OutFile = new FileOutputStream(OutName);
| encoder = new EncoderChannel(OutFile);
|
"""))
for func,arg in shuffle(sample):
result.append(' %s.register(decoder, this);' % func)
pass
for func,arg in shuffle(sample):
result.append(' %s.registerSampleRef(decoder);' % func)
pass
for func,arg in shuffle(sample):
result.append(' %s.register(encoder);' % func)
pass
for func,arg in shuffle(sample):
result.append(' %s.registerSampleRef(encoder);' % func)
pass
result.extend(split_match('^[^|]*\|(.*)$', """
| try {
| decoder.run();
| } catch (java.io.EOFException e) {
| }
| }
| public static void main(String[] arg) throws Exception {
| new java_relay(arg[0], arg[1]);
| }
|}
"""))
print "\n".join(result)
pass
#!/usr/bin/python
# -*- coding: iso8859-15 -*-
import math
import os
import sys
import re
import traceback
import ccode
import cscode
def run_labcomm(base):
if not os.path.exists("gen/java/%s" % base):
os.makedirs("gen/java/%s" % base)
if not os.path.exists("gen/csharp"):
os.makedirs("gen/csharp")
if not os.path.exists("gen/c"):
os.makedirs("gen/c")
if not os.path.exists("gen/python"):
os.makedirs("gen/python")
cmd = " ".join([
"java -jar ../compiler/labComm.jar",
"--c=gen/c/%s.c" % base,
"--h=gen/c/%s.h" % base,
"--cs=gen/csharp/%s.cs" % base,
"--python=gen/python/%s.py" % base,
"--java=gen/java/%s" % base,
"--typeinfo=gen/%s.typeinfo" % base,
"%s.lc" % base
])
err = os.system(cmd)
if err != 0:
sys.exit(err / 256)
class hexwriter(object):
def __init__(self, filename=None):
self.pos = 0
self.ascii = ''
if filename:
self.outfile = open(filename, "w")
else:
self.outfile = None
def write(self, data):
for c in data:
if ' ' <= c and c <= '}':
self.ascii += c
else:
self.ascii += '.'
sys.stdout.write("%2.2x " % ord(c))
self.pos += 1
if self.pos >= 15:
sys.stdout.write("%s\n" % self.ascii)
self.pos = 0
self.ascii = ""
self.outfile.write(data)
def mark(self):
pass
def flush(self):
for i in range(self.pos, 15):
sys.stdout.write(" ")
sys.stdout.write("%s\n" % self.ascii)
self.pos = 0
self.ascii = ""
if self.outfile:
self.outfile.flush()
def generate(decl):
if decl.__class__ == labcomm.sample:
result = []
for values in generate(decl.decl):
result.append((decl, values))
return result
elif decl.__class__ == labcomm.struct:
result = []
if len(decl.field) == 0:
result.append({})
else:
values1 = generate(decl.field[0][1])
values2 = generate(labcomm.struct(decl.field[1:]))
for v1 in values1:
for v2 in values2:
v = dict(v2)
v[decl.field[0][0]] = v1
result.append(v)
return result
elif decl.__class__ == labcomm.array:
if len(decl.indices) == 1:
values = generate(decl.decl)
if decl.indices[0] == 0:
lengths = [0, 1, 2]
else:
lengths = [ decl.indices[0] ]
else:
values = generate(labcomm.array(decl.indices[1:],decl.decl))
if decl.indices[0] == 0:
lengths = [1, 2]
else:
lengths = [ decl.indices[0] ]
result = []
for v in values:
for i in lengths:
element = []
for j in range(i):
element.append(v)
result.append(element)
return result
elif decl.__class__ == labcomm.BOOLEAN:
return [False, True]
elif decl.__class__ == labcomm.BYTE:
return [-128, 0, 127]
elif decl.__class__ == labcomm.SHORT:
return [-32768, 0, 32767]
elif decl.__class__ == labcomm.INTEGER:
return [-2147483648, 0, 2147483647]
elif decl.__class__ == labcomm.LONG:
return [-9223372036854775808, 0, 9223372036854775807]
elif decl.__class__ == labcomm.FLOAT:
return [-math.pi, 0.0, math.pi]
elif decl.__class__ == labcomm.DOUBLE:
return [-math.pi, 0.0, math.pi]
elif decl.__class__ == labcomm.STRING:
return ['string',
'teckenstrng'.decode("iso8859-15")]
print decl
raise Exception("unhandled decl %s" % decl.__class__)
if __name__ == "__main__":
print os.getcwd(), sys.argv
if not os.path.exists("gen"):
os.makedirs("gen")
if len(sys.argv) > 1:
files = [s[0:-3] for s in sys.argv[1:] if s.endswith('.lc')]
else:
files = [s[0:-3] for s in os.listdir(".") if s.endswith('.lc')]
for f in files:
run_labcomm(f)
for f in files:
cscode.test_program("gen/csharp/test_%s.cs" % f,
"gen/%s.typeinfo" % f)
sys.path.insert(0, "../lib/python")
import labcomm;
sys.path.insert(0, "gen/python")
for f in files:
print f
h = hexwriter("gen/%s.vec" % f)
encoder = labcomm.Encoder(h)
signatures = []
i = __import__(f)
for k in dir(i):
v = getattr(i, k)
try:
s = v.signature
except:
s = None
if s:
signatures.append(s)
encoder.add_decl(s)
for s in signatures:
for e in generate(s):
encoder.encode(e[1], s)
h.flush()
ccode.gen(f, signatures)