relay_gen_c.py 3.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/python

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__':
    f = open(sys.argv[1])
    sample = []
    for l in map(lambda s: s.strip(), f):
19
        lang,kind,func,arg,stype = l[1:].split(l[0])
20
        if lang == 'C' and kind == 'sample':
21
            sample.append((func, arg, stype))
22
23
24
25
            pass
        pass
    result = []
    result.extend(split_match('^[^|]*\|(.*)$', """
26
      |// generated by relay_gen_c.py
27
28
29
30
      |#include <sys/types.h>
      |#include <sys/stat.h>
      |#include <fcntl.h>
      |#include <labcomm.h>
Anders Blomdell's avatar
Anders Blomdell committed
31
32
33
      |#include <labcomm_default_error_handler.h>
      |#include <labcomm_default_memory.h>
      |#include <labcomm_default_scheduler.h>
34
35
36
37
      |#include <labcomm_fd_reader.h>
      |#include <labcomm_fd_writer.h>
      |#include "c_code.h"
    """))
38
    for func,arg,stype in sample:
39
40
41
42
        result.extend(split_match('^[^|]*\|(.*)$', """
          |void handle_%(func)s(%(arg)s *v, void *context)
          |{
          |  struct labcomm_encoder *e = context;
43
44
45
46
47
48
49
50
          |  labcomm_encode_%(func)s(e%(valargstr)s);
          |}""" % { 'func': func, 'arg': arg, 'valargstr': '' if stype == "void" else', v' }))
#        result.extend(split_match('^[^|]*\|(.*)$', """
#          |void handle_%(func)s(%(arg)s *v, void *context)
#          |{
#          |  struct labcomm_encoder *e = context;
#          |  labcomm_encode_%(func)s(e, v);
#          |}""" % { 'func': func, 'arg': arg }))
51
52
53
54
55
56
57
58
59
60
61
62
        pass
    result.extend(split_match('^[^|]*\|(.*)$', """
      |int main(int argc, char *argv[]) {
      |  struct labcomm_encoder *e;
      |  struct labcomm_decoder *d;
      |  int in, out;
      |  
      |  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; }
63
64
      |  e = labcomm_encoder_new(labcomm_fd_writer_new(
      |                              labcomm_default_memory, out, 1), 
Anders Blomdell's avatar
Anders Blomdell committed
65
66
67
      |                           labcomm_default_error_handler,
      |                           labcomm_default_memory,
      |                           labcomm_default_scheduler);
68
      |  d = labcomm_decoder_new(labcomm_fd_reader_new(
Anders Blomdell's avatar
Anders Blomdell committed
69
70
71
72
      |                              labcomm_default_memory, in, 1), 
      |                           labcomm_default_error_handler,
      |                           labcomm_default_memory,
      |                           labcomm_default_scheduler);
73
    """))
74
    for func,arg,stype in sample:
75
76
77
78
79
80
81
82
83
84
85
        result.extend(split_match('^[^|]*\|(.*)$', """
          |  labcomm_encoder_register_%(func)s(e);
          |  labcomm_decoder_register_%(func)s(d, handle_%(func)s, e);
        """ % { 'func': func, 'arg': arg }))
    result.extend(split_match('^[^|]*\|(.*)$', """
      |  labcomm_decoder_run(d);
      |  return 0;
      |}
    """))
    print "\n".join(result)
    pass