Moved structs into separate function

This commit is contained in:
Kyler 2024-02-28 00:50:47 -07:00
parent cccd80bec7
commit 07a5fef918
1 changed files with 61 additions and 70 deletions

View File

@ -704,27 +704,10 @@ def _literal_map(literal: (
elif isinstance(literal, lexer.StringLiteral):
return StringLiteral(literal.value)
def enumeration_syntactical_analyzer(tokens: list[lexer.Token]) -> EnumBlock:
pass
def function_syntactical_analyzer(tokens: list[lexer.Token]) -> FunctionBlock:
pass
def struct_syntactical_analyzer(tokens: list[lexer.Token]) -> StructBlock:
pass
def file_syntactical_analyzer(tokens: list[lexer.Token]) -> File:
children: list[Directive | StructBlock | FunctionBlock | EnumBlock] = []
while tokens:
token = tokens.pop(0)
if isinstance(token, lexer.Directive):
children.append(Directive(token.value))
elif isinstance(token, lexer.Keyword):
while True:
match token.value:
case 'pub': pass
case 'struct':
def struct_syntactical_analyzer(
tokens: list[lexer.Token],
public: bool,
) -> StructBlock:
identifier = tokens.pop()
_assert_token(ExpectedIdentifier, identifier)
temp = tokens.pop()
@ -752,10 +735,7 @@ def file_syntactical_analyzer(tokens: list[lexer.Token]) -> File:
_assert_token(ExpectedPunctuation, temp, '*')
pointer = True
temp = tokens.pop()
_assert_token_mult(temp, (
lexer.Keyword,
lexer.Identifier,
))
_assert_token_mult(temp, (lexer.Keyword, lexer.Identifier))
else:
pointer = False
if isinstance(temp, lexer.Keyword):
@ -770,28 +750,39 @@ def file_syntactical_analyzer(tokens: list[lexer.Token]) -> File:
temp = tokens.pop()
_assert_token(ExpectedPunctuation, temp)
if temp.value not in [',', '=']:
raise UnexpectedPunctuation(
temp,
[',', '='],
)
raise UnexpectedPunctuation(temp, [',', '='])
elif temp.value == '=':
temp = tokens.pop()
_assert_token_literal(temp)
literal = _literal_map(temp) # type: ignore
temp = tokens.pop()
else: literal = None
members.append(StructureMember(
member_id,
data_type,
pointer,
static,
literal,
))
members.append(
StructureMember(member_id, data_type, pointer, static, literal))
else:
raise UnexpectedToken(
temp, ["Keyword", "Identifier"])
children.append(StructBlock(
Identifier(identifier.value), False, members))
raise UnexpectedToken(temp, ["Keyword", "Identifier"])
return StructBlock(Identifier(identifier.value), public, members)
def enumeration_syntactical_analyzer(tokens: list[lexer.Token]) -> EnumBlock:
pass
def function_syntactical_analyzer(tokens: list[lexer.Token]) -> FunctionBlock:
pass
def file_syntactical_analyzer(tokens: list[lexer.Token]) -> File:
children: list[Directive | StructBlock | FunctionBlock | EnumBlock] = []
while tokens:
token = tokens.pop(0)
if isinstance(token, lexer.Directive):
children.append(Directive(token.value))
elif isinstance(token, lexer.Keyword):
while True:
match token.value:
case 'pub': pass
case 'struct':
children.append(
struct_syntactical_analyzer(tokens, False))
break
return File(children)