mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-26 01:13:48 +00:00
Integer Enumerations
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@
|
||||
#load "file.odin"
|
||||
|
||||
print_string :: proc(s: string) {
|
||||
file_write(file_get_standard(FILE_STANDARD_OUTPUT), ^s[0], len(s));
|
||||
file_write(file_get_standard(FileStandard.OUTPUT), ^s[0], len(s));
|
||||
}
|
||||
|
||||
byte_reverse :: proc(b: []byte) {
|
||||
|
||||
+30
-3
@@ -42,7 +42,7 @@ variables :: proc() {
|
||||
x, y: int = 1, 2;
|
||||
|
||||
// Type inference
|
||||
apple, banana, carrot := true, 123, "carrot";
|
||||
apple, banana, 世界 := true, 123, "world";
|
||||
|
||||
|
||||
// Basic Types of the Language
|
||||
@@ -73,7 +73,7 @@ variables :: proc() {
|
||||
// untyped rune - rune/i32
|
||||
|
||||
|
||||
// // Zero values
|
||||
// Zero values
|
||||
zero_numeric := 0;
|
||||
zero_boolean := false;
|
||||
zero_pointer := null;
|
||||
@@ -324,7 +324,8 @@ types :: proc() {
|
||||
return y;
|
||||
}
|
||||
|
||||
// transmute only works if the size of the types are equal
|
||||
// NOTE(bill): transmute only works if the size of the types are equal
|
||||
|
||||
/*
|
||||
// in C
|
||||
union {
|
||||
@@ -334,6 +335,32 @@ types :: proc() {
|
||||
*/
|
||||
}
|
||||
|
||||
{ // Enumeration
|
||||
Thing :: type enum {
|
||||
APPLE,
|
||||
FROG,
|
||||
TREE,
|
||||
TOMB,
|
||||
}
|
||||
a := Thing.APPLE;
|
||||
|
||||
Sized :: type enum u64 {
|
||||
APPLE,
|
||||
FROG,
|
||||
TREE,
|
||||
TOMB,
|
||||
}
|
||||
static_assert(size_of(Sized) == size_of(u64));
|
||||
|
||||
Certain :: type enum {
|
||||
APPLE = 3,
|
||||
FROG,
|
||||
TREE = 7,
|
||||
TOMB,
|
||||
}
|
||||
static_assert(Certain.TOMB == 8);
|
||||
}
|
||||
|
||||
|
||||
|
||||
{ // Compound Literals
|
||||
|
||||
+18
-19
@@ -9,10 +9,9 @@ File :: type struct {
|
||||
file_open :: proc(name: string) -> (File, bool) {
|
||||
buf: [300]byte;
|
||||
_ = copy(buf[:], name as []byte);
|
||||
handle := CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null);
|
||||
|
||||
f: File;
|
||||
f.handle = handle as FileHandle;
|
||||
f := File{
|
||||
handle = CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null),
|
||||
};
|
||||
success := f.handle != INVALID_HANDLE_VALUE as FileHandle;
|
||||
return f, success;
|
||||
}
|
||||
@@ -20,10 +19,9 @@ file_open :: proc(name: string) -> (File, bool) {
|
||||
file_create :: proc(name: string) -> (File, bool) {
|
||||
buf: [300]byte;
|
||||
_ = copy(buf[:], name as []byte);
|
||||
handle := CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, null, CREATE_ALWAYS, 0, null);
|
||||
|
||||
f: File;
|
||||
f.handle = handle as FileHandle;
|
||||
f := File{
|
||||
handle = CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, null, CREATE_ALWAYS, 0, null),
|
||||
};
|
||||
success := f.handle != INVALID_HANDLE_VALUE as FileHandle;
|
||||
return f, success;
|
||||
}
|
||||
@@ -38,23 +36,24 @@ file_write :: proc(f: ^File, buf: rawptr, len: int) -> bool {
|
||||
return WriteFile(f.handle, buf, len as i32, ^bytes_written, null) != 0;
|
||||
}
|
||||
|
||||
FileStandardType :: type int;
|
||||
FILE_STANDARD_INPUT : FileStandardType : 0;
|
||||
FILE_STANDARD_OUTPUT : FileStandardType : 1;
|
||||
FILE_STANDARD_ERROR : FileStandardType : 2;
|
||||
FILE_STANDARD__COUNT : FileStandardType : 3;
|
||||
FileStandard :: type enum {
|
||||
INPUT,
|
||||
OUTPUT,
|
||||
ERROR,
|
||||
COUNT,
|
||||
}
|
||||
|
||||
__std_file_set := false;
|
||||
__std_files: [FILE_STANDARD__COUNT]File;
|
||||
__std_files: [FileStandard.COUNT as int]File;
|
||||
|
||||
file_get_standard :: proc(std: FileStandardType) -> ^File {
|
||||
file_get_standard :: proc(std: FileStandard) -> ^File {
|
||||
if (!__std_file_set) {
|
||||
__std_files[FILE_STANDARD_INPUT] .handle = GetStdHandle(STD_INPUT_HANDLE);
|
||||
__std_files[FILE_STANDARD_OUTPUT].handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
__std_files[FILE_STANDARD_ERROR] .handle = GetStdHandle(STD_ERROR_HANDLE);
|
||||
__std_files[FileStandard.INPUT] .handle = GetStdHandle(STD_INPUT_HANDLE);
|
||||
__std_files[FileStandard.OUTPUT].handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
__std_files[FileStandard.ERROR] .handle = GetStdHandle(STD_ERROR_HANDLE);
|
||||
__std_file_set = true;
|
||||
}
|
||||
return ^__std_files[std as int];
|
||||
return ^__std_files[std];
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ win32_print_last_error :: proc() {
|
||||
|
||||
// Yuk!
|
||||
to_c_string :: proc(s: string) -> ^u8 {
|
||||
c_str: ^u8 = heap_alloc(len(s)+1);
|
||||
c_str: ^u8 = alloc(len(s)+1);
|
||||
memory_copy(c_str, ^s[0], len(s));
|
||||
c_str[len(s)] = 0;
|
||||
return c_str;
|
||||
|
||||
+26
-17
@@ -1,7 +1,9 @@
|
||||
#load "win32.odin"
|
||||
|
||||
debug_trap :: proc() #foreign "llvm.debugtrap"
|
||||
|
||||
// TODO(bill): make custom heap procedures
|
||||
heap_alloc :: proc(len: int) -> rawptr {
|
||||
heap_alloc :: proc(len: int) -> rawptr {
|
||||
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
||||
}
|
||||
heap_free :: proc(ptr: rawptr) {
|
||||
@@ -9,7 +11,6 @@ heap_free :: proc(ptr: rawptr) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
memory_compare :: proc(dst, src: rawptr, len: int) -> int {
|
||||
s1, s2: ^u8 = dst, src;
|
||||
for i := 0; i < len; i++ {
|
||||
@@ -226,6 +227,7 @@ __string_cmp :: proc(a, b : string) -> int {
|
||||
return +1;
|
||||
}
|
||||
}
|
||||
|
||||
if len(a) < len(b) {
|
||||
return -1;
|
||||
} else if len(a) > len(b) {
|
||||
@@ -240,11 +242,15 @@ __string_le :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) <
|
||||
__string_ge :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) >= 0; }
|
||||
|
||||
|
||||
AllocationMode :: type int;
|
||||
ALLOCATION_ALLOC :: 0;
|
||||
ALLOCATION_DEALLOC :: 1;
|
||||
ALLOCATION_DEALLOC_ALL :: 2;
|
||||
ALLOCATION_RESIZE :: 3;
|
||||
|
||||
|
||||
AllocationMode :: type enum {
|
||||
ALLOC,
|
||||
DEALLOC,
|
||||
DEALLOC_ALL,
|
||||
RESIZE,
|
||||
}
|
||||
|
||||
|
||||
|
||||
AllocatorProc :: type proc(allocator_data: rawptr, mode: AllocationMode,
|
||||
@@ -272,11 +278,14 @@ DEFAULT_ALIGNMENT :: 2*size_of(int);
|
||||
|
||||
|
||||
__check_context :: proc() {
|
||||
static_assert(AllocationMode.ALLOC == 0);
|
||||
static_assert(AllocationMode.DEALLOC == 1);
|
||||
static_assert(AllocationMode.DEALLOC_ALL == 2);
|
||||
static_assert(AllocationMode.RESIZE == 3);
|
||||
|
||||
if context.allocator.procedure == null {
|
||||
context.allocator = __default_allocator();
|
||||
}
|
||||
|
||||
ptr := __check_context as rawptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -285,18 +294,18 @@ alloc :: proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_AL
|
||||
alloc_align :: proc(size, alignment: int) -> rawptr #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, ALLOCATION_ALLOC, size, alignment, null, 0, 0);
|
||||
return a.procedure(a.data, AllocationMode.ALLOC, size, alignment, null, 0, 0);
|
||||
}
|
||||
|
||||
dealloc :: proc(ptr: rawptr) #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
_ = a.procedure(a.data, ALLOCATION_DEALLOC, 0, 0, ptr, 0, 0);
|
||||
_ = a.procedure(a.data, AllocationMode.DEALLOC, 0, 0, ptr, 0, 0);
|
||||
}
|
||||
dealloc_all :: proc(ptr: rawptr) #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
_ = a.procedure(a.data, ALLOCATION_DEALLOC_ALL, 0, 0, ptr, 0, 0);
|
||||
_ = a.procedure(a.data, AllocationMode.DEALLOC_ALL, 0, 0, ptr, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -304,7 +313,7 @@ resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { r
|
||||
resize_align :: proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, ALLOCATION_RESIZE, new_size, alignment, ptr, old_size, 0);
|
||||
return a.procedure(a.data, AllocationMode.RESIZE, new_size, alignment, ptr, old_size, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -340,13 +349,13 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment:
|
||||
__default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocationMode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
if mode == ALLOCATION_ALLOC {
|
||||
if mode == AllocationMode.ALLOC {
|
||||
return heap_alloc(size);
|
||||
} else if mode == ALLOCATION_RESIZE {
|
||||
} else if mode == AllocationMode.RESIZE {
|
||||
return default_resize_align(old_memory, old_size, size, alignment);
|
||||
} else if mode == ALLOCATION_DEALLOC {
|
||||
} else if mode == AllocationMode.DEALLOC {
|
||||
heap_free(old_memory);
|
||||
} else if mode == ALLOCATION_DEALLOC_ALL {
|
||||
} else if mode == AllocationMode.DEALLOC_ALL {
|
||||
// NOTE(bill): Does nothing
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user