linker.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import sys
  2. import urllib
  3. import urllib2
  4. def link(username, objects, fast):
  5. # Read out all symbol tables that are to be linked
  6. definers = {}
  7. if not fast:
  8. definers["main"] = None
  9. for obj in objects:
  10. print("[LINK] %s" % (obj))
  11. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": username}))).read()
  12. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"read_symbols"', "username": username}))).read()
  13. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % obj, "username": username}))).read()
  14. v = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": username}))).read()
  15. lst = v.rsplit("=", 1)[1]
  16. lst = lst.split("\n")
  17. for e in lst:
  18. if len(e) > 1:
  19. name, defined = e.rsplit(":", 1)
  20. if defined == "1":
  21. if definers.get(name, None):
  22. raise Exception("Double definition for symbol %s\nDefined in %s\nDefined in %s" % (name, definers[name], obj))
  23. definers[name] = obj
  24. else:
  25. if name not in definers:
  26. definers[name] = None
  27. # Check for undefined symbols with this linking set
  28. for symbol in definers:
  29. if definers[symbol] is None:
  30. if symbol not in ["input", "output"]:
  31. # Some symbols are built-ins which only look like functions
  32. raise Exception("Undefined symbol %s" % symbol)
  33. # Ok, we know that all symbols can be defined with this set of files, now link their initializers together
  34. initializers = []
  35. for obj in objects:
  36. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": username}))).read()
  37. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"read_initializers"', "username": username}))).read()
  38. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % obj, "username": username}))).read()
  39. v = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": username}))).read()
  40. start = str(v.split("&", 1)[0].split("=")[1])
  41. initializers.append(start)
  42. # Bind all initializers together
  43. if definers:
  44. print("[LOAD] %s:main()" % definers["main"])
  45. else:
  46. print("[LOAD] main()")
  47. # Set interface to constructors
  48. commands = [("V", '1')]
  49. # Link all initializers together
  50. for init in initializers:
  51. commands.extend([
  52. ("V", '"call"'),
  53. ("V", '"access"'),
  54. ("V", '"resolve"'),
  55. ("V", '"exec"'),
  56. ("V", '1'),
  57. ("V", '"const"'),
  58. ("R", str(init)),
  59. ("V", 'true'),
  60. ])
  61. # Load main function
  62. commands.extend([
  63. ("V", '"return"'),
  64. ("V", 'true'),
  65. ("V", '"call"'),
  66. ("V", '"access"'),
  67. ("V", '"resolve"'),
  68. ("V", '"main"'),
  69. ("V", '0'),
  70. ("V", 'false'),
  71. ])
  72. import json
  73. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "data": json.dumps(commands), "username": username}))).read()
  74. if len(sys.argv) == 1:
  75. print("No username defined")
  76. else:
  77. username = sys.argv[1]
  78. objects = set(sys.argv[2:])
  79. if "--fast" in objects:
  80. objects.remove("--fast")
  81. fast = True
  82. else:
  83. fast = False
  84. if objects:
  85. link(username, objects, fast)
  86. else:
  87. print("No files to link defined")