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,22 +1,22 @@
# Assignment-A2 (Pass 1 and pass 2 macro processor)
try:
source= open('source.txt', 'r')
dum = source.readline()
source = open('source1.txt', 'r')
print("File is read successfully.")
source.seek(0)
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:
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):
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.append(line.strip()) #append the macro definition line
entry.append(line.strip())
while True:
line = source.readline().upper()
if not line:
@ -26,32 +26,43 @@ def macroman(line):
macroman(line)
elif 'MEND' in line:
global MDT, MNT
entry.append(line.strip()) #MEND line appended too
MNT.append([len(MNT)+1, name, len(MDT)+1]) # Eg. 1 Fibo 50
MDT = MDT + entry
entry.append(line.strip())
MNT.append([len(MNT) + 1, name, len(MDT) + 1])
MDT.extend(entry)
return
else:
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():
global MDT, MNT
global MDT, MNT, LIT
while True:
line = source.readline().upper()
if not line: break
if 'MACRO' in line:
macroman(line)
print('\nMNT:')
for a in MNT:
print(a)
print('\n\nMDT:')
i=0
for a in MDT:
i=i+1
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)
pass1()
#pass 2 starts here:
def inserter(sline, name):
global MDT, MNT
sline = ''
@ -66,11 +77,13 @@ def inserter(sline, name):
add += 1
return sline
def pass2():
source.seek(0)
output = open('output.txt', 'w')
output.close()
output = open('output.txt', 'a+')
while True:
sline = source.readline().upper()
if not sline: break
@ -78,9 +91,19 @@ def pass2():
if a[1] in sline and 'MACRO' not in sline:
sline = inserter(sline, a[1])
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:
output.write(sline)
print('done.')
print('\nLPT:')
for i, lpt in enumerate(LPT, start=1):
print(i, ' ', lpt)
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