Add an empty cstdlib.elna source file

This commit is contained in:
2025-08-10 23:43:09 +03:00
parent 87058adcb3
commit 5146ea61b9
11 changed files with 360 additions and 417 deletions

View File

@@ -3,7 +3,7 @@
obtain one at https://mozilla.org/MPL/2.0/. *)
program;
import Common, Lexer;
import cstdlib, cstring, cstdio, cctype, common, Lexer;
const
SEEK_SET* := 0;
@@ -127,30 +127,8 @@ type
External procedures.
*)
proc fopen(pathname: ^Char, mode: ^Char) -> ^FILE; extern;
proc fclose(stream: ^FILE) -> Int; extern;
proc fseek(stream: ^FILE, off: Int, whence: Int) -> Int; extern;
proc rewind(stream: ^FILE); extern;
proc ftell(stream: ^FILE) -> Int; extern;
proc fread(ptr: Pointer, size: Word, nmemb: Word, stream: ^FILE) -> Word; extern;
proc write(fd: Int, buf: Pointer, Word: Int) -> Int; extern;
proc malloc(size: Word) -> Pointer; extern;
proc free(ptr: Pointer); extern;
proc calloc(nmemb: Word, size: Word) -> Pointer; extern;
proc realloc(ptr: Pointer, size: Word) -> Pointer; extern;
proc memset(ptr: ^Char, c: Int, n: Int) -> ^Char; extern;
proc strcmp(s1: ^Char, s2: ^Char) -> Int; extern;
proc strncmp(s1: ^Char, s2: ^Char, n: Word) -> Int; extern;
proc strncpy(dst: ^Char, src: ^Char, dsize: Word) -> ^Char; extern;
proc strcpy(dst: ^Char, src: ^Char) -> ^Char; extern;
proc strlen(ptr: ^Char) -> Word; extern;
proc perror(s: ^Char); extern;
proc exit(code: Int) -> !; extern;
(*
Standard procedures.
*)
@@ -161,12 +139,12 @@ end;
proc write_s(value: String);
begin
write(0, cast(value.ptr: Pointer), cast(value.length: Int))
write(1, cast(value.ptr: Pointer), cast(value.length: Int))
end;
proc write_z(value: ^Char);
begin
write(0, cast(value: Pointer), cast(strlen(value): Int))
write(1, cast(value: Pointer), cast(strlen(value): Int))
end;
proc write_b(value: Bool);
@@ -180,7 +158,8 @@ end;
proc write_c(value: Char);
begin
write(0, cast(@value: Pointer), 1)
putchar(cast(value: Int));
fflush(nil)
end;
proc write_i(value: Int);
@@ -212,22 +191,6 @@ begin
write_i(cast(value: Int))
end;
proc is_digit(c: Char) -> Bool;
return cast(c: Int) >= cast('0': Int) & cast(c: Int) <= cast('9': Int)
end;
proc is_alpha(c: Char) -> Bool;
return cast(c: Int) >= cast('A': Int) & cast(c: Int) <= cast('z': Int)
end;
proc is_alnum(c: Char) -> Bool;
return is_digit(c) or is_alpha(c)
end;
proc is_space(c: Char) -> Bool;
return c = ' ' or c = '\n' or c = '\t'
end;
proc substring(string: String, start: Word, count: Word) -> String;
return String(string.ptr + start, count)
end;
@@ -412,7 +375,7 @@ proc lexer_spaces(source_code: ^SourceCode);
var
current: Char;
begin
while ~source_code_empty(source_code) & is_space(source_code_head(source_code^)) do
while ~source_code_empty(source_code) & isspace(cast(source_code_head(source_code^): Int)) <> 0 do
current := source_code_head(source_code^);
if current = '\n' then
@@ -424,7 +387,7 @@ end;
(* Checker whether the character is allowed in an identificator. *)
proc lexer_is_ident(char: Char) -> Bool;
return is_alnum(char) or char = '_'
return isalnum(cast(char: Int)) <> 0 or char = '_'
end;
proc lexer_identifier(source_code: ^SourceCode, token_content: ^StringBuffer);
@@ -511,7 +474,7 @@ proc lexer_number(source_code: ^SourceCode, token_content: ^Int);
begin
token_content^ := 0;
while ~source_code_empty(source_code) & is_digit(source_code_head(source_code^)) do
while ~source_code_empty(source_code) & isdigit(cast(source_code_head(source_code^): Int)) <> 0 do
token_content^ := token_content^ * 10 + (cast(source_code_head(source_code^): Int) - cast('0': Int));
source_code_advance(source_code)
@@ -607,7 +570,7 @@ begin
first_char := source_code_head(source_code);
if is_alpha(first_char) or first_char = '_' then
if isalpha(cast(first_char: Int)) <> 0 or first_char = '_' then
lexer_identifier(@source_code, token_buffer);
current_token := lexer_categorize(string_buffer_clear(token_buffer))
elsif first_char = '#' then
@@ -616,7 +579,7 @@ begin
current_token.kind := TokenKind.trait;
current_token.value.string := string_dup(string_buffer_clear(token_buffer))
elsif is_digit(first_char) then
elsif isdigit(cast(first_char: Int)) <> 0 then
lexer_number(@source_code, @current_token.value.int_value);
if source_code_expect(@source_code, 'u') then