working i/o

This commit is contained in:
2018-10-28 02:28:22 -04:00
parent 7ebcc1eb09
commit 1c46c2c839
3 changed files with 45 additions and 5 deletions

View File

@@ -19,4 +19,4 @@ FIZZ_BUZZ = [
~ '\0'
]
':::' => FIZZ_BUZZ
':::' => FIZZ_BUZZ

40
sub.py
View File

@@ -10,14 +10,15 @@ suite: prod*
?prod: _named_prod | _direct_prod
_named_prod : alias
_direct_prod : _compound_prod | _rule_prod | _literal_prod | _abstract_prod
_abstract_prod : output
_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
@@ -58,8 +59,8 @@ class Context:
return diff
def expand(self, fmt):
self.string = self.match.expand(fmt)
self.match = ALL_PAT.match(self.string)
self.string = self.match.expand(fmt)
def __str__(self):
return f'Ctx[{self.string!r} {self.match[0]!r} {self.match.groups()}]'
@@ -114,6 +115,31 @@ class Partial:
return f'Part[{self.reg} ::= {self.rhs}]'
class Input:
def apply(self, ctx):
ctx_ = ctx.enter()
ctx_.string = input()
ctx_.match = ALL_PAT.match(ctx_.string)
return ctx.exit(ctx_)
def __str__(self):
return 'Input'
class Output:
def __init__(self, prod):
self.prod = prod
def apply(self, ctx):
_ctx = ctx.enter()
self.prod.apply(_ctx)
print(_ctx.string)
return False
def __str__(self):
return f'Output[{self.prod}]'
class Suite:
def __init__(self, prods, ctx=None):
if ctx is None:
@@ -191,6 +217,13 @@ class ProductionTransformer(Transformer):
match = REGEX_PAT.match(reg)
return re.compile(match[3])
def input(self, prods):
return Input()
def output(self, prods):
prod, = prods
return Output(prod)
def full(self, prods):
lhs, rhs = prods
return Full(lhs, rhs)
@@ -224,6 +257,7 @@ def main():
tform = ProductionTransformer()
pgm = tform.transform(tree)
print(pgm)
print()
print(pgm.run())

View File

@@ -1 +1,7 @@
'hello there' => /(.*)\s+(.*)/ ::= '\g<0> \2 \1'
::: => {
~ "\g<0>"
/[aeiou]/ ::= '_'
/[^aeiou_\W]/ ::= ':'
/_:/ ::= [~ "a" ":"]
}