working i/o
This commit is contained in:
@@ -19,4 +19,4 @@ FIZZ_BUZZ = [
|
|||||||
~ '\0'
|
~ '\0'
|
||||||
]
|
]
|
||||||
|
|
||||||
':::' => FIZZ_BUZZ
|
':::' => FIZZ_BUZZ
|
||||||
|
|||||||
40
sub.py
40
sub.py
@@ -10,14 +10,15 @@ suite: prod*
|
|||||||
?prod: _named_prod | _direct_prod
|
?prod: _named_prod | _direct_prod
|
||||||
|
|
||||||
_named_prod : alias
|
_named_prod : alias
|
||||||
_direct_prod : _compound_prod | _rule_prod | _literal_prod | _abstract_prod
|
_direct_prod : _compound_prod | _rule_prod | _literal_prod | _io_prod
|
||||||
_abstract_prod : output
|
_io_prod : output | input
|
||||||
_compound_prod : cont | sing
|
_compound_prod : cont | sing
|
||||||
_rule_prod : full | part
|
_rule_prod : full | part
|
||||||
_literal_prod : lit | ref
|
_literal_prod : lit | ref
|
||||||
|
|
||||||
alias : name "=" prod // production alias
|
alias : name "=" prod // production alias
|
||||||
output: "~" prod // output production
|
output: "~" prod // output production
|
||||||
|
input: ":::" // input production
|
||||||
|
|
||||||
cont : "{" suite "}" // continual production
|
cont : "{" suite "}" // continual production
|
||||||
sing : "[" suite "]" // singular production
|
sing : "[" suite "]" // singular production
|
||||||
@@ -58,8 +59,8 @@ class Context:
|
|||||||
return diff
|
return diff
|
||||||
|
|
||||||
def expand(self, fmt):
|
def expand(self, fmt):
|
||||||
self.string = self.match.expand(fmt)
|
|
||||||
self.match = ALL_PAT.match(self.string)
|
self.match = ALL_PAT.match(self.string)
|
||||||
|
self.string = self.match.expand(fmt)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'Ctx[{self.string!r} {self.match[0]!r} {self.match.groups()}]'
|
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}]'
|
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:
|
class Suite:
|
||||||
def __init__(self, prods, ctx=None):
|
def __init__(self, prods, ctx=None):
|
||||||
if ctx is None:
|
if ctx is None:
|
||||||
@@ -191,6 +217,13 @@ class ProductionTransformer(Transformer):
|
|||||||
match = REGEX_PAT.match(reg)
|
match = REGEX_PAT.match(reg)
|
||||||
return re.compile(match[3])
|
return re.compile(match[3])
|
||||||
|
|
||||||
|
def input(self, prods):
|
||||||
|
return Input()
|
||||||
|
|
||||||
|
def output(self, prods):
|
||||||
|
prod, = prods
|
||||||
|
return Output(prod)
|
||||||
|
|
||||||
def full(self, prods):
|
def full(self, prods):
|
||||||
lhs, rhs = prods
|
lhs, rhs = prods
|
||||||
return Full(lhs, rhs)
|
return Full(lhs, rhs)
|
||||||
@@ -224,6 +257,7 @@ def main():
|
|||||||
tform = ProductionTransformer()
|
tform = ProductionTransformer()
|
||||||
pgm = tform.transform(tree)
|
pgm = tform.transform(tree)
|
||||||
print(pgm)
|
print(pgm)
|
||||||
|
print()
|
||||||
print(pgm.run())
|
print(pgm.run())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1,7 @@
|
|||||||
'hello there' => /(.*)\s+(.*)/ ::= '\g<0> \2 \1'
|
::: => {
|
||||||
|
~ "\g<0>"
|
||||||
|
/[aeiou]/ ::= '_'
|
||||||
|
/[^aeiou_\W]/ ::= ':'
|
||||||
|
|
||||||
|
/_:/ ::= [~ "a" ":"]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user