Add an empty cstdlib.elna source file
This commit is contained in:
@@ -6,14 +6,11 @@ module;
|
||||
from FIO import File, WriteNBytes, WriteLine, WriteChar, WriteString;
|
||||
from NumberIO import IntToStr;
|
||||
|
||||
from Common import Identifier, ShortString;
|
||||
from Parser import AstLiteralKind, AstUnaryOperator, AstBinaryOperator, AstModule, AstExpression, AstLiteral,
|
||||
AstConstantDeclaration, AstStatement, AstTypedDeclaration, AstCompoundStatement, AstProcedureDeclaration,
|
||||
AstVariableDeclaration, AstImportStatement, AstTypeExpression, AstFieldDeclaration;
|
||||
import common, Parser;
|
||||
|
||||
type
|
||||
TranspilerContext* = record
|
||||
input_name: ShortString;
|
||||
input_name: String;
|
||||
output: File;
|
||||
definition: File;
|
||||
indentation: Word
|
||||
@@ -26,7 +23,7 @@ begin
|
||||
count := 0;
|
||||
|
||||
while count < context^.indentation do
|
||||
WriteString(context^.output, ' ');
|
||||
WriteString(context^.output, " ");
|
||||
count := count + 1u
|
||||
end
|
||||
end;
|
||||
@@ -43,17 +40,17 @@ var
|
||||
written_bytes: Word;
|
||||
current_symbol: ^Identifier;
|
||||
begin
|
||||
WriteString(context^.output, 'FROM ');
|
||||
WriteString(context^.output, "FROM ");
|
||||
written_bytes := WriteNBytes(context^.output, ORD(import_statement^.package[1]), @import_statement^.package[2]);
|
||||
|
||||
WriteString(context^.output, ' IMPORT ');
|
||||
WriteString(context^.output, " IMPORT ");
|
||||
|
||||
current_symbol := import_statement^.symbols;
|
||||
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), @current_symbol^[2]);
|
||||
current_symbol := current_symbol + 1;
|
||||
|
||||
while ORD(current_symbol^[1]) <> 0 do
|
||||
WriteString(context^.output, ', ');
|
||||
while current_symbol^[1] <> '\0' do
|
||||
WriteString(context^.output, ", ");
|
||||
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), @current_symbol^[2]);
|
||||
current_symbol := current_symbol + 1;
|
||||
end;
|
||||
@@ -76,10 +73,10 @@ var
|
||||
buffer: [20]CHAR;
|
||||
written_bytes: Word;
|
||||
begin
|
||||
WriteString(context^.output, ' ');
|
||||
WriteString(context^.output, " ");
|
||||
written_bytes := WriteNBytes(context^.output, ORD(declaration^.constant_name[1]), @declaration^.constant_name[2]);
|
||||
|
||||
WriteString(context^.output, ' = ');
|
||||
WriteString(context^.output, " = ");
|
||||
|
||||
IntToStr(declaration^.constant_value, 0, buffer);
|
||||
WriteString(context^.output, buffer);
|
||||
@@ -92,7 +89,7 @@ var
|
||||
current_declaration: ^^AstConstantDeclaration;
|
||||
begin
|
||||
if declarations^ <> nil then
|
||||
WriteString(context^.output, 'CONST');
|
||||
WriteString(context^.output, "CONST");
|
||||
WriteLine(context^.output);
|
||||
|
||||
current_declaration := declarations;
|
||||
@@ -110,9 +107,9 @@ end;
|
||||
proc transpile_module(context: ^TranspilerContext, result: ^AstModule);
|
||||
begin
|
||||
if result^.main = false then
|
||||
WriteString(context^.output, 'IMPLEMENTATION ')
|
||||
WriteString(context^.output, "IMPLEMENTATION ")
|
||||
end;
|
||||
WriteString(context^.output, 'MODULE ');
|
||||
WriteString(context^.output, "MODULE ");
|
||||
|
||||
(* Write the module name and end the line with a semicolon and newline. *)
|
||||
transpile_module_name(context);
|
||||
@@ -129,10 +126,10 @@ begin
|
||||
transpile_procedure_part(context, result^.procedures);
|
||||
transpile_statement_part(context, result^.statements);
|
||||
|
||||
WriteString(context^.output, 'END ');
|
||||
WriteString(context^.output, "END ");
|
||||
transpile_module_name(context);
|
||||
|
||||
WriteChar(context^.output, '.');
|
||||
WriteChar(context^.output, ".");
|
||||
WriteLine(context^.output)
|
||||
end;
|
||||
|
||||
@@ -143,16 +140,16 @@ var
|
||||
begin
|
||||
current_field := fields;
|
||||
|
||||
while ORD(current_field^.field_name[1]) <> 0 do
|
||||
WriteString(context^.output, ' ');
|
||||
while current_field^.field_name[1] <> '\0' do
|
||||
WriteString(context^.output, " ");
|
||||
written_bytes := WriteNBytes(context^.output, ORD(current_field^.field_name[1]), @current_field^.field_name[2]);
|
||||
|
||||
WriteString(context^.output, ': ');
|
||||
WriteString(context^.output, ": ");
|
||||
transpile_type_expression(context, current_field^.field_type);
|
||||
|
||||
current_field := current_field + 1;
|
||||
|
||||
if ORD(current_field^.field_name[1]) <> 0 then
|
||||
if current_field^.field_name[1] <> '\0' then
|
||||
WriteChar(context^.output, ';')
|
||||
end;
|
||||
WriteLine(context^.output)
|
||||
@@ -161,15 +158,15 @@ end;
|
||||
|
||||
proc transpile_record_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
||||
begin
|
||||
WriteString(context^.output, 'RECORD');
|
||||
WriteString(context^.output, "RECORD");
|
||||
WriteLine(context^.output);
|
||||
transpile_type_fields(context, type_expression^.fields);
|
||||
WriteString(context^.output, ' END')
|
||||
WriteString(context^.output, " END")
|
||||
end;
|
||||
|
||||
proc transpile_pointer_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
||||
begin
|
||||
WriteString(context^.output, 'POINTER TO ');
|
||||
WriteString(context^.output, "POINTER TO ");
|
||||
|
||||
transpile_type_expression(context, type_expression^.target)
|
||||
end;
|
||||
@@ -178,17 +175,17 @@ proc transpile_array_type(context: ^TranspilerContext, type_expression: ^AstType
|
||||
var
|
||||
buffer: [20]CHAR;
|
||||
begin
|
||||
WriteString(context^.output, 'ARRAY');
|
||||
WriteString(context^.output, "ARRAY");
|
||||
|
||||
if type_expression^.length <> 0 then
|
||||
WriteString(context^.output, '[1..');
|
||||
WriteString(context^.output, "[1..");
|
||||
|
||||
IntToStr(type_expression^.length, 0, buffer);
|
||||
WriteString(context^.output, buffer);
|
||||
|
||||
WriteChar(context^.output, ']')
|
||||
end;
|
||||
WriteString(context^.output, ' OF ');
|
||||
WriteString(context^.output, " OF ");
|
||||
|
||||
transpile_type_expression(context, type_expression^.base)
|
||||
end;
|
||||
@@ -200,22 +197,22 @@ var
|
||||
begin
|
||||
current_case := type_expression^.cases;
|
||||
|
||||
WriteString(context^.output, '(');
|
||||
WriteString(context^.output, "(");
|
||||
WriteLine(context^.output);
|
||||
WriteString(context^.output, ' ');
|
||||
WriteString(context^.output, " ");
|
||||
written_bytes := WriteNBytes(context^.output, ORD(current_case^[1]), @current_case^[2]);
|
||||
current_case := current_case + 1;
|
||||
|
||||
while ORD(current_case^[1]) <> 0 do
|
||||
while current_case^[1] <> '\0' do
|
||||
WriteChar(context^.output, ',');
|
||||
WriteLine(context^.output);
|
||||
WriteString(context^.output, ' ');
|
||||
WriteString(context^.output, " ");
|
||||
written_bytes := WriteNBytes(context^.output, ORD(current_case^[1]), @current_case^[2]);
|
||||
|
||||
current_case := current_case + 1
|
||||
end;
|
||||
WriteLine(context^.output);
|
||||
WriteString(context^.output, ' )')
|
||||
WriteString(context^.output, " )")
|
||||
end;
|
||||
|
||||
proc transpile_named_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
||||
@@ -231,7 +228,7 @@ var
|
||||
current_parameter: ^^AstTypeExpression;
|
||||
parameter_count: Word;
|
||||
begin
|
||||
WriteString(context^.output, 'PROCEDURE(');
|
||||
WriteString(context^.output, "PROCEDURE(");
|
||||
current_parameter := type_expression^.parameters;
|
||||
|
||||
while current_parameter^ <> nil do
|
||||
@@ -240,7 +237,7 @@ begin
|
||||
current_parameter := current_parameter + 1;
|
||||
|
||||
if current_parameter^ <> nil then
|
||||
WriteString(context^.output, ', ')
|
||||
WriteString(context^.output, ", ")
|
||||
end
|
||||
end;
|
||||
WriteChar(context^.output, ')')
|
||||
@@ -272,10 +269,10 @@ proc transpile_type_declaration(context: ^TranspilerContext, declaration: ^AstTy
|
||||
var
|
||||
written_bytes: Word;
|
||||
begin
|
||||
WriteString(context^.output, ' ');
|
||||
WriteString(context^.output, " ");
|
||||
|
||||
written_bytes := WriteNBytes(context^.output, ORD(declaration^.identifier[1]), @declaration^.identifier[2]);
|
||||
WriteString(context^.output, ' = ');
|
||||
WriteString(context^.output, " = ");
|
||||
|
||||
transpile_type_expression(context, declaration^.type_expression);
|
||||
write_semicolon(context^.output)
|
||||
@@ -286,7 +283,7 @@ var
|
||||
current_declaration: ^^AstTypedDeclaration;
|
||||
begin
|
||||
if declarations^ <> nil then
|
||||
WriteString(context^.output, 'TYPE');
|
||||
WriteString(context^.output, "TYPE");
|
||||
WriteLine(context^.output);
|
||||
|
||||
current_declaration := declarations;
|
||||
@@ -303,10 +300,10 @@ proc transpile_variable_declaration(context: ^TranspilerContext, declaration: ^A
|
||||
var
|
||||
written_bytes: Word;
|
||||
begin
|
||||
WriteString(context^.output, ' ');
|
||||
WriteString(context^.output, " ");
|
||||
written_bytes := WriteNBytes(context^.output, ORD(declaration^.variable_name[1]), @declaration^.variable_name[2]);
|
||||
|
||||
WriteString(context^.output, ': ');
|
||||
WriteString(context^.output, ": ");
|
||||
|
||||
transpile_type_expression(context, declaration^.variable_type);
|
||||
write_semicolon(context^.output)
|
||||
@@ -317,7 +314,7 @@ var
|
||||
current_declaration: ^^AstVariableDeclaration;
|
||||
begin
|
||||
if declarations^ <> nil then
|
||||
WriteString(context^.output, 'VAR');
|
||||
WriteString(context^.output, "VAR");
|
||||
WriteLine(context^.output);
|
||||
|
||||
current_declaration := declarations;
|
||||
@@ -338,7 +335,7 @@ var
|
||||
parameter_index: Word;
|
||||
current_parameter: ^AstTypedDeclaration;
|
||||
begin
|
||||
WriteString(context^.output, 'PROCEDURE ');
|
||||
WriteString(context^.output, "PROCEDURE ");
|
||||
written_bytes := WriteNBytes(context^.output, ORD(declaration^.name[1]), @declaration^.name[2]);
|
||||
WriteChar(context^.output, '(');
|
||||
|
||||
@@ -347,22 +344,22 @@ begin
|
||||
|
||||
while parameter_index < declaration^.parameter_count do
|
||||
written_bytes := WriteNBytes(context^.output, ORD(current_parameter^.identifier[1]), @current_parameter^.identifier[2]);
|
||||
WriteString(context^.output, ': ');
|
||||
WriteString(context^.output, ": ");
|
||||
transpile_type_expression(context, current_parameter^.type_expression);
|
||||
|
||||
parameter_index := parameter_index + 1u;
|
||||
current_parameter := current_parameter + 1;
|
||||
|
||||
if parameter_index <> declaration^.parameter_count then
|
||||
WriteString(context^.output, '; ')
|
||||
WriteString(context^.output, "; ")
|
||||
end
|
||||
end;
|
||||
|
||||
WriteString(context^.output, ')');
|
||||
WriteString(context^.output, ")");
|
||||
|
||||
(* Check for the return type and write it. *)
|
||||
if declaration^.return_type <> nil then
|
||||
WriteString(context^.output, ': ');
|
||||
WriteString(context^.output, ": ");
|
||||
transpile_type_expression(context, declaration^.return_type)
|
||||
end;
|
||||
write_semicolon(context^.output)
|
||||
@@ -388,10 +385,10 @@ begin
|
||||
| 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')
|
||||
| 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;
|
||||
|
||||
@@ -414,13 +411,13 @@ begin
|
||||
WriteString(context^.output, literal^.string)
|
||||
end;
|
||||
if literal^.kind = AstLiteralKind.null then
|
||||
WriteString(context^.output, 'NIL')
|
||||
WriteString(context^.output, "NIL")
|
||||
end;
|
||||
if (literal^.kind = AstLiteralKind.boolean) & literal^.boolean then
|
||||
WriteString(context^.output, 'TRUE')
|
||||
WriteString(context^.output, "TRUE")
|
||||
end;
|
||||
if (literal^.kind = AstLiteralKind.boolean) & (literal^.boolean = false) then
|
||||
WriteString(context^.output, 'FALSE')
|
||||
WriteString(context^.output, "FALSE")
|
||||
end
|
||||
end;
|
||||
if expression^.kind = astExpressionKindIdentifier then
|
||||
@@ -466,7 +463,7 @@ begin
|
||||
current_argument := current_argument + 1;
|
||||
|
||||
while argument_index < expression^.argument_count do
|
||||
WriteString(context^.output, ', ');
|
||||
WriteString(context^.output, ", ");
|
||||
|
||||
transpile_expression(context, current_argument^);
|
||||
|
||||
@@ -480,44 +477,44 @@ end;
|
||||
|
||||
proc transpile_if_statement(context: ^TranspilerContext, statement: ^AstStatement);
|
||||
begin
|
||||
WriteString(context^.output, 'IF ');
|
||||
WriteString(context^.output, "IF ");
|
||||
transpile_expression(context, statement^.if_condition);
|
||||
|
||||
WriteString(context^.output, ' THEN');
|
||||
WriteString(context^.output, " THEN");
|
||||
WriteLine(context^.output);
|
||||
context^.indentation := context^.indentation + 1u;
|
||||
|
||||
transpile_compound_statement(context, statement^.if_branch);
|
||||
context^.indentation := context^.indentation - 1u;
|
||||
indent(context);
|
||||
WriteString(context^.output, 'END')
|
||||
WriteString(context^.output, "END")
|
||||
end;
|
||||
|
||||
proc transpile_while_statement(context: ^TranspilerContext, statement: ^AstStatement);
|
||||
begin
|
||||
WriteString(context^.output, 'WHILE ');
|
||||
WriteString(context^.output, "WHILE ");
|
||||
transpile_expression(context, statement^.while_condition);
|
||||
|
||||
WriteString(context^.output, ' DO');
|
||||
WriteString(context^.output, " DO");
|
||||
WriteLine(context^.output);
|
||||
context^.indentation := context^.indentation + 1u;
|
||||
|
||||
transpile_compound_statement(context, statement^.while_body);
|
||||
context^.indentation := context^.indentation - 1u;
|
||||
indent(context);
|
||||
WriteString(context^.output, 'END')
|
||||
WriteString(context^.output, "END")
|
||||
end;
|
||||
|
||||
proc transpile_assignment_statement(context: ^TranspilerContext, statement: ^AstStatement);
|
||||
begin
|
||||
transpile_expression(context, statement^.assignee);
|
||||
WriteString(context^.output, ' := ');
|
||||
WriteString(context^.output, " := ");
|
||||
transpile_expression(context, statement^.assignment)
|
||||
end;
|
||||
|
||||
proc transpile_return_statement(context: ^TranspilerContext, statement: ^AstStatement);
|
||||
begin
|
||||
WriteString(context^.output, 'RETURN ');
|
||||
WriteString(context^.output, "RETURN ");
|
||||
|
||||
transpile_expression(context, statement^.returned)
|
||||
end;
|
||||
@@ -567,7 +564,7 @@ end;
|
||||
proc transpile_statement_part(context: ^TranspilerContext, compound: AstCompoundStatement);
|
||||
begin
|
||||
if compound.count > 0 then
|
||||
WriteString(context^.output, 'BEGIN');
|
||||
WriteString(context^.output, "BEGIN");
|
||||
WriteLine(context^.output);
|
||||
|
||||
context^.indentation := context^.indentation + 1u;
|
||||
@@ -586,7 +583,7 @@ begin
|
||||
transpile_variable_part(context, declaration^.variables, false);
|
||||
transpile_statement_part(context, declaration^.statements);
|
||||
|
||||
WriteString(context^.output, 'END ');
|
||||
WriteString(context^.output, "END ");
|
||||
written_bytes := WriteNBytes(context^.output, ORD(declaration^.name[1]), @declaration^.name[2]);
|
||||
|
||||
write_semicolon(context^.output)
|
||||
@@ -610,7 +607,7 @@ begin
|
||||
counter := 1u;
|
||||
last_slash := 0u;
|
||||
|
||||
while (context^.input_name[counter] <> '.') & (ORD(context^.input_name[counter]) <> 0) do
|
||||
while context^.input_name[counter] <> '.' & context^.input_name[counter] <> '\0' do
|
||||
if context^.input_name[counter] = '/' then
|
||||
last_slash := counter
|
||||
end;
|
||||
@@ -623,13 +620,13 @@ begin
|
||||
if last_slash <> 0u then
|
||||
counter := last_slash + 1u
|
||||
end;
|
||||
while (context^.input_name[counter] <> '.') & (ORD(context^.input_name[counter]) <> 0) do
|
||||
while context^.input_name[counter] <> '.' & context^.input_name[counter] <> '\0' do
|
||||
WriteChar(context^.output, context^.input_name[counter]);
|
||||
counter := counter + 1u
|
||||
end
|
||||
end;
|
||||
|
||||
proc transpile*(ast_module: ^AstModule, output: File, definition: File, input_name: ShortString);
|
||||
proc transpile*(ast_module: ^AstModule, output: File, definition: File, input_name: String);
|
||||
var
|
||||
context: TranspilerContext;
|
||||
begin
|
||||
|
Reference in New Issue
Block a user