constructors_object_visitor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import sys
  2. sys.path.append("scripts")
  3. from constructors_visitor import ConstructorsVisitor
  4. from cached_exception import CachedException
  5. import urllib
  6. import urllib2
  7. import json
  8. timeout = 100
  9. class ConstructorsObjectVisitor(ConstructorsVisitor):
  10. def __init__(self, args):
  11. ConstructorsVisitor.__init__(self, args)
  12. self.username = args[0]
  13. self.obj_file = args[1]
  14. self.real_file = args[2]
  15. self.address = args[3]
  16. self.object_symbols = {}
  17. with open(self.real_file, 'r') as f:
  18. import hashlib
  19. md5 = hashlib.md5()
  20. md5.update(f.read())
  21. self.hash_file = md5.hexdigest()
  22. # Create user
  23. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "value": '"%s"' % self.username, "username": "user_manager"}))).read()
  24. simple_filename = self.real_file.rsplit("/")[-1]
  25. print("[COMPILE] %s" % simple_filename)
  26. def dump(self):
  27. v = ConstructorsVisitor.dump(self)
  28. def flush_data(data):
  29. if data:
  30. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username})), timeout=timeout).read()
  31. return []
  32. # Set up interface
  33. flush_data([3, "upload", self.obj_file, self.hash_file, True])
  34. flush_data(v)
  35. # Upload symbol table
  36. data = []
  37. for e, v in self.object_symbols.iteritems():
  38. data.extend([True, e, v])
  39. # Finish the symbol table
  40. data.append(False)
  41. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()
  42. # Wait for kernel to signal that it finished
  43. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "value": '2', "username": self.username}))).read()
  44. v = urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "get_output", "username": self.username}))).read()
  45. v = json.loads(v)
  46. if v == "DONE":
  47. return True
  48. else:
  49. return False
  50. def visit_definition(self, tree):
  51. for a in tree.get_children("ID"):
  52. name = a.get_tail()[0]
  53. self.object_symbols[name] = True
  54. return ConstructorsVisitor.visit_definition(self, tree)
  55. def visit_vardecl(self, tree):
  56. if len(tree.get_tail()) > 2:
  57. for a in tree.get_children("ID"):
  58. name = a.get_tail()[0]
  59. self.object_symbols.setdefault(name, False)
  60. return ConstructorsVisitor.visit_vardecl(self, tree)
  61. else:
  62. return ConstructorsVisitor.visit_vardecl(self, tree)
  63. def visit_funcdecl(self, tree):
  64. for a in tree.get_children("func_name"):
  65. for b in a.get_children("ID"):
  66. name = b.get_tail()[0]
  67. if tree.get_children("func_body") or tree.get_children("ASSIGN"):
  68. self.object_symbols[name] = True
  69. else:
  70. self.object_symbols.setdefault(name, False)
  71. return ConstructorsVisitor.visit_funcdecl(self, tree)