semantics_visitor.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. import hutnparser as hp
  2. import symbol_table as st
  3. import sys
  4. import types_mv
  5. from declare_functions_visitor import DeclareFunctionsVisitor
  6. from 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. from_t = str(from_type)[0].lower()
  216. to_t = str(to_type)[0].lower()
  217. cast_name = "cast_{}2{}".format(from_t, to_t)
  218. return cast_name
  219. def raise_implicit_cast_error(self, from_type, to_type, tree):
  220. cast_name = SemanticsVisitor.cast_name(from_type, to_type)
  221. cast_signature = "{} function {}({})".format(
  222. str(to_type), cast_name, str(from_type))
  223. raise RuntimeError(
  224. "{}:{}:{}: error: cannot perform implicit cast from '{}'"
  225. " to '{}': function '{}' is not found".format(
  226. self.inputfiles[0],
  227. tree.startpos['line'],
  228. tree.startpos['column'],
  229. str(to_type), str(from_type),
  230. cast_signature))
  231. def perform_implicit_cast(self, tree, child, from_type, to_type):
  232. if types_mv.Element in (type(from_type), type(to_type)):
  233. return
  234. if type(from_type) == type(to_type):
  235. return
  236. cast_name = SemanticsVisitor.cast_name(from_type, to_type)
  237. cast_tree = self.func_call(cast_name, [child], tree)
  238. try:
  239. self.visit(cast_tree)
  240. except RuntimeError:
  241. self.raise_implicit_cast_error(from_type, to_type, child)
  242. tree.replace_child(child, cast_tree)
  243. types = {
  244. "Integer": "integer",
  245. "Float": "float",
  246. "Boolean": "bool",
  247. "String": "string",
  248. "Action": "action",
  249. "Element": "element",
  250. "Type": "type"
  251. }
  252. binary_ops = {
  253. "OR": "or",
  254. "AND": "and",
  255. "EQ": "eq",
  256. "NEQ": "neq",
  257. "LT": "lt",
  258. "GT": "gt",
  259. "LE": "lte",
  260. "GE": "gte",
  261. "PLUS": "addition",
  262. "MINUS": "subtraction",
  263. "STAR": "multiplication",
  264. "SLASH": "division"
  265. }
  266. unary_ops = {
  267. "NOT": "not",
  268. "MINUS": "neg"
  269. }
  270. @staticmethod
  271. def call_name_binary(operand_type, operator):
  272. # String joins should also be possible
  273. if str(operand_type) == "String":
  274. if operator.head == "PLUS":
  275. return "string_join"
  276. if operator.head == "EQ":
  277. return "value_eq"
  278. elif operator.head == "NEQ":
  279. return "value_neq"
  280. call_name = "{}_{}".format(SemanticsVisitor.types[str(operand_type)],
  281. SemanticsVisitor.binary_ops[operator.head])
  282. return call_name
  283. @staticmethod
  284. def call_name_unary(operand_type, operator):
  285. call_name = "{}_{}".format(SemanticsVisitor.types[str(operand_type)],
  286. SemanticsVisitor.unary_ops[operator.head])
  287. return call_name
  288. def dump(self):
  289. return self.tree.get_text(with_implicit=True)
  290. # return "No code generation here"
  291. # a visit_* method for each non-terminal in the grammar
  292. def visit_start(self, tree):
  293. self.symbol_table.open_scope()
  294. self.inputfiles.append(tree.inputfile)
  295. for child in tree.get_tail():
  296. self.inputfiles[0] = child.inputfile
  297. self.declare_functions_visitor.visit(child)
  298. for child in tree.get_tail():
  299. self.inputfiles[0] = child.inputfile
  300. self.visit(child)
  301. self.inputfiles.pop()
  302. self.symbol_table.close_scope()
  303. self.tree = tree
  304. def visit_statement(self, tree):
  305. self.visit_children(tree)
  306. def visit_definition(self, tree):
  307. self.visit_vardecl(tree)
  308. def visit_vardecl(self, tree):
  309. type_spec = tree.get_child("type_specifier")
  310. var_id = tree.get_child("ID")
  311. var_type = types_mv.string_to_type(type_spec.get_text())
  312. var_name = var_id.get_text()
  313. symbol = st.Symbol(var_name, var_type,
  314. is_global=self.current_funcdecl is None)
  315. try:
  316. self.symbol_table.add(symbol)
  317. except Exception:
  318. raise RuntimeError(
  319. "{}:{}:{}: error: redeclaration of '{}'".format(
  320. self.inputfiles[0], tree.startpos['line'],
  321. tree.startpos['column'], var_name))
  322. self.set_symbol(tree, symbol)
  323. def visit_assignment(self, tree):
  324. self.visit_children(tree)
  325. self.check_assignment(tree)
  326. def visit_expression(self, tree):
  327. self.visit_children(tree)
  328. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  329. def visit_binary_operation(self, tree):
  330. self.visit_children(tree)
  331. self.replace_child_binary_op_with_call(tree)
  332. def visit_disjunction(self, tree):
  333. self.visit_children(tree)
  334. if len(tree.get_tail()) == 1:
  335. self.replace_child_binary_op_with_call(tree)
  336. else:
  337. self.replace_child_binary_op_with_call(tree, 2)
  338. self.cast_binary_ops_logical(tree)
  339. self.set_type(tree, types_mv.Boolean())
  340. def visit_conjunction(self, tree):
  341. self.visit_children(tree)
  342. if len(tree.get_tail()) == 1:
  343. self.replace_child_binary_op_with_call(tree)
  344. else:
  345. self.replace_child_binary_op_with_call(tree, 2)
  346. self.cast_binary_ops_logical(tree)
  347. self.set_type(tree, types_mv.Boolean())
  348. def visit_comparison(self, tree):
  349. self.visit_children(tree)
  350. if len(tree.get_tail()) == 1:
  351. self.replace_child_binary_op_with_call(tree)
  352. else:
  353. self.replace_child_binary_op_with_call(tree, 2)
  354. self.check_binary_ops_arithmetic(tree)
  355. self.cast_binary_ops_arithmetic(tree)
  356. self.set_type(tree, types_mv.Boolean())
  357. def visit_relation(self, tree):
  358. self.visit_children(tree)
  359. if len(tree.get_tail()) == 1:
  360. self.replace_child_binary_op_with_call(tree)
  361. else:
  362. self.replace_child_binary_op_with_call(tree, 2)
  363. self.check_binary_ops_arithmetic(tree)
  364. self.cast_binary_ops_arithmetic(tree)
  365. self.set_type(tree, types_mv.Boolean())
  366. def visit_sum(self, tree):
  367. self.visit_children(tree)
  368. if len(tree.get_tail()) == 1:
  369. self.replace_child_binary_op_with_call(tree)
  370. else:
  371. self.replace_child_binary_op_with_call(tree, 2)
  372. self.check_binary_ops_arithmetic(tree)
  373. self.cast_binary_ops_arithmetic(tree)
  374. # after the cast both parameters have the same (generalized) type:
  375. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  376. def visit_term(self, tree):
  377. self.visit_children(tree)
  378. if len(tree.get_tail()) == 1:
  379. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  380. else:
  381. self.check_binary_ops_arithmetic(tree)
  382. self.cast_binary_ops_arithmetic(tree)
  383. # after the cast both parameters have the same (generalized) type:
  384. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  385. def visit_factor(self, tree):
  386. self.visit_children(tree)
  387. if tree.get_child("primary") is not None:
  388. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  389. else:
  390. self.replace_child_unary_op_with_call(tree)
  391. def visit_logical_not(self, tree):
  392. self.visit_children(tree)
  393. l = tree.get_tail()[1]
  394. l_type = self.get_type(l)
  395. self.perform_implicit_cast(tree, l, l_type, types_mv.Boolean())
  396. self.set_type(tree, self.get_type(tree.get_tail()[1]))
  397. def visit_invert_sign(self, tree):
  398. self.visit_children(tree)
  399. self.check_unary_ops_arithmetic(tree, "minus")
  400. self.cast_unary_ops_arithmetic(tree)
  401. self.set_type(tree, self.get_type(tree.get_tail()[1]))
  402. def visit_keep_sign(self, tree):
  403. self.visit_children(tree)
  404. self.check_unary_ops_arithmetic(tree, "plus")
  405. self.cast_unary_ops_arithmetic(tree)
  406. self.set_type(tree, self.get_type(tree.get_tail()[1]))
  407. def visit_primary(self, tree):
  408. self.visit_children(tree)
  409. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  410. def visit_parenthesized(self, tree):
  411. self.visit_children(tree)
  412. self.set_type(tree, self.get_type(tree.get_tail()[1]))
  413. def visit_atomvalue(self, tree):
  414. self.visit_children(tree)
  415. self.set_type(tree, self.get_type(tree.get_tail()[0]))
  416. def visit_type_specifier(self, tree):
  417. self.set_type(tree, types_mv.Type())
  418. def visit_actionname(self, tree):
  419. self.set_type(tree, types_mv.Action())
  420. def visit_string(self, tree):
  421. self.set_type(tree, types_mv.String())
  422. def visit_integer(self, tree):
  423. self.set_type(tree, types_mv.Integer())
  424. def visit_float(self, tree):
  425. self.set_type(tree, types_mv.Float())
  426. # there is no such rule in the grammar, we just avoid code duplicates
  427. def visit_id(self, tree):
  428. name = tree.get_text()
  429. #TODO this is set to the function returnvalue, even if we use the function pointer...
  430. try:
  431. symbol = self.symbol_table.get(name)
  432. except KeyError:
  433. raise RuntimeError("{}:{}:{}: error: '{}' is not declared".format(
  434. self.inputfiles[0], tree.startpos['line'],
  435. tree.startpos['column'], name))
  436. self.set_type(tree, symbol.type)
  437. self.set_symbol(tree, symbol)
  438. def visit_rvalue(self, tree):
  439. if len(tree.get_tail()) > 1:
  440. # Complex: dict_read operation needed
  441. child = tree.get_tail()[0]
  442. node = tree.get_child("rvalue")
  443. expression = tree.get_child("expression")
  444. operation = "dict_read"
  445. call_tree = self.func_call(operation, [node, expression], tree)
  446. self.visit(call_tree)
  447. tree.head = call_tree.head
  448. tree._tail = call_tree.tail
  449. tree.tail = call_tree.tail
  450. self.set_type(tree, self.get_type(node))
  451. else:
  452. # Simple
  453. self.visit_id(tree)
  454. def visit_lvalue(self, tree):
  455. self.visit_id(tree)
  456. def visit_func_call(self, tree):
  457. self.visit_children(tree)
  458. symbol = self.get_symbol(tree.get_tail()[0])
  459. self.set_type(tree, symbol.type)
  460. if not symbol.is_func():
  461. if isinstance(symbol.type, types_mv.Element):
  462. #sys.stderr.write("{}:{}:{}: warning: calling a variable of type "
  463. # "'Element'\n".format(self.inputfiles[0],
  464. # tree.startpos['line'],
  465. # tree.startpos['column'],
  466. # symbol.name))
  467. return # allow the call without knowing the declaration
  468. raise RuntimeError(
  469. "{}:{}:{}: error: '{}' is a variable of type '{}', not a "
  470. "function".format(self.inputfiles[0],
  471. tree.startpos['line'],
  472. tree.startpos['column'],
  473. symbol.name,
  474. symbol.type))
  475. expressions = tree.get_children("expression")
  476. if len(expressions) != len(symbol.params):
  477. raise RuntimeError(
  478. "{}:{}:{}: error: wrong number of arguments to "
  479. "function '{}'".format(self.inputfiles[0],
  480. tree.startpos['line'],
  481. tree.startpos['column'],
  482. symbol.signature()))
  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. if symbol.name == "__input":
  501. tree.head = "input"
  502. elif symbol.name == "__output":
  503. tree.head = "output"
  504. def visit_input(self, tree):
  505. pass # no need to visit it again
  506. def visit_output(self, tree):
  507. pass # no need to visit it again
  508. def visit_dictionary(self, tree):
  509. self.set_type(tree, types_mv.Element)
  510. def visit_list(self, tree):
  511. self.set_type(tree, types_mv.Element)
  512. def visit_dict_item(self, tree):
  513. pass
  514. def visit_ifelse(self, tree):
  515. self.visit_children(tree)
  516. expressions = tree.get_children("expression")
  517. for expression in expressions:
  518. self.check_predicate(expression)
  519. def visit_while(self, tree):
  520. self.while_counter += 1
  521. self.visit_children(tree)
  522. self.while_counter -= 1
  523. expression = tree.get_child("expression")
  524. self.check_predicate(expression)
  525. def visit_block(self, tree):
  526. self.symbol_table.open_scope()
  527. self.visit_children(tree)
  528. self.symbol_table.close_scope()
  529. def visit_func_body(self, tree):
  530. self.visit_children(tree)
  531. def visit_funcdecl(self, tree):
  532. # here we only visit the body cause the declaration is already done
  533. # by declare_functions_visitor
  534. if tree.get_child('func_body') is not None:
  535. self.current_funcdecl = tree
  536. self.symbol_table.open_scope()
  537. self.visit_children(tree)
  538. self.symbol_table.close_scope()
  539. self.current_funcdecl = None
  540. def visit_parameter(self, tree):
  541. param_id = tree.get_child("ID")
  542. type_spec = tree.get_child("type_specifier")
  543. param_type = types_mv.string_to_type(type_spec.get_text())
  544. param_name = param_id.get_text()
  545. symbol = st.Symbol(param_name, param_type, is_global=False)
  546. try:
  547. self.symbol_table.add(symbol)
  548. except Exception:
  549. raise RuntimeError(
  550. "{}:{}:{}: error: redeclaration of '{}'".format(
  551. self.inputfiles[0], tree.startpos['line'],
  552. tree.startpos['column'], param_name))
  553. self.set_symbol(tree, symbol)
  554. def visit_return(self, tree):
  555. self.visit_children(tree)
  556. self.check_return(tree)
  557. def visit_bool(self, tree):
  558. self.set_type(tree, types_mv.Boolean())
  559. def visit_break(self, tree):
  560. if self.while_counter == 0:
  561. raise RuntimeError(
  562. "{}:{}:{}: error: break outside of while".format(
  563. self.inputfiles[0], tree.startpos['line'],
  564. tree.startpos['column']))
  565. def visit_continue(self, tree):
  566. if self.while_counter == 0:
  567. raise RuntimeError(
  568. "{}:{}:{}: error: continue outside of while".format(
  569. self.inputfiles[0], tree.startpos['line'],
  570. tree.startpos['column']))