Skip to content

Commit

Permalink
Added the possibility to parse expressions like \f.(\x.x a) correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
theRealProHacker committed Apr 16, 2024
1 parent 09aee8b commit b73031b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@
but also does alpha-reduction which the Haskell-version cannot provide
"""

import re
from itertools import chain
from string import ascii_letters, whitespace
from dataclasses import dataclass

Char = str
letters = set(ascii_letters)
whitespace_pattern = re.compile(r"^\s+")

class Expression:
def all_vars(self)->set[Char]:
Expand Down Expand Up @@ -264,7 +266,15 @@
raise ParseError("Found a special character in the head of a function")
rest = ""
if " " in body:
body, rest = body.split(None, 1)
count = 0
for i,c in enumerate(body):
if c == ")":
count -= 1
elif c == "(":
count += 1
elif whitespace_pattern.match(body[i:]) and not count:
body, rest = body[:i], body[i:].lstrip()
break
result.append(Func(head, parse_expr(body)))
elif first == "(":
closing = find_closing_paren(rest)
Expand Down
12 changes: 11 additions & 1 deletion pylambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
but also does alpha-reduction which the Haskell-version cannot provide
"""

import re
from itertools import chain
from string import ascii_letters, whitespace
from dataclasses import dataclass

Char = str
letters = set(ascii_letters)
whitespace_pattern = re.compile(r"^\s+")

class Expression:
def all_vars(self)->set[Char]:
Expand Down Expand Up @@ -177,7 +179,15 @@ def _parse_expr(rest: str) -> list[Expression]:
raise ParseError("Found a special character in the head of a function")
rest = ""
if " " in body:
body, rest = body.split(None, 1)
count = 0
for i,c in enumerate(body):
if c == ")":
count -= 1
elif c == "(":
count += 1
elif whitespace_pattern.match(body[i:]) and not count:
body, rest = body[:i], body[i:].lstrip()
break
result.append(Func(head, parse_expr(body)))
elif first == "(":
closing = find_closing_paren(rest)
Expand Down

0 comments on commit b73031b

Please sign in to comment.