Guest User

Loadstring Interpreter

a guest
Jan 19th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. ## imports ##
  2. import sys, msvcrt
  3.  
  4. ## variables ##
  5. file = open(sys.argv[1], "r")
  6. loadstring = file.read()
  7. tokens, slots = [], []
  8. index = 0
  9. identifier_set = "_qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"
  10.  
  11. for i in range(255): slots.append(0)
  12.  
  13. ## lexer ##
  14. while len(loadstring) > index:
  15.     if (loadstring[index] in identifier_set):
  16.         temporary_string = ""
  17.         try:
  18.             while (loadstring[index] in identifier_set):
  19.                 temporary_string += loadstring[index]
  20.                 index += 1
  21.         except: ...
  22.         tokens.append(temporary_string)
  23.     index += 1
  24. index = 0
  25.  
  26. ## interpreter ##
  27. while len(tokens) > index:
  28.     ## load and copy #
  29.     if (tokens[index] == "load"):
  30.         index += 1
  31.         value = int(tokens[index])
  32.         index += 1
  33.         slot = int(tokens[index])
  34.         slots[slot] = value
  35.     elif (tokens[index] == "copy"):
  36.         index += 1
  37.         value = slots[int(tokens[index])]
  38.         index += 1
  39.         slot = int(tokens[index])
  40.         slots[slot] = value
  41.  
  42.     ## set_length ##
  43.     elif (tokens[index] == "set_length"):
  44.         index += 1
  45.         value = int(tokens[index])
  46.         index += 1
  47.         slots = []
  48.         for i in range(value): slots.append(int(tokens[index]))
  49.  
  50.     ## input ##
  51.     elif (tokens[index] == "get_character"):
  52.         index += 1
  53.         mode = int(tokens[index])
  54.         if (mode == 1): value = ord(msvcrt.getch())
  55.         else: value = int(msvcrt.getch())
  56.         index += 1
  57.         slot = int(tokens[index])
  58.         slots[slot] = value
  59.     elif (tokens[index] == "get_character_output"):
  60.         index += 1
  61.         mode = int(tokens[index])
  62.         if (mode == 1): value = ord(msvcrt.getche())
  63.         else: value = int(msvcrt.getche())
  64.         index += 1
  65.         slot = int(tokens[index])
  66.         slots[slot] = value
  67.  
  68.     ## compare #
  69.     elif (tokens[index] == "compare"):
  70.         index += 1
  71.         register1 = slots[int(tokens[index])]
  72.         index += 1
  73.         register2 = slots[int(tokens[index])]
  74.         compare_table = [register1, register2]
  75.  
  76.     ## jump instructions ##
  77.     elif (tokens[index] == "jump_equal"):
  78.         if (compare_table[0] == compare_table[1]):
  79.             index += 1
  80.             jump_index = int(tokens[index])
  81.             index = jump_index
  82.             continue
  83.     elif (tokens[index] == "jump_greater"):
  84.         if (compare_table[0] > compare_table[1]):
  85.             index += 1
  86.             jump_index = int(tokens[index])
  87.             index = jump_index
  88.             continue
  89.     elif (tokens[index] == "jump_less"):
  90.         if (compare_table[0] < compare_table[1]):
  91.             index += 1
  92.             jump_index = int(tokens[index])
  93.             index = jump_index
  94.             continue
  95.     elif (tokens[index] == "jump"):
  96.         index += 1
  97.         jump_index = int(tokens[index])
  98.         index = jump_index
  99.         continue
  100.  
  101.     ## math operations ##
  102.     elif (tokens[index] == "add"):
  103.         index += 1
  104.         slot1 = int(tokens[index])
  105.         index += 1
  106.         slot0 = int(tokens[index])
  107.         slots[slot0] += slots[slot1]
  108.     elif (tokens[index] == "subtract"):
  109.         index += 1
  110.         slot1 = int(tokens[index])
  111.         index += 1
  112.         slot0 = int(tokens[index])
  113.         slots[slot0] -= slots[slot1]
  114.     elif (tokens[index] == "clamp"):
  115.         index += 1
  116.         slot1 = tokens[index]
  117.         index += 1
  118.         slot0 = tokens[index]
  119.         slots[int(slot0)] = int(str(slots[int(slot1)]) + str(slots[int(slot0)]))
  120.  
  121.     ## output ##
  122.     elif (tokens[index] == "print"):
  123.         index += 1
  124.         mode = int(tokens[index])
  125.         index += 1
  126.         if (mode == 0): print(slots[int(tokens[index])], end = "")
  127.         else: print(chr(slots[int(tokens[index])]), end = "")
  128.  
  129.     ## halt / break loop ##
  130.     elif (tokens[index] == "halt"):
  131.         print("process finished!1!1!11!!")
  132.         break
  133.     index += 1
  134. input("\n")
Add Comment
Please, Sign In to add comment