summaryrefslogtreecommitdiff
path: root/codegen.py
blob: 6e8aac20968868289d5248e15223fcc6961cfc29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import json
import re
import sys
import yaml


def find_binding(binds, step):
    for bind in binds:
        m = re.match(bind['pattern'], step, re.I | re.M)
        if m is not None:
            return new_match(bind, m)
    return None


def new_match(bind, match):
    return {
        'function': bind['function'],
        'args': match.groupdict(),
    }


def codegen(f, match):
    f.write('args = {}\n'.format(json.dumps(match['args'])))
    f.write('{}(**args)\n'.format(match['function']))



with open('bindings.yaml') as f:
    binds = yaml.safe_load(f)


with open('prelude.py') as f:
    sys.stdout.write(f.read())


with open('scenario.txt') as scenario:
    for step in scenario:
        match = find_binding(binds, step)
        if match is not None:
            codegen(sys.stdout, match)