constructors_visitor.py 9.5 KB

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