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

Method struct_declaration

src/Parser.py:2108–2187  ·  view source on GitHub ↗
(self, is_extern: bool = False)

Source from the content-addressed store, hash-verified

2106 return node if not is_generic else None
2107
2108 def struct_declaration(self, is_extern: bool = False) -> Optional[Node]:
2109 #! BUG: Nested structures
2110 name = self.match_token(TokenKind.IDENT).value
2111 vtype = VariableType(struct_ckind, name=name)
2112 Def.struct_map[name] = Structure(name, vtype, [], [])
2113 Def.type_map[name] = vtype
2114
2115 if is_extern:
2116 return None
2117
2118 # Parses the structure body
2119 Def.struct_name = name
2120 self.next_line()
2121
2122 node = None
2123 while not self.no_more_lines() and self.curr_token().kind != TokenKind.KW_END:
2124 if node is None:
2125 node = self.declaration(is_struct=True)
2126 else:
2127 node = Node(NodeKind.GLUE, void_type,
2128 '', node, self.declaration(is_struct=True))
2129 self.next_line()
2130
2131 # Creates a constructor-like function
2132 # ? Requires some solid refactoring
2133 # ? Duplicated from Parser.fun_declaration
2134 struct = Def.struct_map.get(name)
2135 tmp_name = f'{struct.name}_tmp'
2136 fun_name = name
2137
2138 sig_name = compute_signature(fun_name, struct.elem_types)
2139
2140 # Creates parameter names
2141 full_arg_names = []
2142 Def.fun_name_list.append(sig_name)
2143 for arg_name in struct.elem_names:
2144 full_arg_names.append(full_name_of_var(
2145 arg_name, exhaustive_match=False))
2146 Def.fun_name_list.pop()
2147
2148 signature = FunctionSignature(sig_name, len(
2149 struct.elem_names), full_arg_names, struct.elem_types, struct.vtype, is_extern, False, None)
2150 Def.fun_sig_map[sig_name] = fun_name
2151
2152 Def.context_map[fun_name] = Context(fun_name, curr_scope())
2153 Def.ident_map[fun_name] = VariableMetaKind.FUN
2154 Def.fun_map[fun_name] = Function(fun_name, len(struct.elem_names), struct.elem_names, struct.elem_types,
2155 struct.vtype, 0, False, False, [signature])
2156
2157 def inject_decl(tpl: Tuple[str, str, VariableType]) -> Node:
2158 #!BUG: No array declaration
2159 new_name, og_name, var_type = tpl
2160 og_name = full_name_of_var(og_name)
2161
2162 elem_acc = Node(NodeKind.ELEM_ACC, struct.vtype, '', Node(
2163 NodeKind.IDENT, struct.vtype, tmp_name), Node(NodeKind.IDENT, var_type, new_name))
2164
2165 node = Node(NodeKind.IDENT, var_type, og_name)

Callers 1

statementMethod · 0.95

Calls 15

match_tokenMethod · 0.95
next_lineMethod · 0.95
no_more_linesMethod · 0.95
curr_tokenMethod · 0.95
declarationMethod · 0.95
VariableTypeClass · 0.90
StructureClass · 0.90
NodeClass · 0.90
compute_signatureFunction · 0.90
full_name_of_varFunction · 0.90
FunctionSignatureClass · 0.90
ContextClass · 0.90

Tested by

no test coverage detected