Skip to content

Commit

Permalink
Add parentheses to the felis.parser example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeeeT committed Dec 1, 2024
1 parent 894299b commit d9a2d0e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,28 @@ print(pythags)
Parsing (and evaluating) an arithmetic expression with `felis.parser`:

```python
from felis import option
from felis.option import Some
from felis.parser import *

literal = map(int)(map("".join)(some(digit)))
factor = lambda string: bracket(text("("))(text(")"))(expression)(string)
term_priority_1 = add(literal)(factor)

multiplication = take_after(character("*"))(identity(lambda a: lambda b: a * b))
division = take_after(character("/"))(identity(lambda a: lambda b: a / b))
term_priority_1 = chain_left_1(add(division)(multiplication))(literal)
term_priority_2 = chain_left_1(add(division)(multiplication))(term_priority_1)

addition = take_after(character("+"))(identity(lambda a: lambda b: a + b))
subtraction = take_after(character("-"))(identity(lambda a: lambda b: a - b))
term_priority_2 = chain_left_1(add(subtraction)(addition))(term_priority_1)
term_priority_3 = chain_left_1(add(subtraction)(addition))(term_priority_2)

expression = term_priority_2
expression = term_priority_3

while string := input("> "):
match run(expression)(string):
case None:
print("Syntax error")
case option.Some(result):
case Some(result):
print("Result:", result)
```

Expand Down

0 comments on commit d9a2d0e

Please sign in to comment.