|
|
|
@@ -1,27 +1,34 @@
|
|
|
|
|
(* This Source Code Form is subject to the terms of the Mozilla Public License,
|
|
|
|
|
v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
|
|
|
|
obtain one at https://mozilla.org/MPL/2.0/. *)
|
|
|
|
|
module;
|
|
|
|
|
|
|
|
|
|
from FIO import WriteNBytes, WriteLine, WriteChar, WriteString;
|
|
|
|
|
from SYSTEM import ADR, TSIZE;
|
|
|
|
|
|
|
|
|
|
from FIO import File, WriteNBytes, WriteLine, WriteChar, WriteString;
|
|
|
|
|
from NumberIO import IntToStr;
|
|
|
|
|
|
|
|
|
|
from Common import Identifier, PIdentifier, ShortString;
|
|
|
|
|
from Common import Identifier, ShortString;
|
|
|
|
|
from Parser import AstTypeExpressionKind, AstExpressionKind, AstLiteralKind, AstUnaryOperator, AstBinaryOperator,
|
|
|
|
|
PAstModule, PPAstExpression, PAstExpression, PAstLiteral, PPAstProcedureDeclaration,
|
|
|
|
|
PAstConstantDeclaration, PPAstConstantDeclaration, PPAstStatement, PAstStatement, AstStatementKind,
|
|
|
|
|
AstTypedDeclaration, PAstTypedDeclaration, PPAstTypedDeclaration, AstCompoundStatement, PAstProcedureDeclaration,
|
|
|
|
|
PAstVariableDeclaration, PPAstVariableDeclaration, PAstImportStatement, PPAstImportStatement,
|
|
|
|
|
PAstTypeExpression, PPAstTypeExpression, AstFieldDeclaration, PAstFieldDeclaration;
|
|
|
|
|
AstModule, AstExpression, AstLiteral, AstConstantDeclaration, AstStatement, AstStatementKind,
|
|
|
|
|
AstTypedDeclaration, AstCompoundStatement, AstProcedureDeclaration,
|
|
|
|
|
AstVariableDeclaration, AstImportStatement, AstTypeExpression, AstFieldDeclaration;
|
|
|
|
|
|
|
|
|
|
proc indent(context: PTranspilerContext);
|
|
|
|
|
type
|
|
|
|
|
TranspilerContext* = record
|
|
|
|
|
input_name: ShortString;
|
|
|
|
|
output: File;
|
|
|
|
|
definition: File;
|
|
|
|
|
indentation: Word
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc indent(context: ^TranspilerContext);
|
|
|
|
|
var
|
|
|
|
|
count: CARDINAL;
|
|
|
|
|
count: Word;
|
|
|
|
|
begin
|
|
|
|
|
count := 0;
|
|
|
|
|
|
|
|
|
|
while count < context^.indentation do
|
|
|
|
|
WriteString(context^.output, ' ');
|
|
|
|
|
INC(count)
|
|
|
|
|
count := count + 1u
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
@@ -32,46 +39,46 @@ begin
|
|
|
|
|
WriteLine(output)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_import_statement(context: PTranspilerContext, import_statement: PAstImportStatement);
|
|
|
|
|
proc transpile_import_statement(context: ^TranspilerContext, import_statement: ^AstImportStatement);
|
|
|
|
|
var
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
current_symbol: PIdentifier;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
current_symbol: ^Identifier;
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, 'FROM ');
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(import_statement^.package[1]), ADR(import_statement^.package[2]));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(import_statement^.package[1]), @import_statement^.package[2]);
|
|
|
|
|
|
|
|
|
|
WriteString(context^.output, ' IMPORT ');
|
|
|
|
|
|
|
|
|
|
current_symbol := import_statement^.symbols;
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), ADR(current_symbol^[2]));
|
|
|
|
|
INC(current_symbol, TSIZE(Identifier));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), @current_symbol^[2]);
|
|
|
|
|
INC(current_symbol, #size(Identifier));
|
|
|
|
|
|
|
|
|
|
while ORD(current_symbol^[1]) <> 0 do
|
|
|
|
|
WriteString(context^.output, ', ');
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), ADR(current_symbol^[2]));
|
|
|
|
|
INC(current_symbol, TSIZE(Identifier))
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), @current_symbol^[2]);
|
|
|
|
|
INC(current_symbol, #size(Identifier))
|
|
|
|
|
end;
|
|
|
|
|
write_semicolon(context^.output)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_import_part(context: PTranspilerContext, imports: PPAstImportStatement);
|
|
|
|
|
proc transpile_import_part(context: ^TranspilerContext, imports: ^^AstImportStatement);
|
|
|
|
|
var
|
|
|
|
|
import_statement: PAstImportStatement;
|
|
|
|
|
import_statement: ^AstImportStatement;
|
|
|
|
|
begin
|
|
|
|
|
while imports^ <> nil do
|
|
|
|
|
transpile_import_statement(context, imports^);
|
|
|
|
|
INC(imports, TSIZE(PAstImportStatement))
|
|
|
|
|
INC(imports, #size(^AstImportStatement))
|
|
|
|
|
end;
|
|
|
|
|
WriteLine(context^.output)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_constant_declaration(context: PTranspilerContext, declaration: PAstConstantDeclaration);
|
|
|
|
|
proc transpile_constant_declaration(context: ^TranspilerContext, declaration: ^AstConstantDeclaration);
|
|
|
|
|
var
|
|
|
|
|
buffer: [20]CHAR;
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, ' ');
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.constant_name[1]), ADR(declaration^.constant_name[2]));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.constant_name[1]), @declaration^.constant_name[2]);
|
|
|
|
|
|
|
|
|
|
WriteString(context^.output, ' = ');
|
|
|
|
|
|
|
|
|
@@ -81,9 +88,9 @@ begin
|
|
|
|
|
write_semicolon(context^.output)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_constant_part(context: PTranspilerContext, declarations: PPAstConstantDeclaration, extra_newline: BOOLEAN);
|
|
|
|
|
proc transpile_constant_part(context: ^TranspilerContext, declarations: ^^AstConstantDeclaration, extra_newline: Bool);
|
|
|
|
|
var
|
|
|
|
|
current_declaration: PPAstConstantDeclaration;
|
|
|
|
|
current_declaration: ^^AstConstantDeclaration;
|
|
|
|
|
begin
|
|
|
|
|
if declarations^ <> nil then
|
|
|
|
|
WriteString(context^.output, 'CONST');
|
|
|
|
@@ -93,7 +100,7 @@ begin
|
|
|
|
|
while current_declaration^ <> nil do
|
|
|
|
|
transpile_constant_declaration(context, current_declaration^);
|
|
|
|
|
|
|
|
|
|
INC(current_declaration, TSIZE(PAstConstantDeclaration))
|
|
|
|
|
INC(current_declaration, #size(^AstConstantDeclaration))
|
|
|
|
|
end;
|
|
|
|
|
if extra_newline then
|
|
|
|
|
WriteLine(context^.output)
|
|
|
|
@@ -101,7 +108,7 @@ begin
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_module(context: PTranspilerContext, result: PAstModule);
|
|
|
|
|
proc transpile_module(context: ^TranspilerContext, result: ^AstModule);
|
|
|
|
|
begin
|
|
|
|
|
if result^.main = false then
|
|
|
|
|
WriteString(context^.output, 'IMPLEMENTATION ')
|
|
|
|
@@ -130,21 +137,21 @@ begin
|
|
|
|
|
WriteLine(context^.output)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_type_fields(context: PTranspilerContext, fields: PAstFieldDeclaration);
|
|
|
|
|
proc transpile_type_fields(context: ^TranspilerContext, fields: ^AstFieldDeclaration);
|
|
|
|
|
var
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
current_field: PAstFieldDeclaration;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
current_field: ^AstFieldDeclaration;
|
|
|
|
|
begin
|
|
|
|
|
current_field := fields;
|
|
|
|
|
|
|
|
|
|
while ORD(current_field^.field_name[1]) <> 0 do
|
|
|
|
|
WriteString(context^.output, ' ');
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_field^.field_name[1]), ADR(current_field^.field_name[2]));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_field^.field_name[1]), @current_field^.field_name[2]);
|
|
|
|
|
|
|
|
|
|
WriteString(context^.output, ': ');
|
|
|
|
|
transpile_type_expression(context, current_field^.field_type);
|
|
|
|
|
|
|
|
|
|
INC(current_field , TSIZE(AstFieldDeclaration));
|
|
|
|
|
INC(current_field , #size(AstFieldDeclaration));
|
|
|
|
|
|
|
|
|
|
if ORD(current_field^.field_name[1]) <> 0 then
|
|
|
|
|
WriteChar(context^.output, ';')
|
|
|
|
@@ -153,7 +160,7 @@ begin
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_record_type(context: PTranspilerContext, type_expression: PAstTypeExpression);
|
|
|
|
|
proc transpile_record_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, 'RECORD');
|
|
|
|
|
WriteLine(context^.output);
|
|
|
|
@@ -161,14 +168,14 @@ begin
|
|
|
|
|
WriteString(context^.output, ' END')
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_pointer_type(context: PTranspilerContext, type_expression: PAstTypeExpression);
|
|
|
|
|
proc transpile_pointer_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, 'POINTER TO ');
|
|
|
|
|
|
|
|
|
|
transpile_type_expression(context, type_expression^.target)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_array_type(context: PTranspilerContext, type_expression: PAstTypeExpression);
|
|
|
|
|
proc transpile_array_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
|
|
|
|
var
|
|
|
|
|
buffer: [20]CHAR;
|
|
|
|
|
begin
|
|
|
|
@@ -187,43 +194,43 @@ begin
|
|
|
|
|
transpile_type_expression(context, type_expression^.base)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_enumeration_type(context: PTranspilerContext, type_expression: PAstTypeExpression);
|
|
|
|
|
proc transpile_enumeration_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
|
|
|
|
var
|
|
|
|
|
current_case: PIdentifier;
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
current_case: ^Identifier;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
begin
|
|
|
|
|
current_case := type_expression^.cases;
|
|
|
|
|
|
|
|
|
|
WriteString(context^.output, '(');
|
|
|
|
|
WriteLine(context^.output);
|
|
|
|
|
WriteString(context^.output, ' ');
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_case^[1]), ADR(current_case^[2]));
|
|
|
|
|
INC(current_case, TSIZE(Identifier));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_case^[1]), @current_case^[2]);
|
|
|
|
|
INC(current_case, #size(Identifier));
|
|
|
|
|
|
|
|
|
|
while ORD(current_case^[1]) <> 0 do
|
|
|
|
|
WriteChar(context^.output, ',');
|
|
|
|
|
WriteLine(context^.output);
|
|
|
|
|
WriteString(context^.output, ' ');
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_case^[1]), ADR(current_case^[2]));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_case^[1]), @current_case^[2]);
|
|
|
|
|
|
|
|
|
|
INC(current_case, TSIZE(Identifier))
|
|
|
|
|
INC(current_case, #size(Identifier))
|
|
|
|
|
end;
|
|
|
|
|
WriteLine(context^.output);
|
|
|
|
|
WriteString(context^.output, ' )')
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_named_type(context: PTranspilerContext, type_expression: PAstTypeExpression);
|
|
|
|
|
proc transpile_named_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
|
|
|
|
var
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
begin
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(type_expression^.name[1]), ADR(type_expression^.name[2]))
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(type_expression^.name[1]), @type_expression^.name[2])
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_procedure_type(context: PTranspilerContext, type_expression: PAstTypeExpression);
|
|
|
|
|
proc transpile_procedure_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
|
|
|
|
var
|
|
|
|
|
result: PAstTypeExpression;
|
|
|
|
|
current_parameter: PPAstTypeExpression;
|
|
|
|
|
parameter_count: CARDINAL;
|
|
|
|
|
result: ^AstTypeExpression;
|
|
|
|
|
current_parameter: ^^AstTypeExpression;
|
|
|
|
|
parameter_count: Word;
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, 'PROCEDURE(');
|
|
|
|
|
current_parameter := type_expression^.parameters;
|
|
|
|
@@ -231,7 +238,7 @@ begin
|
|
|
|
|
while current_parameter^ <> nil do
|
|
|
|
|
transpile_type_expression(context, current_parameter^);
|
|
|
|
|
|
|
|
|
|
INC(current_parameter, TSIZE(PAstTypeExpression));
|
|
|
|
|
INC(current_parameter, #size(^AstTypeExpression));
|
|
|
|
|
|
|
|
|
|
if current_parameter^ <> nil then
|
|
|
|
|
WriteString(context^.output, ', ')
|
|
|
|
@@ -240,7 +247,7 @@ begin
|
|
|
|
|
WriteChar(context^.output, ')')
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_type_expression(context: PTranspilerContext, type_expression: PAstTypeExpression);
|
|
|
|
|
proc transpile_type_expression(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
|
|
|
|
begin
|
|
|
|
|
if type_expression^.kind = astTypeExpressionKindRecord then
|
|
|
|
|
transpile_record_type(context, type_expression)
|
|
|
|
@@ -262,22 +269,22 @@ begin
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_type_declaration(context: PTranspilerContext, declaration: PAstTypedDeclaration);
|
|
|
|
|
proc transpile_type_declaration(context: ^TranspilerContext, declaration: ^AstTypedDeclaration);
|
|
|
|
|
var
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, ' ');
|
|
|
|
|
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.identifier[1]), ADR(declaration^.identifier[2]));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.identifier[1]), @declaration^.identifier[2]);
|
|
|
|
|
WriteString(context^.output, ' = ');
|
|
|
|
|
|
|
|
|
|
transpile_type_expression(context, declaration^.type_expression);
|
|
|
|
|
write_semicolon(context^.output)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_type_part(context: PTranspilerContext, declarations: PPAstTypedDeclaration);
|
|
|
|
|
proc transpile_type_part(context: ^TranspilerContext, declarations: ^^AstTypedDeclaration);
|
|
|
|
|
var
|
|
|
|
|
current_declaration: PPAstTypedDeclaration;
|
|
|
|
|
current_declaration: ^^AstTypedDeclaration;
|
|
|
|
|
begin
|
|
|
|
|
if declarations^ <> nil then
|
|
|
|
|
WriteString(context^.output, 'TYPE');
|
|
|
|
@@ -287,18 +294,18 @@ begin
|
|
|
|
|
while current_declaration^ <> nil do
|
|
|
|
|
transpile_type_declaration(context, current_declaration^);
|
|
|
|
|
|
|
|
|
|
INC(current_declaration, TSIZE(PAstTypedDeclaration))
|
|
|
|
|
INC(current_declaration, #size(^AstTypedDeclaration))
|
|
|
|
|
end;
|
|
|
|
|
WriteLine(context^.output)
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_variable_declaration(context: PTranspilerContext, declaration: PAstVariableDeclaration);
|
|
|
|
|
proc transpile_variable_declaration(context: ^TranspilerContext, declaration: ^AstVariableDeclaration);
|
|
|
|
|
var
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, ' ');
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.variable_name[1]), ADR(declaration^.variable_name[2]));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.variable_name[1]), @declaration^.variable_name[2]);
|
|
|
|
|
|
|
|
|
|
WriteString(context^.output, ': ');
|
|
|
|
|
|
|
|
|
@@ -306,9 +313,9 @@ begin
|
|
|
|
|
write_semicolon(context^.output)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_variable_part(context: PTranspilerContext, declarations: PPAstVariableDeclaration, extra_newline: BOOLEAN);
|
|
|
|
|
proc transpile_variable_part(context: ^TranspilerContext, declarations: ^^AstVariableDeclaration, extra_newline: Bool);
|
|
|
|
|
var
|
|
|
|
|
current_declaration: PPAstVariableDeclaration;
|
|
|
|
|
current_declaration: ^^AstVariableDeclaration;
|
|
|
|
|
begin
|
|
|
|
|
if declarations^ <> nil then
|
|
|
|
|
WriteString(context^.output, 'VAR');
|
|
|
|
@@ -318,7 +325,7 @@ begin
|
|
|
|
|
while current_declaration^ <> nil do
|
|
|
|
|
transpile_variable_declaration(context, current_declaration^);
|
|
|
|
|
|
|
|
|
|
INC(current_declaration, TSIZE(PAstVariableDeclaration))
|
|
|
|
|
INC(current_declaration, #size(^AstVariableDeclaration))
|
|
|
|
|
end;
|
|
|
|
|
if extra_newline then
|
|
|
|
|
WriteLine(context^.output)
|
|
|
|
@@ -326,26 +333,26 @@ begin
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_procedure_heading(context: PTranspilerContext, declaration: PAstProcedureDeclaration);
|
|
|
|
|
proc transpile_procedure_heading(context: ^TranspilerContext, declaration: ^AstProcedureDeclaration);
|
|
|
|
|
var
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
parameter_index: CARDINAL;
|
|
|
|
|
current_parameter: PAstTypedDeclaration;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
parameter_index: Word;
|
|
|
|
|
current_parameter: ^AstTypedDeclaration;
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, 'PROCEDURE ');
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.name[1]), ADR(declaration^.name[2]));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.name[1]), @declaration^.name[2]);
|
|
|
|
|
WriteChar(context^.output, '(');
|
|
|
|
|
|
|
|
|
|
parameter_index := 0;
|
|
|
|
|
current_parameter := declaration^.parameters;
|
|
|
|
|
|
|
|
|
|
while parameter_index < declaration^.parameter_count do
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_parameter^.identifier[1]), ADR(current_parameter^.identifier[2]));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(current_parameter^.identifier[1]), @current_parameter^.identifier[2]);
|
|
|
|
|
WriteString(context^.output, ': ');
|
|
|
|
|
transpile_type_expression(context, current_parameter^.type_expression);
|
|
|
|
|
|
|
|
|
|
INC(parameter_index);
|
|
|
|
|
INC(current_parameter, TSIZE(AstTypedDeclaration));
|
|
|
|
|
parameter_index := parameter_index + 1u;
|
|
|
|
|
INC(current_parameter, #size(AstTypedDeclaration));
|
|
|
|
|
|
|
|
|
|
if parameter_index <> declaration^.parameter_count then
|
|
|
|
|
WriteString(context^.output, '; ')
|
|
|
|
@@ -362,83 +369,63 @@ begin
|
|
|
|
|
write_semicolon(context^.output)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_unary_operator(context: PTranspilerContext, operator: AstUnaryOperator);
|
|
|
|
|
proc transpile_unary_operator(context: ^TranspilerContext, operator: AstUnaryOperator);
|
|
|
|
|
begin
|
|
|
|
|
if operator = astUnaryOperatorMinus then
|
|
|
|
|
if operator = AstUnaryOperator.minus then
|
|
|
|
|
WriteChar(context^.output, '-')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astUnaryOperatorNot then
|
|
|
|
|
if operator = AstUnaryOperator.not then
|
|
|
|
|
WriteChar(context^.output, '~')
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_binary_operator(context: PTranspilerContext, operator: AstBinaryOperator);
|
|
|
|
|
proc transpile_binary_operator(context: ^TranspilerContext, operator: AstBinaryOperator);
|
|
|
|
|
begin
|
|
|
|
|
if operator = astBinaryOperatorSum then
|
|
|
|
|
WriteChar(context^.output, '+')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorSubtraction then
|
|
|
|
|
WriteChar(context^.output, '-')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorMultiplication then
|
|
|
|
|
WriteChar(context^.output, '*')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorEquals then
|
|
|
|
|
WriteChar(context^.output, '=')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorNotEquals then
|
|
|
|
|
WriteChar(context^.output, '#')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorLess then
|
|
|
|
|
WriteChar(context^.output, '<')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorGreater then
|
|
|
|
|
WriteChar(context^.output, '>')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorLessEqual then
|
|
|
|
|
WriteString(context^.output, '<=')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorGreaterEqual then
|
|
|
|
|
WriteString(context^.output, '>=')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorDisjunction then
|
|
|
|
|
WriteString(context^.output, 'OR')
|
|
|
|
|
end;
|
|
|
|
|
if operator = astBinaryOperatorConjunction then
|
|
|
|
|
WriteString(context^.output, 'AND')
|
|
|
|
|
case operator of
|
|
|
|
|
AstBinaryOperator.sum: WriteChar(context^.output, '+')
|
|
|
|
|
| AstBinaryOperator.subtraction: WriteChar(context^.output, '-')
|
|
|
|
|
| AstBinaryOperator.multiplication: WriteChar(context^.output, '*')
|
|
|
|
|
| AstBinaryOperator.equals: WriteChar(context^.output, '=')
|
|
|
|
|
| AstBinaryOperator.not_equals: WriteChar(context^.output, '#')
|
|
|
|
|
| AstBinaryOperator.less: WriteChar(context^.output, '<')
|
|
|
|
|
| AstBinaryOperator.greater: WriteChar(context^.output, '>')
|
|
|
|
|
| AstBinaryOperator.less_equal: WriteString(context^.output, '<=')
|
|
|
|
|
| AstBinaryOperator.greater_equal: WriteString(context^.output, '>=')
|
|
|
|
|
| AstBinaryOperator.disjunction: WriteString(context^.output, 'OR')
|
|
|
|
|
| AstBinaryOperatorConjunction: WriteString(context^.output, 'AND')
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_expression(context: PTranspilerContext, expression: PAstExpression);
|
|
|
|
|
proc transpile_expression(context: ^TranspilerContext, expression: ^AstExpression);
|
|
|
|
|
var
|
|
|
|
|
literal: PAstLiteral;
|
|
|
|
|
literal: ^AstLiteral;
|
|
|
|
|
buffer: [20]CHAR;
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
argument_index: CARDINAL;
|
|
|
|
|
current_argument: PPAstExpression;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
argument_index: Word;
|
|
|
|
|
current_argument: ^^AstExpression;
|
|
|
|
|
begin
|
|
|
|
|
if expression^.kind = astExpressionKindLiteral then
|
|
|
|
|
literal := expression^.literal;
|
|
|
|
|
|
|
|
|
|
if literal^.kind = astLiteralKindInteger then
|
|
|
|
|
if literal^.kind = AstLiteralKind.integer then
|
|
|
|
|
IntToStr(literal^.integer, 0, buffer);
|
|
|
|
|
WriteString(context^.output, buffer)
|
|
|
|
|
end;
|
|
|
|
|
if literal^.kind = astLiteralKindString then
|
|
|
|
|
if literal^.kind = AstLiteralKind.string then
|
|
|
|
|
WriteString(context^.output, literal^.string)
|
|
|
|
|
end;
|
|
|
|
|
if literal^.kind = astLiteralKindNull then
|
|
|
|
|
if literal^.kind = AstLiteralKind.null then
|
|
|
|
|
WriteString(context^.output, 'NIL')
|
|
|
|
|
end;
|
|
|
|
|
if (literal^.kind = astLiteralKindBoolean) & literal^.boolean then
|
|
|
|
|
if (literal^.kind = AstLiteralKind.boolean) & literal^.boolean then
|
|
|
|
|
WriteString(context^.output, 'TRUE')
|
|
|
|
|
end;
|
|
|
|
|
if (literal^.kind = astLiteralKindBoolean) & (literal^.boolean = false) then
|
|
|
|
|
if (literal^.kind = AstLiteralKind.boolean) & (literal^.boolean = false) then
|
|
|
|
|
WriteString(context^.output, 'FALSE')
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
if expression^.kind = astExpressionKindIdentifier then
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(expression^.identifier[1]), ADR(expression^.identifier[2]))
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(expression^.identifier[1]), @expression^.identifier[2])
|
|
|
|
|
end;
|
|
|
|
|
if expression^.kind = astExpressionKindDereference then
|
|
|
|
|
transpile_expression(context, expression^.reference);
|
|
|
|
@@ -453,7 +440,7 @@ begin
|
|
|
|
|
if expression^.kind = astExpressionKindFieldAccess then
|
|
|
|
|
transpile_expression(context, expression^.aggregate);
|
|
|
|
|
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]), @expression^.field[2])
|
|
|
|
|
end;
|
|
|
|
|
if expression^.kind = astExpressionKindUnary then
|
|
|
|
|
transpile_unary_operator(context, expression^.unary_operator);
|
|
|
|
@@ -476,70 +463,70 @@ begin
|
|
|
|
|
if expression^.argument_count > 0 then
|
|
|
|
|
transpile_expression(context, current_argument^);
|
|
|
|
|
|
|
|
|
|
argument_index := 1;
|
|
|
|
|
INC(current_argument, TSIZE(PAstExpression));
|
|
|
|
|
argument_index := 1u;
|
|
|
|
|
INC(current_argument, #size(^AstExpression));
|
|
|
|
|
|
|
|
|
|
while argument_index < expression^.argument_count do
|
|
|
|
|
WriteString(context^.output, ', ');
|
|
|
|
|
|
|
|
|
|
transpile_expression(context, current_argument^);
|
|
|
|
|
|
|
|
|
|
INC(current_argument, TSIZE(PAstExpression));
|
|
|
|
|
INC(argument_index)
|
|
|
|
|
INC(current_argument, #size(^AstExpression));
|
|
|
|
|
argument_index := argument_index + 1u
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
WriteChar(context^.output, ')')
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_if_statement(context: PTranspilerContext, statement: PAstStatement);
|
|
|
|
|
proc transpile_if_statement(context: ^TranspilerContext, statement: ^AstStatement);
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, 'IF ');
|
|
|
|
|
transpile_expression(context, statement^.if_condition);
|
|
|
|
|
|
|
|
|
|
WriteString(context^.output, ' THEN');
|
|
|
|
|
WriteLine(context^.output);
|
|
|
|
|
INC(context^.indentation);
|
|
|
|
|
context^.indentation := context^.indentation + 1u;
|
|
|
|
|
|
|
|
|
|
transpile_compound_statement(context, statement^.if_branch);
|
|
|
|
|
DEC(context^.indentation);
|
|
|
|
|
context^.indentation := context^.indentation - 1u;
|
|
|
|
|
indent(context);
|
|
|
|
|
WriteString(context^.output, 'END')
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_while_statement(context: PTranspilerContext, statement: PAstStatement);
|
|
|
|
|
proc transpile_while_statement(context: ^TranspilerContext, statement: ^AstStatement);
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, 'WHILE ');
|
|
|
|
|
transpile_expression(context, statement^.while_condition);
|
|
|
|
|
|
|
|
|
|
WriteString(context^.output, ' DO');
|
|
|
|
|
WriteLine(context^.output);
|
|
|
|
|
INC(context^.indentation);
|
|
|
|
|
context^.indentation := context^.indentation + 1u;
|
|
|
|
|
|
|
|
|
|
transpile_compound_statement(context, statement^.while_body);
|
|
|
|
|
DEC(context^.indentation);
|
|
|
|
|
context^.indentation := context^.indentation - 1u;
|
|
|
|
|
indent(context);
|
|
|
|
|
WriteString(context^.output, 'END')
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_assignment_statement(context: PTranspilerContext, statement: PAstStatement);
|
|
|
|
|
proc transpile_assignment_statement(context: ^TranspilerContext, statement: ^AstStatement);
|
|
|
|
|
begin
|
|
|
|
|
transpile_expression(context, statement^.assignee);
|
|
|
|
|
WriteString(context^.output, ' := ');
|
|
|
|
|
transpile_expression(context, statement^.assignment)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_return_statement(context: PTranspilerContext, statement: PAstStatement);
|
|
|
|
|
proc transpile_return_statement(context: ^TranspilerContext, statement: ^AstStatement);
|
|
|
|
|
begin
|
|
|
|
|
WriteString(context^.output, 'RETURN ');
|
|
|
|
|
|
|
|
|
|
transpile_expression(context, statement^.returned)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_compound_statement(context: PTranspilerContext, statement: AstCompoundStatement);
|
|
|
|
|
proc transpile_compound_statement(context: ^TranspilerContext, statement: AstCompoundStatement);
|
|
|
|
|
var
|
|
|
|
|
current_statement: PPAstStatement;
|
|
|
|
|
index: CARDINAL;
|
|
|
|
|
current_statement: ^^AstStatement;
|
|
|
|
|
index: Word;
|
|
|
|
|
begin
|
|
|
|
|
index := 0;
|
|
|
|
|
current_statement := statement.statements;
|
|
|
|
@@ -547,8 +534,8 @@ begin
|
|
|
|
|
while index < statement.count do
|
|
|
|
|
transpile_statement(context, current_statement^);
|
|
|
|
|
|
|
|
|
|
INC(current_statement, TSIZE(PAstStatement));
|
|
|
|
|
INC(index);
|
|
|
|
|
INC(current_statement, #size(^AstStatement));
|
|
|
|
|
index := index + 1u;
|
|
|
|
|
|
|
|
|
|
if index <> statement.count then
|
|
|
|
|
WriteChar(context^.output, ';')
|
|
|
|
@@ -557,7 +544,7 @@ begin
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_statement(context: PTranspilerContext, statement: PAstStatement);
|
|
|
|
|
proc transpile_statement(context: ^TranspilerContext, statement: ^AstStatement);
|
|
|
|
|
begin
|
|
|
|
|
indent(context);
|
|
|
|
|
|
|
|
|
@@ -578,21 +565,21 @@ begin
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_statement_part(context: PTranspilerContext, compound: AstCompoundStatement);
|
|
|
|
|
proc transpile_statement_part(context: ^TranspilerContext, compound: AstCompoundStatement);
|
|
|
|
|
begin
|
|
|
|
|
if compound.count > 0 then
|
|
|
|
|
WriteString(context^.output, 'BEGIN');
|
|
|
|
|
WriteLine(context^.output);
|
|
|
|
|
|
|
|
|
|
INC(context^.indentation);
|
|
|
|
|
context^.indentation := context^.indentation + 1u;
|
|
|
|
|
transpile_compound_statement(context, compound);
|
|
|
|
|
DEC(context^.indentation)
|
|
|
|
|
context^.indentation := context^.indentation - 1u;
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_procedure_declaration(context: PTranspilerContext, declaration: PAstProcedureDeclaration);
|
|
|
|
|
proc transpile_procedure_declaration(context: ^TranspilerContext, declaration: ^AstProcedureDeclaration);
|
|
|
|
|
var
|
|
|
|
|
written_bytes: CARDINAL;
|
|
|
|
|
written_bytes: Word;
|
|
|
|
|
begin
|
|
|
|
|
transpile_procedure_heading(context, declaration);
|
|
|
|
|
|
|
|
|
@@ -601,58 +588,58 @@ begin
|
|
|
|
|
transpile_statement_part(context, declaration^.statements);
|
|
|
|
|
|
|
|
|
|
WriteString(context^.output, 'END ');
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.name[1]), ADR(declaration^.name[2]));
|
|
|
|
|
written_bytes := WriteNBytes(context^.output, ORD(declaration^.name[1]), @declaration^.name[2]);
|
|
|
|
|
|
|
|
|
|
write_semicolon(context^.output)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_procedure_part(context: PTranspilerContext, declaration: PPAstProcedureDeclaration);
|
|
|
|
|
proc transpile_procedure_part(context: ^TranspilerContext, declaration: ^^AstProcedureDeclaration);
|
|
|
|
|
begin
|
|
|
|
|
while declaration^ <> nil do
|
|
|
|
|
transpile_procedure_declaration(context, declaration^);
|
|
|
|
|
WriteLine(context^.output);
|
|
|
|
|
|
|
|
|
|
INC(declaration, TSIZE(PAstProcedureDeclaration))
|
|
|
|
|
INC(declaration, #size(^AstProcedureDeclaration))
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile_module_name(context: PTranspilerContext);
|
|
|
|
|
proc transpile_module_name(context: ^TranspilerContext);
|
|
|
|
|
var
|
|
|
|
|
counter: CARDINAL;
|
|
|
|
|
last_slash: CARDINAL;
|
|
|
|
|
counter: Word;
|
|
|
|
|
last_slash: Word;
|
|
|
|
|
begin
|
|
|
|
|
counter := 1;
|
|
|
|
|
last_slash := 0;
|
|
|
|
|
counter := 1u;
|
|
|
|
|
last_slash := 0u;
|
|
|
|
|
|
|
|
|
|
while (context^.input_name[counter] <> '.') & (ORD(context^.input_name[counter]) <> 0) do
|
|
|
|
|
if context^.input_name[counter] = '/' then
|
|
|
|
|
last_slash := counter
|
|
|
|
|
end;
|
|
|
|
|
INC(counter)
|
|
|
|
|
counter := counter + 1u
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
if last_slash = 0 then
|
|
|
|
|
counter := 1
|
|
|
|
|
if last_slash = 0u then
|
|
|
|
|
counter := 1u
|
|
|
|
|
end;
|
|
|
|
|
if last_slash <> 0 then
|
|
|
|
|
counter := last_slash + 1
|
|
|
|
|
if last_slash <> 0u then
|
|
|
|
|
counter := last_slash + 1u
|
|
|
|
|
end;
|
|
|
|
|
while (context^.input_name[counter] <> '.') & (ORD(context^.input_name[counter]) <> 0) do
|
|
|
|
|
WriteChar(context^.output, context^.input_name[counter]);
|
|
|
|
|
INC(counter)
|
|
|
|
|
counter := counter + 1u
|
|
|
|
|
end
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
proc transpile(ast_module: PAstModule, output: File, definition: File, input_name: ShortString);
|
|
|
|
|
proc transpile*(ast_module: ^AstModule, output: File, definition: File, input_name: ShortString);
|
|
|
|
|
var
|
|
|
|
|
context: TranspilerContext;
|
|
|
|
|
begin
|
|
|
|
|
context.input_name := input_name;
|
|
|
|
|
context.output := output;
|
|
|
|
|
context.definition := definition;
|
|
|
|
|
context.indentation := 0;
|
|
|
|
|
context.indentation := 0u;
|
|
|
|
|
|
|
|
|
|
transpile_module(ADR(context), ast_module)
|
|
|
|
|
transpile_module(@context, ast_module)
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end.
|
|
|
|
|