artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
118
calculators/builder-solo/run-05/calc/parser.py
Normal file
118
calculators/builder-solo/run-05/calc/parser.py
Normal file
@ -0,0 +1,118 @@
|
||||
"""Recursive-descent parser for arithmetic expressions.
|
||||
|
||||
Grammar (precedence low→high):
|
||||
expr ::= term ( ('+' | '-') term )*
|
||||
term ::= factor ( ('*' | '/') factor )*
|
||||
factor ::= NUMBER | '(' expr ')' | '-' factor
|
||||
|
||||
AST node shapes (all dataclasses):
|
||||
Num(value) — numeric leaf; value is int or float
|
||||
BinOp(op, left, right) — op in ('+', '-', '*', '/'), children are nodes
|
||||
Unary(op, operand) — op is '-', operand is a node
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from calc.lexer import Token, tokenize
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Num:
|
||||
value: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"Num(value={self.value!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class BinOp:
|
||||
op: str
|
||||
left: Any
|
||||
right: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"BinOp(op={self.op!r}, left={self.left!r}, right={self.right!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Unary:
|
||||
op: str
|
||||
operand: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"Unary(op={self.op!r}, operand={self.operand!r})"
|
||||
|
||||
|
||||
class _Parser:
|
||||
def __init__(self, tokens: list):
|
||||
self._tokens = tokens
|
||||
self._pos = 0
|
||||
|
||||
def _peek(self) -> Token:
|
||||
return self._tokens[self._pos]
|
||||
|
||||
def _consume(self) -> Token:
|
||||
tok = self._tokens[self._pos]
|
||||
self._pos += 1
|
||||
return tok
|
||||
|
||||
def _expect(self, kind: str) -> Token:
|
||||
tok = self._peek()
|
||||
if tok.kind != kind:
|
||||
raise ParseError(f"Expected {kind} but got {tok.kind!r}")
|
||||
return self._consume()
|
||||
|
||||
def parse(self):
|
||||
node = self._expr()
|
||||
if self._peek().kind != 'EOF':
|
||||
raise ParseError(f"Unexpected token {self._peek()!r}")
|
||||
return node
|
||||
|
||||
def _expr(self):
|
||||
node = self._term()
|
||||
while self._peek().kind in ('PLUS', 'MINUS'):
|
||||
op = self._consume().value
|
||||
right = self._term()
|
||||
node = BinOp(op, node, right)
|
||||
return node
|
||||
|
||||
def _term(self):
|
||||
node = self._factor()
|
||||
while self._peek().kind in ('STAR', 'SLASH'):
|
||||
op = self._consume().value
|
||||
right = self._factor()
|
||||
node = BinOp(op, node, right)
|
||||
return node
|
||||
|
||||
def _factor(self):
|
||||
tok = self._peek()
|
||||
|
||||
if tok.kind == 'NUMBER':
|
||||
self._consume()
|
||||
return Num(tok.value)
|
||||
|
||||
if tok.kind == 'LPAREN':
|
||||
self._consume()
|
||||
node = self._expr()
|
||||
self._expect('RPAREN')
|
||||
return node
|
||||
|
||||
if tok.kind == 'MINUS':
|
||||
self._consume()
|
||||
operand = self._factor()
|
||||
return Unary('-', operand)
|
||||
|
||||
if tok.kind == 'EOF':
|
||||
raise ParseError("Unexpected end of input")
|
||||
|
||||
raise ParseError(f"Unexpected token {tok!r}")
|
||||
|
||||
|
||||
def parse(tokens: list):
|
||||
"""Parse a token list (from tokenize()) into an AST. Raises ParseError on bad input."""
|
||||
return _Parser(tokens).parse()
|
||||
Reference in New Issue
Block a user