Shorten the module declaration

This commit is contained in:
2025-06-05 12:34:17 +02:00
parent 7c12fc1364
commit 3ca8491f64
7 changed files with 191 additions and 64 deletions

View File

@ -1,4 +1,4 @@
implementation module Transpiler;
module;
from FIO import WriteNBytes, WriteLine, WriteChar, WriteString;
from SYSTEM import ADR, ADDRESS, TSIZE;
@ -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. *)
proc transpiler_lex(lexer: PLexer) -> LexerToken;
@ -44,79 +44,72 @@ begin
written_bytes := WriteNBytes(output, ADDRESS(lexer^.Current - lexer^.Start), lexer^.Start)
end;
proc transpile_import(context: PTranspilerContext);
proc 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)
write_semicolon(context^.output)
end;
proc transpile_import_part(context: PTranspilerContext);
proc 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;
proc transpile_constant(context: PTranspilerContext);
proc transpile_constant_declaration(context: PTranspilerContext, declaration: PAstConstantDeclaration);
var
token: LexerToken;
buffer: [20]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;
proc transpile_constant_part(context: PTranspilerContext) -> PPAstConstantDeclaration;
proc 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;
proc transpile_module(context: PTranspilerContext) -> PAstModule;
@ -127,20 +120,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);
@ -149,9 +134,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);
@ -621,7 +609,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);
@ -664,10 +653,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] <> '.') & (ORD(context^.input_name[counter]) <> 0) do
WriteChar(context^.output, context^.input_name[counter]);