source.assembler.assembler()   D
last analyzed

Complexity

Conditions 13

Size

Total Lines 42
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 35
nop 2
dl 0
loc 42
rs 4.2
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like source.assembler.assembler() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from source.__config__ import MRI
2
from source.__config__ import NON_MRI
3
from source.__config__ import PREUDOMSTRUCTION
4
5
6
def assembler(source: str, symbols_address: dict):
7
    lines = source.split('\n')
8
    # Location Counter
9
    LC = 0
10
    obj_dict = dict()
11
12
    for line in lines:
13
        strings = line.split()
14
        switch = len(strings)
15
        word = strings[0]
16
        if switch == 1:
17
            if word == PREUDOMSTRUCTION[3]:
18
                break
19
            else:
20
                obj_dict[LC] = hex(NON_MRI[word])
21
        elif switch == 2:
22
            if word == PREUDOMSTRUCTION[0]:
23
                LC = int(strings[1]) - 1
24
            else:
25
                if word in MRI:
26
                    obj_dict[LC] = _assemble(strings, symbols_address, 0, 0)
27
                elif word in PREUDOMSTRUCTION:
28
                    obj_dict[LC] = num_converter(word, strings[1])
29
        elif switch == 3:
30
            state = True
31
            if word[-1] == ',':
32
                a = 1
33
                b = 0
34
                if strings[1] in PREUDOMSTRUCTION[1:3]:
35
                    state = False
36
            else:
37
                a = 0
38
                b = 1
39
            if state:
40
                obj_dict[LC] = _assemble(strings, symbols_address, a, b)
41
            else:
42
                obj_dict[LC] = num_converter(word, strings[2])
43
        elif switch == 4:
44
            obj_dict[LC] = _assemble(strings, symbols_address, 1, 1)
45
        LC += 1
46
47
    return obj_dict
48
49
50
def _assemble(strings: list, symbols_address: dict, index1: int, index2: int):
51
    three_bit_part = symbols_address[strings[index1 + 1]]
52
    one_bit_part = MRI[strings[index1]][index2]
53
    assembled = one_bit_part + three_bit_part
54
    return hex(int(assembled, 16))
55
56
57
def num_converter(word: str, number: str) -> str:
58
    """ Converts string number to it's appreciate hexadecimal integer
59
60
    :param word: HEX or DEC for determining with conversion method to use"
61
    :param number: string number to be converted to integer
62
    :return  hexadecimal string representation of input number"""
63
    if word == PREUDOMSTRUCTION[1]:  # word == HEX
64
        return hex(int(number, 16))
65
    else:                            # word == DEC
66
        return hex(int(number))
67