2024-11-06 23:24:49 +05:30
|
|
|
try:
|
2024-11-06 23:27:55 +05:30
|
|
|
source = open('source1.txt', 'r')
|
2024-11-06 23:24:49 +05:30
|
|
|
print("File is read successfully.")
|
|
|
|
source.seek(0)
|
|
|
|
except FileNotFoundError:
|
2024-11-06 23:27:55 +05:30
|
|
|
print('\n\n\nsource2.txt file not found. Create it first!\n\n\n')
|
2024-11-06 23:24:49 +05:30
|
|
|
except IOError:
|
|
|
|
print('The source file has an IO error')
|
|
|
|
|
2024-11-06 23:27:55 +05:30
|
|
|
MDT = [] # Macro Definition Table
|
|
|
|
MNT = [] # Macro Name Table
|
|
|
|
LIT = [] # Literal Table
|
|
|
|
LPT = [] # Literal Pool Table
|
2024-11-06 23:24:49 +05:30
|
|
|
|
|
|
|
|
|
|
|
def macroman(line):
|
2024-11-06 23:27:55 +05:30
|
|
|
name = line.split()[1]
|
2024-11-06 23:24:49 +05:30
|
|
|
entry = []
|
2024-11-06 23:27:55 +05:30
|
|
|
entry.append(line.strip())
|
2024-11-06 23:24:49 +05:30
|
|
|
while True:
|
2024-11-06 23:27:55 +05:30
|
|
|
line = source.readline().upper()
|
2024-11-06 23:24:49 +05:30
|
|
|
if not line:
|
|
|
|
print('No MEND found for: ', name)
|
|
|
|
return
|
|
|
|
if 'MACRO' in line:
|
|
|
|
macroman(line)
|
|
|
|
elif 'MEND' in line:
|
|
|
|
global MDT, MNT
|
2024-11-06 23:27:55 +05:30
|
|
|
entry.append(line.strip())
|
|
|
|
MNT.append([len(MNT) + 1, name, len(MDT) + 1])
|
|
|
|
MDT.extend(entry)
|
2024-11-06 23:24:49 +05:30
|
|
|
return
|
|
|
|
else:
|
|
|
|
entry.append(line.strip())
|
2024-11-06 23:27:55 +05:30
|
|
|
# Check for literals and add to the literal table
|
|
|
|
for word in line.split():
|
|
|
|
if word.startswith('='): # Assuming literals start with '='
|
|
|
|
if word not in LIT:
|
|
|
|
LIT.append(word)
|
2024-11-06 23:24:49 +05:30
|
|
|
|
|
|
|
|
|
|
|
def pass1():
|
2024-11-06 23:27:55 +05:30
|
|
|
global MDT, MNT, LIT
|
2024-11-06 23:24:49 +05:30
|
|
|
while True:
|
|
|
|
line = source.readline().upper()
|
2024-11-06 23:27:55 +05:30
|
|
|
if not line: break
|
2024-11-06 23:24:49 +05:30
|
|
|
if 'MACRO' in line:
|
|
|
|
macroman(line)
|
2024-11-06 23:27:55 +05:30
|
|
|
|
2024-11-06 23:24:49 +05:30
|
|
|
print('\nMNT:')
|
|
|
|
for a in MNT:
|
|
|
|
print(a)
|
2024-11-06 23:27:55 +05:30
|
|
|
|
|
|
|
print('\nMDT:')
|
|
|
|
for i, a in enumerate(MDT, start=1):
|
|
|
|
print(i, ' ', a)
|
|
|
|
|
|
|
|
print('\nLIT:')
|
|
|
|
for i, lit in enumerate(LIT, start=1):
|
|
|
|
print(i, ' ', lit)
|
|
|
|
|
2024-11-06 23:24:49 +05:30
|
|
|
|
|
|
|
pass1()
|
2024-11-06 23:27:55 +05:30
|
|
|
|
|
|
|
|
2024-11-06 23:24:49 +05:30
|
|
|
def inserter(sline, name):
|
|
|
|
global MDT, MNT
|
|
|
|
sline = ''
|
|
|
|
for a in MNT:
|
|
|
|
if a[1] == name:
|
|
|
|
add = a[2]
|
|
|
|
break
|
|
|
|
while True:
|
|
|
|
if 'MEND' in MDT[add]:
|
|
|
|
break
|
|
|
|
sline += MDT[add] + '\n'
|
2024-11-06 23:27:55 +05:30
|
|
|
add += 1
|
2024-11-06 23:24:49 +05:30
|
|
|
return sline
|
|
|
|
|
2024-11-06 23:27:55 +05:30
|
|
|
|
2024-11-06 23:24:49 +05:30
|
|
|
def pass2():
|
|
|
|
source.seek(0)
|
|
|
|
output = open('output.txt', 'w')
|
|
|
|
output.close()
|
|
|
|
output = open('output.txt', 'a+')
|
2024-11-06 23:27:55 +05:30
|
|
|
|
2024-11-06 23:24:49 +05:30
|
|
|
while True:
|
2024-11-06 23:27:55 +05:30
|
|
|
sline = source.readline().upper()
|
|
|
|
if not sline: break
|
2024-11-06 23:24:49 +05:30
|
|
|
for a in MNT:
|
|
|
|
if a[1] in sline and 'MACRO' not in sline:
|
2024-11-06 23:27:55 +05:30
|
|
|
sline = inserter(sline, a[1])
|
2024-11-06 23:24:49 +05:30
|
|
|
d = sline.strip()
|
2024-11-06 23:27:55 +05:30
|
|
|
# Check for literals in the output line
|
|
|
|
for word in d.split():
|
|
|
|
if word.startswith('='):
|
|
|
|
if word not in LPT: # If not already in the literal pool table
|
|
|
|
LPT.append(word)
|
|
|
|
|
2024-11-06 23:24:49 +05:30
|
|
|
if d not in MDT:
|
|
|
|
output.write(sline)
|
2024-11-06 23:27:55 +05:30
|
|
|
|
2024-11-06 23:24:49 +05:30
|
|
|
print('done.')
|
2024-11-06 23:27:55 +05:30
|
|
|
print('\nLPT:')
|
|
|
|
for i, lpt in enumerate(LPT, start=1):
|
|
|
|
print(i, ' ', lpt)
|
2024-11-06 23:24:49 +05:30
|
|
|
|
|
|
|
|
|
|
|
pass2()
|