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

Method declaration

src/Parser.py:2591–2689  ·  view source on GitHub ↗
(self, is_struct: bool = False)

Source from the content-addressed store, hash-verified

2589 f'Unknown meta kind {meta_kind}', self)
2590
2591 def declaration(self, is_struct: bool = False) -> Optional[Node]:
2592 name = self.match_token(TokenKind.IDENT).value
2593 var_name = full_name_of_var(
2594 name, force_local=True, exhaustive_match=False)
2595
2596 is_local = Def.fun_name != ''
2597 full_name = var_name if is_local else full_name_of_var(name, True)
2598 is_implicit = self.curr_token().kind != TokenKind.COLON
2599
2600 if Def.macro_name != '':
2601 Def.ident_map[full_name] = VariableMetaKind.ANY
2602 macro = Def.macro_map.get(Def.macro_name)
2603 macro.local_names.append(full_name)
2604 return None
2605 # print_error('declaration',
2606 # f'Variable declarations within macro ({Def.macro_name}) are not allowed', self)
2607
2608 elem_cnt = 0
2609 var_type = default_type
2610 meta_kind = VariableMetaKind.PRIM
2611 if not is_implicit:
2612 self.match_token(TokenKind.COLON)
2613
2614 if self.curr_token().kind == TokenKind.KW_FUN:
2615 self.match_token(TokenKind.KW_FUN)
2616 sig = self.fun_declaration(is_sig=True)
2617 Def.sig_map[full_name] = sig
2618
2619 var_type = VariableType(sig_ckind, name=full_name)
2620 meta_kind = var_type.meta_kind()
2621 elem_cnt = 0
2622 else:
2623 parsed_type = self.parse_type()
2624 var_type = parsed_type.var_type
2625 elem_cnt = parsed_type.elem_cnt
2626 meta_kind = var_type.meta_kind()
2627
2628 if not self.no_more_tokens():
2629 self.match_token(TokenKind.ASSIGN)
2630
2631 if is_implicit:
2632 if self.curr_token().kind == TokenKind.LBRACE:
2633 print_error(
2634 'declaration',
2635 'Implicit array declaration is not permitted.', self)
2636
2637 node = self.token_list_to_tree()
2638 var_type = node.ntype
2639 meta_kind = var_type.meta_kind()
2640
2641 if var_type.ckind == sig_ckind:
2642 print_error(
2643 'declaration',
2644 'Implicit signature declaration is not permitted.', self)
2645
2646 if var_type.ckind == void_ckind:
2647 print_error('declaration',
2648 'Declaration of implicit void primitive is not allowed.', self)

Callers 2

statementMethod · 0.95
struct_declarationMethod · 0.95

Calls 15

match_tokenMethod · 0.95
curr_tokenMethod · 0.95
fun_declarationMethod · 0.95
meta_kindMethod · 0.95
parse_typeMethod · 0.95
no_more_tokensMethod · 0.95
token_list_to_treeMethod · 0.95
check_identMethod · 0.95
declareMethod · 0.95
full_name_of_varFunction · 0.90
VariableTypeClass · 0.90
print_errorFunction · 0.90

Tested by

no test coverage detected