Split the parser from the code generator

This commit is contained in:
2025-06-11 22:36:35 +02:00
parent 00e557686b
commit e3f094c8a5
7 changed files with 409 additions and 230 deletions

View File

@ -110,7 +110,7 @@ task :backport do
.gsub(/ & /, ' AND ') .gsub(/ & /, ' AND ')
.gsub(/ -> /, ': ') .gsub(/ -> /, ': ')
.gsub(/program;/, "MODULE #{module_name};") .gsub(/program;/, "MODULE #{module_name};")
.gsub(/module;/, "IMPLEMENTATION MODULE #{module_name};") .gsub(/\bmodule;/, "IMPLEMENTATION MODULE #{module_name};")
.gsub(/end\./, "END #{module_name}.") .gsub(/end\./, "END #{module_name}.")
.gsub(/([[:space:]]*)end(;?)$/, '\1END\2') .gsub(/([[:space:]]*)end(;?)$/, '\1END\2')
.gsub(/^([[:space:]]*)(while|return|if)\b/) { |match| match.upcase } .gsub(/^([[:space:]]*)(while|return|if)\b/) { |match| match.upcase }

View File

@ -7,6 +7,7 @@ from M2RTS import HALT, ExitOnHalt;
from Lexer import Lexer, lexer_destroy, lexer_initialize; from Lexer import Lexer, lexer_destroy, lexer_initialize;
from Transpiler import transpile; from Transpiler import transpile;
from CommandLineInterface import PCommandLine, parse_command_line; from CommandLineInterface import PCommandLine, parse_command_line;
from Parser import PAstModule, parse_module;
var var
command_line: PCommandLine; command_line: PCommandLine;
@ -15,6 +16,7 @@ proc compile_from_stream();
var var
lexer: Lexer; lexer: Lexer;
source_input: File; source_input: File;
ast_module: PAstModule;
begin begin
source_input := OpenToRead(command_line^.input); source_input := OpenToRead(command_line^.input);
@ -29,7 +31,8 @@ begin
if IsNoError(source_input) then if IsNoError(source_input) then
lexer_initialize(ADR(lexer), source_input); lexer_initialize(ADR(lexer), source_input);
transpile(ADR(lexer), StdOut, command_line^.input); ast_module := parse_module(ADR(lexer));
transpile(ast_module, StdOut, command_line^.input);
lexer_destroy(ADR(lexer)); lexer_destroy(ADR(lexer));

View File

@ -11,7 +11,7 @@ from MemUtils import MemCopy, MemZero;
from StrCase import Lower; from StrCase import Lower;
const const
CHUNK_SIZE = 65536; CHUNK_SIZE = 85536;
type type
(* (*

View File

@ -153,12 +153,12 @@ TYPE
PAstTypeExpression = POINTER TO AstTypeExpression; PAstTypeExpression = POINTER TO AstTypeExpression;
PPAstTypeExpression = POINTER TO PAstTypeExpression; PPAstTypeExpression = POINTER TO PAstTypeExpression;
AstTypeDeclaration = RECORD AstTypedDeclaration = RECORD
identifier: Identifier; identifier: Identifier;
type_expression: PAstTypeExpression type_expression: PAstTypeExpression
END; END;
PAstTypeDeclaration = POINTER TO AstTypeDeclaration; PAstTypedDeclaration = POINTER TO AstTypedDeclaration;
PPAstTypeDeclaration = POINTER TO PAstTypeDeclaration; PPAstTypedDeclaration = POINTER TO PAstTypedDeclaration;
AstVariableDeclaration = RECORD AstVariableDeclaration = RECORD
variable_name: Identifier; variable_name: Identifier;
@ -167,23 +167,38 @@ TYPE
PAstVariableDeclaration = POINTER TO AstVariableDeclaration; PAstVariableDeclaration = POINTER TO AstVariableDeclaration;
PPAstVariableDeclaration = POINTER TO PAstVariableDeclaration; PPAstVariableDeclaration = POINTER TO PAstVariableDeclaration;
AstProcedureDeclaration = RECORD
name: Identifier;
parameter_count: CARDINAL;
parameters: PAstTypedDeclaration;
return_type: PAstTypeExpression;
constants: PPAstConstantDeclaration;
variables: PPAstVariableDeclaration;
statements: AstCompoundStatement
END;
PAstProcedureDeclaration = POINTER TO AstProcedureDeclaration;
PPAstProcedureDeclaration = POINTER TO PAstProcedureDeclaration;
AstModule = RECORD AstModule = RECORD
main: BOOLEAN;
imports: PPAstImportStatement; imports: PPAstImportStatement;
constants: PPAstConstantDeclaration; constants: PPAstConstantDeclaration;
types: PPAstTypeDeclaration; types: PPAstTypedDeclaration;
variables: PPAstVariableDeclaration variables: PPAstVariableDeclaration;
procedures: PPAstProcedureDeclaration;
statements: AstCompoundStatement
END; END;
PAstModule = POINTER TO AstModule; PAstModule = POINTER TO AstModule;
PROCEDURE parse_type_expression(lexer: PLexer): PAstTypeExpression; PROCEDURE parse_type_expression(lexer: PLexer): PAstTypeExpression;
PROCEDURE parse_type_part(lexer: PLexer): PPAstTypeDeclaration; PROCEDURE parse_type_part(lexer: PLexer): PPAstTypedDeclaration;
PROCEDURE parse_variable_part(lexer: PLexer): PPAstVariableDeclaration; PROCEDURE parse_variable_part(lexer: PLexer): PPAstVariableDeclaration;
PROCEDURE parse_constant_part(lexer: PLexer): PPAstConstantDeclaration; PROCEDURE parse_constant_part(lexer: PLexer): PPAstConstantDeclaration;
PROCEDURE parse_import_part(lexer: PLexer): PPAstImportStatement; PROCEDURE parse_import_part(lexer: PLexer): PPAstImportStatement;
PROCEDURE parse_designator(lexer: PLexer): PAstExpression; PROCEDURE parse_designator(lexer: PLexer): PAstExpression;
PROCEDURE parse_expression(lexer: PLexer): PAstExpression; PROCEDURE parse_expression(lexer: PLexer): PAstExpression;
PROCEDURE parse_return_statement(lexer: PLexer): PAstStatement; PROCEDURE parse_statement_part(lexer: PLexer): AstCompoundStatement;
PROCEDURE parse_assignment_statement(lexer: PLexer; assignee: PAstExpression): PAstStatement; PROCEDURE parse_procedure_part(lexer: PLexer): PPAstProcedureDeclaration;
PROCEDURE parse_call_statement(lexer: PLexer; call: PAstExpression): PAstStatement; PROCEDURE parse_module(lexer: PLexer): PAstModule;
END Parser. END Parser.

View File

@ -109,7 +109,7 @@ begin
result^.length := token.integerKind; result^.length := token.integerKind;
token := transpiler_lex(lexer); token := transpiler_lex(lexer)
end; end;
token := transpiler_lex(lexer); token := transpiler_lex(lexer);
result^.base := parse_type_expression(lexer); result^.base := parse_type_expression(lexer);
@ -235,10 +235,10 @@ begin
return result return result
end; end;
proc parse_type_declaration(lexer: PLexer) -> PAstTypeDeclaration; proc parse_type_declaration(lexer: PLexer) -> PAstTypedDeclaration;
var var
token: LexerToken; token: LexerToken;
result: PAstTypeDeclaration; result: PAstTypedDeclaration;
begin begin
token := lexer_current(lexer); token := lexer_current(lexer);
@ -254,16 +254,16 @@ begin
return result return result
end; end;
proc parse_type_part(lexer: PLexer) -> PPAstTypeDeclaration; proc parse_type_part(lexer: PLexer) -> PPAstTypedDeclaration;
var var
token: LexerToken; token: LexerToken;
result: PPAstTypeDeclaration; result: PPAstTypedDeclaration;
current_declaration: PPAstTypeDeclaration; current_declaration: PPAstTypedDeclaration;
declaration_count: CARDINAL; declaration_count: CARDINAL;
begin begin
token := lexer_current(lexer); token := lexer_current(lexer);
ALLOCATE(result, TSIZE(PAstTypeDeclaration)); ALLOCATE(result, TSIZE(PAstTypedDeclaration));
current_declaration := result; current_declaration := result;
declaration_count := 0; declaration_count := 0;
@ -273,16 +273,16 @@ begin
while token.kind = lexerKindIdentifier do while token.kind = lexerKindIdentifier do
INC(declaration_count); INC(declaration_count);
REALLOCATE(result, TSIZE(PAstTypeDeclaration) * (declaration_count + 1)); REALLOCATE(result, TSIZE(PAstTypedDeclaration) * (declaration_count + 1));
current_declaration := result; current_declaration := result;
INC(current_declaration, TSIZE(PAstTypeDeclaration) * (declaration_count - 1)); INC(current_declaration, TSIZE(PAstTypedDeclaration) * (declaration_count - 1));
current_declaration^ := parse_type_declaration(lexer); current_declaration^ := parse_type_declaration(lexer);
token := transpiler_lex(lexer) token := transpiler_lex(lexer)
end end
end; end;
if declaration_count <> 0 then if declaration_count <> 0 then
INC(current_declaration, TSIZE(PAstTypeDeclaration)) INC(current_declaration, TSIZE(PAstTypedDeclaration))
end; end;
current_declaration^ := nil; current_declaration^ := nil;
@ -481,18 +481,18 @@ begin
NEW(literal); NEW(literal);
literal^.kind := astLiteralKindInteger; literal^.kind := astLiteralKindInteger;
literal^.integer := token.integerKind; literal^.integer := token.integerKind
end; end;
if (token.kind = lexerKindCharacter) or (token.kind = lexerKindString) then if (token.kind = lexerKindCharacter) or (token.kind = lexerKindString) then
NEW(literal); NEW(literal);
literal^.kind := astLiteralKindString; literal^.kind := astLiteralKindString;
literal^.string := token.stringKind; literal^.string := token.stringKind
end; end;
if token.kind = lexerKindNull then if token.kind = lexerKindNull then
NEW(literal); NEW(literal);
literal^.kind := astLiteralKindNull; literal^.kind := astLiteralKindNull
end; end;
if token.kind = lexerKindBoolean then if token.kind = lexerKindBoolean then
NEW(literal); NEW(literal);
@ -522,7 +522,7 @@ begin
NEW(result); NEW(result);
result^.kind := astExpressionKindLiteral; result^.kind := astExpressionKindLiteral;
result^.literal := literal; result^.literal := literal
end; end;
if (result = nil) & (next_token.kind = lexerKindMinus) then if (result = nil) & (next_token.kind = lexerKindMinus) then
NEW(result); NEW(result);
@ -658,7 +658,7 @@ begin
result^.kind := astExpressionKindBinary; result^.kind := astExpressionKindBinary;
result^.binary_operator := operator; result^.binary_operator := operator;
result^.lhs := left; result^.lhs := left;
result^.rhs := right; result^.rhs := right
end; end;
return result return result
@ -757,4 +757,243 @@ begin
return result return result
end; end;
proc parse_compound_statement(lexer: PLexer) -> AstCompoundStatement;
var
result: AstCompoundStatement;
token: LexerToken;
current_statement: PPAstStatement;
old_count: CARDINAL;
begin
result.count := 0;
result.statements := nil;
token := lexer_current(lexer);
while token.kind <> lexerKindEnd do
old_count := result.count;
INC(result.count);
REALLOCATE(result.statements, TSIZE(PAstStatement) * result.count);
current_statement := result.statements;
INC(current_statement, TSIZE(PAstStatement) * old_count);
current_statement^ := parse_statement(lexer);
token := lexer_current(lexer)
end;
return result
end;
proc parse_statement(lexer: PLexer) -> PAstStatement;
var
token: LexerToken;
statement: PAstStatement;
designator: PAstExpression;
begin
statement := nil;
token := transpiler_lex(lexer);
if token.kind = lexerKindIf then
statement := parse_if_statement(lexer)
end;
if token.kind = lexerKindWhile then
statement := parse_while_statement(lexer)
end;
if token.kind = lexerKindReturn then
statement := parse_return_statement(lexer)
end;
if token.kind = lexerKindIdentifier then
designator := parse_designator(lexer);
token := lexer_current(lexer);
if token.kind = lexerKindAssignment then
statement := parse_assignment_statement(lexer, designator)
end;
if token.kind <> lexerKindAssignment then
statement := parse_call_statement(lexer, designator)
end
end;
return statement
end;
proc parse_if_statement(lexer: PLexer) -> PAstStatement;
var
token: LexerToken;
result: PAstStatement;
begin
NEW(result);
result^.kind := astStatementKindIf;
token := transpiler_lex(lexer);
result^.if_condition := parse_expression(lexer);
result^.if_branch := parse_compound_statement(lexer);
token := transpiler_lex(lexer);
return result
end;
proc parse_while_statement(lexer: PLexer) -> PAstStatement;
var
token: LexerToken;
result: PAstStatement;
begin
NEW(result);
result^.kind := astStatementKindWhile;
token := transpiler_lex(lexer);
result^.while_condition := parse_expression(lexer);
result^.while_body := parse_compound_statement(lexer);
token := transpiler_lex(lexer);
return result
end;
proc parse_statement_part(lexer: PLexer) -> AstCompoundStatement;
var
token: LexerToken;
compound: AstCompoundStatement;
begin
compound.count := 0;
compound.statements := nil;
token := lexer_current(lexer);
if token.kind = lexerKindBegin then
compound := parse_compound_statement(lexer)
end;
return compound
end;
proc parse_procedure_heading(lexer: PLexer) -> PAstProcedureDeclaration;
var
token: LexerToken;
declaration: PAstProcedureDeclaration;
parameter_index: CARDINAL;
current_parameter: PAstTypedDeclaration;
begin
NEW(declaration);
token := transpiler_lex(lexer);
declaration^.name := token.identifierKind;
token := transpiler_lex(lexer);
declaration^.parameters := nil;
declaration^.parameter_count := 0;
token := transpiler_lex(lexer);
while token.kind <> lexerKindRightParen do
parameter_index := declaration^.parameter_count;
INC(declaration^.parameter_count);
REALLOCATE(declaration^.parameters, TSIZE(AstTypedDeclaration) * declaration^.parameter_count);
current_parameter := declaration^.parameters;
INC(current_parameter, TSIZE(AstTypedDeclaration) * parameter_index);
current_parameter^.identifier := token.identifierKind;
token := transpiler_lex(lexer);
token := transpiler_lex(lexer);
current_parameter^.type_expression := parse_type_expression(lexer);
token := transpiler_lex(lexer);
if token.kind = lexerKindComma then
token := transpiler_lex(lexer)
end
end;
token := transpiler_lex(lexer);
declaration^.return_type := nil;
(* Check for the return type and write it. *)
if token.kind = lexerKindArrow then
token := transpiler_lex(lexer);
declaration^.return_type := parse_type_expression(lexer);
token := transpiler_lex(lexer)
end;
token := transpiler_lex(lexer);
return declaration
end;
proc parse_procedure_declaration(lexer: PLexer) -> PAstProcedureDeclaration;
var
token: LexerToken;
declaration: PAstProcedureDeclaration;
begin
declaration := parse_procedure_heading(lexer);
declaration^.constants := parse_constant_part(lexer);
declaration^.variables := parse_variable_part(lexer);
declaration^.statements := parse_statement_part(lexer);
token := transpiler_lex(lexer);
token := transpiler_lex(lexer);
return declaration
end;
proc parse_procedure_part(lexer: PLexer) -> PPAstProcedureDeclaration;
var
token: LexerToken;
current_declaration: PPAstProcedureDeclaration;
result: PPAstProcedureDeclaration;
declaration_count: CARDINAL;
declaration_index: CARDINAL;
begin
token := lexer_current(lexer);
declaration_count := 0;
declaration_index := 0;
ALLOCATE(result, TSIZE(PAstProcedureDeclaration));
while token.kind = lexerKindProc do
INC(declaration_count);
REALLOCATE(result, TSIZE(PAstProcedureDeclaration) * (declaration_count + 1));
current_declaration := result;
INC(current_declaration, TSIZE(PAstProcedureDeclaration) * declaration_index);
current_declaration^ := parse_procedure_declaration(lexer);
token := lexer_current(lexer);
declaration_index := declaration_count
end;
current_declaration := result;
INC(current_declaration, TSIZE(PAstProcedureDeclaration) * declaration_index);
current_declaration^ := nil;
return result
end;
proc parse_module(lexer: PLexer) -> PAstModule;
var
token: LexerToken;
result: PAstModule;
begin
NEW(result);
token := transpiler_lex(lexer);
result^.main := true;
if token.kind = lexerKindModule then
result^.main := false
end;
token := transpiler_lex(lexer);
(* Write the module body. *)
token := transpiler_lex(lexer);
result^.imports := parse_import_part(lexer);
result^.constants := parse_constant_part(lexer);
result^.types := parse_type_part(lexer);
result^.variables := parse_variable_part(lexer);
result^.procedures := parse_procedure_part(lexer);
result^.statements := parse_statement_part(lexer);
token := transpiler_lex(lexer);
token := transpiler_lex(lexer);
return result
end;
end. end.

View File

@ -4,15 +4,15 @@ FROM FIO IMPORT File;
FROM Common IMPORT ShortString; FROM Common IMPORT ShortString;
FROM Lexer IMPORT PLexer, Lexer; FROM Lexer IMPORT PLexer, Lexer;
FROM Parser IMPORT PAstModule;
TYPE TYPE
TranspilerContext = RECORD TranspilerContext = RECORD
input_name: ShortString; input_name: ShortString;
output: File; output: File
lexer: PLexer
END; END;
PTranspilerContext = POINTER TO TranspilerContext; PTranspilerContext = POINTER TO TranspilerContext;
PROCEDURE transpile(lexer: PLexer; output: File; input_name: ShortString); PROCEDURE transpile(ast_module: PAstModule; output: File; input_name: ShortString);
END Transpiler. END Transpiler.

View File

@ -1,6 +1,6 @@
module; module;
from FIO import StdErr, WriteNBytes, WriteLine, WriteChar, WriteString; from FIO import WriteNBytes, WriteLine, WriteChar, WriteString;
from SYSTEM import ADR, ADDRESS, TSIZE; from SYSTEM import ADR, ADDRESS, TSIZE;
from NumberIO import IntToStr; from NumberIO import IntToStr;
@ -10,13 +10,11 @@ from MemUtils import MemCopy, MemZero;
from Common import Identifier, PIdentifier, ShortString; from Common import Identifier, PIdentifier, ShortString;
from Lexer import Lexer, LexerToken, lexer_current, lexer_lex, LexerKind; from Lexer import Lexer, LexerToken, lexer_current, lexer_lex, LexerKind;
from Parser import AstTypeExpressionKind, AstExpressionKind, AstLiteralKind, AstUnaryOperator, AstBinaryOperator, from Parser import AstTypeExpressionKind, AstExpressionKind, AstLiteralKind, AstUnaryOperator, AstBinaryOperator,
AstModule, PAstModule, AstExpression, PPAstExpression, PAstExpression, PAstLiteral, AstModule, PAstModule, AstExpression, PPAstExpression, PAstExpression, PAstLiteral, PPAstProcedureDeclaration,
PAstConstantDeclaration, PPAstConstantDeclaration, PAstStatement, AstStatementKind, PAstConstantDeclaration, PPAstConstantDeclaration, PPAstStatement, PAstStatement, AstStatementKind,
AstTypeDeclaration, PAstTypeDeclaration, PPAstTypeDeclaration, AstTypedDeclaration, PAstTypedDeclaration, PPAstTypedDeclaration, AstCompoundStatement, PAstProcedureDeclaration,
PAstVariableDeclaration, PPAstVariableDeclaration, PAstImportStatement, PPAstImportStatement, PAstVariableDeclaration, PPAstVariableDeclaration, PAstImportStatement, PPAstImportStatement,
PAstTypeExpression, PPAstTypeExpression, AstFieldDeclaration, PAstFieldDeclaration, PAstTypeExpression, PPAstTypeExpression, AstFieldDeclaration, PAstFieldDeclaration;
parse_type_expression, parse_variable_part, parse_type_part, parse_constant_part, parse_import_part,
parse_designator, parse_expression, parse_return_statement, parse_assignment_statement, parse_call_statement;
(* Calls lexer_lex() but skips the comments. *) (* Calls lexer_lex() but skips the comments. *)
proc transpiler_lex(lexer: PLexer) -> LexerToken; proc transpiler_lex(lexer: PLexer) -> LexerToken;
@ -118,15 +116,11 @@ begin
end end
end; end;
proc transpile_module(context: PTranspilerContext) -> PAstModule; proc transpile_module(context: PTranspilerContext, result: PAstModule);
var var
token: LexerToken; token: LexerToken;
result: PAstModule;
begin begin
NEW(result); if result^.main = false then
token := transpiler_lex(context^.lexer);
if token.kind = lexerKindModule then
WriteString(context^.output, 'IMPLEMENTATION ') WriteString(context^.output, 'IMPLEMENTATION ')
end; end;
WriteString(context^.output, 'MODULE '); WriteString(context^.output, 'MODULE ');
@ -134,37 +128,23 @@ begin
(* Write the module name and end the line with a semicolon and newline. *) (* Write the module name and end the line with a semicolon and newline. *)
transpile_module_name(context); transpile_module_name(context);
token := transpiler_lex(context^.lexer);
write_semicolon(context^.output); write_semicolon(context^.output);
WriteLine(context^.output); WriteLine(context^.output);
(* Write the module body. *) (* Write the module body. *)
token := transpiler_lex(context^.lexer);
result^.imports := parse_import_part(context^.lexer);
transpile_import_part(context, result^.imports); transpile_import_part(context, result^.imports);
result^.constants := parse_constant_part(context^.lexer);
transpile_constant_part(context, result^.constants); transpile_constant_part(context, result^.constants);
result^.types := parse_type_part(context^.lexer);
transpile_type_part(context, result^.types); transpile_type_part(context, result^.types);
result^.variables := parse_variable_part(context^.lexer);
transpile_variable_part(context, result^.variables); transpile_variable_part(context, result^.variables);
transpile_procedure_part(context, result^.procedures);
transpile_procedure_part(context); transpile_statement_part(context, result^.statements);
transpile_statement_part(context);
WriteString(context^.output, 'END '); WriteString(context^.output, 'END ');
transpile_module_name(context); transpile_module_name(context);
token := transpiler_lex(context^.lexer);
WriteChar(context^.output, '.'); WriteChar(context^.output, '.');
WriteLine(context^.output)
token := transpiler_lex(context^.lexer);
WriteLine(context^.output);
return result
end; end;
proc transpile_type_fields(context: PTranspilerContext, fields: PAstFieldDeclaration); proc transpile_type_fields(context: PTranspilerContext, fields: PAstFieldDeclaration);
@ -301,7 +281,7 @@ begin
end end
end; end;
proc transpile_type_declaration(context: PTranspilerContext, declaration: PAstTypeDeclaration); proc transpile_type_declaration(context: PTranspilerContext, declaration: PAstTypedDeclaration);
var var
written_bytes: CARDINAL; written_bytes: CARDINAL;
begin begin
@ -314,9 +294,9 @@ begin
write_semicolon(context^.output) write_semicolon(context^.output)
end; end;
proc transpile_type_part(context: PTranspilerContext, declarations: PPAstTypeDeclaration); proc transpile_type_part(context: PTranspilerContext, declarations: PPAstTypedDeclaration);
var var
current_declaration: PPAstTypeDeclaration; current_declaration: PPAstTypedDeclaration;
begin begin
if declarations^ <> nil then if declarations^ <> nil then
WriteString(context^.output, 'TYPE'); WriteString(context^.output, 'TYPE');
@ -326,7 +306,7 @@ begin
while current_declaration^ <> nil do while current_declaration^ <> nil do
transpile_type_declaration(context, current_declaration^); transpile_type_declaration(context, current_declaration^);
INC(current_declaration, TSIZE(PAstTypeDeclaration)) INC(current_declaration, TSIZE(PAstTypedDeclaration))
end; end;
WriteLine(context^.output) WriteLine(context^.output)
end end
@ -363,51 +343,41 @@ begin
end end
end; end;
proc transpile_procedure_heading(context: PTranspilerContext) -> LexerToken; proc transpile_procedure_heading(context: PTranspilerContext, declaration: PAstProcedureDeclaration);
var var
token: LexerToken; token: LexerToken;
result: LexerToken; written_bytes: CARDINAL;
type_expression: PAstTypeExpression; parameter_index: CARDINAL;
current_parameter: PAstTypedDeclaration;
begin begin
WriteString(context^.output, 'PROCEDURE '); WriteString(context^.output, 'PROCEDURE ');
written_bytes := WriteNBytes(context^.output, ORD(declaration^.name[1]), ADR(declaration^.name[2]));
result := transpiler_lex(context^.lexer);
write_current(context^.lexer, context^.output);
token := transpiler_lex(context^.lexer);
WriteChar(context^.output, '('); WriteChar(context^.output, '(');
token := transpiler_lex(context^.lexer); parameter_index := 0;
while token.kind <> lexerKindRightParen do current_parameter := declaration^.parameters;
write_current(context^.lexer, context^.output);
token := transpiler_lex(context^.lexer); while parameter_index < declaration^.parameter_count do
written_bytes := WriteNBytes(context^.output, ORD(current_parameter^.identifier[1]), ADR(current_parameter^.identifier[2]));
WriteString(context^.output, ': '); WriteString(context^.output, ': ');
token := transpiler_lex(context^.lexer); transpile_type_expression(context, current_parameter^.type_expression);
type_expression := parse_type_expression(context^.lexer); INC(parameter_index);
transpile_type_expression(context, type_expression); INC(current_parameter, TSIZE(AstTypedDeclaration));
token := transpiler_lex(context^.lexer); if parameter_index <> declaration^.parameter_count then
if (token.kind = lexerKindSemicolon) or (token.kind = lexerKindComma) then WriteString(context^.output, '; ')
WriteString(context^.output, '; ');
token := transpiler_lex(context^.lexer)
end end
end; end;
WriteString(context^.output, ')'); WriteString(context^.output, ')');
token := transpiler_lex(context^.lexer);
(* Check for the return type and write it. *) (* Check for the return type and write it. *)
if token.kind = lexerKindArrow then if declaration^.return_type <> nil then
WriteString(context^.output, ': '); WriteString(context^.output, ': ');
token := transpiler_lex(context^.lexer); transpile_type_expression(context, declaration^.return_type)
write_current(context^.lexer, context^.output);
token := transpiler_lex(context^.lexer)
end; end;
token := transpiler_lex(context^.lexer); write_semicolon(context^.output)
write_semicolon(context^.output);
return result
end; end;
proc transpile_unary_operator(context: PTranspilerContext, operator: AstUnaryOperator); proc transpile_unary_operator(context: PTranspilerContext, operator: AstUnaryOperator);
@ -470,7 +440,7 @@ begin
if literal^.kind = astLiteralKindInteger then if literal^.kind = astLiteralKindInteger then
IntToStr(literal^.integer, 0, buffer); IntToStr(literal^.integer, 0, buffer);
WriteString(context^.output, buffer); WriteString(context^.output, buffer)
end; end;
if literal^.kind = astLiteralKindString then if literal^.kind = astLiteralKindString then
WriteString(context^.output, literal^.string) WriteString(context^.output, literal^.string)
@ -501,7 +471,7 @@ begin
if expression^.kind = astExpressionKindFieldAccess then if expression^.kind = astExpressionKindFieldAccess then
transpile_expression(context, expression^.aggregate); transpile_expression(context, expression^.aggregate);
WriteChar(context^.output, '.'); WriteChar(context^.output, '.');
written_bytes := WriteNBytes(context^.output, ORD(expression^.field[1]), ADR(expression^.field[2])); written_bytes := WriteNBytes(context^.output, ORD(expression^.field[1]), ADR(expression^.field[2]))
end; end;
if expression^.kind = astExpressionKindUnary then if expression^.kind = astExpressionKindUnary then
transpile_unary_operator(context, expression^.unary_operator); transpile_unary_operator(context, expression^.unary_operator);
@ -540,52 +510,34 @@ begin
end end
end; end;
proc transpile_if_statement(context: PTranspilerContext) -> PAstStatement; proc transpile_if_statement(context: PTranspilerContext, statement: PAstStatement);
var var
token: LexerToken; token: LexerToken;
result: PAstStatement;
begin begin
NEW(result); if statement <> nil then
result^.kind := astStatementKindIf;
WriteString(context^.output, ' IF '); WriteString(context^.output, ' IF ');
transpile_expression(context, statement^.if_condition);
token := transpiler_lex(context^.lexer);
result^.if_condition := parse_expression(context^.lexer);
transpile_expression(context, result^.if_condition);
token := lexer_current(context^.lexer);
WriteString(context^.output, ' THEN'); WriteString(context^.output, ' THEN');
WriteLine(context^.output); WriteLine(context^.output);
transpile_statements(context);
WriteString(context^.output, ' END');
token := transpiler_lex(context^.lexer);
return result transpile_compound_statement(context, statement^.if_branch);
WriteString(context^.output, ' END')
end
end; end;
proc transpile_while_statement(context: PTranspilerContext) -> PAstStatement; proc transpile_while_statement(context: PTranspilerContext, statement: PAstStatement);
var var
token: LexerToken; token: LexerToken;
result: PAstStatement;
begin begin
NEW(result);
result^.kind := astStatementKindWhile;
WriteString(context^.output, ' WHILE '); WriteString(context^.output, ' WHILE ');
transpile_expression(context, statement^.while_condition);
token := transpiler_lex(context^.lexer);
result^.while_condition := parse_expression(context^.lexer);
transpile_expression(context, result^.while_condition);
token := lexer_current(context^.lexer);
WriteString(context^.output, ' DO'); WriteString(context^.output, ' DO');
WriteLine(context^.output); WriteLine(context^.output);
transpile_statements(context);
WriteString(context^.output, ' END');
token := transpiler_lex(context^.lexer);
return result transpile_compound_statement(context, statement^.while_body);
WriteString(context^.output, ' END')
end; end;
proc transpile_assignment_statement(context: PTranspilerContext, statement: PAstStatement); proc transpile_assignment_statement(context: PTranspilerContext, statement: PAstStatement);
@ -599,108 +551,81 @@ proc transpile_return_statement(context: PTranspilerContext, statement: PAstStat
begin begin
WriteString(context^.output, ' RETURN '); WriteString(context^.output, ' RETURN ');
transpile_expression(context, statement^.returned); transpile_expression(context, statement^.returned)
end; end;
proc transpile_statement(context: PTranspilerContext); proc transpile_compound_statement(context: PTranspilerContext, statement: AstCompoundStatement);
var var
token: LexerToken; current_statement: PPAstStatement;
written_bytes: CARDINAL; index: CARDINAL;
statement: PAstStatement;
designator: PAstExpression;
begin begin
token := transpiler_lex(context^.lexer); index := 0;
current_statement := statement.statements;
if token.kind = lexerKindIf then while index < statement.count do
statement := transpile_if_statement(context) transpile_statement(context, current_statement^);
end;
if token.kind = lexerKindWhile then
statement := transpile_while_statement(context)
end;
if token.kind = lexerKindReturn then
statement := parse_return_statement(context^.lexer);
transpile_return_statement(context, statement)
end;
if token.kind = lexerKindIdentifier then
designator := parse_designator(context^.lexer);
token := lexer_current(context^.lexer);
if token.kind = lexerKindAssignment then INC(current_statement, TSIZE(PAstStatement));
statement := parse_assignment_statement(context^.lexer, designator); INC(index);
transpile_assignment_statement(context, statement)
end;
if token.kind <> lexerKindAssignment then
statement := parse_call_statement(context^.lexer, designator);
transpile_expression(context, designator);
written_bytes := WriteNBytes(StdErr, 5, context^.lexer^.start); if index <> statement.count then
WriteLine(StdErr);
end
end
end;
proc transpile_statements(context: PTranspilerContext);
var
token: LexerToken;
begin
token := lexer_current(context^.lexer);
while token.kind <> lexerKindEnd do
transpile_statement(context);
token := lexer_current(context^.lexer);
if token.kind = lexerKindSemicolon then
WriteChar(context^.output, ';') WriteChar(context^.output, ';')
end; end;
WriteLine(context^.output) WriteLine(context^.output)
end end
end; end;
proc transpile_statement_part(context: PTranspilerContext); proc transpile_statement(context: PTranspilerContext, statement: PAstStatement);
var
token: LexerToken;
begin begin
token := lexer_current(context^.lexer); if statement^.kind = astStatementKindIf then
if token.kind = lexerKindBegin then transpile_if_statement(context, statement)
WriteString(context^.output, 'BEGIN'); end;
WriteLine(context^.output); if statement^.kind = astStatementKindWhile then
transpile_statements(context) transpile_while_statement(context, statement)
end;
if statement^.kind = astStatementKindReturn then
transpile_return_statement(context, statement)
end;
if statement^.kind = astStatementKindAssignment then
transpile_assignment_statement(context, statement)
end;
if statement^.kind = astStatementKindCall then
transpile_expression(context, statement^.call)
end end
end; end;
proc transpile_procedure_declaration(context: PTranspilerContext); proc transpile_statement_part(context: PTranspilerContext, compound: AstCompoundStatement);
var
token: LexerToken;
seen_variables: PPAstVariableDeclaration;
written_bytes: CARDINAL;
seen_constants: PPAstConstantDeclaration;
begin begin
token := transpile_procedure_heading(context); if compound.count > 0 then
seen_constants := parse_constant_part(context^.lexer); WriteString(context^.output, 'BEGIN');
transpile_constant_part(context, seen_constants); WriteLine(context^.output);
transpile_compound_statement(context, compound)
seen_variables := parse_variable_part(context^.lexer); end
transpile_variable_part(context, seen_variables);
transpile_statement_part(context);
WriteString(context^.output, 'END ');
written_bytes := WriteNBytes(context^.output, ORD(token.identifierKind[1]), ADR(token.identifierKind[2]));
token := transpiler_lex(context^.lexer);
write_semicolon(context^.output);
token := transpiler_lex(context^.lexer)
end; end;
proc transpile_procedure_part(context: PTranspilerContext); proc transpile_procedure_declaration(context: PTranspilerContext, declaration: PAstProcedureDeclaration);
var var
token: LexerToken; written_bytes: CARDINAL;
begin begin
token := lexer_current(context^.lexer); transpile_procedure_heading(context, declaration);
while token.kind = lexerKindProc do transpile_constant_part(context, declaration^.constants);
transpile_procedure_declaration(context); transpile_variable_part(context, declaration^.variables);
token := lexer_current(context^.lexer); transpile_statement_part(context, declaration^.statements);
WriteLine(context^.output)
WriteString(context^.output, 'END ');
written_bytes := WriteNBytes(context^.output, ORD(declaration^.name[1]), ADR(declaration^.name[2]));
write_semicolon(context^.output)
end;
proc transpile_procedure_part(context: PTranspilerContext, declaration: PPAstProcedureDeclaration);
begin
while declaration^ <> nil do
transpile_procedure_declaration(context, declaration^);
WriteLine(context^.output);
INC(declaration, TSIZE(PAstProcedureDeclaration))
end end
end; end;
@ -728,20 +653,17 @@ begin
while (context^.input_name[counter] <> '.') & (ORD(context^.input_name[counter]) <> 0) do while (context^.input_name[counter] <> '.') & (ORD(context^.input_name[counter]) <> 0) do
WriteChar(context^.output, context^.input_name[counter]); WriteChar(context^.output, context^.input_name[counter]);
INC(counter) INC(counter)
end; end
end; end;
proc transpile(lexer: PLexer, output: File, input_name: ShortString); proc transpile(ast_module: PAstModule, output: File, input_name: ShortString);
var var
token: LexerToken;
context: TranspilerContext; context: TranspilerContext;
ast_module: PAstModule;
begin begin
context.input_name := input_name; context.input_name := input_name;
context.output := output; context.output := output;
context.lexer := lexer;
ast_module := transpile_module(ADR(context)) transpile_module(ADR(context), ast_module)
end; end;
end. end.