Move write procedures to the common module
This commit is contained in:
@@ -3,11 +3,10 @@
|
|||||||
obtain one at https://mozilla.org/MPL/2.0/. *)
|
obtain one at https://mozilla.org/MPL/2.0/. *)
|
||||||
module;
|
module;
|
||||||
|
|
||||||
from FIO import WriteString, WriteChar, WriteLine, StdErr;
|
import cstdlib, cstring, common;
|
||||||
import cstdlib, common;
|
|
||||||
|
|
||||||
type
|
type
|
||||||
CommandLine = record
|
CommandLine* = record
|
||||||
input: ^Char;
|
input: ^Char;
|
||||||
output: ^Char;
|
output: ^Char;
|
||||||
lex: Bool;
|
lex: Bool;
|
||||||
@@ -17,16 +16,16 @@ type
|
|||||||
proc parse_command_line*(argc: Int, argv: ^^Char) -> ^CommandLine;
|
proc parse_command_line*(argc: Int, argv: ^^Char) -> ^CommandLine;
|
||||||
var
|
var
|
||||||
parameter: ^Char;
|
parameter: ^Char;
|
||||||
i: Word;
|
i: Int;
|
||||||
result: ^CommandLine;
|
result: ^CommandLine;
|
||||||
parsed: Bool;
|
parsed: Bool;
|
||||||
begin
|
begin
|
||||||
i := 1u;
|
i := 1;
|
||||||
NEW(result);
|
result := cast(malloc(#size(CommandLine)): ^CommandLine);
|
||||||
result^.lex := false;
|
result^.lex := false;
|
||||||
result^.parse := false;
|
result^.parse := false;
|
||||||
memset(@result^.input, 0, 256);
|
result^.input := nil;
|
||||||
result^.output[1] := CHAR(0);
|
result^.output := nil;
|
||||||
|
|
||||||
while i < argc & result <> nil do
|
while i < argc & result <> nil do
|
||||||
parameter := (argv + i)^;
|
parameter := (argv + i)^;
|
||||||
@@ -41,10 +40,10 @@ begin
|
|||||||
result^.parse := true
|
result^.parse := true
|
||||||
end;
|
end;
|
||||||
if strcmp(parameter, "-o\0".ptr) = 0 then
|
if strcmp(parameter, "-o\0".ptr) = 0 then
|
||||||
i := i + 1u;
|
i := i + 1;
|
||||||
|
|
||||||
if i = argc then
|
if i = argc then
|
||||||
WriteString(StdErr, "Fatal error: expecting a file name following -o.");
|
write_s("Fatal error: expecting a file name following -o.");
|
||||||
result := nil
|
result := nil
|
||||||
end;
|
end;
|
||||||
if i < argc then
|
if i < argc then
|
||||||
@@ -57,12 +56,11 @@ begin
|
|||||||
parsed := true;
|
parsed := true;
|
||||||
|
|
||||||
if result^.input <> nil then
|
if result^.input <> nil then
|
||||||
WriteString(StdErr, "Fatal error: only one source file can be compiled at once. First given \"");
|
write_s("Fatal error: only one source file can be compiled at once. First given \"");
|
||||||
WriteString(StdErr, result^.input);
|
write_z(result^.input);
|
||||||
WriteString(StdErr, "\", then \"");
|
write_s("\", then \"");
|
||||||
WriteString(StdErr, parameter);
|
write_z(parameter);
|
||||||
WriteString(StdErr, "\".");
|
write_s("\".\n");
|
||||||
WriteLine(StdErr);
|
|
||||||
result := nil
|
result := nil
|
||||||
end;
|
end;
|
||||||
if result <> nil then
|
if result <> nil then
|
||||||
@@ -70,20 +68,18 @@ begin
|
|||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
if ~parsed then
|
if ~parsed then
|
||||||
WriteString(StdErr, "Fatal error: unknown command line options: ");
|
write_s("Fatal error: unknown command line options: ");
|
||||||
|
|
||||||
WriteString(StdErr, parameter);
|
write_z(parameter);
|
||||||
WriteChar(StdErr, '.');
|
write_s(".\n");
|
||||||
WriteLine(StdErr);
|
|
||||||
|
|
||||||
result := nil
|
result := nil
|
||||||
end;
|
end;
|
||||||
|
|
||||||
i := i + 1u
|
i := i + 1
|
||||||
end;
|
end;
|
||||||
if result <> nil & result^.input = nil then
|
if result <> nil & result^.input = nil then
|
||||||
WriteString(StdErr, "Fatal error: no input files.");
|
write_s("Fatal error: no input files.\n");
|
||||||
WriteLine(StdErr);
|
|
||||||
result := nil
|
result := nil
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@@ -432,7 +432,7 @@ begin
|
|||||||
token^.kind := LexerKind.identifier;
|
token^.kind := LexerKind.identifier;
|
||||||
|
|
||||||
token^.identifierKind[1] := cast(lexer^.current.iterator - lexer^.start.iterator: Char);
|
token^.identifierKind[1] := cast(lexer^.current.iterator - lexer^.start.iterator: Char);
|
||||||
memcpy(@token^.identifierKind[2], lexer^.start.iterator, ORD(token^.identifierKind[1]));
|
memcpy(@token^.identifierKind[2], lexer^.start.iterator, cast(token^.identifierKind[1]: Word));
|
||||||
|
|
||||||
if compare_keyword("program", lexer^.start, lexer^.current.iterator) then
|
if compare_keyword("program", lexer^.start, lexer^.current.iterator) then
|
||||||
token^.kind := LexerKind._program
|
token^.kind := LexerKind._program
|
||||||
|
@@ -271,7 +271,7 @@ end;
|
|||||||
proc parse_array_type(parser: ^Parser) -> ^AstTypeExpression;
|
proc parse_array_type(parser: ^Parser) -> ^AstTypeExpression;
|
||||||
var
|
var
|
||||||
token: LexerToken;
|
token: LexerToken;
|
||||||
buffer: [20]CHAR;
|
buffer: [20]Char;
|
||||||
result: ^AstTypeExpression;
|
result: ^AstTypeExpression;
|
||||||
begin
|
begin
|
||||||
NEW(result);
|
NEW(result);
|
||||||
|
@@ -37,21 +37,20 @@ end;
|
|||||||
|
|
||||||
proc transpile_import_statement(context: ^TranspilerContext, import_statement: ^AstImportStatement);
|
proc transpile_import_statement(context: ^TranspilerContext, import_statement: ^AstImportStatement);
|
||||||
var
|
var
|
||||||
written_bytes: Word;
|
|
||||||
current_symbol: ^Identifier;
|
current_symbol: ^Identifier;
|
||||||
begin
|
begin
|
||||||
WriteString(context^.output, "FROM ");
|
WriteString(context^.output, "FROM ");
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(import_statement^.package[1]), @import_statement^.package[2]);
|
transpile_identifier(context, import_statement^.package);
|
||||||
|
|
||||||
WriteString(context^.output, " IMPORT ");
|
WriteString(context^.output, " IMPORT ");
|
||||||
|
|
||||||
current_symbol := import_statement^.symbols;
|
current_symbol := import_statement^.symbols;
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), @current_symbol^[2]);
|
transpile_identifier(context, current_symbol^);
|
||||||
current_symbol := current_symbol + 1;
|
current_symbol := current_symbol + 1;
|
||||||
|
|
||||||
while current_symbol^[1] <> '\0' do
|
while current_symbol^[1] <> '\0' do
|
||||||
WriteString(context^.output, ", ");
|
WriteString(context^.output, ", ");
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(current_symbol^[1]), @current_symbol^[2]);
|
transpile_identifier(context, current_symbol^);
|
||||||
current_symbol := current_symbol + 1;
|
current_symbol := current_symbol + 1;
|
||||||
end;
|
end;
|
||||||
write_semicolon(context^.output)
|
write_semicolon(context^.output)
|
||||||
@@ -70,11 +69,10 @@ end;
|
|||||||
|
|
||||||
proc transpile_constant_declaration(context: ^TranspilerContext, declaration: ^AstConstantDeclaration);
|
proc transpile_constant_declaration(context: ^TranspilerContext, declaration: ^AstConstantDeclaration);
|
||||||
var
|
var
|
||||||
buffer: [20]CHAR;
|
buffer: [20]Char;
|
||||||
written_bytes: Word;
|
|
||||||
begin
|
begin
|
||||||
WriteString(context^.output, " ");
|
WriteString(context^.output, " ");
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(declaration^.constant_name[1]), @declaration^.constant_name[2]);
|
transpile_identifier(context, declaration^.constant_name);
|
||||||
|
|
||||||
WriteString(context^.output, " = ");
|
WriteString(context^.output, " = ");
|
||||||
|
|
||||||
@@ -135,14 +133,13 @@ end;
|
|||||||
|
|
||||||
proc transpile_type_fields(context: ^TranspilerContext, fields: ^AstFieldDeclaration);
|
proc transpile_type_fields(context: ^TranspilerContext, fields: ^AstFieldDeclaration);
|
||||||
var
|
var
|
||||||
written_bytes: Word;
|
|
||||||
current_field: ^AstFieldDeclaration;
|
current_field: ^AstFieldDeclaration;
|
||||||
begin
|
begin
|
||||||
current_field := fields;
|
current_field := fields;
|
||||||
|
|
||||||
while current_field^.field_name[1] <> '\0' do
|
while current_field^.field_name[1] <> '\0' do
|
||||||
WriteString(context^.output, " ");
|
WriteString(context^.output, " ");
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(current_field^.field_name[1]), @current_field^.field_name[2]);
|
transpile_identifier(context, current_field^.field_name);
|
||||||
|
|
||||||
WriteString(context^.output, ": ");
|
WriteString(context^.output, ": ");
|
||||||
transpile_type_expression(context, current_field^.field_type);
|
transpile_type_expression(context, current_field^.field_type);
|
||||||
@@ -173,7 +170,7 @@ end;
|
|||||||
|
|
||||||
proc transpile_array_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
proc transpile_array_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
||||||
var
|
var
|
||||||
buffer: [20]CHAR;
|
buffer: [20]Char;
|
||||||
begin
|
begin
|
||||||
WriteString(context^.output, "ARRAY");
|
WriteString(context^.output, "ARRAY");
|
||||||
|
|
||||||
@@ -193,21 +190,20 @@ end;
|
|||||||
proc transpile_enumeration_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
proc transpile_enumeration_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
||||||
var
|
var
|
||||||
current_case: ^Identifier;
|
current_case: ^Identifier;
|
||||||
written_bytes: Word;
|
|
||||||
begin
|
begin
|
||||||
current_case := type_expression^.cases;
|
current_case := type_expression^.cases;
|
||||||
|
|
||||||
WriteString(context^.output, "(");
|
WriteString(context^.output, "(");
|
||||||
WriteLine(context^.output);
|
WriteLine(context^.output);
|
||||||
WriteString(context^.output, " ");
|
WriteString(context^.output, " ");
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(current_case^[1]), @current_case^[2]);
|
transpile_identifier(context, current_case^);
|
||||||
current_case := current_case + 1;
|
current_case := current_case + 1;
|
||||||
|
|
||||||
while current_case^[1] <> '\0' do
|
while current_case^[1] <> '\0' do
|
||||||
WriteChar(context^.output, ',');
|
WriteChar(context^.output, ',');
|
||||||
WriteLine(context^.output);
|
WriteLine(context^.output);
|
||||||
WriteString(context^.output, " ");
|
WriteString(context^.output, " ");
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(current_case^[1]), @current_case^[2]);
|
transpile_identifier(context, current_case^);
|
||||||
|
|
||||||
current_case := current_case + 1
|
current_case := current_case + 1
|
||||||
end;
|
end;
|
||||||
@@ -215,11 +211,11 @@ begin
|
|||||||
WriteString(context^.output, " )")
|
WriteString(context^.output, " )")
|
||||||
end;
|
end;
|
||||||
|
|
||||||
proc transpile_named_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
proc transpile_identifier(context: ^TranspilerContext, identifier: Identifier);
|
||||||
var
|
var
|
||||||
written_bytes: Word;
|
written_bytes: Word;
|
||||||
begin
|
begin
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(type_expression^.name[1]), @type_expression^.name[2])
|
written_bytes := WriteNBytes(context^.output, cast(identifier[1]: Word), @identifier[2])
|
||||||
end;
|
end;
|
||||||
|
|
||||||
proc transpile_procedure_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
proc transpile_procedure_type(context: ^TranspilerContext, type_expression: ^AstTypeExpression);
|
||||||
@@ -261,7 +257,7 @@ begin
|
|||||||
transpile_procedure_type(context, type_expression)
|
transpile_procedure_type(context, type_expression)
|
||||||
end;
|
end;
|
||||||
if type_expression^.kind = astTypeExpressionKindNamed then
|
if type_expression^.kind = astTypeExpressionKindNamed then
|
||||||
transpile_named_type(context, type_expression)
|
transpile_identifier(context, type_expression^.name)
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -271,7 +267,7 @@ var
|
|||||||
begin
|
begin
|
||||||
WriteString(context^.output, " ");
|
WriteString(context^.output, " ");
|
||||||
|
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(declaration^.identifier[1]), @declaration^.identifier[2]);
|
transpile_identifier(context^.output, declaration^.identifier);
|
||||||
WriteString(context^.output, " = ");
|
WriteString(context^.output, " = ");
|
||||||
|
|
||||||
transpile_type_expression(context, declaration^.type_expression);
|
transpile_type_expression(context, declaration^.type_expression);
|
||||||
@@ -297,11 +293,9 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
proc transpile_variable_declaration(context: ^TranspilerContext, declaration: ^AstVariableDeclaration);
|
proc transpile_variable_declaration(context: ^TranspilerContext, declaration: ^AstVariableDeclaration);
|
||||||
var
|
|
||||||
written_bytes: Word;
|
|
||||||
begin
|
begin
|
||||||
WriteString(context^.output, " ");
|
WriteString(context^.output, " ");
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(declaration^.variable_name[1]), @declaration^.variable_name[2]);
|
transpile_identifier(context, declaration^.variable_name);
|
||||||
|
|
||||||
WriteString(context^.output, ": ");
|
WriteString(context^.output, ": ");
|
||||||
|
|
||||||
@@ -331,19 +325,18 @@ end;
|
|||||||
|
|
||||||
proc transpile_procedure_heading(context: ^TranspilerContext, declaration: ^AstProcedureDeclaration);
|
proc transpile_procedure_heading(context: ^TranspilerContext, declaration: ^AstProcedureDeclaration);
|
||||||
var
|
var
|
||||||
written_bytes: Word;
|
|
||||||
parameter_index: Word;
|
parameter_index: Word;
|
||||||
current_parameter: ^AstTypedDeclaration;
|
current_parameter: ^AstTypedDeclaration;
|
||||||
begin
|
begin
|
||||||
WriteString(context^.output, "PROCEDURE ");
|
WriteString(context^.output, "PROCEDURE ");
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(declaration^.name[1]), @declaration^.name[2]);
|
transpile_identifier(context, declaration^.name);
|
||||||
WriteChar(context^.output, '(');
|
WriteChar(context^.output, '(');
|
||||||
|
|
||||||
parameter_index := 0;
|
parameter_index := 0;
|
||||||
current_parameter := declaration^.parameters;
|
current_parameter := declaration^.parameters;
|
||||||
|
|
||||||
while parameter_index < declaration^.parameter_count do
|
while parameter_index < declaration^.parameter_count do
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(current_parameter^.identifier[1]), @current_parameter^.identifier[2]);
|
transpile_identifier(context, current_parameter^.identifier);
|
||||||
WriteString(context^.output, ": ");
|
WriteString(context^.output, ": ");
|
||||||
transpile_type_expression(context, current_parameter^.type_expression);
|
transpile_type_expression(context, current_parameter^.type_expression);
|
||||||
|
|
||||||
@@ -395,8 +388,7 @@ end;
|
|||||||
proc transpile_expression(context: ^TranspilerContext, expression: ^AstExpression);
|
proc transpile_expression(context: ^TranspilerContext, expression: ^AstExpression);
|
||||||
var
|
var
|
||||||
literal: ^AstLiteral;
|
literal: ^AstLiteral;
|
||||||
buffer: [20]CHAR;
|
buffer: [20]Char;
|
||||||
written_bytes: Word;
|
|
||||||
argument_index: Word;
|
argument_index: Word;
|
||||||
current_argument: ^^AstExpression;
|
current_argument: ^^AstExpression;
|
||||||
begin
|
begin
|
||||||
@@ -421,7 +413,7 @@ begin
|
|||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
if expression^.kind = astExpressionKindIdentifier then
|
if expression^.kind = astExpressionKindIdentifier then
|
||||||
written_bytes := WriteNBytes(context^.output, ORD(expression^.identifier[1]), @expression^.identifier[2])
|
transpile_identifier(context, expression^.identifier)
|
||||||
end;
|
end;
|
||||||
if expression^.kind = astExpressionKindDereference then
|
if expression^.kind = astExpressionKindDereference then
|
||||||
transpile_expression(context, expression^.reference);
|
transpile_expression(context, expression^.reference);
|
||||||
@@ -436,7 +428,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]), @expression^.field[2])
|
transpile_identifier(contextexpression^.field)
|
||||||
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);
|
||||||
@@ -574,8 +566,6 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
proc transpile_procedure_declaration(context: ^TranspilerContext, declaration: ^AstProcedureDeclaration);
|
proc transpile_procedure_declaration(context: ^TranspilerContext, declaration: ^AstProcedureDeclaration);
|
||||||
var
|
|
||||||
written_bytes: Word;
|
|
||||||
begin
|
begin
|
||||||
transpile_procedure_heading(context, declaration);
|
transpile_procedure_heading(context, declaration);
|
||||||
|
|
||||||
@@ -584,7 +574,7 @@ begin
|
|||||||
transpile_statement_part(context, declaration^.statements);
|
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]);
|
transpile_identifier(context^.output, declaration^.name);
|
||||||
|
|
||||||
write_semicolon(context^.output)
|
write_semicolon(context^.output)
|
||||||
end;
|
end;
|
||||||
|
@@ -3,6 +3,8 @@
|
|||||||
obtain one at https://mozilla.org/MPL/2.0/. *)
|
obtain one at https://mozilla.org/MPL/2.0/. *)
|
||||||
module;
|
module;
|
||||||
|
|
||||||
|
import cstring, cstdio;
|
||||||
|
|
||||||
type
|
type
|
||||||
Identifier = [256]Char;
|
Identifier = [256]Char;
|
||||||
TextLocation* = record
|
TextLocation* = record
|
||||||
@@ -10,4 +12,60 @@ type
|
|||||||
column: Word
|
column: Word
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
proc write*(fd: Int, buf: Pointer, Word: Int) -> Int; extern;
|
||||||
|
|
||||||
|
proc write_s*(value: String);
|
||||||
|
begin
|
||||||
|
write(1, cast(value.ptr: Pointer), cast(value.length: Int))
|
||||||
|
end;
|
||||||
|
|
||||||
|
proc write_z*(value: ^Char);
|
||||||
|
begin
|
||||||
|
write(1, cast(value: Pointer), cast(strlen(value): Int))
|
||||||
|
end;
|
||||||
|
|
||||||
|
proc write_b*(value: Bool);
|
||||||
|
begin
|
||||||
|
if value then
|
||||||
|
write_s("true")
|
||||||
|
else
|
||||||
|
write_s("false")
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
|
||||||
|
proc write_c*(value: Char);
|
||||||
|
begin
|
||||||
|
putchar(cast(value: Int));
|
||||||
|
fflush(nil)
|
||||||
|
end;
|
||||||
|
|
||||||
|
proc write_i*(value: Int);
|
||||||
|
var
|
||||||
|
digit: Int;
|
||||||
|
n: Word;
|
||||||
|
buffer: [10]Char;
|
||||||
|
begin
|
||||||
|
n := 10u;
|
||||||
|
|
||||||
|
if value = 0 then
|
||||||
|
write_c('0')
|
||||||
|
end;
|
||||||
|
while value <> 0 do
|
||||||
|
digit := value % 10;
|
||||||
|
value := value / 10;
|
||||||
|
|
||||||
|
buffer[n] := cast(cast('0': Int) + digit: Char);
|
||||||
|
n := n - 1u
|
||||||
|
end;
|
||||||
|
while n < 10u do
|
||||||
|
n := n + 1u;
|
||||||
|
write_c(buffer[n])
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
|
||||||
|
proc write_u*(value: Word);
|
||||||
|
begin
|
||||||
|
write_i(cast(value: Int))
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
obtain one at https://mozilla.org/MPL/2.0/. *)
|
obtain one at https://mozilla.org/MPL/2.0/. *)
|
||||||
program;
|
program;
|
||||||
|
|
||||||
import cstdlib, cstring, cstdio, cctype, common, Lexer;
|
import cstdlib, cstdio, cctype, common, Lexer;
|
||||||
|
|
||||||
const
|
const
|
||||||
SEEK_SET* := 0;
|
SEEK_SET* := 0;
|
||||||
@@ -113,7 +113,7 @@ type
|
|||||||
end;
|
end;
|
||||||
location: Location
|
location: Location
|
||||||
end;
|
end;
|
||||||
CommandLine* = record
|
CommandLine = record
|
||||||
input: ^Char;
|
input: ^Char;
|
||||||
lex: Bool;
|
lex: Bool;
|
||||||
parse: Bool
|
parse: Bool
|
||||||
@@ -123,74 +123,13 @@ type
|
|||||||
data: ^Token
|
data: ^Token
|
||||||
end;
|
end;
|
||||||
|
|
||||||
(*
|
|
||||||
External procedures.
|
|
||||||
*)
|
|
||||||
|
|
||||||
proc write(fd: Int, buf: Pointer, Word: Int) -> Int; extern;
|
|
||||||
|
|
||||||
(*
|
(*
|
||||||
Standard procedures.
|
Standard procedures.
|
||||||
*)
|
*)
|
||||||
|
|
||||||
proc reallocarray(ptr: Pointer, n: Word, size: Word) -> Pointer;
|
proc reallocarray(ptr: Pointer, n: Word, size: Word) -> Pointer;
|
||||||
return realloc(ptr, n * size)
|
return realloc(ptr, n * size)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
proc write_s(value: String);
|
|
||||||
begin
|
|
||||||
write(1, cast(value.ptr: Pointer), cast(value.length: Int))
|
|
||||||
end;
|
|
||||||
|
|
||||||
proc write_z(value: ^Char);
|
|
||||||
begin
|
|
||||||
write(1, cast(value: Pointer), cast(strlen(value): Int))
|
|
||||||
end;
|
|
||||||
|
|
||||||
proc write_b(value: Bool);
|
|
||||||
begin
|
|
||||||
if value then
|
|
||||||
write_s("true")
|
|
||||||
else
|
|
||||||
write_s("false")
|
|
||||||
end
|
|
||||||
end;
|
|
||||||
|
|
||||||
proc write_c(value: Char);
|
|
||||||
begin
|
|
||||||
putchar(cast(value: Int));
|
|
||||||
fflush(nil)
|
|
||||||
end;
|
|
||||||
|
|
||||||
proc write_i(value: Int);
|
|
||||||
var
|
|
||||||
digit: Int;
|
|
||||||
n: Word;
|
|
||||||
buffer: [10]Char;
|
|
||||||
begin
|
|
||||||
n := 10u;
|
|
||||||
|
|
||||||
if value = 0 then
|
|
||||||
write_c('0')
|
|
||||||
end;
|
|
||||||
while value <> 0 do
|
|
||||||
digit := value % 10;
|
|
||||||
value := value / 10;
|
|
||||||
|
|
||||||
buffer[n] := cast(cast('0': Int) + digit: Char);
|
|
||||||
n := n - 1u
|
|
||||||
end;
|
|
||||||
while n < 10u do
|
|
||||||
n := n + 1u;
|
|
||||||
write_c(buffer[n])
|
|
||||||
end
|
|
||||||
end;
|
|
||||||
|
|
||||||
proc write_u(value: Word);
|
|
||||||
begin
|
|
||||||
write_i(cast(value: Int))
|
|
||||||
end;
|
|
||||||
|
|
||||||
proc substring(string: String, start: Word, count: Word) -> String;
|
proc substring(string: String, start: Word, count: Word) -> String;
|
||||||
return String(string.ptr + start, count)
|
return String(string.ptr + start, count)
|
||||||
end;
|
end;
|
||||||
|
Reference in New Issue
Block a user