-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.py
More file actions
64 lines (59 loc) · 1.72 KB
/
operators.py
File metadata and controls
64 lines (59 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from collections.abc import Callable
from dataclasses import dataclass
import math
@dataclass
class Operator:
"""Defines an operator with its function, precedence, and associativity.
Attributes:
func: Binary function that takes two floats and returns float or None
precedence: Integer precedence level (higher indicates earlier compute)
Could be negative to indicate bitwise operations, but precedence
level should follow conventions.
associative: Whether the operator is associative.
"""
func: Callable[[float, float], float | None]
precedence: int
associative: bool
# Comment in/out operators you wish to use or add your own.
OPERATORS: dict[str, Operator] = {
"+": Operator(
func=lambda x, y: x + y,
precedence=1,
associative=True,
),
"-": Operator(
func=lambda x, y: x - y,
precedence=1,
associative=False,
),
"*": Operator(
func=lambda x, y: x * y,
precedence=2,
associative=True,
),
"/": Operator(
func=lambda x, y: x / y if y != 0 else None,
precedence=2,
associative=False,
),
# "**": Operator(
# func=lambda x, y: None
# if abs(y) > 1e2
# or abs(x) > 1e3
# or (x == 0 and y < 0)
# or isinstance((temp := x**y), complex)
# else temp,
# precedence=5,
# associative=False,
# ),
# "logbase": Operator(
# func=lambda x, y: None if x <= 0 or y <= 1 else math.log(x, y),
# precedence=4,
# associative=False,
# ),
# "%": Operator(
# func=lambda x, y: x % y if y != 0 else None,
# precedence=2,
# associative=False,
# ),
}