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

Method parse_type

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

Source from the content-addressed store, hash-verified

1177 return gen_names
1178
1179 def parse_type(self) -> ParsedType:
1180 meta_kind = VariableMetaKind.PRIM
1181 type_str = self.curr_token().value
1182 var_type = type_of(type_str)
1183 elem_type = var_type
1184 elem_cnt = 0
1185 self.next_token()
1186
1187 # Fixes bug in when the argumen gets the type thru an alias
1188 if var_type.ckind in (ptr_ckind, ref_ckind):
1189 elem_type = VariableType(
1190 var_type.elem_ckind, name=var_type.name)
1191
1192 if not self.no_more_tokens() and self.curr_token().kind == TokenKind.LBRACE:
1193 self.next_token()
1194 meta_kind = VariableMetaKind.ARR
1195
1196 elem_cnt = int(self.match_token(TokenKind.INT_LIT).value)
1197 self.match_token(TokenKind.RBRACE)
1198
1199 if not self.no_more_tokens() and self.curr_token().kind == TokenKind.BIT_AND:
1200 print_error('parse_type',
1201 'Fixed-size references are not allowed', parser=self)
1202
1203 if not self.no_more_tokens() and self.curr_token().kind == TokenKind.AND:
1204 self.next_token()
1205 meta_kind = VariableMetaKind.RV_REF
1206
1207 elif not self.no_more_tokens() and self.curr_token().kind == TokenKind.BIT_AND:
1208 self.next_token()
1209 meta_kind = VariableMetaKind.REF
1210
1211 elif not self.no_more_tokens() and self.curr_token().kind == TokenKind.MULT:
1212 self.next_token()
1213 meta_kind = VariableMetaKind.PTR
1214
1215 # Type correction
1216 elem_ckind = var_type.ckind
1217 if meta_kind == VariableMetaKind.ARR:
1218 var_type = VariableType(arr_ckind, elem_ckind)
1219 if meta_kind == VariableMetaKind.BOOL:
1220 var_type = VariableType(bool_ckind, elem_ckind)
1221 if meta_kind == VariableMetaKind.PTR:
1222 var_type = VariableType(
1223 ptr_ckind, elem_ckind, name=var_type.name)
1224 if meta_kind == VariableMetaKind.REF:
1225 var_type = VariableType(
1226 ref_ckind, elem_ckind, name=var_type.name)
1227 if meta_kind == VariableMetaKind.RV_REF:
1228 var_type = VariableType(
1229 rv_ref_ckind, elem_ckind, name=var_type.name)
1230
1231 return ParsedType(var_type, elem_type, elem_cnt)
1232
1233 def parse_type_list(self):
1234 pass

Callers 1

declarationMethod · 0.95

Calls 8

curr_tokenMethod · 0.95
next_tokenMethod · 0.95
no_more_tokensMethod · 0.95
match_tokenMethod · 0.95
type_ofFunction · 0.90
VariableTypeClass · 0.90
print_errorFunction · 0.90
ParsedTypeClass · 0.90

Tested by

no test coverage detected