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

Method struct_elem_declaration

src/Parser.py:2441–2472  ·  view source on GitHub ↗
(self, names: List[str], og_struct: Structure)

Source from the content-addressed store, hash-verified

2439 return Node(NodeKind.STR_LIT, type_of_lit(NodeKind.STR_LIT), f'"{value}"')
2440
2441 def struct_elem_declaration(self, names: List[str], og_struct: Structure) -> Optional[Node]:
2442 for new_name, og_name, var_type in zip(names, og_struct.elem_names, og_struct.elem_types):
2443 meta_kind = var_type.meta_kind()
2444 Def.context_map[new_name] = Context(new_name, curr_scope())
2445 Def.ident_map[new_name] = meta_kind
2446
2447 if meta_kind == VariableMetaKind.STRUCT:
2448 Def.var_off += size_of(var_type.ckind)
2449
2450 struct = Def.struct_map.get(var_type.name)
2451 self.struct_elem_declaration(struct.elem_names, struct)
2452
2453 elif meta_kind in (VariableMetaKind.PRIM, VariableMetaKind.BOOL, VariableMetaKind.ANY):
2454 Def.var_off += size_of(var_type.ckind)
2455 Def.var_map[new_name] = Def.var_map[og_name]
2456 Def.var_map[new_name].off = Def.var_off
2457
2458 elif meta_kind == VariableMetaKind.ARR:
2459 arr = Def.arr_map.get(og_name)
2460 elem_cnt = arr.elem_cnt
2461 elem_type = arr.elem_type
2462 Def.var_off += size_of(elem_type.ckind) * elem_cnt
2463 Def.arr_map[new_name] = arr
2464
2465 elif meta_kind in (VariableMetaKind.PTR, VariableMetaKind.REF):
2466 ptr = Def.ptr_map.get(og_name)
2467 Def.var_off += size_of(var_type.ckind)
2468 Def.ptr_map[new_name] = ptr
2469
2470 else:
2471 print_error('struct_elem_declaration',
2472 f'Unknown meta kind {meta_kind} (type="{rev_type_of(var_type)}", new_name="{new_name}", og_name="{og_name}")', self)
2473
2474 def declare(self, full_name: str, var_type: VariableType, elem_ckind: VariableCompKind = default_ckind, elem_cnt: int = 0, is_local: bool = True, is_struct: bool = False, init_node: Optional[Node] = None):
2475 meta_kind = var_type.meta_kind()

Callers 3

for_statementMethod · 0.95
fun_declarationMethod · 0.95
declareMethod · 0.95

Calls 6

ContextClass · 0.90
curr_scopeFunction · 0.90
size_ofFunction · 0.90
print_errorFunction · 0.90
rev_type_ofFunction · 0.90
meta_kindMethod · 0.80

Tested by

no test coverage detected