MCPcopy Create free account
hub / github.com/NICUP14/MiniLang / ret_statement

Method ret_statement

src/Parser.py:1659–1774  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1657 return node
1658
1659 def ret_statement(self) -> Optional[Node]:
1660 if Def.macro_name != '':
1661 if Def.fun_name == '':
1662 print_error('ret_statement',
1663 'Return not allowed in macro.', parser=self)
1664
1665 return None
1666
1667 if Def.fun_name == '':
1668 print_error('ret_statement',
1669 'Cannot return from outside a function', self)
1670
1671 sig_name = Def.fun_name
1672 fun_name = Def.fun_name
1673 if fun_name in Def.fun_sig_map:
1674 fun_name = Def.fun_sig_map.get(fun_name)
1675
1676 fun = Def.fun_map.get(fun_name)
1677
1678 sig = None
1679 for signature in fun.signatures:
1680 if signature.name == sig_name:
1681 sig = signature
1682
1683 destr_stmts = self.inject_destr(
1684 fun_name, sig_name, sig.arg_names, sig.arg_types)
1685
1686 ret_void = self.no_more_tokens()
1687 if sig.ret_type == Def.void_type or ret_void:
1688 if not self.no_more_tokens():
1689 print_error('ret_statement',
1690 'Cannot return a non-void value from a void function', self)
1691
1692 if not type_compatible(NodeKind.FUN_CALL, void_type.ckind, sig.ret_type.ckind):
1693 print_error('ret_statement',
1694 f'The return type differs from the function\'s ({rev_type_of(void_type)} != {rev_type_of(sig.ret_type)})', self)
1695
1696 if Def.fun_has_ret and not type_compatible(NodeKind.FUN_CALL, void_type.ckind, Def.fun_ret_type.ckind):
1697 print_error('ret_statement',
1698 f'Cannot deduce implicit return value of {fun_name} ({rev_type_of(Def.fun_ret_type)} != {rev_type_of(void_type)})', parser=self)
1699
1700 Def.fun_has_ret = True
1701
1702 node = Node(NodeKind.RET, void_type, '')
1703 if destr_stmts:
1704 node = glue_statements([destr_stmts, node])
1705
1706 return node
1707 else:
1708 node = self.token_list_to_tree()
1709
1710 if node.ntype == void_type:
1711 print_error('ret_statment',
1712 f'Cannot return expression of void type in {Def.fun_name}', parser=self)
1713
1714 if not type_compatible(NodeKind.FUN_CALL, node.ntype.ckind, sig.ret_type.ckind):
1715 print_error('ret_statement',
1716 f'The return type differs from the function\'s ({rev_type_of(node.ntype)} != {rev_type_of(sig.ret_type)})', self)

Callers 1

statementMethod · 0.95

Calls 15

inject_destrMethod · 0.95
no_more_tokensMethod · 0.95
token_list_to_treeMethod · 0.95
refMethod · 0.95
declareMethod · 0.95
print_errorFunction · 0.90
type_compatibleFunction · 0.90
rev_type_ofFunction · 0.90
NodeClass · 0.90
glue_statementsFunction · 0.90
full_name_of_varFunction · 0.90
_find_signatureFunction · 0.90

Tested by

no test coverage detected