Compare commits
No commits in common. "3abe4f123d3ec5fc0b73d3e2bedf3ffad33615ab" and "ccfa65c05720f46fef35ee407acc5fcef02c5970" have entirely different histories.
3abe4f123d
...
ccfa65c057
@ -1,4 +1,4 @@
|
|||||||
# Assignment-A1 - Pass 1 assembler
|
# Assignment-A1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
source = open('source.txt','r')
|
source = open('source.txt','r')
|
||||||
|
@ -1,81 +1,89 @@
|
|||||||
# Assignment-A2 - Two-pass macro processor code in Python
|
class MacroProcessor:
|
||||||
|
def __init__(self):
|
||||||
|
self.macro_name_table = {}
|
||||||
|
self.macro_definition_table = {}
|
||||||
|
self.mdt_index = 0
|
||||||
|
|
||||||
# BEGINNING OF CODE
|
def process_pass1(self, source_code):
|
||||||
# Required databases
|
inside_macro = False
|
||||||
macro_definition_table = {} # Storing all macro definitions with their names
|
current_macro_name = None
|
||||||
macro_name_table = {} # Storing macro names and their index
|
macro_definition = []
|
||||||
##########################################################################
|
|
||||||
def process_pass1(source_code):
|
|
||||||
mdt_index = 0
|
|
||||||
macro_definition = []
|
|
||||||
current_macro_name = None
|
|
||||||
inside_macro = False
|
|
||||||
|
|
||||||
for line in source_code:
|
for line in source_code:
|
||||||
tokens = line.strip().split() # store each word of the line of source code
|
tokens = line.strip().split()
|
||||||
|
|
||||||
if (not tokens): # skips blank lines
|
if not tokens:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if (tokens[0] == 'MACRO'): # beginning of macro definition
|
if tokens[0] == 'MACRO':
|
||||||
inside_macro = True
|
inside_macro = True
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if (inside_macro == True and tokens[0] == 'MEND'): # if end of macro is reached
|
if inside_macro and tokens[0] == 'MEND':
|
||||||
inside_macro = False
|
inside_macro = False
|
||||||
macro_definition_table[current_macro_name] = macro_definition[:]
|
self.macro_definition_table[current_macro_name] = macro_definition[:]
|
||||||
macro_name_table[current_macro_name] = mdt_index
|
self.macro_name_table[current_macro_name] = self.mdt_index
|
||||||
mdt_index += len(macro_definition)
|
self.mdt_index += len(macro_definition)
|
||||||
macro_definition = []
|
macro_definition = []
|
||||||
current_macro_name = None
|
current_macro_name = None
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if (inside_macro == True): # processing contents of macro
|
if inside_macro:
|
||||||
if (not current_macro_name):
|
if not current_macro_name:
|
||||||
current_macro_name = tokens[0]
|
current_macro_name = tokens[0]
|
||||||
macro_definition.append(line.strip())
|
macro_definition.append(line.strip())
|
||||||
##########################################################################
|
else:
|
||||||
def process_pass2(source_code):
|
pass
|
||||||
output = []
|
|
||||||
inside_macro = False
|
|
||||||
|
|
||||||
for line in source_code:
|
def process_pass2(self, source_code):
|
||||||
tokens = line.strip().split()
|
output = []
|
||||||
|
inside_macro = False
|
||||||
|
|
||||||
if (not tokens or tokens[0] == 'MACRO'): # skipping spaces, MACRO and MEND keywords
|
for line in source_code:
|
||||||
inside_macro = True
|
tokens = line.strip().split()
|
||||||
continue
|
|
||||||
elif (tokens[0] == 'MEND'):
|
|
||||||
inside_macro = False
|
|
||||||
continue
|
|
||||||
|
|
||||||
if inside_macro:
|
if not tokens:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
macro_name = tokens[0]
|
if tokens[0] == 'MACRO':
|
||||||
if macro_name in macro_name_table: # expand macro from source code
|
inside_macro = True
|
||||||
args = tokens[1:]
|
continue
|
||||||
macro_def = macro_definition_table[macro_name]
|
elif tokens[0] == 'MEND':
|
||||||
for expanded_line in macro_def:
|
inside_macro = False
|
||||||
for i, arg in enumerate(args):
|
continue
|
||||||
expanded_line = expanded_line.replace(f"&ARG{i+1}", arg)
|
|
||||||
output.append(expanded_line)
|
|
||||||
else: # append line if not a macro
|
|
||||||
output.append(line.strip())
|
|
||||||
|
|
||||||
return output
|
if inside_macro:
|
||||||
##########################################################################
|
continue
|
||||||
def display():
|
|
||||||
print("Macro Name Table (MNT):")
|
macro_name = tokens[0]
|
||||||
for name, index in macro_name_table.items():
|
if macro_name in self.macro_name_table:
|
||||||
print(f"Macro Name: {name} | Index: {index}")
|
macro_def = self.macro_definition_table[macro_name]
|
||||||
|
args = tokens[1:]
|
||||||
|
output.extend(self.expand_macro(macro_def, args))
|
||||||
|
else:
|
||||||
|
output.append(line.strip())
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
def expand_macro(self, macro_def, args):
|
||||||
|
expanded_lines = []
|
||||||
|
for line in macro_def:
|
||||||
|
for i, arg in enumerate(args):
|
||||||
|
line = line.replace(f"&ARG{i+1}", arg)
|
||||||
|
expanded_lines.append(line)
|
||||||
|
return expanded_lines
|
||||||
|
|
||||||
|
def display_tables(self):
|
||||||
|
print("Macro Name Table (MNT):")
|
||||||
|
for name, index in self.macro_name_table.items():
|
||||||
|
print(f"Macro Name: {name}, MDT Index: {index}")
|
||||||
|
|
||||||
|
print("\nMacro Definition Table (MDT):")
|
||||||
|
for name, definition in self.macro_definition_table.items():
|
||||||
|
print(f"Macro Name: {name}")
|
||||||
|
for line in definition:
|
||||||
|
print(f"\t{line}")
|
||||||
|
|
||||||
print("Macro Definition Table (MDT):")
|
|
||||||
for name, definition in macro_definition_table.items():
|
|
||||||
print(f"Macro: {name}")
|
|
||||||
for line in definition:
|
|
||||||
print(f"\t{line}")
|
|
||||||
##########################################################################
|
|
||||||
source_code = [
|
source_code = [
|
||||||
"MACRO",
|
"MACRO",
|
||||||
"INCR &ARG1",
|
"INCR &ARG1",
|
||||||
@ -88,13 +96,16 @@ source_code = [
|
|||||||
"START",
|
"START",
|
||||||
"INCR A",
|
"INCR A",
|
||||||
"DECR B",
|
"DECR B",
|
||||||
"END"
|
"END",
|
||||||
]
|
]
|
||||||
##########################################################################
|
|
||||||
process_pass1(source_code)
|
macro_processor = MacroProcessor()
|
||||||
display()
|
|
||||||
print("PASS 2:")
|
macro_processor.process_pass1(source_code)
|
||||||
expanded_code = process_pass2(source_code)
|
macro_processor.display_tables()
|
||||||
for i in expanded_code:
|
|
||||||
print(i)
|
expanded_code = macro_processor.process_pass2(source_code)
|
||||||
# END OF CODE
|
|
||||||
|
print("\nExpanded Source Code (Pass 2):")
|
||||||
|
for line in expanded_code:
|
||||||
|
print(line)
|
||||||
|
Loading…
Reference in New Issue
Block a user