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

Method fun_declaration

src/Parser.py:1819–2106  ·  view source on GitHub ↗
(self, is_extern: bool = False, in_generic: bool = False, is_sig: bool = False)

Source from the content-addressed store, hash-verified

1817 return destr_stmts
1818
1819 def fun_declaration(self, is_extern: bool = False, in_generic: bool = False, is_sig: bool = False) -> Optional[Node]:
1820 # Needed for generics
1821 parser = Parser(self)
1822
1823 # Needed for extern
1824 name = ''
1825 full_name = ''
1826 if not is_sig:
1827 name = self.match_token(TokenKind.IDENT).value
1828 full_name = full_name_of_fun(name, force_global=True)
1829
1830 # Needed for extern
1831 is_generic = False
1832 is_variadic = False
1833 arg_names: list[str] = []
1834 arg_types: list[VariableType] = []
1835 elem_types: list[VariableType] = []
1836 elem_cnts: list[int] = []
1837
1838 # Generic type parser
1839 gen_names = []
1840 if not self.no_more_tokens() and self.curr_token().kind == TokenKind.LBRACE:
1841 is_generic = True
1842
1843 self.match_token(TokenKind.LBRACE)
1844 while not self.no_more_tokens() and self.curr_token().kind != TokenKind.RBRACE:
1845 gen_name = self.match_token(TokenKind.IDENT).value
1846 gen_names.append(gen_name)
1847
1848 if not self.no_more_tokens() and self.curr_token().kind != TokenKind.RBRACE:
1849 self.match_token(TokenKind.COMMA)
1850 self.match_token(TokenKind.RBRACE)
1851
1852 if not is_sig and not is_generic and Def.fun_name != '':
1853 print_error('fun_declaration',
1854 'Local functions are not allowed', self)
1855
1856 # Defines the generic type params as aliases of a generic type
1857 if in_generic:
1858 is_generic = False
1859 elif is_generic:
1860 for gen_name in gen_names:
1861 gen_type = VariableType(gen_ckind, name=gen_name)
1862 Def.type_map[gen_name] = gen_type
1863
1864 has_args = not self.no_more_tokens() and self.curr_token().kind == TokenKind.LPAREN
1865 if has_args:
1866 self.match_token(TokenKind.LPAREN)
1867 while self.curr_token().kind not in (TokenKind.RPAREN, TokenKind.PER_FUN):
1868 arg_name = self.match_token(TokenKind.IDENT).value
1869 self.match_token(TokenKind.COLON)
1870
1871 type_str = ''
1872 arg_type = None
1873 elem_type = None
1874 elem_cnt = 0
1875 if not self.no_more_tokens() and self.curr_token().kind == TokenKind.KW_FUN:
1876 self.next_token()

Callers 3

_gen_fun_callMethod · 0.95
statementMethod · 0.95
declarationMethod · 0.95

Calls 15

match_tokenMethod · 0.95
no_more_tokensMethod · 0.95
curr_tokenMethod · 0.95
next_tokenMethod · 0.95
match_token_fromMethod · 0.95
check_identMethod · 0.95
meta_kindMethod · 0.95
next_lineMethod · 0.95
compound_statementMethod · 0.95
inject_destrMethod · 0.95
full_name_of_funFunction · 0.90

Tested by

no test coverage detected