3
1

Added more defs in macro code such as literal table and stuff.

This commit is contained in:
K 2024-11-06 23:27:55 +05:30
parent d1dc6d1414
commit c6ae17976f
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
3 changed files with 60 additions and 45 deletions

View File

@ -1,24 +1,24 @@
# Assignment-A2 (Pass 1 and pass 2 macro processor)
try: try:
source= open('source.txt', 'r') source = open('source1.txt', 'r')
dum = source.readline()
print("File is read successfully.") print("File is read successfully.")
source.seek(0) source.seek(0)
except FileNotFoundError: except FileNotFoundError:
print('\n\n\nsource.txt file not found.\nOpen Macroprocessor Folder in VSC[not the main folder]\n\n\n') print('\n\n\nsource2.txt file not found. Create it first!\n\n\n')
except IOError: except IOError:
print('The source file has an IO error') print('The source file has an IO error')
MDT = [] # Macro Definition Table
MNT = [] # Macro Name Table
LIT = [] # Literal Table
LPT = [] # Literal Pool Table
MDT =[]
MNT =[]
def macroman(line): def macroman(line):
name = line.split()[1] #A concise way to get the 2nd word-> name of macro. index =1. first split and then get 2nd word name = line.split()[1]
entry = [] entry = []
entry.append(line.strip()) #append the macro definition line entry.append(line.strip())
while True: while True:
line=source.readline().upper() line = source.readline().upper()
if not line: if not line:
print('No MEND found for: ', name) print('No MEND found for: ', name)
return return
@ -26,32 +26,43 @@ def macroman(line):
macroman(line) macroman(line)
elif 'MEND' in line: elif 'MEND' in line:
global MDT, MNT global MDT, MNT
entry.append(line.strip()) #MEND line appended too entry.append(line.strip())
MNT.append([len(MNT)+1, name, len(MDT)+1]) # Eg. 1 Fibo 50 MNT.append([len(MNT) + 1, name, len(MDT) + 1])
MDT = MDT + entry MDT.extend(entry)
return return
else: else:
entry.append(line.strip()) entry.append(line.strip())
# 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)
def pass1(): def pass1():
global MDT, MNT global MDT, MNT, LIT
while True: while True:
line = source.readline().upper() line = source.readline().upper()
if not line:break if not line: break
if 'MACRO' in line: if 'MACRO' in line:
macroman(line) macroman(line)
print('\nMNT:') print('\nMNT:')
for a in MNT: for a in MNT:
print(a) print(a)
print('\n\nMDT:')
i=0 print('\nMDT:')
for a in MDT: for i, a in enumerate(MDT, start=1):
i=i+1 print(i, ' ', a)
print (i, ' ',a)
print('\nLIT:')
for i, lit in enumerate(LIT, start=1):
print(i, ' ', lit)
pass1() pass1()
#pass 2 starts here:
def inserter(sline, name): def inserter(sline, name):
global MDT, MNT global MDT, MNT
sline = '' sline = ''
@ -63,24 +74,36 @@ def inserter(sline, name):
if 'MEND' in MDT[add]: if 'MEND' in MDT[add]:
break break
sline += MDT[add] + '\n' sline += MDT[add] + '\n'
add+=1 add += 1
return sline return sline
def pass2(): def pass2():
source.seek(0) source.seek(0)
output = open('output.txt', 'w') output = open('output.txt', 'w')
output.close() output.close()
output = open('output.txt', 'a+') output = open('output.txt', 'a+')
while True: while True:
sline= source.readline().upper() sline = source.readline().upper()
if not sline:break if not sline: break
for a in MNT: for a in MNT:
if a[1] in sline and 'MACRO' not in sline: if a[1] in sline and 'MACRO' not in sline:
sline=inserter(sline, a[1]) sline = inserter(sline, a[1])
d = sline.strip() d = sline.strip()
# 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)
if d not in MDT: if d not in MDT:
output.write(sline) output.write(sline)
print('done.') print('done.')
print('\nLPT:')
for i, lpt in enumerate(LPT, start=1):
print(i, ' ', lpt)
pass2() pass2()

View File

@ -1,21 +0,0 @@
macro add num1, num2
a1
maCro sub num1, num2
s1
s2
s3
mend
a2
mend
The code above should not come in output coz it's just definitions.
The actual code begins from here.
sure, you can write any shit in the file.
start 1000
mover areg, 50
movem breg, 100
I am now calling first function. The following line should be replaced by the body:
add areg, 30
dbms_output.print_line('Present Ma'am!')
equ areg, 40
add 5,6
sub 3,4

View File

@ -0,0 +1,13 @@
MACRO FIBO
MOVE A, =5
ADD A, B
MEND
MACRO SUM
ADD A, B
MOVE C, =100
MEND
START:
FIBO
SUM