File size: 3,127 Bytes
3f02d46
 
 
 
 
 
 
 
 
a417ea3
3f02d46
a417ea3
 
3f02d46
a417ea3
 
 
 
 
 
3f02d46
a417ea3
c9abdca
 
 
 
 
 
a417ea3
 
 
 
 
 
 
3f02d46
c9abdca
 
 
 
 
 
3f02d46
 
 
 
 
a417ea3
 
 
 
3f02d46
c9abdca
3f02d46
 
c9abdca
 
3f02d46
 
 
 
 
 
a417ea3
 
 
 
3f02d46
c9abdca
3f02d46
 
c9abdca
 
3f02d46
 
 
 
 
 
a417ea3
 
 
 
3f02d46
c9abdca
3f02d46
 
c9abdca
 
3f02d46
 
 
 
 
 
a417ea3
 
 
 
3f02d46
c9abdca
3f02d46
 
 
 
 
c9abdca
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'''
ARTHIMETIC OPERATORS
This file contains Python classes that define the arithmetic operators for program synthesis.
'''

'''
CLASS DEFINITIONS
''' 

class IntegerVariable:
    '''
    Class to represent an integer variable. Note that position is the position of the variable in the input.
    For example, if the input is [4, 5, 6] and the variable is the third element (i.e., 6), then position = 2.
    '''
    def __init__(self, position):
        self.value = None           # value of the variable, initially None
        self.position = position    # position of the variable in the arguments to program
        self.type = int             # type of the variable

    def assign(self, value):
        self.value = value

    def evaluate(self):
        return self.value
    
    def str(self):
        return f"x{self.position}"

class IntegerConstant:
    '''
    Class to represent an integer constant.
    '''
    def __init__(self, value):
        self.value = value  # value of the constant
        self.type = int     # type of the constant

    def evaluate(self):
        return self.value
    
    def str(self):
        return str(self.value)

class Add:
    '''
    Operator to add two numerical values.
    '''
    def __init__(self):
        self.arity = 2                  # number of arguments
        self.arg_types = [int, int]     # argument types
        self.return_type = int          # return type
        self.weight = 1                 # weight

    def evaluate(self, x, y):
        return x + y
    
    def str(self, x, y):
        return f"({x} + {y})"

class Subtract:
    '''
    Operator to subtract two numerical values.
    '''
    def __init__(self):
        self.arity = 2                  # number of arguments
        self.arg_types = [int, int]     # argument types
        self.return_type = int          # return type
        self.weight = 1                 # weight

    def evaluate(self, x, y):
        return x - y
    
    def str(self, x, y):
        return f"({x} - {y})"
    
class Multiply:
    '''
    Operator to multiply two numerical values.
    '''
    def __init__(self):
        self.arity = 2                  # number of arguments
        self.arg_types = [int, int]     # argument types
        self.return_type = int          # return type
        self.weight = 1                 # weight

    def evaluate(self, x, y):
        return x * y
    
    def str(self, x, y):
        return f"({x} * {y})" 

class Divide:
    '''
    Operator to divide two numerical values.
    '''
    def __init__(self):
        self.arity = 2                  # number of arguments
        self.arg_types = [int, int]     # argument types
        self.return_type = int          # return type
        self.weight = 1                 # weight

    def evaluate(self, x, y):
        try: # check for division by zero error
            return x / y
        except ZeroDivisionError:
            return None
    
    def str(self, x, y):
        return f"({x} / {y})"


'''
GLOBAL CONSTANTS
''' 

# define operators
arithmetic_operators = [Add(), Subtract(), Multiply(), Divide()]