diff --git a/Codes/Python version/Assignment-A2 (Macro)/Code-A2.py b/Codes/Python version/Assignment-A2 (Macro)/Code-A2.py index ebc2b1e..cda87c5 100644 --- a/Codes/Python version/Assignment-A2 (Macro)/Code-A2.py +++ b/Codes/Python version/Assignment-A2 (Macro)/Code-A2.py @@ -1,24 +1,24 @@ -# 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() + line = source.readline().upper() if not line: print('No MEND found for: ', name) return @@ -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 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 (i, ' ',a) + + 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 = '' @@ -63,24 +74,36 @@ def inserter(sline, name): if 'MEND' in MDT[add]: break sline += MDT[add] + '\n' - add+=1 + 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 + sline = source.readline().upper() + if not sline: break for a in MNT: if a[1] in sline and 'MACRO' not in sline: - sline=inserter(sline, a[1]) + 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() diff --git a/Codes/Python version/Assignment-A2 (Macro)/source.txt b/Codes/Python version/Assignment-A2 (Macro)/source.txt deleted file mode 100644 index 5cb83e8..0000000 --- a/Codes/Python version/Assignment-A2 (Macro)/source.txt +++ /dev/null @@ -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 diff --git a/Codes/Python version/Assignment-A2 (Macro)/source1.txt b/Codes/Python version/Assignment-A2 (Macro)/source1.txt new file mode 100644 index 0000000..8f458d5 --- /dev/null +++ b/Codes/Python version/Assignment-A2 (Macro)/source1.txt @@ -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