commit 962988df7a5bfc0c64adfcc0cd3525eed637d913 Author: Fsan Date: Sat Apr 1 21:11:26 2023 -0300 Init commit diff --git a/main.py b/main.py new file mode 100644 index 0000000..64ffded --- /dev/null +++ b/main.py @@ -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)