This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbitops_parse.py
134 lines (108 loc) · 3.93 KB
/
tbitops_parse.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#
# tbitops_parse.py - Turns text-written bitwise operations, of my
# standard, into simple separate statements and
# pieces
#
# Written By: SockNastre
#
def is_variable(c, cBefore, cAfter):
"""
Checks if character is a variable based on what is after and before it
c: character to check
cBefore: character before character to check
cAfter: character after character to check
returns: if the character is a variable or not
returnType: boolean
"""
# variables have space before and after, sometimes paranthesis can be
# in that place instead of a space
if (cBefore != ' ' and cBefore != '('):
return False
elif (cAfter != ' ' and cAfter != ')'):
return False
else:
return True
def grab_variables(tBitOps):
"""
Gets variables from string based on standard for algorithm
tBitOps: textual bitwise-operations to get variables from
returns: array of grabbed variables as strings
returnType: string[]
"""
variableArray = [] # this will be returned in the end
tBitOpsLength = len(tBitOps) # used to loop through indicies later
for i in range(tBitOpsLength):
# cBefore and cAfter will be the characters before and after the
# character at the current index
cBefore = ' '
c = tBitOps[i]
cAfter = ' '
# setting up cBefore and cAfter
if i != 0:
cBefore = tBitOps[i - 1]
if i + 1 != tBitOpsLength:
cAfter = tBitOps[i + 1]
isCVariable = is_variable(c, cBefore, cAfter)
if isCVariable:
variableArray.append(c)
return variableArray
def is_operation(c, cBefore, cAfter):
"""
Checks if character is part of an operation based on what is after and
before it
c: character to check
cBefore: character before character to check
cAfter: character after character to check
returns: if the character is part of an operation or not
returnType: boolean
"""
# variables that ensure character is part of operation
isCVariable = is_variable(c, cBefore, cAfter)
isCParenthesis = c == '(' or c == ')'
isCBeforeValid = cBefore == ' ' or cBefore == '('
# operations have a space/parenthesis before them, the character itself
# cannot be equal to a left-hand or right-hand parenthesis
if (not isCVariable and not isCParenthesis and isCBeforeValid):
return True
else:
return False
def grab_operations(tBitOps):
"""
Gets operations from string based on standard for algorithm
tBitOps: textual bitwise-operations to get operations from
returns: array of grabbed operations as strings
returnType: string[]
"""
operationArray = [] # this will be returned in the end
tBitOpsLength = len(tBitOps) # used to loop through indicies later
for i in range(tBitOpsLength):
cBefore = ' '
c = tBitOps[i]
cAfter = ' '
# setting up cBefore and cAfter; the characters before and after the
# character at the current index
if i != 0:
cBefore = tBitOps[i - 1]
if i + 1 != tBitOpsLength:
cAfter = tBitOps[i + 1]
# if found true, entire operation gets grabbed
if (is_operation(c, cBefore, cAfter)):
operation = ""
while c != ' ':
operation += c
i += 1
c = tBitOps[i]
operationArray.append(operation)
return operationArray
# grabbing tBitOps and then getting variables and operations
tBitOps = input("\nEnter textual bitwise-operation(s): ")
variableArray = grab_variables(tBitOps)
operationArray = grab_operations(tBitOps)
# printing out individual variables
print("\nVariables:")
for variable in variableArray:
print(variable)
# printing out individual strings
print("\nOperations:")
for operation in operationArray:
print(operation)