constructors_visitor.py 9.3 KB

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