constructors_visitor.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import cPickle as pickle
  2. from visitor import Visitor
  3. class ConstructorsVisitor(Visitor):
  4. def __init__(self, args):
  5. Visitor.__init__(self, args)
  6. self.constructors = []
  7. self.free_id = 0
  8. def dump(self):
  9. return self.constructors
  10. # return pickle.dumps(self.constructors, pickle.HIGHEST_PROTOCOL)
  11. def add_constructors(self, *constructors):
  12. self.constructors.extend(constructors)
  13. # a visit_* method for each non-terminal in the grammar
  14. def visit_start(self, tree):
  15. # declare all functions
  16. for child in tree.get_children("funcdecl"):
  17. if self.pre_visit_funcdecl(child):
  18. self.add_constructors(True)
  19. tail = tree.get_tail_without(["newline"])
  20. # tail = tree.get_children("funcdecl") +\
  21. # tree.get_tail_without(["newline", "funcdecl"])
  22. last = tail[-1]
  23. for child in tail[:-1]:
  24. if child.head in ["return", "break", "continue"]:
  25. last = child
  26. break
  27. else:
  28. # funcdecl may add no constructors
  29. new_constructors_were_added = self.visit(child)
  30. if child.head == "func_call": # pop 'false'
  31. self.constructors.pop()
  32. if new_constructors_were_added:
  33. self.add_constructors(True)
  34. new_constructors_were_added = self.visit(last)
  35. if last.head == "func_call": # pop 'false'
  36. self.constructors.pop()
  37. if new_constructors_were_added:
  38. if last.head not in ["return", "break", "continue"]:
  39. self.add_constructors(False)
  40. elif self.constructors:
  41. self.constructors.pop() # pop 'true'
  42. self.add_constructors(False)
  43. def declare(self, symbol):
  44. if symbol.is_global:
  45. symbol.node = symbol.name
  46. self.add_constructors("global", symbol.node)
  47. else:
  48. symbol.node = str(self.free_id)
  49. self.add_constructors("declare", symbol.node)
  50. self.free_id += 1
  51. def visit_assignment(self, tree):
  52. self.add_constructors("assign")
  53. self.visit(tree.get_tail()[0])
  54. self.visit(tree.get_tail()[2])
  55. return True
  56. def visit_expression(self, tree):
  57. self.visit_children(tree)
  58. def visit_binary_operation(self, tree):
  59. self.visit_children(tree)
  60. def visit_disjunction(self, tree):
  61. self.visit_children(tree)
  62. def visit_conjunction(self, tree):
  63. self.visit_children(tree)
  64. def visit_comparison(self, tree):
  65. self.visit_children(tree)
  66. def visit_relation(self, tree):
  67. self.visit_children(tree)
  68. def visit_sum(self, tree):
  69. self.visit_children(tree)
  70. def visit_term(self, tree):
  71. self.visit_children(tree)
  72. def visit_factor(self, tree):
  73. self.visit_children(tree)
  74. def visit_primary(self, tree):
  75. self.visit_children(tree)
  76. def visit_parenthesized(self, tree):
  77. self.visit_children(tree)
  78. def visit_atomvalue(self, tree):
  79. self.visit_children(tree)
  80. def visit_type_specifier(self, tree):
  81. self.add_constructors("const",
  82. {"value": tree.get_text()})
  83. def visit_actionname(self, tree):
  84. self.add_constructors("const",
  85. {"value": tree.get_tail()[0].get_text()[1:]})
  86. def visit_string(self, tree):
  87. self.add_constructors("const", tree.get_text()[1:-1])
  88. def visit_integer(self, tree):
  89. self.add_constructors("const", int(tree.get_text()))
  90. def visit_float(self, tree):
  91. self.add_constructors("const", float(tree.get_text()))
  92. def visit_rvalue(self, tree):
  93. self.add_constructors("access")
  94. self.visit_lvalue(tree)
  95. def visit_lvalue(self, tree):
  96. symbol = self.get_symbol(tree)
  97. # TODO: split visit_funcdecl in pre_visit_funcdecl and visit_funcdecl
  98. if symbol.node is None:
  99. raise Exception("Undefined variable: %s" % (symbol.name))
  100. self.add_constructors("resolve", symbol.node)
  101. def visit_func_call(self, tree):
  102. symbol = self.get_symbol(tree.get_tail()[0])
  103. self.add_constructors("call")
  104. if hasattr(symbol, "pathmv"):
  105. self.add_constructors("deref", symbol.pathmv)
  106. else:
  107. self.visit(tree.get_tail()[0])
  108. expressions = tree.get_children("expression")
  109. self.add_constructors(len(expressions))
  110. for expression in expressions:
  111. self.visit(expression)
  112. self.add_constructors(False)
  113. return True
  114. def visit_input(self, tree):
  115. self.add_constructors("input")
  116. return True
  117. def visit_output(self, tree):
  118. self.add_constructors("output")
  119. self.visit(tree.get_child("expression"))
  120. return True
  121. def visit_dictionary(self, tree):
  122. pass # TODO: list and dictionary
  123. def visit_list(self, tree):
  124. pass # TODO: list and dictionary
  125. def visit_dict_item(self, tree):
  126. pass # TODO: list and dictionary
  127. def visit_ifelse(self, tree):
  128. self.add_constructors("if")
  129. expressions = tree.get_children("expression")
  130. blocks = tree.get_children("block")
  131. self.visit(expressions[0]) # condition
  132. self.visit(blocks[0]) # then-clause
  133. for e, b in zip(expressions[1:], blocks[1:]):
  134. self.add_constructors(True, "if") # else-if-clause
  135. self.visit(e)
  136. self.visit(b)
  137. if len(expressions) != len(blocks):
  138. self.add_constructors(True) # else-clause
  139. self.visit(blocks[-1])
  140. else:
  141. self.add_constructors(False) # no else_clause
  142. for i in range(len(expressions)-1):
  143. self.add_constructors(False) # no next
  144. return True
  145. def visit_while(self, tree):
  146. self.add_constructors("while")
  147. self.visit(tree.get_child("expression"))
  148. self.visit(tree.get_child("block"))
  149. return True
  150. def visit_block(self, tree):
  151. tail = tree.get_tail_without(["newline", "indent"])
  152. last = tail[-1]
  153. for child in tail[:-1]:
  154. if child.head in ["return", "break", "continue"]:
  155. last = child
  156. break
  157. else:
  158. self.visit(child)
  159. if child.head == "func_call": # pop 'false'
  160. self.constructors.pop()
  161. self.add_constructors(True)
  162. self.visit(last)
  163. if last.head == "func_call": # pop 'false'
  164. self.constructors.pop()
  165. if last.head not in ["return", "break", "continue"]:
  166. self.add_constructors(False)
  167. def visit_func_body(self, tree):
  168. self.visit_children(tree)
  169. def pre_visit_funcdecl(self, tree):
  170. func_body = tree.get_child("func_body")
  171. symbol = self.get_symbol(tree)
  172. symbol.node = symbol.name
  173. return False
  174. def visit_funcdecl(self, tree):
  175. func_body = tree.get_child("func_body")
  176. symbol = self.get_symbol(tree)
  177. if func_body:
  178. if symbol.name in ["input", "output"]:
  179. return False
  180. if tree.get_children("MUTABLE"):
  181. self.add_constructors("mutable_funcdef")
  182. else:
  183. self.add_constructors("funcdef")
  184. self.add_constructors(symbol.node,
  185. len(symbol.params))
  186. for p in tree.get_children("parameter"):
  187. self.visit(p)
  188. self.visit(func_body)
  189. return True
  190. elif tree.get_child("ASSIGN"):
  191. # TODO: fix funcdecl special case: "X function f(...) = ..."
  192. # Note: replicates "Element x; x = ?primiteves/a" behavior
  193. # Dangerous: SemanticsVisitor handles it as a function
  194. # pathmv is needed in visit_func_call(self, tree)
  195. symbol.pathmv = tree.get_child("ANYTHING").get_text()
  196. self.add_constructors("global", symbol.node, "deref", symbol.pathmv)
  197. # reason: "X function f(Y) = Z" adds no constructors
  198. return True
  199. else:
  200. # Just a declaration, so skip
  201. return False
  202. def visit_parameter(self, tree):
  203. symbol = self.get_symbol(tree)
  204. symbol.node = str(self.free_id)
  205. self.add_constructors(symbol.node)
  206. self.free_id += 1
  207. def visit_continue(self, tree):
  208. self.add_constructors("continue")
  209. return True
  210. def visit_break(self, tree):
  211. self.add_constructors("break")
  212. return True
  213. def visit_return(self, tree):
  214. self.add_constructors("return")
  215. if len(tree.get_tail()) > 2:
  216. self.add_constructors(True)
  217. self.visit(tree.get_tail()[1])
  218. else:
  219. self.add_constructors(False)
  220. return True
  221. def visit_bool(self, tree):
  222. if tree.get_text() == "True":
  223. self.add_constructors("const", True)
  224. else:
  225. self.add_constructors("const", False)
  226. def visit_definition(self, tree):
  227. # First declare it
  228. symbol = self.get_symbol(tree)
  229. self.declare(symbol)
  230. if tree.get_children("ASSIGN"):
  231. # Determine whether it is just a constant, or a deref
  232. atom = tree.get_child("atomvalue")
  233. if atom.get_child("deref"):
  234. # Deref
  235. dest = atom.get_child("deref").get_child("ANYTHING")
  236. if dest is None:
  237. # Just an empty questionmark!
  238. self.add_constructors("empty")
  239. else:
  240. self.add_constructors("deref", dest.get_text())
  241. else:
  242. # Constant
  243. self.visit(atom)
  244. else:
  245. # TODO empty is not really the same as undefined!
  246. self.add_constructors("empty")
  247. return True