diff --git a/Codes/macro 1.py b/Codes/macro 1.py new file mode 100755 index 0000000..fe10652 --- /dev/null +++ b/Codes/macro 1.py @@ -0,0 +1,54 @@ +#Original Code, Engineered by Afan Shaikh. +try: + source= open('source.txt', 'r') + dum = source.readline() + print("File is read successfully.") + source.seek(0) +except FileNotFoundError: + print('\n\n\nsource.txt file not found. Create it first!\n\n\n') +except IOError: + print('The source file has an IO error') + + +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 + entry = [] + entry.append(line.strip()) #append the macro definition line + while True: + line=source.readline().upper() + if not line: + print('No MEND found for: ', name) + return + if 'MACRO' in 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 + return + else: + entry.append(line.strip()) + + +def driver(): + global MDT, MNT + 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 (i, ' ',a) + +driver() + diff --git a/Codes/pass 1.py b/Codes/pass 1.py new file mode 100755 index 0000000..a788e25 --- /dev/null +++ b/Codes/pass 1.py @@ -0,0 +1,110 @@ +#Original Code, Engineered by Afan Shaikh. no AI. +try: + source = open('source.txt','r') + data = source.read() + print('File read successfully bro \n\n') + source.seek(0) +except FileNotFoundError: #A little bit of exception handling + print('\n\n\nFile Not found. Create a source.txt first! u dummy!\n\n\n ') +except IOError: + print('There was an IO error') +LT_index = 0 #index of LT table +ST_index = 0 #index of ST table +add = 0 # address in source code +MOT = {'STOP': '00','ADD': '01','SUB': '02','MULT': '03','MOVER': '04','MOVEM': '05','COMP': '06','BC': '07','DIV': '08','READ': '09','PRINT': '10','START': '01','END': '02','ORIGIN': '03','LTORG': '05','DS': '01','DC': '02','AREG,': '01','BREG,': '02','EQ':'01'} +ST = [] +code=[] +LT=[] +MC = [] +# LT, ST are lists of lists. code= intermediate code table + + +def classy(text): + '''This function will return the class of the word to be inputted in the Intermediate table''' + text = text.upper() + if text in ['STOP','ADD','SUB', 'MULT','MOVER','MOVEM','COMP','BC','DIV','READ', 'PRINT']: + return 'IS' + elif text in ['START','END','ORIGIN','LTORG']: + return 'AD' + elif text in ['DS','DC']: + return 'DL' + elif text in ['AREG,','BREG,']: return 'RG' + elif text in ['EQ']: return 'CC' + else: return 'None' + +def handle_start(): + '''This function gives you the starting address of the code''' + line= source.readline() + words=line.split() + if words[0].upper()=='START': + return int(words[1]) + else: + print("No Start Statement! Abort!\n") + return 0 + + + +def pass1(): + add=handle_start() + if not add: + print("Ending Pass 1 due to Above error.") + return + global ST_index, LT_index # to modify global variables, use global keyword + while True: + line=source.readline()# handlestart function reads line 1 and we start from the second line. + if not line: + break + words= line.split() + for w in words: + w=w.upper() + if w[0]=='=': + entry=[LT_index,w, add] + LT.append(entry) + LT_index +=1 + elif classy(w)== 'None': + for term in ST: + if w== term[1]: break # I check if the label is already present in ST. + else: + entry=[ST_index,w, add] + ST.append(entry) + ST_index +=1 + add+=1 + print('LT:') + for a in LT: + print(a) + print('\n\n\nST:') + for a in ST: + print(a) + +def intermediate(): + source.seek(0) + while True: + entry=[] + ind = 0 + line=source.readline() + if not line: + break + words=line.split() + for w in words: + w=w.upper() + if classy(w)!='None': #it is a directive + entry.append((classy(w),MOT[w])) + elif w[0]== '=': #it is a literal. + for a in LT: + if a[1]==w: + ind = a[0] + break + entry.append(('L',ind)) + else: #it is a symbol + for a in ST: + if a[1]==w: + ind = a[0] + break + entry.append(('S',ind)) + code.append(entry) + print("\nThe Intermediate code is:") + for entry in code: + print(entry) + +pass1() +intermediate() \ No newline at end of file diff --git a/Codes/source (macro).txt b/Codes/source (macro).txt new file mode 100755 index 0000000..d4a2496 --- /dev/null +++ b/Codes/source (macro).txt @@ -0,0 +1,11 @@ +macro add +a1 +maCro sub +s1 +s2 +s3 +mend +a2 +mend +These lines should not come in MNT +help haha aha diff --git a/Codes/source (pass1).txt b/Codes/source (pass1).txt new file mode 100755 index 0000000..0548fd2 --- /dev/null +++ b/Codes/source (pass1).txt @@ -0,0 +1,4 @@ +Start 100 +Label MOVER AREG, =5 +add areg, =999 +sub breg, x