linker.py 4.2 KB

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