-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.py
135 lines (125 loc) · 3.73 KB
/
part1.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
content = [] # stores the body of macro excluding the comments
name = [] # stores the name of macro so as to identify at the time of invocation
matrix = [] # stores arguments
fo = open("test1.txt","r") # open test file
lines = fo.readlines()
obj = open("result1.txt","w") # open result file
n = len(lines)
i = 0
def defineMacro(): # function to store the body, name, arguments and type of invocation of macro
global i
line = lines[i] # function is called when #DEF is encountered
line = line.strip("\n")
line = line.strip("\t")
line = line.strip(" ")
line = line.strip("#DEF")
line = line.strip(" ")
nameString = line
name.append(line)
i = i+1
list1 =[] # stores parameters in particular order
index1 = name.index(nameString)
while i<n and "&" in lines[i] : # loop to store parameters
temp = lines[i]
temp = temp.strip("\t")
temp = temp.strip("\n")
temp = temp.strip(" ")
list1.append(temp)
i = i+1
matrix.insert(index1,list1) # storing list1 in matrix
string = ""
content.insert(index1,string)
while i<n and "END" not in lines[i]: # loop to store content
temp = lines[i]
if "#DEF" in temp: # recursive call on finding a nested macro
defineMacro()
temp = temp.strip("\t")
temp = temp.strip("\n")
temp = temp.strip(" ")
temp = temp.strip("#DEF")
temp = temp.strip(" ")
index2 = name.index(temp)
string = string + content[index2]
temp = ""
elif "//" in temp: # removing contents from the macro body
x = temp.index("//")
temp = temp[0:x] + "\n"
string = string + temp
i = i+1
content[index1] = string # finally updating the content
def expandMacro(): #Function to expand macro on invocation
line = lines[i]
line = line.strip("\t")
line = line.strip("\n")
line = line.strip(" ")
list1 = line.split(" ")
index1 = name.index(list1[0])
parameters = matrix[index1] # geting parameters in one list
string = content[index1] # getting content in a string
x = len(list1[0])
line = line[x:len(line)] #getting arguments passed in a list
line = line.strip(" ")
arguments = line.split(",")
for k in range(0,len(arguments)):
arguments[k] = arguments[k].strip(" ")
res = ""
if len(parameters)>0 and "=" in parameters[0]: # this runs in case keyword invocation of macro
key = []
value = []
for k in range(0,len(parameters)):
temp = parameters[k].split("=")
for l in range(0,len(temp)):
temp[l] = temp[l].strip(" ")
key.append(temp[0])
value.append(temp[1])
for k in range(0,len(arguments)):
temp = arguments[k].split("=")
for l in range(0,len(temp)):
temp[l] = temp[l].strip(" ")
index3 = key.index(temp[0])
value[index3] = temp[1]
j = 0
while j<len(string):
if string[j] == "&":
word = ""
while j<len(string) and string[j]!=" " and string[j]!="\n" and string[j]!="\t":
word = word + string[j]
j = j+1
j = j-1
index2 = key.index(word)
res = res + value[index2]
else:
res = res + string[j]
j = j+1
obj.write(res)
else: # this runs in case of positional invocation
j = 0
while j<len(string): # loop to substitute parameters in the body of macro
if string[j] == "&":
word = ""
while j<len(string) and string[j]!=" " and string[j]!="\n" and string[j]!="\t":
word = word + string[j]
j = j+1
j = j-1
index2 = parameters.index(word)
res = res + arguments[index2]
else:
res = res + string[j]
j = j+1
obj.write(res)
while i<n: # going through the text file line by line
line = lines[i]
line = line.strip("\n")
line = line.strip("\t")
line = line.strip(" ")
if "#DEF" in line:
defineMacro()
else:
list1 = line.split(" ")
if list1[0] in name:
expandMacro()
else:
obj.write(lines[i])
i = i+1
obj.close()
fo.close()