Parse the variable part to AST

This commit is contained in:
2025-06-04 12:13:40 +02:00
parent ff547a295d
commit 92ba0ff871
3 changed files with 83 additions and 29 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. *)
PROCEDURE transpiler_lex(lexer: PLexer): LexerToken;
@ -131,7 +131,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);
@ -145,7 +147,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);
@ -316,40 +320,34 @@ BEGIN
WriteLine(context^.output)
END
END transpile_type_part;
PROCEDURE transpile_variable_declaration(context: PTranspilerContext);
PROCEDURE 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 transpile_variable_declaration;
PROCEDURE transpile_variable_part(context: PTranspilerContext): PPAstVariableDeclaration;
PROCEDURE 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 transpile_variable_part;
PROCEDURE transpile_procedure_heading(context: PTranspilerContext): LexerToken;
VAR
@ -593,7 +591,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 ');