semantics_visitor.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. import hutn_compiler.hutnparser as hp
  2. import hutn_compiler.symbol_table as st
  3. import sys
  4. import hutn_compiler.types_mv as types_mv
  5. from hutn_compiler.declare_functions_visitor import DeclareFunctionsVisitor
  6. from hutn_compiler.visitor import Visitor
  7. class SemanticsVisitor(Visitor):
  8. def __init__(self, args):
  9. Visitor.__init__(self, args)
  10. self.symbol_table = st.SymbolTable()
  11. # there is only one input file, list is for sharing it among visitors
  12. self.inputfiles = []
  13. # Count whether we are in a while or not
  14. self.while_counter = 0
  15. # inherited attribute, set in funcdecl and used in return,
  16. # to ensure that (returned type == declared type)
  17. self.current_funcdecl = None
  18. self.declare_functions_visitor =\
  19. DeclareFunctionsVisitor(self.symbol_table, self.inputfiles)
  20. @staticmethod
  21. def incompatible_types(l_type, r_type):
  22. if type(l_type) != type(r_type):
  23. if types_mv.Void in (type(l_type), type(r_type)):
  24. return True
  25. if types_mv.Element in (type(l_type), type(r_type)):
  26. return False
  27. if l_type.isNotNumber() or r_type.isNotNumber():
  28. return True
  29. return False
  30. def do_check_binary_ops_arithmetic(self, l, r):
  31. l_type, r_type = self.get_type(l), self.get_type(r)
  32. if SemanticsVisitor.incompatible_types(l_type, r_type):
  33. raise RuntimeError(
  34. "{}:{}:{}: error: invalid operands to binary operator "
  35. "(have {} and {})".format(self.inputfiles[0],
  36. l.startpos['line'],
  37. l.startpos['column'],
  38. str(l_type),
  39. str(r_type)))
  40. def check_binary_ops_arithmetic(self, tree):
  41. l, r = tree.get_tail()[0], tree.get_tail()[2]
  42. self.do_check_binary_ops_arithmetic(l, r)
  43. def generalize_binary_ops_arithmetic(self, tree):
  44. l, r = tree.get_tail()[0], tree.get_tail()[2]
  45. l_type, r_type = self.get_type(l), self.get_type(r)
  46. return types_mv.generalize_arithmetic(l_type, r_type)
  47. def check_unary_ops_arithmetic(self, tree, operator_name):
  48. l = tree.get_tail()[1]
  49. l_type = self.get_type(l)
  50. if l_type.isNotNumber():
  51. raise RuntimeError(
  52. "{}:{}:{}: error: wrong type argument to unary {} "
  53. "({})".format(self.inputfiles[0],
  54. l.startpos['line'],
  55. l.startpos['column'],
  56. operator_name,
  57. str(l_type)))
  58. def promote_unary_ops_arithmetic(self, tree):
  59. l = tree.get_tail()[1]
  60. l_type = self.get_type(l)
  61. try:
  62. return types_mv.promote_arithmetic(l_type)
  63. except RuntimeError:
  64. raise RuntimeError(
  65. "Pathological situation in promote_unary_ops_arithmetic: "
  66. "check_unary_ops_arithmetic has not been executed")
  67. # if r_type is provided, r is not used
  68. def do_check_assignment(self, l, r, r_type = None):
  69. if r_type is not None:
  70. l_type = self.get_type(l)
  71. else:
  72. l_type, r_type = self.get_type(l), self.get_type(r)
  73. if SemanticsVisitor.incompatible_types(l_type, r_type):
  74. raise RuntimeError("{}:{}:{}: error: cannot assign a value of "
  75. "type '{}' to a variable of type '{}'"
  76. .format(self.inputfiles[0],
  77. l.startpos['line'],
  78. l.startpos['column'],
  79. str(r_type),
  80. str(l_type)))
  81. def check_assignment(self, tree):
  82. l, r = tree.get_tail()[0], tree.get_tail()[2]
  83. self.do_check_assignment(l, r)
  84. def check_return(self, tree):
  85. l = self.current_funcdecl
  86. if len(tree.get_tail()) > 2:
  87. r = tree.get_tail()[1]
  88. r_type = None
  89. else:
  90. r = None
  91. r_type = types_mv.Void()
  92. if l:
  93. self.do_check_assignment(l, r, r_type)
  94. else:
  95. raise RuntimeError(
  96. "{}:{}:{}: error: 'return' is used outside of a function"
  97. .format(self.inputfiles[0],
  98. tree.startpos['line'],
  99. tree.startpos['column']))
  100. def check_predicate(self, tree):
  101. if isinstance(self.get_type(tree), types_mv.Element):
  102. return
  103. if self.get_type(tree).isNotNumber():
  104. raise RuntimeError(
  105. "{}:{}:{}: error: predicates of type '{}' are not allowed"
  106. .format(self.inputfiles[0],
  107. tree.startpos['line'],
  108. tree.startpos['column'],
  109. self.get_type(tree)))
  110. def replace_child_binary_op_with_call(self, tree, i=0):
  111. if i == -1:
  112. child = tree
  113. else:
  114. child = tree.get_tail()[i]
  115. if len(child.get_tail()) > 1:
  116. try:
  117. l, op, r = child.get_tail()
  118. except:
  119. # Something went wrong... this code is severely broken
  120. return
  121. l_type, r_type = self.get_type(l), self.get_type(r)
  122. if type(l_type) != type(r_type):
  123. print("Error: " + str(l_type) + " <-> " + str(r_type))
  124. raise RuntimeError(
  125. "{}:{}:{}: error: children were not casted".format(
  126. self.inputfiles[0],
  127. tree.startpos['line'],
  128. tree.startpos['column']
  129. ))
  130. call_name = SemanticsVisitor.call_name_binary(l_type, op)
  131. call_tree = self.func_call(call_name, [l, r], tree)
  132. try:
  133. self.visit(call_tree)
  134. except RuntimeError:
  135. call_signature = "{0} function {1}({2}, {2})".format(
  136. str(types_mv.Boolean()), call_name, l_type)
  137. raise RuntimeError(
  138. "{}:{}:{}: error: cannot perform {}: function '{}' is "
  139. "not found".format(
  140. self.inputfiles[0],
  141. tree.startpos['line'],
  142. tree.startpos['column'],
  143. child.head,
  144. call_signature))
  145. if i == -1:
  146. tree.head = call_tree.head
  147. tree.tail = call_tree.tail
  148. tree._tail = None
  149. else:
  150. tree.replace_child(child, call_tree)
  151. self.set_type(tree, self.get_type(tree.get_tail()[i]))
  152. def replace_child_unary_op_with_call(self, tree):
  153. child = tree.get_tail()[0]
  154. if child.head == "keep_sign":
  155. tree.replace_child(child, child.get_tail()[1])
  156. else:
  157. op, l = child.get_tail()
  158. l_type = self.get_type(l)
  159. call_name = SemanticsVisitor.call_name_unary(l_type, op)
  160. call_tree = self.func_call(call_name, [l], tree)
  161. try:
  162. self.visit(call_tree)
  163. except RuntimeError:
  164. call_signature = "{0} function {1}({2})".format(
  165. str(types_mv.Boolean()), call_name, l_type)
  166. raise RuntimeError(
  167. "{}:{}:{}: error: cannot perform {}: function '{}' is "
  168. "not found".format(
  169. self.inputfiles[0],
  170. tree.startpos['line'],
  171. tree.startpos['column'],
  172. child.head,
  173. call_signature))
  174. tree.replace_child(child, call_tree)
  175. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  176. def cast_binary_ops_arithmetic(self, tree):
  177. l, op, r = tree.get_tail()
  178. l_type, r_type = self.get_type(l), self.get_type(r)
  179. if type(l_type) != type(r_type): # if two different numeric types
  180. g_type = types_mv.generalize_arithmetic(l_type, r_type)
  181. self.perform_implicit_cast(tree, l, l_type, g_type)
  182. self.perform_implicit_cast(tree, r, r_type, g_type)
  183. def cast_binary_ops_logical(self, tree):
  184. l, op, r = tree.get_tail()
  185. l_type, r_type = self.get_type(l), self.get_type(r)
  186. self.perform_implicit_cast(tree, l, l_type, types_mv.Boolean())
  187. self.perform_implicit_cast(tree, r, r_type, types_mv.Boolean())
  188. def cast_unary_ops_arithmetic(self, tree):
  189. l = tree.get_tail()[1]
  190. l_type = self.get_type(l)
  191. p_type = self.promote_unary_ops_arithmetic(tree)
  192. self.perform_implicit_cast(tree, l, l_type, p_type)
  193. def func_call(self, name, params, old_tree):
  194. startpos = old_tree.startpos
  195. endpos = old_tree.endpos
  196. inputfile = old_tree.inputfile
  197. tree = hp.Tree(
  198. "func_call",
  199. [
  200. hp.Tree("rvalue",
  201. [
  202. hp.Tree("ID", [name], startpos, endpos, inputfile)
  203. ],
  204. startpos, endpos, inputfile),
  205. # Tokens have no impact on visit_func_call. So leave them out.
  206. ],
  207. startpos, endpos, inputfile)
  208. for p in params:
  209. self.replace_child_binary_op_with_call(p, -1)
  210. params = [hp.Tree("expression", [p], startpos, endpos, inputfile) for p in params]
  211. tree.tail.extend(params)
  212. return hp.Tree("expression", [tree], startpos, endpos, inputfile)
  213. @staticmethod
  214. def cast_name(from_type, to_type):
  215. to_t = str(to_type)[0].lower()
  216. cast_name = "cast_" + {"f": "float", "i": "integer", "b": "boolean", "s": "string"}[to_t]
  217. return cast_name
  218. def raise_implicit_cast_error(self, from_type, to_type, tree):
  219. cast_name = SemanticsVisitor.cast_name(from_type, to_type)
  220. cast_signature = "{} function {}({})".format(
  221. str(to_type), cast_name, str(from_type))
  222. raise RuntimeError(
  223. "{}:{}:{}: error: cannot perform implicit cast from '{}'"
  224. " to '{}': function '{}' is not found".format(
  225. self.inputfiles[0],
  226. tree.startpos['line'],
  227. tree.startpos['column'],
  228. str(to_type), str(from_type),
  229. cast_signature))
  230. def perform_implicit_cast(self, tree, child, from_type, to_type):
  231. if types_mv.Element in (type(from_type), type(to_type)):
  232. return
  233. if type(from_type) == type(to_type):
  234. return
  235. cast_name = SemanticsVisitor.cast_name(from_type, to_type)
  236. cast_tree = self.func_call(cast_name, [child], tree)
  237. try:
  238. self.visit(cast_tree)
  239. except RuntimeError:
  240. self.raise_implicit_cast_error(from_type, to_type, child)
  241. tree.replace_child(child, cast_tree)
  242. types = {
  243. "Integer": "integer",
  244. "Float": "float",
  245. "Boolean": "bool",
  246. "String": "string",
  247. "Action": "action",
  248. "Element": "element",
  249. "Type": "type"
  250. }
  251. binary_ops = {
  252. "OR": "or",
  253. "AND": "and",
  254. "EQ": "eq",
  255. "NEQ": "neq",
  256. "LT": "lt",
  257. "GT": "gt",
  258. "LE": "lte",
  259. "GE": "gte",
  260. "PLUS": "addition",
  261. "MINUS": "subtraction",
  262. "STAR": "multiplication",
  263. "SLASH": "division"
  264. }
  265. unary_ops = {
  266. "NOT": "not",
  267. "MINUS": "neg"
  268. }
  269. @staticmethod
  270. def call_name_binary(operand_type, operator):
  271. # String joins should also be possible
  272. if str(operand_type) == "String":
  273. if operator.head == "PLUS":
  274. return "string_join"
  275. if operator.head == "EQ":
  276. return "value_eq"
  277. elif operator.head == "NEQ":
  278. return "value_neq"
  279. call_name = "{}_{}".format(SemanticsVisitor.types[str(operand_type)],
  280. SemanticsVisitor.binary_ops[operator.head])
  281. return call_name
  282. @staticmethod
  283. def call_name_unary(operand_type, operator):
  284. call_name = "{}_{}".format(SemanticsVisitor.types[str(operand_type)],
  285. SemanticsVisitor.unary_ops[operator.head])
  286. return call_name
  287. def dump(self):
  288. return self.tree.get_text(with_implicit=True)
  289. # return "No code generation here"
  290. # a visit_* method for each non-terminal in the grammar
  291. def visit_start(self, tree):
  292. self.symbol_table.open_scope()
  293. self.inputfiles.append(tree.inputfile)
  294. for child in tree.get_tail():
  295. self.inputfiles[0] = child.inputfile
  296. self.declare_functions_visitor.visit(child)
  297. for child in tree.get_tail():
  298. self.inputfiles[0] = child.inputfile
  299. self.visit(child)
  300. self.inputfiles.pop()
  301. self.symbol_table.close_scope()
  302. self.tree = tree
  303. def visit_statement(self, tree):
  304. self.visit_children(tree)
  305. def visit_definition(self, tree):
  306. self.visit_vardecl(tree)
  307. def visit_vardecl(self, tree):
  308. type_spec = tree.get_child("type_specifier")
  309. var_id = tree.get_child("ID")
  310. var_type = types_mv.string_to_type(type_spec.get_text())
  311. var_name = var_id.get_text()
  312. symbol = st.Symbol(var_name, var_type,
  313. is_global=self.current_funcdecl is None)
  314. try:
  315. self.symbol_table.add(symbol)
  316. except Exception:
  317. raise RuntimeError(
  318. "{}:{}:{}: error: redeclaration of '{}'".format(
  319. self.inputfiles[0], tree.startpos['line'],
  320. tree.startpos['column'], var_name))
  321. self.set_symbol(tree, symbol)
  322. def visit_assignment(self, tree):
  323. self.visit_children(tree)
  324. self.check_assignment(tree)
  325. def visit_expression(self, tree):
  326. self.visit_children(tree)
  327. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  328. def visit_binary_operation(self, tree):
  329. self.visit_children(tree)
  330. self.replace_child_binary_op_with_call(tree)
  331. def visit_disjunction(self, tree):
  332. self.visit_children(tree)
  333. if len(tree.get_tail()) == 1:
  334. self.replace_child_binary_op_with_call(tree)
  335. else:
  336. self.replace_child_binary_op_with_call(tree, 2)
  337. self.cast_binary_ops_logical(tree)
  338. self.set_type(tree, types_mv.Boolean())
  339. def visit_conjunction(self, tree):
  340. self.visit_children(tree)
  341. if len(tree.get_tail()) == 1:
  342. self.replace_child_binary_op_with_call(tree)
  343. else:
  344. self.replace_child_binary_op_with_call(tree, 2)
  345. self.cast_binary_ops_logical(tree)
  346. self.set_type(tree, types_mv.Boolean())
  347. def visit_comparison(self, tree):
  348. self.visit_children(tree)
  349. if len(tree.get_tail()) == 1:
  350. self.replace_child_binary_op_with_call(tree)
  351. else:
  352. self.replace_child_binary_op_with_call(tree, 2)
  353. self.check_binary_ops_arithmetic(tree)
  354. self.cast_binary_ops_arithmetic(tree)
  355. self.set_type(tree, types_mv.Boolean())
  356. def visit_relation(self, tree):
  357. self.visit_children(tree)
  358. if len(tree.get_tail()) == 1:
  359. self.replace_child_binary_op_with_call(tree)
  360. else:
  361. self.replace_child_binary_op_with_call(tree, 2)
  362. self.check_binary_ops_arithmetic(tree)
  363. self.cast_binary_ops_arithmetic(tree)
  364. self.set_type(tree, types_mv.Boolean())
  365. def visit_sum(self, tree):
  366. self.visit_children(tree)
  367. if len(tree.get_tail()) == 1:
  368. self.replace_child_binary_op_with_call(tree)
  369. else:
  370. self.replace_child_binary_op_with_call(tree, 2)
  371. self.check_binary_ops_arithmetic(tree)
  372. self.cast_binary_ops_arithmetic(tree)
  373. # after the cast both parameters have the same (generalized) type:
  374. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  375. def visit_term(self, tree):
  376. self.visit_children(tree)
  377. if len(tree.get_tail()) == 1:
  378. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  379. else:
  380. self.check_binary_ops_arithmetic(tree)
  381. self.cast_binary_ops_arithmetic(tree)
  382. # after the cast both parameters have the same (generalized) type:
  383. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  384. def visit_factor(self, tree):
  385. self.visit_children(tree)
  386. if tree.get_child("primary") is not None:
  387. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  388. else:
  389. self.replace_child_unary_op_with_call(tree)
  390. def visit_logical_not(self, tree):
  391. self.visit_children(tree)
  392. l = tree.get_tail()[1]
  393. l_type = self.get_type(l)
  394. self.perform_implicit_cast(tree, l, l_type, types_mv.Boolean())
  395. self.set_type(tree, self.get_type(tree.get_tail()[1]))
  396. def visit_invert_sign(self, tree):
  397. self.visit_children(tree)
  398. self.check_unary_ops_arithmetic(tree, "minus")
  399. self.cast_unary_ops_arithmetic(tree)
  400. self.set_type(tree, self.get_type(tree.get_tail()[1]))
  401. def visit_keep_sign(self, tree):
  402. self.visit_children(tree)
  403. self.check_unary_ops_arithmetic(tree, "plus")
  404. self.cast_unary_ops_arithmetic(tree)
  405. self.set_type(tree, self.get_type(tree.get_tail()[1]))
  406. def visit_primary(self, tree):
  407. self.visit_children(tree)
  408. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  409. def visit_parenthesized(self, tree):
  410. self.visit_children(tree)
  411. self.set_type(tree, self.get_type(tree.get_tail()[1]))
  412. def visit_atomvalue(self, tree):
  413. self.visit_children(tree)
  414. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  415. def visit_type_specifier(self, tree):
  416. self.set_type(tree, types_mv.Type())
  417. def visit_actionname(self, tree):
  418. self.set_type(tree, types_mv.Action())
  419. def visit_string(self, tree):
  420. self.set_type(tree, types_mv.String())
  421. def visit_integer(self, tree):
  422. self.set_type(tree, types_mv.Integer())
  423. def visit_float(self, tree):
  424. self.set_type(tree, types_mv.Float())
  425. # there is no such rule in the grammar, we just avoid code duplicates
  426. def visit_id(self, tree):
  427. name = tree.get_text()
  428. #TODO this is set to the function returnvalue, even if we use the function pointer...
  429. try:
  430. symbol = self.symbol_table.get(name)
  431. except KeyError:
  432. raise RuntimeError("{}:{}:{}: error: '{}' is not declared".format(
  433. self.inputfiles[0], tree.startpos['line'],
  434. tree.startpos['column'], name))
  435. self.set_type(tree, symbol.type)
  436. self.set_symbol(tree, symbol)
  437. def visit_rvalue(self, tree):
  438. if len(tree.get_tail()) > 1:
  439. # Complex: dict_read operation needed
  440. child = tree.get_tail()[0]
  441. node = tree.get_child("rvalue")
  442. expression = tree.get_child("expression")
  443. operation = "dict_read"
  444. call_tree = self.func_call(operation, [node, expression], tree)
  445. self.visit(call_tree)
  446. tree.head = call_tree.head
  447. tree._tail = call_tree.tail
  448. tree.tail = call_tree.tail
  449. self.set_type(tree, self.get_type(node))
  450. else:
  451. # Simple
  452. self.visit_id(tree)
  453. def visit_lvalue(self, tree):
  454. self.visit_id(tree)
  455. def visit_func_call(self, tree):
  456. self.visit_children(tree)
  457. symbol = self.get_symbol(tree.get_tail()[0])
  458. self.set_type(tree, symbol.type)
  459. if not symbol.is_func():
  460. if isinstance(symbol.type, types_mv.Element):
  461. #sys.stderr.write("{}:{}:{}: warning: calling a variable of type "
  462. # "'Element'\n".format(self.inputfiles[0],
  463. # tree.startpos['line'],
  464. # tree.startpos['column'],
  465. # symbol.name))
  466. return # allow the call without knowing the declaration
  467. raise RuntimeError(
  468. "{}:{}:{}: error: '{}' is a variable of type '{}', not a "
  469. "function".format(self.inputfiles[0],
  470. tree.startpos['line'],
  471. tree.startpos['column'],
  472. symbol.name,
  473. symbol.type))
  474. expressions = tree.get_children("expression")
  475. if len(expressions) != len(symbol.params):
  476. raise RuntimeError(
  477. "{}:{}:{}: error: wrong number of arguments to "
  478. "function '{}'".format(self.inputfiles[0],
  479. tree.startpos['line'],
  480. tree.startpos['column'],
  481. symbol.signature()))
  482. """
  483. for i in range(len(expressions)):
  484. arg_type = self.get_type(expressions[i])
  485. param_type = symbol.params[i]
  486. if SemanticsVisitor.incompatible_types(arg_type, param_type):
  487. raise RuntimeError(
  488. "{}:{}:{}: error: argument {} has type '{}' instead of "
  489. "'{}', calling function '{}'".format(
  490. self.inputfiles[0],
  491. tree.startpos['line'],
  492. tree.startpos['column'],
  493. i + 1,
  494. str(arg_type),
  495. str(param_type),
  496. symbol.signature()))
  497. if type(arg_type) != type(param_type):
  498. self.perform_implicit_cast(tree, expressions[i], arg_type,
  499. param_type)
  500. """
  501. if symbol.name == "__input":
  502. tree.head = "input"
  503. elif symbol.name == "__output":
  504. tree.head = "output"
  505. def visit_input(self, tree):
  506. pass # no need to visit it again
  507. def visit_output(self, tree):
  508. pass # no need to visit it again
  509. def visit_dictionary(self, tree):
  510. self.set_type(tree, types_mv.Element)
  511. def visit_list(self, tree):
  512. self.set_type(tree, types_mv.Element)
  513. def visit_dict_item(self, tree):
  514. pass
  515. def visit_ifelse(self, tree):
  516. self.visit_children(tree)
  517. expressions = tree.get_children("expression")
  518. for expression in expressions:
  519. self.check_predicate(expression)
  520. def visit_while(self, tree):
  521. self.while_counter += 1
  522. self.visit_children(tree)
  523. self.while_counter -= 1
  524. expression = tree.get_child("expression")
  525. self.check_predicate(expression)
  526. def visit_block(self, tree):
  527. self.symbol_table.open_scope()
  528. self.visit_children(tree)
  529. self.symbol_table.close_scope()
  530. def visit_func_body(self, tree):
  531. self.visit_children(tree)
  532. def visit_funcdecl(self, tree):
  533. # here we only visit the body cause the declaration is already done
  534. # by declare_functions_visitor
  535. if tree.get_child('func_body') is not None:
  536. self.current_funcdecl = tree
  537. self.symbol_table.open_scope()
  538. self.visit_children(tree)
  539. self.symbol_table.close_scope()
  540. self.current_funcdecl = None
  541. def visit_parameter(self, tree):
  542. param_id = tree.get_child("ID")
  543. type_spec = tree.get_child("type_specifier")
  544. param_type = types_mv.string_to_type(type_spec.get_text())
  545. param_name = param_id.get_text()
  546. symbol = st.Symbol(param_name, param_type, is_global=False)
  547. try:
  548. self.symbol_table.add(symbol)
  549. except Exception:
  550. raise RuntimeError(
  551. "{}:{}:{}: error: redeclaration of '{}'".format(
  552. self.inputfiles[0], tree.startpos['line'],
  553. tree.startpos['column'], param_name))
  554. self.set_symbol(tree, symbol)
  555. def visit_return(self, tree):
  556. self.visit_children(tree)
  557. self.check_return(tree)
  558. def visit_bool(self, tree):
  559. self.set_type(tree, types_mv.Boolean())
  560. def visit_break(self, tree):
  561. if self.while_counter == 0:
  562. raise RuntimeError(
  563. "{}:{}:{}: error: break outside of while".format(
  564. self.inputfiles[0], tree.startpos['line'],
  565. tree.startpos['column']))
  566. def visit_continue(self, tree):
  567. if self.while_counter == 0:
  568. raise RuntimeError(
  569. "{}:{}:{}: error: continue outside of while".format(
  570. self.inputfiles[0], tree.startpos['line'],
  571. tree.startpos['column']))