Parse the variable part to AST

This commit is contained in:
2025-06-04 12:14:04 +02:00
parent 82f0d40a56
commit 7c12fc1364
5 changed files with 89 additions and 31 deletions

View File

@ -12,9 +12,9 @@ from Lexer import Lexer, LexerToken, lexer_current, lexer_lex, LexerKind;
from Parser import AstModule, PAstModule, AstTypeExpressionKind,
AstConstantDeclaration, PPAstConstantDeclaration,
AstTypeDeclaration, PAstTypeDeclaration, PPAstTypeDeclaration,
AstVariableDeclaration, PPAstVariableDeclaration,
PAstTypeExpression, AstTypeExpression, PPAstTypeExpression, AstFieldDeclaration, PAstFieldDeclaration,
parse_type_expression, parse_type_declaration, parse_type_part;
PAstVariableDeclaration, PPAstVariableDeclaration,
PAstTypeExpression, PPAstTypeExpression, AstFieldDeclaration, PAstFieldDeclaration,
parse_type_expression, parse_variable_part, parse_type_part;
(* Calls lexer_lex() but skips the comments. *)
proc transpiler_lex(lexer: PLexer) -> LexerToken;
@ -138,7 +138,9 @@ begin
WriteString(context^.output, 'MODULE ');
(* Write the module name and end the line with a semicolon and newline. *)
token := transpiler_lex(context^.lexer);
if token.kind <> lexerKindProgram then
token := transpiler_lex(context^.lexer);
end;
transpile_module_name(context);
token := transpiler_lex(context^.lexer);
@ -152,7 +154,9 @@ begin
result^.constants := transpile_constant_part(context);
result^.types := parse_type_part(context^.lexer);
transpile_type_part(context, result^.types);
result^.variables := transpile_variable_part(context);
result^.variables := parse_variable_part(context^.lexer);
transpile_variable_part(context, result^.variables);
transpile_procedure_part(context);
transpile_statement_part(context);
@ -334,41 +338,35 @@ begin
end
end;
proc transpile_variable_declaration(context: PTranspilerContext);
proc transpile_variable_declaration(context: PTranspilerContext, declaration: PAstVariableDeclaration);
var
token: LexerToken;
type_expression: PAstTypeExpression;
written_bytes: CARDINAL;
begin
WriteString(context^.output, ' ');
token := lexer_current(context^.lexer);
write_current(context^.lexer, context^.output);
written_bytes := WriteNBytes(context^.output, ORD(declaration^.variable_name[1]), ADR(declaration^.variable_name[2]));
token := transpiler_lex(context^.lexer);
WriteString(context^.output, ': ');
token := transpiler_lex(context^.lexer);
type_expression := parse_type_expression(context^.lexer);
transpile_type_expression(context, type_expression);
token := transpiler_lex(context^.lexer);
transpile_type_expression(context, declaration^.variable_type);
write_semicolon(context^.output)
end;
proc transpile_variable_part(context: PTranspilerContext) -> PPAstVariableDeclaration;
proc transpile_variable_part(context: PTranspilerContext, declarations: PPAstVariableDeclaration);
var
token: LexerToken;
current_declaration: PPAstVariableDeclaration;
begin
token := lexer_current(context^.lexer);
if token.kind = lexerKindVar then
if declarations^ <> nil then
WriteString(context^.output, 'VAR');
WriteLine(context^.output);
token := transpiler_lex(context^.lexer);
while token.kind = lexerKindIdentifier do
transpile_variable_declaration(context);
token := transpiler_lex(context^.lexer)
end
end;
return nil
current_declaration := declarations;
while current_declaration^ <> nil do
transpile_variable_declaration(context, current_declaration^);
INC(current_declaration, TSIZE(PAstVariableDeclaration))
end;
WriteLine(context^.output)
end
end;
proc transpile_procedure_heading(context: PTranspilerContext) -> LexerToken;
@ -624,7 +622,9 @@ var
begin
token := transpile_procedure_heading(context);
seen_constants := transpile_constant_part(context);
seen_variables := transpile_variable_part(context);
seen_variables := parse_variable_part(context^.lexer);
transpile_variable_part(context, seen_variables);
transpile_statement_part(context);
WriteString(context^.output, 'END ');