From 8fc9bb237a4c7eecf49c91894d657395552b8621 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 28 Oct 2018 02:44:39 -0400 Subject: [PATCH] package refactor --- main.py | 14 +++++ thue/__init__.py | 0 thue/parser.py | 100 ++++++++++++++++++++++++++++++++++++ sub.py => thue/prods.py | 111 ++-------------------------------------- 4 files changed, 117 insertions(+), 108 deletions(-) create mode 100644 main.py create mode 100644 thue/__init__.py create mode 100644 thue/parser.py rename sub.py => thue/prods.py (56%) diff --git a/main.py b/main.py new file mode 100644 index 0000000..1c4ca07 --- /dev/null +++ b/main.py @@ -0,0 +1,14 @@ +import thue.parser + + +def main(): + with open('testing.ret') as f: + src = f.read() + + pgm = thue.parser.compile(src) + res = pgm.run() + print(res) + + +if __name__ == '__main__': + main() diff --git a/thue/__init__.py b/thue/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/thue/parser.py b/thue/parser.py new file mode 100644 index 0000000..2f2b8d6 --- /dev/null +++ b/thue/parser.py @@ -0,0 +1,100 @@ +import re + +import lark +from lark import Lark + +import thue.prods + +grammar = r''' +program: suite + +suite: prod* + +?prod: _named_prod | _direct_prod + +_named_prod : alias +_direct_prod : _compound_prod | _rule_prod | _literal_prod | _io_prod +_io_prod : output | input +_compound_prod : cont | sing +_rule_prod : full | part +_literal_prod : lit | ref + +alias : name "=" prod // production alias +output: "~" prod // output production +input: ":::" // input production + +cont : "{" suite "}" // continual production +sing : "[" suite "]" // singular production + +full : _direct_prod "=>" prod // full application +part : ctx "::=" prod // partial application +ref : name // production reference + +lit : STRING // literal production +name : CNAME // named production + +ctx : REGEX + +CNAME : /[a-z_][a-z0-9_]*/i +STRING: /(r)?(['"])(.*?)(?" prod // full application -part : ctx "::=" prod // partial application -ref : name // production reference - -lit : STRING // literal production -name : CNAME // named production - -ctx : REGEX - -CNAME : /[a-z_][a-z0-9_]*/i -STRING: /(r)?(['"])(.*?)(?