Spaces:
Running
Running
File size: 2,099 Bytes
3f02d46 |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
'''
ARTHIMETIC OPERATORS
This file contains Python classes that define the arithmetic operators for program synthesis.
'''
'''
CLASS DEFINITIONS
'''
class IntegerValue:
'''
Class to represent an arithmetic value.
'''
def __init__(self, value):
self.value = value
self.type = int
class Add:
'''
Operator to add two numerical values.
'''
def __init__(self):
self.arity = 2 # number of arguments of function
self.weight = 1 # weight of function
self.return_type = int # return type of function
def __call__(self, x, y):
return x + y
def str(x, y):
return f"{x} + {y}"
class Subtract:
'''
Operator to subtract two numerical values.
'''
def __init__(self):
self.arity = 2 # number of arguments of function
self.weight = 1 # weight of function
self.return_type = int # return type of function
def __call__(self, x, y):
return x - y
def str(x, y):
return f"{x} - {y}"
class Multiply:
'''
Operator to multiply two numerical values.
'''
def __init__(self):
self.arity = 2 # number of arguments of function
self.weight = 1 # weight of function
self.return_type = int # return type of function
def __call__(self, x, y):
return x * y
def str(x, y):
return f"{x} * {y}"
class Divide:
'''
Operator to divide two numerical values.
'''
def __init__(self):
self.arity = 2 # number of arguments of function
self.weight = 1 # weight of function
self.return_type = int # return type of function
def __call__(self, x, y):
try: # check for division by zero error
return x / y
except ZeroDivisionError:
return None
def str(x, y):
return f"{x} / {y}"
'''
FUNCTION DEFINITIONS
'''
'''
GLOBAL CONSTANTS
'''
# define operators
arithmetic_operators = [Add(), Subtract(), Multiply(), Divide()] |