constructors_object_visitor.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. for e, v in self.object_symbols.iteritems():
  37. data.extend([True, e, v])
  38. # Finish the symbol table
  39. data.append(False)
  40. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()
  41. # Wait for kernel to signal that it finished
  42. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "value": '2', "username": self.username}))).read()
  43. v = urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "get_output", "username": self.username}))).read()
  44. v = json.loads(v)
  45. if v == "DONE":
  46. return True
  47. else:
  48. return False
  49. def visit_definition(self, tree):
  50. for a in tree.get_children("ID"):
  51. name = a.get_tail()[0]
  52. self.object_symbols[name] = True
  53. return ConstructorsVisitor.visit_definition(self, tree)
  54. def visit_vardecl(self, tree):
  55. if len(tree.get_tail()) > 2:
  56. for a in tree.get_children("ID"):
  57. name = a.get_tail()[0]
  58. self.object_symbols.setdefault(name, False)
  59. return ConstructorsVisitor.visit_vardecl(self, tree)
  60. else:
  61. return ConstructorsVisitor.visit_vardecl(self, tree)
  62. def visit_funcdecl(self, tree):
  63. for a in tree.get_children("func_name"):
  64. for b in a.get_children("ID"):
  65. name = b.get_tail()[0]
  66. if tree.get_children("func_body") or tree.get_children("ASSIGN"):
  67. self.object_symbols[name] = True
  68. else:
  69. self.object_symbols.setdefault(name, False)
  70. return ConstructorsVisitor.visit_funcdecl(self, tree)