Shorten the module declaration

This commit is contained in:
2025-06-05 12:33:15 +02:00
parent 92ba0ff871
commit 9bc6b50b94
3 changed files with 183 additions and 60 deletions

View File

@ -10,11 +10,11 @@ FROM MemUtils IMPORT MemCopy, MemZero;
FROM Common IMPORT Identifier, PIdentifier, ShortString;
FROM Lexer IMPORT Lexer, LexerToken, lexer_current, lexer_lex, LexerKind;
FROM Parser IMPORT AstModule, PAstModule, AstTypeExpressionKind,
AstConstantDeclaration, PPAstConstantDeclaration,
PAstConstantDeclaration, PPAstConstantDeclaration,
AstTypeDeclaration, PAstTypeDeclaration, PPAstTypeDeclaration,
PAstVariableDeclaration, PPAstVariableDeclaration,
PAstVariableDeclaration, PPAstVariableDeclaration, PAstImportStatement, PPAstImportStatement,
PAstTypeExpression, PPAstTypeExpression, AstFieldDeclaration, PAstFieldDeclaration,
parse_type_expression, parse_variable_part, parse_type_part;
parse_type_expression, parse_variable_part, parse_type_part, parse_constant_part, parse_import_part;
(* Calls lexer_lex() but skips the comments. *)
PROCEDURE transpiler_lex(lexer: PLexer): LexerToken;
@ -41,76 +41,69 @@ VAR
BEGIN
written_bytes := WriteNBytes(output, ADDRESS(lexer^.Current - lexer^.Start), lexer^.Start)
END write_current;
PROCEDURE transpile_import(context: PTranspilerContext);
PROCEDURE transpile_import_statement(context: PTranspilerContext; import_statement: PAstImportStatement);
VAR
token: LexerToken;
written_bytes: CARDINAL;
current_symbol: PIdentifier;
BEGIN
WriteString(context^.output, 'FROM ');
token := transpiler_lex(context^.lexer);
written_bytes := WriteNBytes(context^.output, ORD(import_statement^.package[1]), ADR(import_statement^.package[2]));
write_current(context^.lexer, context^.output);
token := transpiler_lex(context^.lexer);
WriteString(context^.output, ' IMPORT ');
token := transpiler_lex(context^.lexer);
write_current(context^.lexer, context^.output);
current_symbol := import_statement^.symbols;
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), ADR(current_symbol^[2]));
INC(current_symbol, TSIZE(Identifier));
token := transpiler_lex(context^.lexer);
WHILE token.kind <> lexerKindSemicolon DO
WHILE ORD(current_symbol^[1]) <> 0 DO
WriteString(context^.output, ', ');
token := transpiler_lex(context^.lexer);
write_current(context^.lexer, context^.output);
token := transpiler_lex(context^.lexer)
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), ADR(current_symbol^[2]));
INC(current_symbol, TSIZE(Identifier))
END;
write_semicolon(context^.output);
token := transpiler_lex(context^.lexer)
END transpile_import;
PROCEDURE transpile_import_part(context: PTranspilerContext);
write_semicolon(context^.output)
END transpile_import_statement;
PROCEDURE transpile_import_part(context: PTranspilerContext; imports: PPAstImportStatement);
VAR
token: LexerToken;
import_statement: PAstImportStatement;
BEGIN
token := lexer_current(context^.lexer);
WHILE token.kind = lexerKindFrom DO
transpile_import(context);
token := lexer_current(context^.lexer)
WHILE imports^ <> NIL DO
transpile_import_statement(context, imports^);
INC(imports, TSIZE(PAstImportStatement))
END;
WriteLine(context^.output)
END transpile_import_part;
PROCEDURE transpile_constant(context: PTranspilerContext);
PROCEDURE transpile_constant_declaration(context: PTranspilerContext; declaration: PAstConstantDeclaration);
VAR
token: LexerToken;
buffer: ARRAY[1..20] OF CHAR;
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^.constant_name[1]), ADR(declaration^.constant_name[2]));
token := transpiler_lex(context^.lexer);
WriteString(context^.output, ' = ');
token := transpiler_lex(context^.lexer);
write_current(context^.lexer, context^.output);
IntToStr(declaration^.constant_value, 0, buffer);
WriteString(context^.output, buffer);
token := transpiler_lex(context^.lexer);
write_semicolon(context^.output)
END transpile_constant;
PROCEDURE transpile_constant_part(context: PTranspilerContext): PPAstConstantDeclaration;
END transpile_constant_declaration;
PROCEDURE transpile_constant_part(context: PTranspilerContext; declarations: PPAstConstantDeclaration);
VAR
token: LexerToken;
current_declaration: PPAstConstantDeclaration;
BEGIN
token := lexer_current(context^.lexer);
IF token.kind = lexerKindConst THEN
IF declarations^ <> NIL THEN
WriteString(context^.output, 'CONST');
WriteLine(context^.output);
token := transpiler_lex(context^.lexer);
WHILE token.kind = lexerKindIdentifier DO
transpile_constant(context);
token := transpiler_lex(context^.lexer)
END
END;
RETURN NIL
current_declaration := declarations;
WHILE current_declaration^ <> NIL DO
transpile_constant_declaration(context, current_declaration^);
INC(current_declaration, TSIZE(PAstConstantDeclaration))
END;
WriteLine(context^.output)
END
END transpile_constant_part;
PROCEDURE transpile_module(context: PTranspilerContext): PAstModule;
VAR
@ -120,20 +113,12 @@ BEGIN
ALLOCATE(result, TSIZE(AstModule));
token := transpiler_lex(context^.lexer);
IF token.kind = lexerKindDefinition THEN
WriteString(context^.output, 'DEFINITION ');
token := transpiler_lex(context^.lexer)
END;
IF token.kind = lexerKindImplementation THEN
WriteString(context^.output, 'IMPLEMENTATION ');
token := transpiler_lex(context^.lexer)
IF token.kind = lexerKindModule THEN
WriteString(context^.output, 'IMPLEMENTATION ')
END;
WriteString(context^.output, 'MODULE ');
(* Write the module name and end the line with a semicolon and newline. *)
IF token.kind <> lexerKindProgram THEN
token := transpiler_lex(context^.lexer);
END;
transpile_module_name(context);
token := transpiler_lex(context^.lexer);
@ -142,9 +127,12 @@ BEGIN
(* Write the module body. *)
token := transpiler_lex(context^.lexer);
transpile_import_part(context);
result^.constants := transpile_constant_part(context);
result^.imports := parse_import_part(context^.lexer);
transpile_import_part(context, result^.imports);
result^.constants := parse_constant_part(context^.lexer);
transpile_constant_part(context, result^.constants);
result^.types := parse_type_part(context^.lexer);
transpile_type_part(context, result^.types);
@ -590,7 +578,8 @@ VAR
seen_constants: PPAstConstantDeclaration;
BEGIN
token := transpile_procedure_heading(context);
seen_constants := transpile_constant_part(context);
seen_constants := parse_constant_part(context^.lexer);
transpile_constant_part(context, seen_constants);
seen_variables := parse_variable_part(context^.lexer);
transpile_variable_part(context, seen_variables);
@ -631,10 +620,10 @@ BEGIN
END;
IF last_slash = 0 THEN
counter := 1;
counter := 1
END;
IF last_slash <> 0 THEN
counter := last_slash + 1;
counter := last_slash + 1
END;
WHILE (context^.input_name[counter] <> '.') AND (ORD(context^.input_name[counter]) <> 0) DO
WriteChar(context^.output, context^.input_name[counter]);