Skip to content

Commit aa2e740

Browse files
authored
Add files via upload
1 parent 9606948 commit aa2e740

17 files changed

+1392
-0
lines changed

macro(part1).odt

13.2 KB
Binary file not shown.

macro(part2).odt

13.9 KB
Binary file not shown.

macro.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name = []
2+
content = []
3+
matrix = []
4+
fo = open("test.txt","r")
5+
lines = fo.readlines()
6+
obj = open("result.txt","w")
7+
n = len(lines)
8+
i=0
9+
10+
11+
while i<n :
12+
line = lines[i]
13+
line = line.strip(" ")
14+
if "#DEF" in line:
15+
line = line.strip("#DEF")
16+
line = line.strip("\n")
17+
line = line.strip("\t")
18+
line = line.strip(" ")
19+
name.append(line)
20+
i=i+1
21+
list1 = []
22+
while i<n and "&" in lines[i]:
23+
temp = lines[i]
24+
temp = temp.strip("\t")
25+
temp=temp.strip("\n")
26+
temp=temp.strip(" ")
27+
list1.append(temp)
28+
i = i+1
29+
matrix.append(list1)
30+
string = "";
31+
while i<n and "END" not in lines[i]:
32+
temp = lines[i]
33+
temp = temp.strip(" ")
34+
if "//" in temp:
35+
x = temp.index("//")
36+
temp = temp[0:x] + "\n"
37+
string = string + temp
38+
i = i+1
39+
content.append(string)
40+
41+
else:
42+
line1 = line.strip("\n")
43+
line1 = line1.strip("\t")
44+
line1 = line1.strip(" ")
45+
list2 = line1.split(" ")
46+
47+
if list2[0] in name:
48+
x = len(list2[0])
49+
line1 = line1[x:(len(line1))]
50+
line1 = line1.strip(" ")
51+
list3 = line1.split(",")
52+
for j in range(0,len(list3)):
53+
list3[j] = list3[j].strip(" ")
54+
index1 = name.index(list2[0])
55+
stri = content[index1]
56+
57+
res = ""
58+
list4 = matrix[index1]
59+
print(list4)
60+
list5 = stri.split("\n")
61+
j=0
62+
while j<len(stri) :
63+
inde=0
64+
if stri[j] == "&":
65+
word = "&"
66+
j=j+1
67+
while j<len(stri) and stri[j]!="\n" and stri[j]!=" " and stri[j]!="\t":
68+
word = word + stri[j]
69+
j = j+1
70+
j = j-1
71+
if word in list4:
72+
inde = list4.index(word)
73+
res = res + list3[inde]
74+
print(list3[inde])
75+
else :
76+
res = res + stri[j]
77+
j =j + 1
78+
obj.write(res)
79+
print(res)
80+
else:
81+
obj.write(line)
82+
83+
i = i+1

part1.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
content = [] # stores the body of macro excluding the comments
2+
name = [] # stores the name of macro so as to identify at the time of invocation
3+
matrix = [] # stores arguments
4+
fo = open("test1.txt","r") # open test file
5+
lines = fo.readlines()
6+
obj = open("result1.txt","w") # open result file
7+
n = len(lines)
8+
i = 0
9+
def defineMacro(): # function to store the body, name, arguments and type of invocation of macro
10+
global i
11+
line = lines[i] # function is called when #DEF is encountered
12+
line = line.strip("\n")
13+
line = line.strip("\t")
14+
line = line.strip(" ")
15+
line = line.strip("#DEF")
16+
line = line.strip(" ")
17+
nameString = line
18+
name.append(line)
19+
i = i+1
20+
list1 =[] # stores parameters in particular order
21+
index1 = name.index(nameString)
22+
while i<n and "&" in lines[i] : # loop to store parameters
23+
temp = lines[i]
24+
temp = temp.strip("\t")
25+
temp = temp.strip("\n")
26+
temp = temp.strip(" ")
27+
list1.append(temp)
28+
i = i+1
29+
30+
matrix.insert(index1,list1) # storing list1 in matrix
31+
string = ""
32+
content.insert(index1,string)
33+
while i<n and "END" not in lines[i]: # loop to store content
34+
temp = lines[i]
35+
if "#DEF" in temp: # recursive call on finding a nested macro
36+
defineMacro()
37+
temp = temp.strip("\t")
38+
temp = temp.strip("\n")
39+
temp = temp.strip(" ")
40+
temp = temp.strip("#DEF")
41+
temp = temp.strip(" ")
42+
index2 = name.index(temp)
43+
string = string + content[index2]
44+
temp = ""
45+
elif "//" in temp: # removing contents from the macro body
46+
x = temp.index("//")
47+
temp = temp[0:x] + "\n"
48+
string = string + temp
49+
i = i+1
50+
content[index1] = string # finally updating the content
51+
52+
53+
def expandMacro(): #Function to expand macro on invocation
54+
line = lines[i]
55+
line = line.strip("\t")
56+
line = line.strip("\n")
57+
line = line.strip(" ")
58+
59+
list1 = line.split(" ")
60+
index1 = name.index(list1[0])
61+
parameters = matrix[index1] # geting parameters in one list
62+
string = content[index1] # getting content in a string
63+
x = len(list1[0])
64+
line = line[x:len(line)] #getting arguments passed in a list
65+
line = line.strip(" ")
66+
arguments = line.split(",")
67+
for k in range(0,len(arguments)):
68+
arguments[k] = arguments[k].strip(" ")
69+
res = ""
70+
if len(parameters)>0 and "=" in parameters[0]: # this runs in case keyword invocation of macro
71+
key = []
72+
value = []
73+
for k in range(0,len(parameters)):
74+
temp = parameters[k].split("=")
75+
for l in range(0,len(temp)):
76+
temp[l] = temp[l].strip(" ")
77+
key.append(temp[0])
78+
value.append(temp[1])
79+
for k in range(0,len(arguments)):
80+
temp = arguments[k].split("=")
81+
for l in range(0,len(temp)):
82+
temp[l] = temp[l].strip(" ")
83+
index3 = key.index(temp[0])
84+
value[index3] = temp[1]
85+
j = 0
86+
while j<len(string):
87+
if string[j] == "&":
88+
word = ""
89+
while j<len(string) and string[j]!=" " and string[j]!="\n" and string[j]!="\t":
90+
word = word + string[j]
91+
j = j+1
92+
j = j-1
93+
index2 = key.index(word)
94+
res = res + value[index2]
95+
else:
96+
res = res + string[j]
97+
j = j+1
98+
obj.write(res)
99+
else: # this runs in case of positional invocation
100+
j = 0
101+
while j<len(string): # loop to substitute parameters in the body of macro
102+
if string[j] == "&":
103+
word = ""
104+
while j<len(string) and string[j]!=" " and string[j]!="\n" and string[j]!="\t":
105+
word = word + string[j]
106+
j = j+1
107+
j = j-1
108+
index2 = parameters.index(word)
109+
res = res + arguments[index2]
110+
else:
111+
res = res + string[j]
112+
j = j+1
113+
obj.write(res)
114+
115+
116+
while i<n: # going through the text file line by line
117+
line = lines[i]
118+
line = line.strip("\n")
119+
line = line.strip("\t")
120+
line = line.strip(" ")
121+
if "#DEF" in line:
122+
defineMacro()
123+
124+
else:
125+
list1 = line.split(" ")
126+
if list1[0] in name:
127+
expandMacro()
128+
else:
129+
obj.write(lines[i])
130+
131+
i = i+1
132+
obj.close()
133+
fo.close()
134+
135+

0 commit comments

Comments
 (0)