Advertisement
quantumech

Untitled

Oct 13th, 2020 (edited)
1,898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. from os import sys
  2. from re import sub
  3.  
  4. if len(sys.argv) == 1:
  5.     print("[PREPROCESSOR ERR] Filename not specified.")
  6.     exit()
  7. elif len(sys.argv) == 2:
  8.     print("[PREPROCESSOR ERR] Output location not specified.")
  9.     exit()
  10.  
  11. args = sys.argv[1:]
  12. filename = args[0]
  13. output_path = args[1]
  14.  
  15. sym_table = {}
  16.  
  17. with open(filename) as file:
  18.     lines = file.read().split("\n")
  19.  
  20.     for i in range(len(lines)):
  21.         if lines[i].find("DEF_SYMS") >= 0:
  22.             line = lines[i][(lines[i].find("DEF_SYMS ") + len("DEF_SYMS ")):]
  23.             curr_line = line.split(" ")
  24.             curr_line = [e.split("=") for e in curr_line]
  25.  
  26.             try:
  27.                 for e in curr_line:
  28.                     sym_table[e[0]] = e[1]
  29.             except:
  30.                 print("[PREPROCESSOR ERR] DEF_SYMS directive must be formatted exactly like the following (_ = space) 'DEF_SYMS_<sym1>=<v1>_<sym2>=<v2>...'")
  31.                 break
  32.        
  33.         elif lines[i].find("END_SYMS") >= 0:
  34.             line = lines[i][(lines[i].find("END_SYMS ") + len("END_SYMS ")):]
  35.             curr_line = line.split(" ")
  36.  
  37.             try:
  38.                 for e in curr_line:
  39.                     if e in sym_table.keys():
  40.                         sym_table.pop(e)
  41.                     else:
  42.                         print("[PREPROCESSOR ERR] Cannot delete symbol: '" + e + "'")
  43.                         break
  44.             except:
  45.                 print("[PREPROCESSOR ERR] END_SYMS directive must be formatted exactly like the following (_ = space) 'END_SYMS_<sym1>_<sym2>..'")
  46.                 break
  47.  
  48.         elif lines[i].find("END_ALL_SYMS") >= 0:
  49.             sym_table = {}
  50.        
  51.         else:
  52.             for key, value in sorted(sym_table.items(), key=lambda x: len(x)):
  53.                 lines[i] = sub(r"\b" + key + r"\b", value, lines[i])
  54.        
  55.     output = "\n".join(lines)
  56.    
  57.     try:
  58.         with open(output_path, "r") as out: # When you use 'with', Python closes the file handle automatically when finished
  59.             if out.read() != "":
  60.                 if input(output_path + " is not empty. Are you sure you want to overwrite it? (y/n): ") != "y":
  61.                     print("Cancelling preprocessing")
  62.                 else:
  63.                     with open(output_path, "w+") as out:
  64.                         out.write(output)
  65.                         print("Successfully preprocessed", filename)
  66.     except:
  67.         with open(output_path, "w") as out:
  68.             out.write(output)
  69.             print("Successfully preprocessed", filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement