artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
109
calculators/builder-adversary-stateless/run-02/calc/parser.py
Normal file
109
calculators/builder-adversary-stateless/run-02/calc/parser.py
Normal file
@ -0,0 +1,109 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Num:
|
||||
value: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"Num({self.value!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class BinOp:
|
||||
op: str
|
||||
left: Any
|
||||
right: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"BinOp({self.op!r}, {self.left!r}, {self.right!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Unary:
|
||||
op: str
|
||||
operand: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"Unary({self.op!r}, {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 and tok.kind != kind:
|
||||
raise ParseError(f"expected {kind}, got {tok.kind!r} ({tok.value!r})")
|
||||
self._pos += 1
|
||||
return tok
|
||||
|
||||
def parse(self):
|
||||
node = self._expr()
|
||||
tok = self._peek()
|
||||
if tok.kind != 'EOF':
|
||||
raise ParseError(f"unexpected token {tok.kind!r} ({tok.value!r})")
|
||||
return node
|
||||
|
||||
def _expr(self):
|
||||
# expr → term (('+' | '-') term)*
|
||||
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):
|
||||
# term → unary (('*' | '/') unary)*
|
||||
node = self._unary()
|
||||
while self._peek().kind in ('STAR', 'SLASH'):
|
||||
op = self._consume().value
|
||||
right = self._unary()
|
||||
node = BinOp(op, node, right)
|
||||
return node
|
||||
|
||||
def _unary(self):
|
||||
# unary → '-' unary | primary
|
||||
if self._peek().kind == 'MINUS':
|
||||
op = self._consume().value
|
||||
operand = self._unary()
|
||||
return Unary(op, operand)
|
||||
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('LPAREN')
|
||||
node = self._expr()
|
||||
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})")
|
||||
|
||||
|
||||
def parse(tokens) -> Any:
|
||||
"""Parse a token list from calc.lexer.tokenize into an AST.
|
||||
|
||||
AST node types:
|
||||
Num(value) — numeric literal
|
||||
BinOp(op, left, right) — binary operation; op in {'+','-','*','/'}
|
||||
Unary(op, operand) — unary minus; op == '-'
|
||||
|
||||
Raises ParseError on malformed input.
|
||||
"""
|
||||
return _Parser(tokens).parse()
|
||||
Reference in New Issue
Block a user