summaryrefslogtreecommitdiff
path: root/codegen.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2019-05-17 09:36:19 +0300
committerLars Wirzenius <liw@liw.fi>2019-05-17 09:36:19 +0300
commit5893afdacd1b8f37c5d8aee8ade71064747dda27 (patch)
tree4d69a4f3392315c59d43960d74b67cf2e619f410 /codegen.py
parent99e0c83f98f56e26725ac86bcf1569e5ba012178 (diff)
downloadsaga-poc-5893afdacd1b8f37c5d8aee8ade71064747dda27.tar.gz
Add: example proof-of-concept code generation
Diffstat (limited to 'codegen.py')
-rw-r--r--codegen.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/codegen.py b/codegen.py
new file mode 100644
index 0000000..6e8aac2
--- /dev/null
+++ b/codegen.py
@@ -0,0 +1,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)