Init commit

This commit is contained in:
Fsan 2023-04-01 21:11:26 -03:00
commit 962988df7a
1 changed files with 46 additions and 0 deletions

46
main.py Normal file
View File

@ -0,0 +1,46 @@
def consume(i):
return bool(i) and (i[0], i[1:])
def op(i):
return bool(i) and i[0] in '|&>' and (i[0], i[1:])
def atom(i):
return bool(i) and i[0].isalpha() and (i[0], i[1:])
def left_b(i):
return bool(i) and i[0] in '(' and (i[0], i[1:])
def right_b(i):
return bool(i) and i[0] in ')' and (i[0], i[1:])
def either(i, a, b):
return bool(i) and (a(i) or b(i))
def seq(*parsers):
def apply(i):
res = []
for p in parsers:
if not (n := p(i)):
print(i)
print(p)
return False
val, i = n
res.append(val)
return (res, i)
return apply
def term(i):
return either(i, atom, closed_exp)
def closed_exp(i):
return seq(left_b, term, op, term, right_b)(i)
def exp(i):
return seq(term, op, term)(i)
text = "a|(b&c)"
if result := exp(text):
val, left = result
print(val)
print(left)