Use colon instead of as to cast

This commit is contained in:
2025-02-14 08:05:03 +01:00
parent c564847c6b
commit ee4ebf64b9
10 changed files with 488 additions and 290 deletions

View File

@ -63,46 +63,46 @@ const
type
Position* = record
line: Word;
line: Word
column: Word
end
Location* = record
first: Position;
first: Position
last: Position
end
SourceCode = record
position: Position;
position: Position
text: String
end
TokenValue* = union
int_value: Int;
string_value: pointer to Char;
string: String;
boolean_value: Bool;
int_value: Int
string_value: pointer to Char
string: String
boolean_value: Bool
char_value: Char
end
Token* = record
kind: Int;
value: TokenValue;
kind: Int
value: TokenValue
location: Location
end
FILE* = record
dummy: Int
end
CommandLine* = record
input: pointer to Char;
tokenize: Bool;
input: pointer to Char
tokenize: Bool
syntax_tree: Bool
end
Literal* = record
value: Int
end
ConstantDefinition* = record
name: pointer to Char;
name: pointer to Char
body: pointer to Literal
end
ConstantPart* = record
elements: pointer to pointer to ConstantDefinition;
elements: pointer to pointer to ConstantDefinition
count: Word
end
Program* = record
@ -148,12 +148,12 @@ end
proc write_s(value: String);
begin
write(0, value.ptr, value.length)
write(0, cast(value.ptr: pointer to Byte), cast(value.length: Int))
end
proc write_z(value: pointer to Char);
begin
write(0, value, strlen(value))
write(0, cast(value: pointer to Byte), cast(strlen(value): Int))
end
proc write_b(value: Bool);
@ -167,7 +167,7 @@ end
proc write_c(value: Char);
begin
write(0, @value, 1)
write(0, cast(@value: pointer to Byte), 1)
end
proc write_i(value: Int);
@ -184,7 +184,7 @@ begin
digit := value % 10;
value := value / 10;
buffer[n] := cast(cast('0' as Int) + digit as Char);
buffer[n] := cast(cast('0': Int) + digit: Char);
n := n - 1u
end;
while n < 10u do
@ -195,17 +195,17 @@ end
proc write_u(value: Word);
begin
write_i(value)
write_i(cast(value: Int))
end
proc is_digit(c: Char) -> Bool;
begin
return cast(c as Int) >= cast('0' as Int) and cast(c as Int) <= cast('9' as Int)
return cast(c: Int) >= cast('0': Int) and cast(c: Int) <= cast('9': Int)
end
proc is_alpha(c: Char) -> Bool;
begin
return cast(c as Int) >= cast('A' as Int) and cast(c as Int) <= cast('z' as Int)
return cast(c: Int) >= cast('A': Int) and cast(c: Int) <= cast('z': Int)
end
proc is_alnum(c: Char) -> Bool;
@ -232,7 +232,7 @@ proc string_dup(origin: String) -> String;
var
copy: pointer to Char;
begin
copy := cast(malloc(origin.length) as pointer to Char);
copy := cast(malloc(origin.length): pointer to Char);
strncpy(copy, origin.ptr, origin.length);
return String(copy, origin.length)
@ -246,9 +246,7 @@ proc make_position() -> Position;
var
result: Position;
begin
result.line := 1u;
result.column := 1u;
return result
return Position(1u, 1u)
end
proc read_source(filename: pointer to Char, result: pointer to String) -> Bool;
@ -274,12 +272,12 @@ begin
end;
rewind(input_file);
input := malloc(source_size);
if fread(input, source_size, 1, input_file) <> 1u then
input := malloc(cast(source_size: Word));
if fread(input, cast(source_size: Word), 1u, input_file) <> 1u then
return false
end;
result^.length := cast(source_size as Word);
result^.ptr := cast(input as pointer to Char);
result^.length := cast(source_size: Word);
result^.ptr := cast(input: pointer to Char);
return true
end
@ -373,12 +371,12 @@ begin
while source_code^.text.length > 1u do
if source_code^.text[1u] = '*' and source_code^.text[2u] = ')' then
source_code^ := advance_source(source_code^, 2u);
token_content^ := substring(token_content^, 0, content_length);
token_content^ := substring(token_content^, 0u, content_length);
return true
end;
content_length := content_length + 1u;
source_code^ := advance_source(source_code^, 1)
source_code^ := advance_source(source_code^, 1u)
end;
return false
@ -413,8 +411,8 @@ begin
if token_end^ <> '\"' then
return input
end;
token_length := cast(token_end - input as Word);
current_token^.value.string_value := cast(calloc(token_length, 1) as pointer to Char);
token_length := cast(token_end - input: Word);
current_token^.value.string_value := cast(calloc(token_length, 1u): pointer to Char);
is_valid := true;
constructed_string := current_token^.value.string_value;
@ -565,7 +563,7 @@ begin
write_s("u>")
elsif current_token^.kind = TOKEN_CHARACTER then
write_c('<');
write_i(current_token^.value.char_value);
write_i(cast(current_token^.value.char_value: Int));
write_s("c>")
elsif current_token^.kind = TOKEN_STRING then
write_s("\"...\"")
@ -671,7 +669,7 @@ begin
source_code := skip_spaces(source_code);
while source_code.text.length <> 0u do
tokens := cast(reallocarray(tokens, tokens_size^ + 1u, sizeof(Token)) as pointer to Token);
tokens := cast(reallocarray(cast(tokens: pointer to Byte), tokens_size^ + 1u, Token.size): pointer to Token);
current_token := tokens + tokens_size^;
first_char := source_code.text[1u];
@ -681,7 +679,7 @@ begin
elsif is_digit(first_char) then
token_end := nil;
current_token^.value.int_value := strtol(source_code.text.ptr, @token_end, 10);
token_length := cast(token_end - source_code.text.ptr as Word);
token_length := cast(token_end - source_code.text.ptr: Word);
if token_end^ = 'u' then
current_token^.kind := TOKEN_WORD;
@ -712,7 +710,7 @@ begin
source_code := advance_source(source_code, 1u)
elsif first_char = '\'' then
token_end := lex_character(source_code.text.ptr + 1, current_token);
token_length := cast(token_end - source_code.text.ptr as Word);
token_length := cast(token_end - source_code.text.ptr: Word);
if token_end^ = '\'' then
current_token^.kind := TOKEN_CHARACTER;
@ -725,7 +723,7 @@ begin
if token_end^ = '"' then
current_token^.kind := TOKEN_STRING;
token_length := cast(token_end - source_code.text.ptr as Word);
token_length := cast(token_end - source_code.text.ptr: Word);
source_code := advance_source(source_code, token_length + 1u)
end
elsif first_char = '[' then
@ -823,7 +821,7 @@ end
proc parse_literal(tokens: pointer to pointer to Token, tokens_size: pointer to Word) -> pointer to Literal;
begin
return cast(calloc(1, sizeof(Literal)) as pointer to Literal)
return cast(calloc(1u, Literal.size): pointer to Literal)
end
proc parse_constant_definition(tokens: pointer to pointer to Token,
@ -831,9 +829,9 @@ proc parse_constant_definition(tokens: pointer to pointer to Token,
var
result: pointer to ConstantDefinition;
begin
result := cast(calloc(1, sizeof(ConstantDefinition)) as pointer to ConstantDefinition);
result := cast(calloc(1u, ConstantDefinition.size): pointer to ConstantDefinition);
result^.name := cast(malloc(strlen(tokens^^.value.string_value)) as pointer to Char);
result^.name := cast(malloc(strlen(tokens^^.value.string_value)): pointer to Char);
strcpy(result^.name, tokens^^.value.string_value);
tokens^ := tokens^ + 2u;
@ -855,7 +853,7 @@ var
result: pointer to Program,
current_constant: pointer to pointer to ConstantDefinition;
begin
result := cast(calloc(1, sizeof(Program)) as pointer to Program);
result := cast(calloc(1u, Program.size): pointer to Program);
result^.constants.elements := nil;
result^.constants.count := 0u;
@ -866,8 +864,11 @@ begin
while tokens_size^ > 0u and tokens^^.kind = TOKEN_IDENTIFIER do
result^.constants.elements := cast(
reallocarray(result^.constants.elements, result^.constants.count + 1u, sizeof(pointer to ConstantDefinition))
as pointer to pointer to ConstantDefinition);
reallocarray(
cast(result^.constants.elements: pointer to Byte),
result^.constants.count + 1u,
(pointer to ConstantDefinition).size
) : pointer to pointer to ConstantDefinition);
current_constant := result^.constants.elements + result^.constants.count;
result^.constants.count := result^.constants.count + 1u;
@ -887,7 +888,7 @@ var
result: pointer to CommandLine;
begin
i := 1;
result := cast(malloc(sizeof(CommandLine)) as pointer to CommandLine);
result := cast(malloc(CommandLine.size): pointer to CommandLine);
result^.tokenize := false;
result^.syntax_tree := false;
result^.input := nil;
@ -950,5 +951,5 @@ begin
end
begin
exit(process(count, parameters))
exit(process(cast(count: Int), cast(parameters: pointer to pointer to Char)))
end.