Skip to content

Commit

Permalink
Base expressions on dataclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Jul 8, 2024
1 parent 47d633b commit eabd93d
Show file tree
Hide file tree
Showing 3 changed files with 336 additions and 391 deletions.
14 changes: 7 additions & 7 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ You can also easily define your own objects to use inside an expression:

.. doctest::

>>> from pymbolic.primitives import Expression
>>> class FancyOperator(Expression):
... def __init__(self, operand):
... self.operand = operand
...
... def __getinitargs__(self):
... return (self.operand,)
>>> from pymbolic.primitives import Expression, augment_expression_dataclass
>>> from dataclasses import dataclass
>>>
>>> @augment_expression_dataclass
... @dataclass(frozen=True)
... class FancyOperator(Expression):
... operand: Expression
...
... mapper_method = "map_fancy_operator"
...
Expand Down
5 changes: 4 additions & 1 deletion pymbolic/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from sys import intern
from typing import ClassVar, Dict, List, Tuple

from immutabledict import immutabledict

import pytools.lex
from pytools import memoize_method

Expand Down Expand Up @@ -333,7 +335,8 @@ def parse_postfix(self, pstate, min_precedence, left_exp):
args, kwargs = self.parse_arglist(pstate)

if kwargs:
left_exp = primitives.CallWithKwargs(left_exp, args, kwargs)
left_exp = primitives.CallWithKwargs(
left_exp, args, immutabledict(kwargs))
else:
left_exp = primitives.Call(left_exp, args)

Expand Down
Loading

0 comments on commit eabd93d

Please sign in to comment.