|
| 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