artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
104
calculators/builder-adversary/run-05/calc/parser.py
Normal file
104
calculators/builder-adversary/run-05/calc/parser.py
Normal file
@ -0,0 +1,104 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Union
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Num:
|
||||
value: Union[int, float]
|
||||
|
||||
def __repr__(self):
|
||||
return f"Num({self.value})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class BinOp:
|
||||
op: str
|
||||
left: object
|
||||
right: object
|
||||
|
||||
def __repr__(self):
|
||||
return f"BinOp({self.op}, {self.left!r}, {self.right!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Unary:
|
||||
op: str
|
||||
operand: object
|
||||
|
||||
def __repr__(self):
|
||||
return f"Unary({self.op}, {self.operand!r})"
|
||||
|
||||
|
||||
class _Parser:
|
||||
def __init__(self, tokens):
|
||||
self.tokens = tokens
|
||||
self.pos = 0
|
||||
|
||||
def _peek(self):
|
||||
return self.tokens[self.pos]
|
||||
|
||||
def _consume(self, kind=None):
|
||||
tok = self.tokens[self.pos]
|
||||
if kind is not None and tok.kind != kind:
|
||||
raise ParseError(f"expected {kind!r}, got {tok.kind!r}")
|
||||
self.pos += 1
|
||||
return tok
|
||||
|
||||
def _expr(self):
|
||||
left = self._term()
|
||||
while self._peek().kind in ('PLUS', 'MINUS'):
|
||||
op = self._consume().kind
|
||||
right = self._term()
|
||||
left = BinOp(op, left, right)
|
||||
return left
|
||||
|
||||
def _term(self):
|
||||
left = self._unary()
|
||||
while self._peek().kind in ('STAR', 'SLASH'):
|
||||
op = self._consume().kind
|
||||
right = self._unary()
|
||||
left = BinOp(op, left, right)
|
||||
return left
|
||||
|
||||
def _unary(self):
|
||||
if self._peek().kind == 'MINUS':
|
||||
self._consume()
|
||||
return Unary('MINUS', self._unary())
|
||||
return self._primary()
|
||||
|
||||
def _primary(self):
|
||||
tok = self._peek()
|
||||
if tok.kind == 'NUMBER':
|
||||
self._consume()
|
||||
return Num(tok.value)
|
||||
if tok.kind == 'LPAREN':
|
||||
self._consume()
|
||||
node = self._expr()
|
||||
if self._peek().kind != 'RPAREN':
|
||||
raise ParseError(f"expected ')', got {self._peek().kind!r}")
|
||||
self._consume()
|
||||
return node
|
||||
raise ParseError(f"unexpected token {tok.kind!r}")
|
||||
|
||||
|
||||
def parse(tokens) -> object:
|
||||
"""Parse a token list into an AST.
|
||||
|
||||
Nodes:
|
||||
Num(value) — numeric literal (int or float)
|
||||
BinOp(op, left, right) — binary op; op in {'PLUS','MINUS','STAR','SLASH'}
|
||||
Unary(op, operand) — unary minus; op == 'MINUS'
|
||||
|
||||
Raises ParseError on malformed input.
|
||||
"""
|
||||
p = _Parser(tokens)
|
||||
if p._peek().kind == 'EOF':
|
||||
raise ParseError("empty input")
|
||||
node = p._expr()
|
||||
if p._peek().kind != 'EOF':
|
||||
raise ParseError(f"unexpected token {p._peek().kind!r} after expression")
|
||||
return node
|
||||
Reference in New Issue
Block a user