Skip to content

Documentation for symbols.py

titan.common.symbols

Information

Bases: NamedTuple

Tuple to store information about the datatype and what operation is being performed.

Source code in titan/common/symbols.py
82
83
84
85
class Information(NamedTuple):
    """ Tuple to store information about the datatype and what operation is being performed."""
    datatype: DataType
    operation: Operation

LiteralSymbolGroup

Bases: set, Enum

Enum containing symbols corresponding to operations.

Source code in titan/common/symbols.py
76
77
78
79
80
class LiteralSymbolGroup(set, Enum):
    """ Enum containing symbols corresponding to operations."""
    ARITHMETIC = {"+", "-", "*", "/"}
    COMPARISON = {">=", ">", "<", "<=", "==", "!="}
    BITWISE = {"<<" , ">>"}

Operation

Bases: Enum

Enum containing possible operations, such as declaration or arithmetic.

Source code in titan/common/symbols.py
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
class Operation(Enum):
    """ Enum containing possible operations, such as declaration or arithmetic."""
    # vars
    VARIABLE_DECLARATION = auto()
    CONSTANT_DECLARATION = auto()
    GLOBAL_VAR_DECLARATION = auto()
    GLOBAL_CONST_DECLARATION = auto()

    # funcs
    FUNCTION_DECLARATION = auto()
    FUNCTION_IN_VAR_PARAM = auto()
    FUNCTION_OUT_VAR_PARAM = auto()

    # operations
    ASSIGNMENT = auto()
    STORE = auto()
    LOAD = auto()
    ADD = "+"
    SUB = "-"
    MULT = "*"
    DIV = "/"

    # comparisons
    DECISION = auto()
    LESS_THAN = "<"
    LESS_OR_EQ = "<="
    GREATER_THAN = ">"
    GREATER_OR_EQ = ">="
    EQUAL_TO = "=="
    NOT_EQUAL_TO = "!="

    # logical
    SHIFT_LEFT = "<<"
    SHIFT_RIGHT = ">>"

    # misc
    NOP = auto()

    # array ops
    ARRAY_INDEX = auto()
    ARRAY_LOAD = auto()
    ARRAY_STORE = auto()

Operation_Type

Bases: set, Enum

Enum containing sets of titan.common.symbols.Operation, bundled into common groups.

Note

These groups are:

  • ARITHMETIC
  • GENERIC_CONSTANT_DECLARATION
  • GENERIC_VARAIBLE_DECLARATION
  • COMPARISON
  • BITWISE
Source code in titan/common/symbols.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Operation_Type(set, Enum):
    """ Enum containing sets of ``titan.common.symbols.Operation``, bundled into common groups.

        Note:
            These groups are:

            - ``ARITHMETIC``
            - ``GENERIC_CONSTANT_DECLARATION``
            - ``GENERIC_VARAIBLE_DECLARATION``
            - ``COMPARISON``
            - ``BITWISE``
    """
    ARITHMETIC = {Operation.ADD, Operation.SUB, Operation.MULT, Operation.DIV}
    GENERIC_CONSTANT_DECLARATION = {Operation.CONSTANT_DECLARATION, Operation.GLOBAL_CONST_DECLARATION}
    GENERIC_VARIABLE_DECLARATION = {Operation.VARIABLE_DECLARATION, Operation.GLOBAL_VAR_DECLARATION}
    COMPARISON = {Operation.LESS_THAN, Operation.LESS_OR_EQ, 
                  Operation.GREATER_THAN, Operation.GREATER_OR_EQ, 
                  Operation.EQUAL_TO, Operation.NOT_EQUAL_TO}
    BITWISE = {Operation.SHIFT_LEFT, Operation.SHIFT_RIGHT}
    ARRAY_OPERATIONS = {Operation.ARRAY_INDEX, Operation.ARRAY_LOAD, Operation.ARRAY_STORE}

SymbolTable

Simple symbol table class.

Attributes:

Name Type Description
content

Dictionary to store the symbols.

Source code in titan/common/symbols.py
 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class SymbolTable():
    """ Simple symbol table class.

        Attributes:
            content: Dictionary to store the symbols.
    """
    # TODO: come up with a better solution -- using a dict for unique
    # variable names will not work when the scope changes
    # OR ---- new symbol table every scope change?
    content = {}

    def __init__(self):
        pass

    # add entry
    def add(self, expression, information: Information):
        """ Add an entry to the symbol table.

            Args:
                expression: Symbol to add.
                information: Store information about the operation.
        """
        self.content.update({expression: information})

    # delete entry
    def delete(self, expression):
        """ Delete an entry from the symbol table.

            Args:
                expression: Symbol to delete.
        """
        del self.content[expression]

    # return using key
    def get(self, expression) -> Information:
        """ Get information about the symbol.

            Args:
                expression: Symbol to query.

            Returns:
                Information about the symbol.
        """
        return self.content.get(expression)

    # if exists bool
    def exists(self, expression) -> bool:
        """ Check if a symbol exists.

            Args:
                expression: Symbol to check.

            Returns:
                True if symbol exists, else False.
        """
        return True if expression in self.content else False

delete(expression)

Delete an entry from the symbol table.

Parameters:

Name Type Description Default
expression

Symbol to delete.

required
Source code in titan/common/symbols.py
115
116
117
118
119
120
121
def delete(self, expression):
    """ Delete an entry from the symbol table.

        Args:
            expression: Symbol to delete.
    """
    del self.content[expression]

exists(expression)

Check if a symbol exists.

Parameters:

Name Type Description Default
expression

Symbol to check.

required

Returns:

Type Description
bool

True if symbol exists, else False.

Source code in titan/common/symbols.py
136
137
138
139
140
141
142
143
144
145
def exists(self, expression) -> bool:
    """ Check if a symbol exists.

        Args:
            expression: Symbol to check.

        Returns:
            True if symbol exists, else False.
    """
    return True if expression in self.content else False

get(expression)

Get information about the symbol.

Parameters:

Name Type Description Default
expression

Symbol to query.

required

Returns:

Type Description
Information

Information about the symbol.

Source code in titan/common/symbols.py
124
125
126
127
128
129
130
131
132
133
def get(self, expression) -> Information:
    """ Get information about the symbol.

        Args:
            expression: Symbol to query.

        Returns:
            Information about the symbol.
    """
    return self.content.get(expression)

Last update: 2023-12-24