Change "instances" class variables of Smybols and children of Symbols to Dict instead of List. Start expand.py (not completed)

This commit is contained in:
Clément Barthélemy 2024-02-20 01:33:03 +01:00
parent 77c8973eb7
commit 1738fa5d4c
6 changed files with 41 additions and 27 deletions

View file

@ -7,18 +7,22 @@ class Operator(Symbols):
"""
Represent an operator, like +, *, sin, anything that can be applied to an expression
"""
instances = []
instances = {}
def __init__(self, name: str, precedence: int, call: Callable):
def __init__(self, name: str, precedence: int, call: Callable, repeated_op: Operator = None):
"""
:param name of the operator
:param precedence: precedence of the operator, higher is better
:param call: function to apply the operator
:param repeated_op: if you repeat the operator what do you get ?
for exemple a+a+a+a -> 4*a, the repeated_op of Add is Mul
"""
super().__init__(name)
self.precedence = precedence
self.call = call
Operator.instances.append(self)
self.repeated_op = repeated_op
Operator.instances[name] = self
def __repr__(self):
return f'{self.name}'
@ -29,11 +33,11 @@ class UnaryOperator(Operator):
Represent a unary operator, like sin, cos, - etc...
all operators that take only one argument
"""
instances = []
instances = {}
def __init__(self, name: str, precedence: int, call: Callable):
UnaryOperator.instances.append(self)
super().__init__(name, precedence, call)
def __init__(self, name: str, precedence: int, call: Callable, repeated_op: Operator = None):
UnaryOperator.instances[name] = self
super().__init__(name, precedence, call, repeated_op)
def apply(self, expr):
return self.call(expr)
@ -77,11 +81,11 @@ class BinOperator(Operator):
"""
# Used to store all the instances of BinOperator, used in the parser
instances = []
instances = {}
def __init__(self, name: str, precedence: int, properties: BinProperties, call: Callable):
BinOperator.instances.append(self)
super().__init__(name, precedence, call)
def __init__(self, name: str, precedence: int, properties: BinProperties, call: Callable, repeated_op: Operator = None ):
BinOperator.instances[name] = self
super().__init__(name, precedence, call, repeated_op)
self.properties = properties
def apply(self, left, right):

View file

@ -1,16 +1,16 @@
from __future__ import annotations
class Symbols:
"""
All maths things (other than number) that will be parsed need to be of "Symbols" class
All maths things (other than integers) that will be parsed need to be of "Symbols" class
"""
instances = []
instances = {}
def __init__(self, name):
assert name not in Symbols.instances, f'Symbol with name {name} already exists'
self.name = name
Symbols.instances.append(self)
Symbols.instances[name] = self
def __repr__(self):
return self.name
@ -39,10 +39,10 @@ class Var(Symbols):
"""
variable, like 'x' in x+2
"""
instances = []
instances = {}
def __init__(self, name):
super().__init__(name)
self.__class__.instances.append(self)
self.__class__.instances[name] = self