117 lines
3.0 KiB
Python
117 lines
3.0 KiB
Python
"""Recursive-descent parser for calc expressions.
|
|
|
|
AST node shapes:
|
|
Num(value) — numeric literal; value is int or float
|
|
BinOp(op, left, right) — binary op; op is one of '+' '-' '*' '/'
|
|
Unary(op, operand) — unary op; op is '-'
|
|
|
|
All nodes implement __repr__ for easy shape inspection.
|
|
|
|
Grammar (lowest → highest precedence):
|
|
expr : term (('+' | '-') term)*
|
|
term : unary (('*' | '/') unary)*
|
|
unary : '-' unary | primary
|
|
primary : NUMBER | '(' expr ')'
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
from dataclasses import dataclass
|
|
from typing import Union
|
|
|
|
|
|
class ParseError(Exception):
|
|
pass
|
|
|
|
|
|
@dataclass
|
|
class Num:
|
|
value: Union[int, float]
|
|
|
|
def __repr__(self) -> str:
|
|
return f"Num({self.value!r})"
|
|
|
|
|
|
@dataclass
|
|
class BinOp:
|
|
op: str
|
|
left: "Node"
|
|
right: "Node"
|
|
|
|
def __repr__(self) -> str:
|
|
return f"BinOp({self.op!r}, {self.left!r}, {self.right!r})"
|
|
|
|
|
|
@dataclass
|
|
class Unary:
|
|
op: str
|
|
operand: "Node"
|
|
|
|
def __repr__(self) -> str:
|
|
return f"Unary({self.op!r}, {self.operand!r})"
|
|
|
|
|
|
Node = Union[Num, BinOp, Unary]
|
|
|
|
|
|
def parse(tokens: list) -> Node:
|
|
"""Parse a token list into an AST. Raises ParseError on malformed input."""
|
|
p = _Parser(tokens)
|
|
tree = p.expr()
|
|
if p.current().kind != "EOF":
|
|
raise ParseError(f"unexpected token {p.current()!r} after expression")
|
|
return tree
|
|
|
|
|
|
class _Parser:
|
|
def __init__(self, tokens: list) -> None:
|
|
self._tokens = tokens
|
|
self._pos = 0
|
|
|
|
def current(self):
|
|
return self._tokens[self._pos]
|
|
|
|
def consume(self, kind: str = None):
|
|
tok = self._tokens[self._pos]
|
|
if kind is not None and tok.kind != kind:
|
|
raise ParseError(
|
|
f"expected {kind!r} but got {tok.kind!r} ({tok.value!r})"
|
|
)
|
|
self._pos += 1
|
|
return tok
|
|
|
|
def expr(self) -> Node:
|
|
node = self.term()
|
|
while self.current().kind in ("PLUS", "MINUS"):
|
|
op = self.consume().value
|
|
node = BinOp(op, node, self.term())
|
|
return node
|
|
|
|
def term(self) -> Node:
|
|
node = self.unary()
|
|
while self.current().kind in ("STAR", "SLASH"):
|
|
op = self.consume().value
|
|
node = BinOp(op, node, self.unary())
|
|
return node
|
|
|
|
def unary(self) -> Node:
|
|
if self.current().kind == "MINUS":
|
|
self.consume("MINUS")
|
|
return Unary("-", self.unary())
|
|
return self.primary()
|
|
|
|
def primary(self) -> Node:
|
|
tok = self.current()
|
|
if tok.kind == "NUMBER":
|
|
self.consume("NUMBER")
|
|
return Num(tok.value)
|
|
if tok.kind == "LPAREN":
|
|
self.consume("LPAREN")
|
|
node = self.expr()
|
|
if self.current().kind != "RPAREN":
|
|
raise ParseError("unclosed '(' — expected ')'")
|
|
self.consume("RPAREN")
|
|
return node
|
|
if tok.kind == "EOF":
|
|
raise ParseError("unexpected end of input")
|
|
raise ParseError(f"unexpected token {tok.kind!r} ({tok.value!r})")
|