Replace Byte pointer with a generic pointer type
This commit is contained in:
44
source.elna
44
source.elna
@ -96,17 +96,17 @@ type
|
||||
end
|
||||
FILE* = record end
|
||||
StringBuffer* = record
|
||||
data: ^Byte
|
||||
data: Pointer
|
||||
size: Word
|
||||
capacity: Word
|
||||
end
|
||||
SourceCode = record
|
||||
position: Position
|
||||
|
||||
input: ^Byte
|
||||
empty: proc(^Byte) -> Bool
|
||||
advance: proc(^Byte)
|
||||
head: proc(^Byte) -> Char
|
||||
input: Pointer
|
||||
empty: proc(Pointer) -> Bool
|
||||
advance: proc(Pointer)
|
||||
head: proc(Pointer) -> Char
|
||||
end
|
||||
Token* = record
|
||||
kind: TokenKind
|
||||
@ -137,13 +137,13 @@ 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: ^Byte, size: Word, nmemb: Word, stream: ^FILE) -> Word; extern
|
||||
proc write(fd: Int, buf: ^Byte, Word: Int) -> 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) -> ^Byte; extern
|
||||
proc free(ptr: ^Byte); extern
|
||||
proc calloc(nmemb: Word, size: Word) -> ^Byte; extern
|
||||
proc realloc(ptr: ^Byte, size: Word) -> ^Byte; 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
|
||||
|
||||
@ -160,18 +160,18 @@ proc exit(code: Int) -> !; extern
|
||||
Standard procedures.
|
||||
*)
|
||||
|
||||
proc reallocarray(ptr: ^Byte, n: Word, size: Word) -> ^Byte;
|
||||
proc reallocarray(ptr: Pointer, n: Word, size: Word) -> Pointer;
|
||||
return realloc(ptr, n * size)
|
||||
end
|
||||
|
||||
proc write_s(value: String);
|
||||
begin
|
||||
write(0, cast(value.ptr: ^Byte), cast(value.length: Int))
|
||||
write(0, cast(value.ptr: Pointer), cast(value.length: Int))
|
||||
end
|
||||
|
||||
proc write_z(value: ^Char);
|
||||
begin
|
||||
write(0, cast(value: ^Byte), cast(strlen(value): Int))
|
||||
write(0, cast(value: Pointer), cast(strlen(value): Int))
|
||||
end
|
||||
|
||||
proc write_b(value: Bool);
|
||||
@ -185,7 +185,7 @@ end
|
||||
|
||||
proc write_c(value: Char);
|
||||
begin
|
||||
write(0, cast(@value: ^Byte), 1)
|
||||
write(0, cast(@value: Pointer), 1)
|
||||
end
|
||||
|
||||
proc write_i(value: Int);
|
||||
@ -268,7 +268,7 @@ begin
|
||||
buffer^.capacity := buffer^.capacity + 1024u;
|
||||
buffer^.data := realloc(buffer^.data, buffer^.capacity)
|
||||
end;
|
||||
(buffer^.data + buffer^.size)^ := cast(char: Byte);
|
||||
cast(buffer^.data + buffer^.size: ^Char)^ := cast(char: Char);
|
||||
buffer^.size := buffer^.size + 1u
|
||||
end
|
||||
|
||||
@ -306,21 +306,21 @@ begin
|
||||
return result
|
||||
end
|
||||
|
||||
proc source_file_empty(source_input: ^Byte) -> Bool;
|
||||
proc source_file_empty(source_input: Pointer) -> Bool;
|
||||
var
|
||||
source_file: ^SourceFile
|
||||
begin
|
||||
source_file := cast(source_input: ^SourceFile);
|
||||
|
||||
if source_file^.index > source_file^.size then
|
||||
source_file^.size := fread(cast(@source_file^.buffer: ^Byte), 1u, 1024u, source_file^.handle);
|
||||
source_file^.size := fread(cast(@source_file^.buffer: Pointer), 1u, 1024u, source_file^.handle);
|
||||
source_file^.index := 1u
|
||||
end;
|
||||
|
||||
return source_file^.size = 0u
|
||||
end
|
||||
|
||||
proc source_file_head(source_input: ^Byte) -> Char;
|
||||
proc source_file_head(source_input: Pointer) -> Char;
|
||||
var
|
||||
source_file: ^SourceFile
|
||||
begin
|
||||
@ -329,7 +329,7 @@ begin
|
||||
return source_file^.buffer[source_file^.index]
|
||||
end
|
||||
|
||||
proc source_file_advance(source_input: ^Byte);
|
||||
proc source_file_advance(source_input: Pointer);
|
||||
var
|
||||
source_file: ^SourceFile
|
||||
begin
|
||||
@ -597,7 +597,7 @@ var
|
||||
new_length: Word
|
||||
begin
|
||||
new_length := lexer^.length + 1u;
|
||||
lexer^.data := cast(reallocarray(cast(lexer^.data: ^Byte), new_length, #size(Token)): ^Token);
|
||||
lexer^.data := cast(reallocarray(cast(lexer^.data: Pointer), new_length, #size(Token)): ^Token);
|
||||
(lexer^.data + lexer^.length)^ := token;
|
||||
lexer^.length := new_length
|
||||
end
|
||||
@ -1069,7 +1069,7 @@ begin
|
||||
end;
|
||||
|
||||
source_code.position := Position(1u, 1u);
|
||||
source_code.input := cast(source_file: ^Byte);
|
||||
source_code.input := cast(source_file: Pointer);
|
||||
source_code.empty := source_file_empty;
|
||||
source_code.head := source_file_head;
|
||||
source_code.advance := source_file_advance;
|
||||
|
Reference in New Issue
Block a user