Move write procedures to the common module

This commit is contained in:
2025-08-14 14:06:57 +03:00
parent 5146ea61b9
commit cec020ea92
6 changed files with 101 additions and 118 deletions

View File

@@ -3,7 +3,7 @@
obtain one at https://mozilla.org/MPL/2.0/. *)
program;
import cstdlib, cstring, cstdio, cctype, common, Lexer;
import cstdlib, cstdio, cctype, common, Lexer;
const
SEEK_SET* := 0;
@@ -113,7 +113,7 @@ type
end;
location: Location
end;
CommandLine* = record
CommandLine = record
input: ^Char;
lex: Bool;
parse: Bool
@@ -123,74 +123,13 @@ type
data: ^Token
end;
(*
External procedures.
*)
proc write(fd: Int, buf: Pointer, Word: Int) -> Int; extern;
(*
Standard procedures.
*)
proc reallocarray(ptr: Pointer, n: Word, size: Word) -> Pointer;
return realloc(ptr, n * size)
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;
return String(string.ptr + start, count)
end;