-
Notifications
You must be signed in to change notification settings - Fork 0
/
decomposer.py
172 lines (127 loc) · 4.61 KB
/
decomposer.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
The contents of this file is responsible for translating the command
from human readible line into peaces which whould be able to execute
by the program
"""
from errors import EmptyLine, ArgumentNotExpected, NotEnoughArguments, \
TooManyArgumentsToUnpack
def lineCleanup(line : str, isText : bool = False) -> str | None:
"""
Given a line of text, this function check if line is not empty
and if so, does it contain anything else than a commment
If true, function returns the remaingn part, else None
line - line of document to cleanup
isText - this flag might indicate that we are dealing with muliline
text which happens to contain just en empty line
ex.
var word "Intro
outro$"
In this example line shouldn't be remove at the beginning
"""
# Empty line
if len(line) == 0: return None
nline = line.strip()
# Only white characters
if len(nline) == 0 and not isText: return None
# Only comment with white characters
elif nline.startswith(";"): return None
else:
for c in range(len(nline)):
if nline[c] == ';':
nline = nline[:c]
nline.strip()
break
return nline
def lineDecomposition(line : str) -> list:
"""
Decomposition of line into order and arguments
"""
# No possibility of line with sentence
if not ("'" in line or '"' in line):
splitted = []
if "," in line:
splitted = line.split(",")
# Cleaning up white cha
for e in range(len(splitted)):
splitted[e] = splitted[e].strip()
parts = []
for elem in splitted:
parts += elem.split(" ")
for p in range(len(parts)-1,-1,-1):
if parts[p] == "":
parts.pop(p)
return parts
# There are ' or " in this line which suggest string
else:
(lap, rap, lq, rq) = (len(line), len(line), 0, 0)
if "'" in line:
lap = line.index("'")
rap = line[-1::-1].index("'")
if '"' in line:
lq = line.index('"')
rq = line[-1::-1].index('"')
ul = min(lq, lap) # ultimate left start of string-like thing
ur = max(rq, rap) # ultimate right end of string-like thing
parts = line[:ul].split(" ") + [line[ul:ur+1]] + \
line[ur:].split(" ")
for p in range(len(parts)-1,-1,-1):
if parts[p] == "":
parts.pop(p)
return parts
def paramComparer(template : dict, values : list):
"""
This function is responsible to detect, if the given parameters
are in fact correctly put to the value - however, the function itself
doesn't automatically check, if the parametres are correct (if the value
is withing range, or if given parameter exist)
INC 10 -> WRONG
ADD AX,10 -> GOOD
ADD AX,-18.5 -> GOOD !!! (there are two params needed in ADD, 2 were passed)
"""
if len(values) == 1:
command = values[0].upper()
elif len(values) == 2:
command, arg1 = values
elif len(values) == 3:
command, arg1, arg2 = values
elif len(values) > 3:
command, arg1, arg2, *args = values
else:
raise EmptyLine
"""
The probem here, is that we can have ' ala dw "ala" ' , meanign assing
to variable - what to do then?
"""
def functionAssigner(template : dict, values : list) -> dict:
"""
If the given parammeters correspont to the signature of function
("visually" not gramatically), function would return a dictionary
which would allow to execute function with paramteres
EX:
ADD AX, 10
function = ADD | arg1 = AX | arg2 = 10
"""
defaultArgumentsNumber = len(template.keys())
givenArgumetnsNumber = len(values)
if defaultArgumentsNumber == 0 and givenArgumetnsNumber > 0:
raise ArgumentNotExpected
elif defaultArgumentsNumber > givenArgumetnsNumber:
raise NotEnoughArguments
elif defaultArgumentsNumber < givenArgumetnsNumber:
raise TooManyArgumentsToUnpack
alignment = {}
for k,i in zip(list(template.keys()),range(defaultArgumentsNumber)):
alignment[k] = values[i]
return alignment
def lineProcessing(line : str, tempalate : dict):
"""
This function is the main line processing function
it's purpose is to be an interface between engine, and
function, which does preporcess the line and checks
it's content and do work depending on what's inside
"""
try:
...
except EmptyLine:
...
...